记一次免费让网站启用HTTPS的过程 centos7设置服务为开机自启动(以crond.serivce为)

写在前面

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

 

环境

阿里云服务器 ECSphp

centos 7css

nginxhtml

 

操做步骤

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

按页面所示步骤执行:node

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)python

$ which nginx

解决办法:(这两行命令大体就是使目录之间创建软链接,进行同步)linux

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

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

$ sudo certbot --nginx certonly

按提示回车就好了~git

 

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

 

感谢:

相关文章
相关标签/搜索