咱們使用的服務器是在公司內部,用聯通送的ip,經過路由器隱射使得外網能夠訪問,咱們在這個服務器搭建了不少工具,好比Gitlab,聊天工具,網盤等,訪問都很麻煩,沒有備案,都必須帶上端口號訪問對應的服務,聽說80端口被封了,假設有了https就能夠默認443端口,就不用帶端口號了,經過https訪問默認瀏覽器會給你帶上443端口,下面是我使用 Let's Encrypt 提供的SSL證書,記錄配置SSL的安裝實踐過程。html
首先要安裝 Let's Encrypt
證書用的工具,這個能夠在CentOS 的 EPEL 倉庫裏找到它,在找到它以前,先檢查是否存在 EPEL
源:git
# 進入目錄檢查是否存在 EPEL 源,通常狀況文件名稱 epel.repo
cd /etc/yum.repos.d/
複製代碼
若是不存在能夠直接安裝github
sudo yum install epel-release -y
複製代碼
sudo yum install certbot-nginx -y
複製代碼
sudo certbot --nginx
# Saving debug log to /var/log/letsencrypt/letsencrypt.log
# The nginx plugin is not working; there may be problems with your existing configuration.
# The error was: NoInstallationError()
# 若是你報上面錯誤運行下面,命令解決問題
which nginx # 查看目錄
#輸出 /usr/local/nginx/sbin/nginx
yum info nginx
複製代碼
解決方法算法
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/ /etc/nginx
複製代碼
sudo certbot --nginx
# Saving debug log to /var/log/letsencrypt/letsencrypt.log
# Error while running nginx -c /etc/nginx/nginx.conf -t.
# nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
# nginx: configuration file /etc/nginx/nginx.conf test failed
# The nginx plugin is not working; there may be problems with your existing configuration.
# The error was: MisconfigurationError('Error while running nginx -c /etc/nginx/nginx.conf -t.\n\nnginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)\nnginx: configuration file /etc/nginx/nginx.conf test failed\n',)
複製代碼
解決方法,這個解決方法就是讓certbot
認爲你的,你的配置存在,而且將SSL配置寫入你的nginx配置文件中,而後拷貝配置到你的默認 nginx 配置中。哈哈爲了方便 nginx 啓動不用指定配置,也沒有看到 certbot
工具提供指定目錄的命令,暫時我就這麼解決吧。vim
# nginx 默認配置文件目錄不 /etc/nginx/ 目錄下,
# 須要拷貝/usr/local/nginx/conf目錄下的所有文件
# 複製到/etc/nginx/目錄下
cp -r /usr/local/nginx/conf/* /etc/nginx/
# 編輯開機啓動將全部目錄換成/etc/nginx/
vim /lib/systemd/system/nginx.service
cp /lib/systemd/system/nginx.service{,.bak}
# 測試配置是否正確
nginx -t -c /etc/nginx/nginx.conf
複製代碼
正確以後將nginx
中的 SSL
配置複製到你的原來正在運行的配置中,在默認安裝目錄配置/usr/local/nginx/conf/
。會在配置中生成以下內容,主要是複製這個。api
{
ssl_certificate /etc/letsencrypt/live/chat.wangchujiang.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/chat.wangchujiang.com/privkey.pem; # managed by Certbot
}
複製代碼
首先運行證書生成命令,選擇你要配置SSL
證書的網站,這個是基於你網站已經在nginx
中配置好了的狀況。瀏覽器
sudo certbot --nginx certonly
# Saving debug log to /var/log/letsencrypt/letsencrypt.log
# Plugins selected: Authenticator nginx, Installer nginx
# Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org
#
# Which names would you like to activate HTTPS for?
# -------------------------------------------------------------------------------
# 1: chat.wangchujiang.com
# 2: g.wangchujiang.com
# 3: pan.wangchujiang.com
# -------------------------------------------------------------------------------
# Select the appropriate numbers separated by commas and/or spaces, or leave input
# blank to select all options shown (Enter 'c' to cancel): 2
複製代碼
上面選擇了g.wangchujiang.com
生成證書,下面是驗證過程,添加 certonly
參數表示只生成證書。緩存
# Obtaining a new certificate
# Performing the following challenges:
# tls-sni-01 challenge for g.wangchujiang.com
# Waiting for verification...
# Cleaning up challenges
#
# IMPORTANT NOTES:
# - Congratulations! Your certificate and chain have been saved at:
# /etc/letsencrypt/live/g.wangchujiang.com/fullchain.pem
# Your key file has been saved at:
# /etc/letsencrypt/live/g.wangchujiang.com/privkey.pem
# Your cert will expire on 2018-03-13. To obtain a new or tweaked
# version of this certificate in the future, simply run certbot
# again. To non-interactively renew *all* of your certificates, run
# "certbot 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
複製代碼
上面生成成功了,能夠添加到 nginx 配置中,這下完事兒了,下面是一端nginx的配置實例。bash
# http 重定向到 https
server {
listen 80;
server_name g.wangchujiang.com;
rewrite ^ https://$http_host$request_uri? permanent;
# Enables or disables emitting nginx version on error pages and in the "Server" response header field.
server_tokens off;
}
# https 的配置
server {
listen 443 ssl;
server_name g.wangchujiang.com;
ssl_certificate /etc/letsencrypt/live/g.wangchujiang.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/g.wangchujiang.com/privkey.pem;
# 禁止在header中出現服務器版本,防止黑客利用版本漏洞攻擊
server_tokens off;
# 設置ssl/tls會話緩存的類型和大小。若是設置了這個參數通常是shared,buildin可能會參數內存碎片,默認是none,和off差很少,停用緩存。如shared:SSL:10m表示我全部的nginx工做進程共享ssl會話緩存,官網介紹說1M能夠存放約4000個sessions。
ssl_session_cache shared:SSL:1m;
# 客戶端能夠重用會話緩存中ssl參數的過時時間,內網系統默認5分鐘過短了,能夠設成30m即30分鐘甚至4h。
ssl_session_timeout 5m;
# 選擇加密套件,不一樣的瀏覽器所支持的套件(和順序)可能會不一樣。
# 這裏指定的是OpenSSL庫可以識別的寫法,你能夠經過 openssl -v cipher 'RC4:HIGH:!aNULL:!MD5'(後面是你所指定的套件加密算法) 來看所支持算法。
ssl_ciphers HIGH:!aNULL:!MD5;
# 設置協商加密算法時,優先使用咱們服務端的加密套件,而不是客戶端瀏覽器的加密套件。
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
複製代碼