注意:

使用 Oracle Cloud Infrastructure 发布可从 Internet 访问的 Web 服务器,网址为 IPv6

简介

在本教程中,我们将解释如何在 Oracle Cloud Infrastructure (OCI) 中创建 Web 服务器(OCI 实例),并将通过 Internet 访问此 Web 服务器。我们不仅要使此 Web 服务器从公共 IPv4 地址进行访问,而且还可以通过完全配置的 DNS 记录 (FQDN) 从公共 IPv6 地址进行访问。

图像

:本教程中使用的域已创建并配置为仅用于测试。

图像

目标

任务 1:在启用 IPv6 的情况下创建新 VCN

现在,我们使用新的 IPv4 和 IPv6 网络空间创建了一个新的 VCN,可以使用这些网络空间来切分 IPv4 和 IPv6 子网。

任务 2:在启用 IPv6 的情况下创建新子网

现在,该子网已准备好用于 IPv4 和 IPv6 地址。

任务 3:在启用 IPv6 的情况下创建新实例

图像

下一个逻辑任务是使用 SSH 访问实例,以便我们可以开始安装和配置 Web 服务器。尝试使用公共 IPv4 地址和私有密钥连接到 Web 服务器时,无法连接,因为在任务 1 中创建的 VCN 没有 Internet 网关,现在我们需要先在 VCN 中创建该 Internet 网关,再配置路由。

图像

任务 4:创建新的 Internet 网关并配置路由

要允许从 Internet 访问新实例 (webserver),首先使用 SSH 并最终转换为 HTTP,我们必须建立 Internet 网关。

任务 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 记录

注:本教程中使用的域已创建并配置为仅用于测试。

现在,可从 Internet 访问网站或 Web 服务器。我们将进一步配置一个 FQDN,该 FQDN 比 IPv4 地址或 IPv6 地址更容易记住。对于本教程,我们使用以下子域作为示例:oci.iwanhoogendoorn.nl 表示新 Web 服务器。

任务 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 的证明。

      图像

      安装并正确配置网站后,将显示以下网页。此页将提供有关本地和远程 IPv4 或 IPv6 地址的信息,具体取决于您使用的 IP 版本。此页面还将介绍您正在使用的协议(HTTP 或 HTTPS)。

      图像

任务 9:从 Internet 测试 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 帮助中心