LNMP下配置免费SSL安全证书(https)

本文作者:陈进坚
博客地址:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
联系方式:jian1098@qq.com
版权声明:文章仅在本人博客和CSDN博客中发布,所有文章未经授权禁止转载!

环境要求

  • LNMP
  • 域名

安装依赖

$ cd /root/
$ wget https://dl.eff.org/certbot-auto --no-check-certificate
$ chmod +x ./certbot-auto
$ ./certbot-auto -n

生成证书

注意将 www.vpser.net 替换成你的域名

单域名生成证书:

$ ./certbot-auto certonly --email youemail@vpser.net --agree-tos --no-eff-email --webroot -w /home/wwwroot/www.vpser.net -d www.vpser.net

多域名单目录生成单证书:(即一个网站多个域名使用同一个证书)

$ ./certbot-auto certonly --email youemail@vpser.net --agree-tos --no-eff-email --webroot -w /home/wwwroot/www.vpser.net -d www.vpser.net -d bbs.vpser.net

多域名多目录生成一个证书:(即一次生成多个域名的一个证书)

$ ./certbot-auto certonly --email youemail@vpser.net --agree-tos --no-eff-email --webroot -w /home/wwwroot/www.vpser.net -d www.vpser.net -d bbs.vpser.net -w /home/wwwroot/lnmp.org -d www.lnmp.org -d lnmp.org

提示以下信息表示生成成功

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/www.vpser.net/fullchain.pem. Your cert will
expire on 2016-10-01. To obtain a new or tweaked version of this
certificate in the future, simply run certbot-auto again. To
non-interactively renew *all* of your certificates, run
"certbot-auto renew"
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

生成的证书会存在:/etc/letsencrypt/live/www.vpser.net/ 目录下,检查是否有两个.pom的证书文件

修改配置

  • 下载配置文件并替换掉所有的www.example.net为你的域名,仅限单域名

链接:https://pan.baidu.com/s/1HC1ErFjtoDe8akzzWys29A 密码:i2fi

  • 上传至/usr/local/nginx/conf/vhost 并替换原来的文件

  • 执行:/etc/init.d/nginx reload 重新载入配置使其生效。

到这里你的域名应该就已经变成https了

证书续期

因为证书只有90天,所以建议使用crontab进行自动续期,间隔5天就可以了,不能太频繁:

$ crontab -e
0 3 */5 * * /root/certbot-auto renew --disable-hook-validation --renew-hook "/etc/init.d/nginx reload"

本文参考:https://www.vpser.net/build/letsencrypt-free-ssl.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LNMP 环境(Linux + Nginx + MySQL + PHP)中配置 SSL 证书,你可以按照以下步骤进行操作: 1. 获取 SSL 证书:你可以购买 SSL 证书或使用免费的工具(如 Let's Encrypt)来获取证书。确保你获得了证书文件(通常是以 `.crt` 结尾)和私钥文件(通常是以 `.key` 结尾)。 2. 将证书和私钥文件放置在服务器上:将证书和私钥文件上传到服务器上的安全目录中,例如 `/etc/nginx/ssl/`。 3. 配置 Nginx:找到 Nginx 的虚拟主机配置文件(通常位于 `/etc/nginx/conf.d/your_site.conf`),并进行以下修改: ``` server { listen 443 ssl; server_name your_domain.com; ssl_certificate /etc/nginx/ssl/your_certificate.crt; ssl_certificate_key /etc/nginx/ssl/your_private_key.key; # 可选:为了增加安全性,你可以配置其他 SSL 相关的设置,例如: # ssl_protocols, ssl_ciphers, ssl_prefer_server_ciphers, ssl_session_cache 等 location / { root /path/to/your/web/files; index index.php index.html index.htm; try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_pass unix:/var/run/php/php<version>-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } ``` 将 `your_domain.com` 替换为你的域名,将 `/etc/nginx/ssl/your_certificate.crt` 和 `/etc/nginx/ssl/your_private_key.key` 替换为你实际的证书和私钥文件的路径。将 `/path/to/your/web/files` 替换为你网站文件的实际路径。将 `<version>` 替换为你安装的 PHP 版本号。 4. 重启 Nginx:保存并关闭配置文件后,使用以下命令重启 Nginx 以使配置生效: ``` sudo systemctl restart nginx ``` 完成以上步骤后,你的 LNMP 环境将通过 HTTPS 提供安全连接,并且能够处理 PHP 文件。请确保在防火墙中打开了 443 端口,并在 DNS 中正确地将域名指向你的服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值