linux服务器上在nginx配置SSL证书

一 .前言

随着SSL证书时代的到来,https逐渐成为网站的标配。letsencrypt更是因为免费深受广大用户的喜欢。之前实践过windows服务器上安装letsencrypt,有专门的一键安装工具。linux服务器上如果是使用apache也可以通过宝塔来一键安装letsencrypt。但是目前因为很多项目中都会使用nginx作为前端服务器,而宝塔没有提供nginx的一键安装letsencrypt功能,所以考虑自己动手在linux系统上安装

二 .letsencrypt和certbot的关系

从certbot的官网中可以看到这么一段"Certbot is an easy-to-use client that fetches a certificate from Let’s Encrypt—an open certificate authority launched by the EFF, Mozilla, and others—and deploys it to a web server."由此可见,certbot是一个可以用来获取letsencrypt证书的客户端,并且是被letsencrypt官方推荐的客户端,其他可用的客户端请见letsencrypt官方介绍。所以我们今天的主角就是certbot了

三.安装certbot

众所周知,python是有两个版本并行发展的,python2和python3,但是certbot的python2版本的支持似乎已经接近了尾声,具体表现是在执行证书renew时会报错的,看到有些文章提到使用./certbot renew --no-self-upgrade 可以解决问题,但无奈我的却不好使,仍然报错。最终找到官方的一篇文章介绍需要安装python3环境,并且安装pyenv来确保不影响你系统本身的python环境,pyenv的作用是python的虚拟环境,使系统上可以同时运行python2和python3.具体安装的参考文章看这里。这里我也把大概的步骤贴出来

1.安装pyenv

# Counter "/tmp" being mounted noexec
export TMPDIR=/var/tmp

# Install pyenv
curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash

2.保存环境变量~/.bashrc

export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

 

3.安装python 3.5.0 注意目前pyton最低版本要求是3.5.0,如果按照官网提示安装3.4.2,再安装其他工具时会提示python版本低。以后说不定3.5.0也不是最低要求,请大家根据实际情况调整。

pyenv install 3.5.0

4.安装certbot 请注意第一句"export PYENV_VERSION=3.5.0"很重要,表示是在这个版本的python环境下安装.因为我是在nginx下安装,所以最后安装certbox-nginx插件

# Run the following commands with the designated Python version
export PYENV_VERSION=3.5.0

# Upgrade setuptools
pip install setuptools --upgrade

# Install certbot
pip install certbot requests requests-toolbelt pbr

# Optionally, install plugins
pip install certbot-nginx

5.建立certbot的软链接到系统安装路径下

ln -s /root/.pyenv/versions/3.5.0/bin/certbot /usr/local/bin/certbot

 

6.建立nginx路径的软链接。这一步看你自己安装nginx的路径,certbox配置nginx的安装路径是/etc/nginx,如果你的nginx是在这个路径,那就不需要建软链接。因为我的nginx是通过宝塔安装的,安装路径为/www/server/nginx.所以执行以下语句建立软链接

ln -s /www/server/nginx/conf/ /etc/nginx

 

7.创建证书 执行以下命令

certbot --nginx

8.自动续租。因为nginx下可能管理多个web网站,每个网站建立ssl的时间不一样。我们就创建一个定时任务每天执行一次检查有没有到期

0 2 * * * certbot renew

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章