finch最近正在研究一個新的網站系統,閒的沒事想搞搞ssl,結果搞了兩天,遇到不少問題,如今記錄並分享一下經驗。php
環境以前搭建好了是Ubuntu14+nginx+php5+mysqlhtml
sudo apt-get update sudo add-apt-repository ppa:certbot/certbot sudo apt-get update sudo apt-get install python-certbot-nginx
若是提示找不到add-apt-repository那麼安裝下邊的這個就行了:python
sudo apt-get install python-software-properties sudo apt-get install software-properties-common
若是沒有Python的話本身安裝就行了這裏就不說了。mysql
安裝let's encrypt證書有兩種方法,能夠自行百度。這裏用的其中一種,就是先要中止80端口的程序,也就是nginx:linux
service nginx stop
生成單域名證書:nginx
certbot certonly --standalone --email your@email.com -d yourdomain.com
上邊的your@email.com和yourdomain.com記得寫成你本身的,email地址能夠用於之後找回丟失的證書。web
接下來會提示是否接受許可條款,是否安裝之類的話,若是以前安裝過證書還會提示你是保留仍是從新生成。看狀況本身選擇就好了。sql
出現這段文字就是證書生成成功了。由於let's encrypt的免費證書有90的期限,過了期限就要本身續期(好比上邊顯示個人證書會在2018-05-10到期)。上邊的紅色部分就是以前你寫的域名,這兩個路徑必定要注意,這是你的兩個證書的目錄,下邊配置nginx的時候要用到。vim
目錄「/etc/letsencrypt/live/你剛纔寫的域名/」下的幾個文件:安全
cert.pem - Apache服務器端證書
chain.pem - Apache根證書和中繼證書
fullchain.pem - Nginx所須要ssl_certificate文件
privkey.pem - 安全證書KEY文件
首先解釋一下nginx的幾個文件夾的區別和做用:
nginx.conf:是nginx的配置文件,這裏咱們不須要改這個。
sites-available:是虛擬機vhost的配置文件夾。
sites-enabled:是上邊sites-available文件夾內文件的軟鏈接文件夾。至於爲何nginx這麼玩自行百度吧。
其實看看nginx.conf中就會發現它並無引用sites-available,而是直接引用的sites-enabled。
下邊咱們須要在sites-available文件夾裏邊建立ssl.conf來保存咱們的ssl配置:
sudo vim /etc/nginx/sites-available/ssl.conf
(提示沒有vim的自行安裝便可)
#把下邊的內容複製進去,而後按照提示修改 server { # SSL configuration listen 443 ssl; listen [::]:443 ssl; ssl on; ssl_certificate /etc/letsencrypt/live/以前你填寫的域名/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/以前你填寫的域名/privkey.pem; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; root /var/www/html; #這裏要改爲你本身的nginx網站的root目錄 # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; #這裏加上index.php就好了 }
而後如今要在sites-enabled文件夾中創建這個文件的軟鏈接:
sudo ln -s /etc/nginx/sites-available/ssl.conf /etc/nginx/sites-enabled/ssl.conf
而後重啓或者從新加載nginx配置文件
service nginx restart
若是想讓網站默認只能訪問https://,把80的連接強制跳轉到443端口,能夠在sites-available下邊再寫一個文件:
server { listen 80 default_server; listen [::]:80 default_server; server_name 剛纔你寫的域名; rewrite ^(.*) https://$server_name$1 permanent; }而後像剛纔那樣再創建一個軟鏈接,而後重啓nginx便可。
若是上邊的rewrite不成功的話,能夠在原來的80的配置文件裏直接寫一個301重定向就能夠了。挺簡單的。
#這是原來的80端口的配置文件 server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /home/judge/src/web; index index.php index.html index.htm; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # With php5-cgi alone: fastcgi_pass 127.0.0.1:9000; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } #在下邊這裏寫一個301重定向就能夠了。由於有的人說let's encrypt證書的認證可能須要一個80端口,因此不能所有轉換爲443端口 location / { return 301 https://$server_name$request_uri; } }
而後reload nginx就能夠了。之後輸入http的網址會直接跳轉到https的網址了。
其實到這裏就結束了。
問題1:輸入https://的網址會直接下載首頁php文件。直接訪問http://就能正常訪問。
解決:在剛纔ssl.conf裏邊加上這段。這個問題是由於nginx默認沒有解析.php文件而是直接打開了。(查看答案來源)
location ~ .*\.php$ { fastcgi_pass 127.0.0.1:9000; }
問題2:輸入了上邊的話,沒有下載首頁,但是直接顯示502錯誤。
解決:把上邊的話改爲下邊的就行了。上邊的的 fastcgi_pass 變量應該是錯的,適用於 PHP 5.3 及如下,在 PHP 5.4 以後,php5-fpm 並非監聽 9000 端口(答案來源和具體解釋)
location ~ \.php$ { # fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; }
這下就行了。
因爲個人這個網站系統裏邊有點小問題,因此暫時不顯示綠色的https。後邊不想搞了。
其餘的頁面訪問時正常的綠色:
歡呼三聲:喵!喵!喵!
本文參考的網絡資源:
http://www.javashuo.com/article/p-fpnadbzg-en.html
http://www.linuxidc.com/Linux/2017-02/140111.htm
http://www.cnblogs.com/taosim/articles/3291638.html
https://zhidao.baidu.com/question/581467526.html
很是感謝上面的大佬和前輩!