CentOS+Apache使用免費SSL證書實現https網站

如下爲centos6.4下配置使用流程html

1. 環境,httpd等配置略git

yum 安裝openssl和openssl-devel,httpd-develgithub

sudo yum install opensslapache

sudo yum install openssl-develcentos

sudo yum install httpd-devel安全

 

爲apache安裝配置mod_ssl和mod_wsgiide

sudo yum -y install mod_ssl網站

sudo yum -y install mod_wsgirest

(注意,經過以上方式,yum會自動配置好並引用mod_ssl的相關conf,無需在http.conf文件之中另外引用)htm

 

 

2. 使用xdtianyu童鞋編寫的腳本(網址https://github.com/xdtianyu/scripts/blob/master/lets-encrypt/README-CN.md)自動生成Let's Encrypt(https://letsencrypt.org)的免費SSL證書:

sudo wget https://raw.githubusercontent.com/xdtianyu/scripts/master/lets-encrypt/letsencrypt.conf
sudo wget https://raw.githubusercontent.com/xdtianyu/scripts/master/lets-encrypt/letsencrypt.sh
sudo chmod +x letsencrypt.sh

 

修改letsencrypt.conf配置文件

ACCOUNT_KEY="letsencrypt-account.key"
DOMAIN_KEY="example.com.key"
DOMAIN_DIR="/var/www/example.com"
DOMAINS="DNS:example.com,DNS:whatever.example.com"
#ECC=TRUE
#LIGHTTPD=TRUE

 

 

將其中的example.com改成你的域名,將DIR之中的目錄改成你的首頁所在目錄,確保此時能夠經過example.com正常訪問到你的網站(由於以後生成證書的時候須要經過域名訪問你的網站,若是沒法訪問,證書會報錯)

而後生成證書:

sudo ./letsencrypt.sh letsencrypt.conf

會生成一堆文件,其中要用到的是:

example.chained.crt
example.com.key

 

3. 修改http.conf

找到「<Directory」  ,把 AllowOverride None改成 AllowOverride All

添加NameVirtualHost *:443

添加以下的段:

<VirtualHost _default_:443>
DocumentRoot 「/path/to/html/example.com"
Servername https://example.com/
SSLEngine on
SSLCertificateFile /path/to/cert/example.chained.crt
SSLCertificateKeyFile /path/to/cert/example.com.key
</VirtualHost>

 

重啓apache:

sudo service httpd restart

 

此時能夠經過https://example.com訪問網站,而且地址欄左邊會出現綠色小鎖頭,表示ssl證書已經配置成功。

 

4. 配置跳轉

若是須要輸入http://example.com以後自動跳轉到https://example.com

要在/path/to/html/example.com目錄下,添加.htaccess文件,在其中添加以下內容:

RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

 

此時訪問http://example.com便可自動跳轉到https://example.com

 

5.按期更新Let's Encrypt的免費ssl證書:

設置一個cron命令每個月執行第二步之中的生成證書命令:

0 0 1 * * /path/to/letsencrypt.sh /path/to/letsencrypt.conf >> /var/log/lets-encrypt.log 2>&1

(上述命令會在/path/to/目錄下生產所有的ssl證書文件)

再加一條apache的reload指令,讓ssl證書生效

0 0 1 * * service httpd reload

 

6.安全起見,在httpd.conf里加一條禁止經過ip訪問的規則

<VirtualHost *:80>
ServerName xx.xx.xx.x
<Location />
        Order Allow,Deny
        Deny from all
</Location>
</VirtualHost>

 

 

7. 最後注意一點,let's encrypt目前還不支持通配符ssl證書,只支持單域名證書,例如你申請了www.hello.com的證書,那麼該證書對info.hello.com是無效的,須要另申請一個info.hello.com的ssl證書

相關文章
相關標籤/搜索