跳转到主要内容

自建站wordpress如何实现https(ssl)自动续期:LNMP+ACME

Dawn tawn

1. 环境准备

假设你已经用 LNMP 一键安装包(https://lnmp.org/)成功搭建了 LNMP 环境,安装了 Apache、MySQL、PHP。

2. 创建站点

使用 LNMP 自带的 vhost.sh 脚本快速创建网站目录和 Apache 配置。

cd /root/lnmp

# LNMP 一键包安装目录
./vhost.sh

选择:

  • 1. Use HTTP Only(只使用 HTTP,方便后续 SSL 配置)
  • 输入域名:shopifycoder.com
  • 输入站点根目录路径:默认 /data/wwwroot/shopifycoder (建议保持默认)
  • 选择 PHP 版本及其他相关设置完成站点创建。

3. 安装 acme.sh 并申请证书

如果没安装 acme.sh,先安装:

curl https://get.acme.sh | sh -s email=1584605353@qq.com
source ~/.bashrc  # 或 source ~/.zshrc,激活环境变量

申请 SSL 证书(使用 Apache 模式自动验证):

acme.sh --issue -d shopifycoder.com -d www.shopifycoder.com --apache

说明

  • 多域名证书同时包含顶级域名和 www 子域名。
  • 如果默认 CA 不满足需求,可执行:
acme.sh --set-default-ca --server letsencrypt

acme.sh --set-default-ca --server zerossl

然后再申请。

4. 安装证书到指定目录

将申请好的证书安装到 Apache SSL 目录(目录需先创建):

mkdir -p /usr/local/apache/conf/ssl/shopifycodercom
acme.sh --install-cert -d shopifycoder.com \
  --cert-file /usr/local/apache/conf/ssl/shopifycodercom/shopifycoder.com.cer \
  --key-file /usr/local/apache/conf/ssl/shopifycodercom/shopifycoder.com.key \
  --fullchain-file /usr/local/apache/conf/ssl/shopifycodercom/shopifycoder.com.cert \
  --reloadcmd "systemctl reload httpd.service"

--reloadcmd 参数指定续期后重载 Apache。

5. 配置 Apache 虚拟主机支持 HTTPS

编辑站点配置文件 /usr/local/apache/conf/vhost/shopifycoder.com.conf(路径根据你实际情况调整):

<VirtualHost *:80>
    ServerName shopifycoder.com
    ServerAlias www.shopifycoder.com

    # HTTP 全部跳转到 HTTPS
    Redirect permanent / https://shopifycoder.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName shopifycoder.com
    ServerAlias www.shopifycoder.com

    DocumentRoot "/data/wwwroot/shopifycoder"

    SSLEngine on
    SSLCertificateFile /usr/local/apache/conf/ssl/shopifycodercom/shopifycoder.com.cer
    SSLCertificateKeyFile /usr/local/apache/conf/ssl/shopifycodercom/shopifycoder.com.key
    SSLCertificateChainFile /usr/local/apache/conf/ssl/shopifycodercom/shopifycoder.com.cert

    <Directory "/data/wwwroot/shopifycoder">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog "/data/wwwlogs/shopifycoder.com_error_apache.log"
    CustomLog "/data/wwwlogs/shopifycoder.com_apache.log" common
</VirtualHost>
注意:如果 Apache 版本较新,SSLCertificateChainFile 可能不需要,完整链证书放在 SSLCertificateFile 中即可。

6. 重启 Apache

systemctl restart httpd.service

如果报错,可尝试:

service httpd restart

或者:

sudo /usr/local/apache/bin/apachectl restart

7. 测试 HTTPS

打开浏览器访问 https://shopifycoder.com,查看是否显示安全锁标志。


8. 自动续期设置

acme.sh 会自动安装定时任务来续期证书。

你可以手动测试续期是否成功:

acme.sh --renew -d shopifycoder.com --force

如果续期成功,会自动替换证书文件并执行 --reloadcmd 重载 Apache。


9. 额外注意事项

  • 证书目录权限需确保 Apache 可以读取。
  • 确保域名 DNS 解析正确,能解析到你的服务器 IP。
  • LNMP 一键包的 Apache 配置路径可能不同,请根据实际情况调整。

总结

本文详细介绍了基于 LNMP 一键包创建站点、用 acme.sh 申请和安装 Let’s Encrypt 免费证书的全流程,保证你的 shopifycoder.com 网站通过 HTTPS 安全访问。
跟着做,一步步就能完成安全站点部署!