使用Let’s Encrypt将http网站升级为https

自己的服务器到期,转移自己博客内容至此。

系统环境:CentOS 7

网站环境:LNMP

http站点升级为https,主要为以下三个步骤:

     1、获取Let’s Encrypt免费证书

     2、配置Nginx开启https

     3、配置定期自动更新证书                 

一、获取Let’s Encrypt免费证书

    a、Let’s Encrypt客户端的安装

 [root@haproxy ~]# yum install certbot -y   #安装证书客户端

        发现无法安装,然后用的是oneinstack的安装脚本( 官网:https://oneinstack.com/)
应该是环境或者python版本导致,无需深究,仅传播自己使用过的方法,减少弯路。
       oneinstack下载到服务器后,tar xf 解压,进入oneinstack目录运行addons脚本:

[root@haproxy oneinstack]# ./addons.sh   #运行当前目录addons.sh脚本文件
 选择 7. Install/Uninstall Let’s Encrypt client  安装,等待安装完成,选择q,退出。
[root@haproxy oneinstack]# yum install certbot -y   #安装证书客户端,正常。

    b、获取Let’s Encrypt证书

[root@xifeng ~]# /usr/local/tengine/sbin/nginx -s stop
#先停止nginx,不然提示443端口被占用,无法安装
[root@xifeng ~]# certbot certonly –standalone -d mycloud.liuanhuaming.top
#创建mycloud.liuanhuaming.top域名的证书,可以接多个域名,前面要加-d参数。

首次提示输入邮箱地址,后面默认回车即可。

二、配置nginx开启https

以下为我的博客nginx配置:

server {
listen 80;
server_name www.liuanhuaming.top liuanhuaming.top;
rewrite ^(.*) https://www.liuanhuaming.top$1 permanent;
}

server {
listen 443 ssl;
ssl on;
server_name www.liuanhuaming.top;
root /var/html/wordpress;
access_log /data/wwwlogs/www.liuanhuaming.top_nginx.log combined;
index index.php;

ssl_certificate /etc/letsencrypt/live/liuanhuaming.top/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/liuanhuaming.top/privkey.pem;

location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
location ~ /\.ht {
deny all;
}
}

重启nginx,再打开网站,将会变成https://www.liuanhuaming.top了。

三、配置定期自动更新证书

[root@xifeng renewal]# certbot renew –pre-hook “service nginx stop” –post-hook “service nginx start”
#更新证书前停止nginx,更新完成后启动nginx。因为443端口需要停止。

提示证书尚未到期,无需续签。配置定时任务,添加每隔两个月凌晨2:30更新:

[root@xifeng renewal]# crontab -e
30 2 * */2 * certbot renew –pre-hook “service nginx stop” –post-hook “service nginx start”

完成!

 

猜你喜欢

转载自www.cnblogs.com/catinsky/p/9134315.html