nginx配置Let's Encrypt https证书与开启http2协议

386
原文链接: www.yodfz.com

一、环境要求

  1. git
  2. openSSL 版本要求大于1.0.2
  3. nginx 最新稳定版本1.12.2
  4. certBot

二、预装环境

请先将openSSL升级到版本>1.0.2,nginx>= 1.12.2

nginx需要升级到1.12.2版本

2.1 安装certBot

cd /
mkdir soft
cd soft
mkdir staticHtml   //这个有用处后面会说
git clone https://github.com/certbot/certbot

2.2 配置nginx的certbot认证目录

切换到nginx的conf.d目录。 打开需要配置https域名的配置文件, 在配置文件的server模块中加入以下的话。

location ^~ /.well-known/acme-challenge/ {
   default_type "text/plain";
   root     /soft/staticHtml;
}

重启nginx以便映射生效

service nginx restart

三、申请证书

sudo certbot certonly --webroot -w /soft/staticHtml/ -d your.domain.com

在经过一会时间之后,成功会提示以下信息

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/a.domain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/a.domain.com/privkey.pem
   Your cert will expire on 2018-07-02. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

3.1 参数说明

3.1.1 --webroot

certbot生成域名验证文件所在文件目录。

3.1.2 -d

需要生成证书的域名,多个域名可以用-d链接。如:

-d a.domain.com -d b.domain.com -d c.domain.com

3.1.3 -email

接收有关账户的重要通知的邮箱地址

-email domain@domain.com

3.1.4 --webroot

把身份认证文件放置在服务器的网页根目录下

3.1.5 -w

指定生成验证文件所在的目录,且这个目录可以通过申请的域名访问进行验证。

四、 配置nginx SSL

拷贝一份原来http的nginx配置文件,并且修改与新增以下配置。

server {
        listen 443 ssl http2;

        ssl_certificate /etc/letsencrypt/live/your.domain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/your.domain.com/privkey.pem;
        ssl_trusted_certificate /etc/letsencrypt/live/your.domain.com/chain.pem;
}

重启nginx server。

通过浏览器打开your.domain.com 即可查看是否成功开启http支持与http2功能了。

五、 需求软件安装

5.1 git安装

建议通过yum安装

yum update
yum install git

5.2 openSSL安装

nginx需求openSSL版本大于 1.0.2,建议升级至1.1.0+,因为在此之后的版本可以支持移动端更友好的Google ChaCha20 加密方式(ARM手机优化,更快更省电)。

首先查看openSSL版本

openssl version

下载安装

cd /soft
mkdir back
wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz
tar -zxf openssl-1.1.0e.tar.gz
cd openssl-1.1.0h
./config
make
make test
make install
# 备份旧的openssl到之间在soft下建立的back文件夹
mv /usr/bin/openssl /soft/back
ln -s /usr/local/bin/openssl /usr/bin/openssl

# 更新ssl链接库
ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

# 删除旧的符号链接
rm /bin/openssl

# 添加新的
ln -s /usr/local/bin/openssl /bin/openssl

# 查看版本
openssl version
# 如果为1.1.0h表示更新openSSL成功

另外重启一下nginx然后输入nginx -V

nginx version: nginx/1.12.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
built with OpenSSL 1.1.0h  27 Mar 2018
TLS SNI support enabled

查看OpenSSL版本是否已经变更为了最新版本,如果没有,请参考5.3重新编译更新nginx

5.3

下载最新的nginx

cd /soft
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar -zxf nginx-1.12.2.tar.gz
cd nginx-1.12.2.tar.gz

查看当前系统的编译配置

nginx -V
config arguments:--prefix=/usr/share/nginx ..........

复制保存好config arguments后面的配置。

配置nginx

./configure 这里粘贴之前的配置

如果提示一些警告说明你没有安装需求的类库,根据提示的信息使用yum安装相关库支持。

备份之前nginx的conf文件

cp -r /etc/nginx/conf.d  /soft/back

重新编译,覆盖安装

make
make install

重启服务

service nginx restart