附註:

使用 Oracle Cloud Infrastructure 發布可透過 IPv6 從網際網路存取的 Web 伺服器

簡介

在本教學課程中,我們將說明如何在 Oracle Cloud Infrastructure (OCI) 內建立 Web 伺服器 (OCI 執行處理),並讓此 Web 伺服器能夠從網際網路存取。我們不僅可以從公用 IPv4 位址存取此 Web 伺服器,還可以從已完全設定 DNS 記錄 (FQDN) 的公用 IPv6 位址存取。

圖像

注意:我們在此教學課程中使用的網域已建立並設定為僅供測試之用。

圖像

目標

作業 1:建立啟用 IPv6 的新 VCN

我們現在已建立一個新的 VCN,其中包含新的 IPv4 和 IPv6 網路空間,可用來分割 IPv4 和 IPv6 子網路。

作業 2:建立啟用 IPv6 的新子網路

子網路現在已可供 IPv4 和 IPv6 位址使用。

作業 3:建立啟用 IPv6 的新執行處理

圖像

下一個邏輯工作將使用 SSH 存取執行處理,因此我們可以開始安裝並設定 Web 伺服器。嘗試使用公用 IPv4 位址和私密金鑰連線至 Web 伺服器時,無法連線,因為在任務 1 中建立的 VCN 沒有網際網路閘道,現在我們需要先在 VCN 中建立該網際網路閘道,然後再設定路由。

圖像

作業 4:建立新的網際網路閘道並設定路由

若要能夠從網際網路存取新的執行處理 (Web 伺服器),一開始使用 SSH 並最終轉換成 HTTP,我們必須建立網際網路閘道。

作業 5:預設安全清單上的開啟 HTTP 與 HTTPS

建立新的 VCN 時,會在該 VCN 內的子網路套用預設安全清單。依照預設,ICMP 和 SSH 允許內送 (傳入)。我們必須將 HTTP 和 HTTPS 新增至該清單,以允許 Web 伺服器的內送流量。

安全規則已就緒,且 VCN 子網路上允許使用 IPv4 和 IPv6 流量,我們將在執行處理上安裝 Web 伺服器應用程式。

作業 6:在執行處理上安裝 Web 伺服器

在安裝 Web 伺服器應用程式之前,請先快速驗證防火牆連接埠是否正確設定。請使用下列網站 IPV6 Online Port Scanner 。此網站有 IPv6 連接埠掃描器,但您也可以輸入 IPv4 位址。

作業 7:設定 DNS 記錄

注意:我們在此教學課程中使用的網域已建立並設定為僅供測試之用。

現在,網站或 Web 伺服器可以從網際網路連線。我們將進一步設定一個可以比 IPv4 位址或 IPv6 位址更容易記住的 FQDN。本教學課程使用下列子網域作為新 Web 伺服器的範例:oci.iwanhoogendoorn.nl

工作 8:使用 NGINX 和 PHP 建立自訂網站

我們將建立自訂的 PHP 網站或腳本,提供更多有關 NGINX 網路和 IP 位址的資訊。

若要整合 NGINX 與 PHP,請遵循下列步驟:

  1. 編輯 PHP-FPM 組態檔以整合 NGINX

    編輯 /etc/php-fpm.d/www.conf 檔案。

    sudo nano /etc/php-fpm.d/www.conf
    
    • 變更前的 /etc/php-fpm.d/www.conf 檔案內容。

      ; Unix user/group of processes
      ; Note: The user is mandatory. If the group is not set, the default user's group
      ;	will be used.
      ; RPM: apache user chosen to provide access to the same directories as httpd
      **user =** apache
      ; RPM: Keep a group allowed to write in log dir.
      **group =** apache
      
    • 變更後 /etc/php-fpm.d/www.conf 檔案的內容。

      ; Unix user/group of processes
      ; Note: The user is mandatory. If the group is not set, the default user's group
      ;	will be used.
      ; RPM: apache user chosen to provide access to the same directories as httpd
      **user = nginx**
      ; RPM: Keep a group allowed to write in log dir.
      **group = nginx**
      
  2. 編輯 NGINX 組態檔以整合 PHP-FPM

    • 編輯 /etc/nginx/conf.d/default.conf 檔案。

      [opc@ih-webserver-01 html]$ sudo nano /etc/nginx/conf.d/default.conf
      
    • 增加下列組態。

      location / {
      **# where is the NGINX root www folder?**
          root   /usr/share/nginx/html;
      # what are the default index files to look for in the directory?
          index **index.php** index.html index.htm;
      }
      
      # tell NGINX what to do when it sees PHP-FPM giving a 404 HTTP status in the response
      **location = /404.php {
      # where is the NGINX root www folder?
        root   /usr/share/nginx/html;
      # not return error responses with relevant status codes
        fastcgi_intercept_errors off;
      # where is PHP-FPM listening? the socket
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
      # everytime we're in this location, tell PHP-FPM the complete script filename to be executed
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      # and don't forget to tell PHP-FPM stuff like SERVER_NAME:
        include fastcgi_params;
      }**
      
      # tell NGINX what to do when it sees PHP-FPM a .php file
      **location ~ \.php$ {
      # where is the NGINX root www folder?
        root   /usr/share/nginx/html;
      # define custom error pages
        error_page 404 /404.php;
      # return error responses with relevant status codes
        fastcgi_intercept_errors on;
      # where is PHP-FPM listening? the socket
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
      # everytime we're in this location, tell PHP-FPM the complete script filename to be executed
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      # and don't forget to tell PHP-FPM stuff like SERVER_NAME:
        include fastcgi_params;
      }**
      
  3. 編輯 php.ini 組態檔

    • 編輯 /etc/php.ini 檔案。

      [opc@ih-webserver-01 /]$ sudo nano /etc/php.ini
      
    • 請確定下列行存在、已變更或未註釋。

      cgi.fix_pathinfo = 0;
      
  4. 重新啟動 PHP-FPM 和 NGINX 服務

    • 重新啟動 PHP-FPM 服務。

      sudo systemctl start php-fpm
      
    • 請重新啟動 NGINX 服務。

      sudo systemctl restart nginx
      
  5. 建立自訂頁面以處理並測試適當的網頁

    • 建立自訂 404.php 頁面。如果頁面不存在,並嘗試存取時,必須正確處理重新導向至自訂 404 頁面。

      [opc@ih-webserver-01 html]$ sudo nano 404.php
      

      自訂 404.php 頁面的代碼。

      <?php
        header("HTTP/1.0 404 Not Found");
      ?>
      <html>
        <head>
          <title>404 Error - Page Not Found</title>
        </head>
        <body>404 Error - Page Not Found!</body>
      </html>
      
    • 建立自訂 info.php 頁面。

      [opc@ih-webserver-01 html]$ sudo nano info.php
      

      info.php 頁面的代碼。

      <?php
      
      phpinfo();
      
    • 測試 info.php 網站,提供 PHP 使用 NGNIX 的證明。

      圖像

      當網站安裝並正確設定時,會顯示下列網頁。此頁面將根據您用來連線的 IP 版本,提供本機和遠端 IPv4 或 IPv6 位址的相關資訊。此頁面也會說明您使用的通訊協定、HTTP 或 HTTPS。

      圖像

工作 9:從網際網路測試 Web 伺服器

注意:我們在此教學課程中使用的網域已建立並設定為僅供測試之用。

即使我們已經使用多種方法測試網站或 Web 伺服器的連線能力。我們想要使用下列網站執行最後的測試,以一次測試連線能力和 DNS 記錄組態。您的網站 IPv6 已就緒嗎?

  1. 使用 FQDN oci.iwanhoogendoorn.nl 進行測試。
  2. 驗證 IPv4 DNS 記錄是否正確設定。
  3. 驗證 IPv6 DNS 記錄是否正確設定。
  4. 確認網站可於 IPv4 連線。
  5. 確認網站可於 IPv6 連線。

圖像

認可

其他學習資源

瀏覽 docs.oracle.com/learn 的其他實驗室,或前往 Oracle Learning YouTube 頻道存取更多免費學習內容。此外,請造訪 education.oracle.com/learning-explorer 以成為 Oracle Learning Explorer。

如需產品文件,請造訪 Oracle Help Center