本次站点域名为:
fumino.xoho.top
服务器公网 IP 为:
47.79.144.138
服务器环境为阿里云轻量应用服务器,运行 WordPress,Web 服务使用 nginx,站点目录为:
/www/wwwroot/wordpress
本次 HTTPS 访问链路为:
浏览器 -> Cloudflare -> 阿里云服务器 -> nginx -> WordPress
域名 fumino.xoho.top 当前经过 Cloudflare 代理访问。用户在浏览器中访问域名时,请求会先进入 Cloudflare,再由 Cloudflare 回源到阿里云服务器 47.79.144.138。
一开始服务器只监听了 HTTP 端口 80,没有监听 HTTPS 端口 443。通过服务器检查可以看到 nginx 版本为:
nginx -v
输出:
nginx version: nginx/1.26.3
检查监听端口:
ss -lntp | grep -E ':80|:443'
最初只看到 80 端口:
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",...))
因此需要在阿里云控制台为服务器开放 443/TCP 端口。开放后,服务器就可以对外提供 HTTPS 服务。
nginx 的主配置文件位于:
/www/server/nginx/conf/nginx.conf
WordPress 的 nginx 站点配置位于:
/www/server/panel/vhost/nginx/127.0.0.1.conf
WordPress 程序目录位于:
/www/wwwroot/wordpress
先把站点绑定到正式域名:
DOMAIN=fumino.xoho.top ROOT=/www/wwwroot/wordpress CONF=/www/server/panel/vhost/nginx/127.0.0.1.conf cp "$CONF" "$CONF.bak.$(date +%F-%H%M%S)" grep -q "$DOMAIN" "$CONF" || sed -i "s/server_name .*/server_name $DOMAIN wordpress 127.0.0.1;/" "$CONF" nginx -t && /www/server/nginx/sbin/nginx -s reload
然后安装 Certbot:
dnf install -y certbot
使用 Let’s Encrypt 申请免费 SSL 证书:
certbot certonly --webroot \ -w /www/wwwroot/wordpress \ -d fumino.xoho.top \ --agree-tos \ --register-unsafely-without-email \ --non-interactive
证书申请成功后,证书文件位置为:
/etc/letsencrypt/live/fumino.xoho.top/fullchain.pem /etc/letsencrypt/live/fumino.xoho.top/privkey.pem
新建 HTTPS nginx 配置文件:
cat >/www/server/panel/vhost/nginx/fumino.xoho.top.ssl.conf <<'EOF' server { listen 443 ssl http2; server_name fumino.xoho.top; root /www/wwwroot/wordpress; index index.php index.html index.htm; ssl_certificate /etc/letsencrypt/live/fumino.xoho.top/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/fumino.xoho.top/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; include enable-php-83.conf; include /www/server/panel/vhost/rewrite/127.0.0.1.conf; location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md) { return 404; } location ~ /\.well-known { allow all; } access_log /www/wwwlogs/fumino.xoho.top.log; error_log /www/wwwlogs/fumino.xoho.top.error.log; } EOF
检查 nginx 配置并重载:
nginx -t && /www/server/nginx/sbin/nginx -s reload
再次检查端口监听:
ss -lntp | grep -E ':80|:443'
可以看到 nginx 已经同时监听 80 和 443:
LISTEN 0 511 0.0.0.0:443 0.0.0.0:* users:(("nginx",...)) LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",...))
为了让 HTTP 自动跳转到 HTTPS,在原来的 80 端口配置中加入 301 跳转:
sed -i '/server_name fumino.xoho.top wordpress 127.0.0.1;/a \ return 301 https://$host$request_uri;' /www/server/panel/vhost/nginx/127.0.0.1.conf nginx -t && /www/server/nginx/sbin/nginx -s reload
验证 HTTP 跳转:
curl -I http://fumino.xoho.top
返回:
HTTP/1.1 301 Moved Permanently Location: https://fumino.xoho.top/
验证 HTTPS 访问:
curl -I https://fumino.xoho.top -k
返回:
HTTP/2 200 server: cloudflare content-type: text/html; charset=UTF-8
也可以直接验证源站 nginx:
curl -I https://127.0.0.1 --resolve fumino.xoho.top:443:127.0.0.1 -k
返回:
HTTP/2 200 server: nginx content-type: text/html; charset=UTF-8
到这里,HTTPS 已经配置完成。
最后需要把 WordPress 后台的站点地址也改为 HTTPS。进入 WordPress 后台:
设置 -> 常规
将下面两个地址都改成:
包括:
WordPress 地址 URL 站点地址 URL
如果无法进入后台,也可以通过数据库修改:
UPDATE wp_options SET option_value = 'https://fumino.xoho.top' WHERE option_name IN ('home', 'siteurl');
完成后,浏览器访问:
即可使用 HTTPS 访问 WordPress 站点。
Comments NOTHING