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

 

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