技術存檔:建站(二),升級https

在被http劫持的廣告坑過以後,就想着之後要弄網站就搞https的。大過年閒着沒事,買了雲服務器,把本身的域名從新解析綁定了一遍,最後升級成了https

阿里雲服務器ESC購買時間:2018-02-19
系統和版本:CentOS 7.x
web服務器:nginx(根據我的需求而定)html

好久沒用過,發現ESC多了個安全組規則的東西。各端口須要配置對應的受權規則才能訪問,總體安全性更高了。nginx

當前網站選擇最簡單的DV證書便可,花了一點時間查資料後,選擇申請Let`s Encrypt免費SSL證書。git

Let’s Encrypt is a free, automated, and open Certificate Authority.

1、HTTP環境

配置完成後能正常訪問http環境便可

一、web服務器安裝

# 安裝nginx
yum install nginx -y
# 啓動
systemctl start nginx

二、配置安全組規則策略

默認會有提示,或者在雲服務器ESC中二級菜單下,找到安全組,而後配置規則添加安全組規則,http和https規則配置如圖github

clipboard.png

三、域名解析配置(略)

2、SSL證書

一、證書生成和配置

可直接使用 Certbot 工具生成和配置SSL證書:web

# 安裝git
yum install git -y
# 下載工具到本地
git clone https://github.com/certbot/certbot.git
cd certbot/
# 這裏是配置nginx服務,請根據狀況運行對應命令,具體請閱讀Certbot文檔
./certbot-auto --nginx

根據提示輸入對應命令,如郵箱、選擇是否繼續、須要認證的域名(空格隔開)等,郵箱須要點擊認證。部分命令配置用於生成本地nginx.conf配置,顯示以下:安全

clipboard.png

二、SSL安全組規則策略

配置安全組規則策略圖片中的443端口須要開啓服務器

三、重啓服務並測試

nginx.conf文件將被Certbot工具自動配置完成,配置完成代碼以下工具

server {
    server_name lxlazy.com; # managed by Certbot
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/lxlazy.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/lxlazy.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = lxlazy.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen       80 ;
    listen       [::]:80 ;
    server_name lxlazy.com;
    return 404; # managed by Certbot
}

將默認的server配置註釋掉後,重啓nginx,測試效果測試

systemctl restart nginx

3、其餘

一、nginx重啓出錯解決辦法

重啓可能遇到如下問題 invalid PID number "" in "/var/run/nginx.pid",或nginx -c still could not bind網站

# 先查看配置,再看端口,解除端口占用後重啓nginx,通常均可以解決問題
# 配置
nginx -t
# 端口
netstat -ntlp

實在不行重啓主機吧……

二、部署結果

clipboard.png

相關文章
相關標籤/搜索