使用let's encrypt为你的Ubuntu14+nginx网站保驾护航!

finch最近正在研究一个新的网站系统,闲的没事想搞搞ssl,结果搞了两天,遇到不少问题,如今记录并分享一下经验。搞笑图1php

环境以前搭建好了是Ubuntu14+nginx+php5+mysqlhtml

 

如今开始使用let's encrypt官方给出的安装脚本安装便可:

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的几个文件夹的区别和做用:

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://就能正常访问。

输入https直接进入下载

解决:在刚才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

很是感谢上面的大佬和前辈!

最后附上本文连接:https://my.oschina.net/finchxu/blog/1621049

相关文章
相关标签/搜索