使用Nginx将WordPress等应用程序配置为支持SSL

../

我在CentOS Stream 8上安装了PHP7.4,并在Nginx上运行。然后我也安装了WordPress 6.0,并进行了测试。已经确认它在80端口正常工作。我试着让它们支持SSL,并在443端口上进行访问。

您可以使用“CentOS8.2上的SSL证书(Let’s Encrypt)设置步骤”中获取的SSL密钥。只需适当地编写nginx.conf等文件,并重新启动即可完成操作。

$ ls -lag /etc/nginx
drwxr-xr-x 2 root root 4096 2022-06-23 16:30 conf.d
-rw-r--r-- 1 root root 1077 2021-12-22 04:43 fastcgi.conf
-rw-r--r-- 1 root root 1007 2021-12-22 04:43 fastcgi_params
-rw-r--r-- 1 root root  910 2022-06-24 10:21 nginx-443.conf       // 新規
-rw-r--r-- 1 root root  476 2022-06-24 10:20 nginx-80.conf        // 新規
-rw-r--r-- 1 root root  530 2022-06-24 10:24 nginx-location.conf  // 新規
-rw-r--r-- 1 root root 1225 2022-06-24 10:13 nginx.conf           // 編集

我将nginx.conf文件修改如下,并仅展示了更改的部分。

    • ユーザーはApacheと連携したいのでwwwに変更しておく。「NginxとApacheの競合を避けて、wwwユーザに統一した」を参照のこと

 

    httpディレクティブ内のserver記述を、nginx-80.confとnginx-443.confに分離している
$ vi /etc/nginx/nginx.conf

$ user nginx;
user www;
...
http {
	...
    include /etc/nginx/nginx-80.conf;
    include /etc/nginx/nginx-443.conf;
}

nginx-80.conf的内容如下。

    • 443をdefault_serverにしたいので、80からは外しておく

 

    locationディレクティブは、共通化のためnginx-location.confにまとめておく
$ vi /etc/nginx/nginx-80.conf

    server {

#       listen       80 default_server;
        listen       80;
#       listen       [::]:80 default_server;
        listen       [::]:80;
#       server_name  _;
        server_name  kankeri.com www.kankeri.com;
#       root         /usr/share/nginx/html;
        root         /opt/php74/webapps;

        include /etc/nginx/default.d/*.conf;
        include /etc/nginx/nginx-location.conf;
    }

nginx-443.conf的内容如下所示。

    • 443をdefault_serverにしておく

 

    • SSLの鍵は「CentOS8.2でSSL証明書(Let’s Encrypt)をセットアップする手順」で取得したものを使う

 

    locationディレクティブは、共通化のためnginx-location.confにまとめておく
$ vi /etc/nginx/nginx-443.conf

    server {
        
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
#       server_name  _;
        server_name  kankeri.com www.kankeri.com;
#       root         /usr/share/nginx/html;
        root         /opt/php74/webapps;

#       ssl_certificate "/etc/pki/nginx/server.crt";
        ssl_certificate "/etc/letsencrypt/live/kankeri.com/fullchain.pem";
#       ssl_certificate_key "/etc/pki/nginx/private/server.key";
        ssl_certificate_key "/etc/letsencrypt/live/kankeri.com/privkey.pem";
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers PROFILE=SYSTEM;
        ssl_prefer_server_ciphers on;

        include /etc/nginx/default.d/*.conf;
        include /etc/nginx/nginx-location.conf;
    }

nginx-location.conf的内容如下。

    • オリジナルのnginx.confの記述をコピペして改変している

 

    .phpのとき、fastcgi(php-fpm)を呼び出す
$ vi /etc/nginx/nginx-location.conf

    index index.php index.html;
    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        include fastcgi_params;
    }
    location ~ /\. { deny all; }
    location ~* /wp-config.php { deny all; }
    location ~* /(?:uploads|files)/.*\.php$ { deny all; }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
    error_page 404 /404.html;
    location = /40x.html { }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html { }

$ nginx -t

起初,由于对php-fpm不太了解,我进行了调查。

    • FPM:FastCGI Process Managerのこと。PHP用に php-fpm がある。

 

    • FastCGI:プロセス初回実行時に該当プロセスを一定時間サーバー内に保持することで、次回以降、実行時の「プロセスの起動/終了」を省略し、 高速化や、プロセスの起動/終了に伴うCPUへの負荷を軽減する

 

    php-fpm:PHP用のFPM。単純なcgiだと、処理ごとにプロセスの生成と破棄を繰り返すため、オーバーヘッドが大きい。php-fpmだと、プロセスの使い回しができ、オーバーヘッドがなくなる。OPCacheも使える。

我已经检查了与php-fpm相关的设置。这次不需要进行任何更改,现有的设置已经足够了。我将重新启动php-fpm和Nginx。

$ vi /etc/php-fpm.conf					// 特に触らず
$ vi /etc/php-fpm.d/www.conf			// 特に触らず
$ vi /etc/nginx/conf.d/php-fpm.conf 	// 特に触らず
upstream php-fpm {
   server unix:/run/php-fpm/www.sock;
}
$ ls -lag /run/php-fpm/
-rw-r--r-- 1 root root 7 2022-06-23 16:56 php-fpm.pid
srw-rw---- 1 www  www  0 2022-06-23 16:56 www.sock

$ systemctl restart php-fpm
$ systemctl restart nginx
../
广告
将在 10 秒后关闭
bannerAds