记一次免费让网站启用HTTPS的过程

写在前面

个人网站运行将近2个月了,期间根据酷壳的一篇教程如何免费的让网站启用HTTPS做了一次,中间遇到问题就放下了。昨天孙三苗问我网站地址说要添加友链,出于好奇想看他网站长什么样,顺道也加一下友链。访问后发现他网站已经启用https了,于是按捺不住内心的冲动。以下是采用Let’s Encrypt免费方案,以及过程中遇到的问题和解决办法。

 

环境

阿里云服务器 ECS

centos 7

nginx

 

操作步骤

访问 https://certbot.eff.org 选择相应的SoftWare和System。(比如我的Nginx和Centos/Rhel7)

按页面所示步骤执行:

1)安装Certbot

$ yum -y install yum-utils
$ yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional

  

$ sudo yum install certbot python2-certbot-nginx

 

2)获取证书

$ sudo certbot --nginx certonly

执行到这一步出问题了,问题大致是 /etc/nginx 下找不到相应文件夹,certbot 获取证书的同时会修改nginx的配置文件,找不到该文件所以才会报错因为我的Nginx并没有安装在/etc目录下,而是在/usr/bin/nginx。各人情况可能不太一样,可以通过命令查看你的nginx目录(我的nginx配置文件在/usr/local/nginx/conf/下名为nginx.conf)

$ which nginx

解决办法:(这两行命令大致就是使目录之间建立软连接,进行同步)

$ ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
$ ln -s /usr/local/nginx/conf/ /etc/nginx

然后,继续执行获取证书时出问题的命令:

$ sudo certbot --nginx certonly

按提示回车就行了~

 

3)查看nginx配置文件信息 (/usr/local/nginx/conf/nginx.conf)

  1 user  www www;
  2 
  3 worker_processes auto;
  4 
  5 error_log  /home/wwwlogs/nginx_error.log  crit;
  6 
  7 pid        /usr/local/nginx/logs/nginx.pid;
  8 
  9 #Specifies the value for maximum file descriptors that can be opened by this process.
 10 worker_rlimit_nofile 51200;
 11 
 12 events
 13     {
 14         use epoll;
 15         worker_connections 51200;
 16         multi_accept on;
 17     }
 18 
 19 http
 20     {
 21         include       mime.types;
 22         default_type  application/octet-stream;
 23 
 24         server_names_hash_bucket_size 128;
 25         client_header_buffer_size 32k;
 26         large_client_header_buffers 4 32k;
 27         client_max_body_size 100m;
 28 
 29         sendfile   on;
 30         tcp_nopush on;
 31 
 32         keepalive_timeout 60;
 33 
 34         tcp_nodelay on;
 35 
 36         fastcgi_connect_timeout 300;
 37         fastcgi_send_timeout 300;
 38         fastcgi_read_timeout 300;
 39         fastcgi_buffer_size 64k;
 40         fastcgi_buffers 4 64k;
 41         fastcgi_busy_buffers_size 128k;
 42         fastcgi_temp_file_write_size 256k;
 43 
 44         gzip on;
 45         gzip_min_length  1k;
 46         gzip_buffers     4 16k;
 47         gzip_http_version 1.1;
 48         gzip_comp_level 2;
 49         gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
 50         gzip_vary on;
 51         gzip_proxied   expired no-cache no-store private auth;
 52         gzip_disable   "MSIE [1-6]\.";
 53 
 54         #limit_conn_zone $binary_remote_addr zone=perip:10m;
 55         ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
 56 
 57         server_tokens off;
 58         access_log off;
 59 
 60 server
 61     {
 62         #listen [::]:80 default_server ipv6only=on;
 63         server_name www.liangyadong.com liangyadong.com;
 64         index index.html index.htm index.php;
 65         root  /home/wwwroot/default;
 66 
 67         if (-f $request_filename/index.html){
 68             rewrite (.*) $1/index.html break;
 69         }
 70         
 71         if (-f $request_filename/index.php){
 72             rewrite (.*) $1/index.php;
 73         }
 74         
 75         if (!-f $request_filename){
 76             rewrite (.*) /index.php;
 77         }
 78 
 79         #error_page   404   /404.html;
 80 
 81         # Deny access to PHP files in specific directory
 82         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
 83 
 84         include enable-php.conf;
 85 
 86         location /nginx_status
 87         {
 88             stub_status on;
 89             access_log   off;
 90         }
 91 
 92         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 93         {
 94             expires      30d;
 95         }
 96 
 97         location ~ .*\.(js|css)?$
 98         {
 99             expires      12h;
100         }
101 
102         location ~ /.well-known {
103             allow all;
104         }
105 
106         location ~ /\.
107         {
108             deny all;
109         }
110 
111         access_log  /home/wwwlogs/access.log;
112     
113     listen 443 ssl http2; # managed by Certbot
114     ssl_certificate /etc/letsencrypt/live/liangyadong.com/fullchain.pem; # managed by Certbot
115     ssl_certificate_key /etc/letsencrypt/live/liangyadong.com/privkey.pem; # managed by Certbot
116     include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
117     ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
118 
119 }
120 include vhost/*.conf;
121 
122 
123 server
124     {
125     if ($host = www.liangyadong.com) {
126         return 301 https://$host$request_uri;
127     } # managed by Certbot
128 
129 
130     if ($host = liangyadong.com) {
131         return 301 https://$host$request_uri;
132     } # managed by Certbot
133 
134         
135         listen 80 default_server;
136         server_name www.liangyadong.com liangyadong.com;
137     return 404; # managed by Certbot
138 
139 }}

其中,113~139中加了# managed by Certbot注释的就是自动添加的内容。

 

4)通过步骤3)可以发现需要用到443端口,所以要在防火墙配置文件中进行配置443端口(/etc/sysconfig/iptables)。

 

5)重启nginx服务,重启防火墙设置。

$ service nginx restart 
$ service iptables restart

于是满怀信心的打开自己的网站发现并不能访问!

 

6)之所以到这里还不行,是因为阿里云服务器有一个安全组规则管理。

然后添加安全组规则,将443端口添加进来就哦了~

 

7)验证端口是否已成功开启(在线检测工具http://coolaf.com/tool/port

 

8)访问网站验证。

 

9)明明是https了,为什么不是安全锁而是感叹号呢?

原因在于:网站页面上面引用了不是https的资源,最常见的就是引用的图片、logo点击事件设置的超链接等地方,友链不一定有影响(可以设置试试,我这里友链没有影响)。

解决办法:貌似没什么快捷的方法,反正我是把硬编码的地方把http改成了https,其中最多的就是图片引用链接,因为文章本身不多,而且引用的图片之前是通过后台管理上传到服务器的,现在已全部修改为从微薄图床引用,引用路径都是https。(方法比较笨,有其他解决方案可以留言,拯救一下我这坨菜鸡)

补充:如果用的是wordpress建站记得修改常规选项中的站点地址。(设置>常规)

 

启用https后效果图如下:

 友链没有影响,主要影响是logo的超链接,以及网站首页超链接。

 

10)添加定时任务(用于更新证书)

执行命令添加定时任务(修改命令相同)

$ crontab -e

添加内容:

0 0 1 * * /usr/bin/certbot renew --force-renewal        #每月1号凌晨强制更新Let's Encrypt证书
5 0 1 * * /usr/sbin/service nginx restart               #每月1号凌晨更新证书后重启nginx

保存,设置crond服务为开机自启动。(此时其实已经是自启动的了,难道说默认就是?

查看、设置自启动可查看该篇centos7设置服务为开机自启动(以crond.serivce为)

另外,从centos7开始命令貌似已经变了。

任务 旧指令 新指令
使某服务自动启动 chkconfig --level 3 httpd on systemctl enable httpd.service
使某服务不自动启动 chkconfig --level 3 httpd off systemctl disable httpd.service
检查服务状态 service httpd status systemctl status httpd.service (服务详细信息) systemctl is-active httpd.service (仅显示是否 Active)
显示所有已启动的服务 chkconfig --list systemctl list-units --type=service
启动某服务 service httpd start systemctl start httpd.service
停止某服务 service httpd stop systemctl stop httpd.service
重启某服务 service httpd restart systemctl restart httpd.service

 

感谢:

 

posted @ 2019-04-17 13:51  习惯沉淀  阅读(2269)  评论(0编辑  收藏  举报