我的網站運行將近2個月了,期間根據酷殼的一篇教程如何免費的讓網站啓用HTTPS作了一次,中間遇到問題就放下了。昨天孫三苗問我網站地址說要添加友鏈,出於好奇想看他網站長什麼樣,順道也加一下友鏈。訪問後發現他網站已經啓用https了,因而按捺不住心裏的衝動。如下是採用Let’s Encrypt免費方案,以及過程當中遇到的問題和解決辦法。javascript
阿里雲服務器 ECSphp
centos 7css
nginxhtml
訪問 https://certbot.eff.org 選擇相應的SoftWare和System。(好比個人Nginx和Centos/Rhel7)java
按頁面所示步驟執行:node
$ 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
$ 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
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註釋的就是自動添加的內容。
$ service nginx restart
$ service iptables restart
因而滿懷信心的打開本身的網站發現並不能訪問!
而後添加安全組規則,將443端口添加進來就哦了~
緣由在於:網站頁面上面引用了不是https的資源,最多見的就是引用的圖片、logo點擊事件設置的超連接等地方,友鏈不必定有影響(能夠設置試試,我這裏友鏈沒有影響)。
解決辦法:貌似沒什麼快捷的方法,反正我是把硬編碼的地方把http改爲了https,其中最多的就是圖片引用連接,由於文章自己很少,並且引用的圖片以前是經過後臺管理上傳到服務器的,如今已所有修改成從微薄圖牀引用,引用路徑都是https。(方法比較笨,有其餘解決方案能夠留言,拯救一下我這坨菜雞)
補充:若是用的是wordpress建站記得修改常規選項中的站點地址。(設置>常規)
啓用https後效果圖以下:
友鏈沒有影響,主要影響是logo的超連接,以及網站首頁超連接。
執行命令添加定時任務(修改命令相同)
$ 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 |