
本文共 3589 字,大约阅读时间需要 11 分钟。
透過ZeroSSL為Apache網站申請憑證
申請步驟
SSL certificate setup
首先到https://manage.sslforfree.com/login註冊一個帳號並登入後,會看到以下畫面:
點選Create SSL Certificate
這一格的New Certificate
。
在Enter Domains
處輸入事先申請好的域名,其它欄位如:Validity
,CSR & Contact
,Finalize Your Order
皆維持預設值。
在驗證方法這裡選取HTTP File Upload
,接下來我們需要在己有的Apache server上建立一個頁面,讓任何人都人從該頁面上下載ZeroSSL所提供的Auth File。
Setup Apache download server
首先,進入/var/www/html
,創建.htaccess
文件,填入以下內容:
AddType application/octect-stream .txt
然後建立.well-known/pki-validation/
兩層目錄,把下載下來的Auth File放到該目錄內:
mkdir -p .well-known/pki-validationmv <auth_file>.txt .well-known/pki-validation
至此,可以在瀏覽器中開啟http://<domain_name>/.well-known/pki-validation/<auth_file>.txt
,應該可以看到Auth File的內容。確認無誤後在ZeroSSL上點選Next Step
。
Installing SSL Certificate on Apache
接著下載ZeroSSL所提供的zip檔,解壓後會得到以下三個檔案:
ca_bundle.crtcertificate.crtprivate.key
把ca_bundle.crt
及certificate.crt
放入/etc/ssl/certs
;把private.key
放入/etc/ssl/private
。
接著修改/etc/apache2/apache2.conf
,加入:
SSLEngine onSSLCertificateFile /etc/ssl/certs/certificate.crtSSLCertificateKeyFile /etc/ssl/private/private.keySSLCertificateChainFile /etc/ssl/certs/ca_bundle.crt
然後啟用SSL module:
sudo a2enmod ssl
最後重啟Apache:
sudo systemctl restart apache2.service# service apache2 restart #in docker
完成後在ZeroSSL上點選Check Installation
。
至此,憑證申請完成,以後可以使用https://
來訪問你自己的網域了!
Apply to different webpage
做完以上的步驟後就可以訪問Apache默認的網頁了,但是如果想要訪問其它網頁呢?以下以redmine為例。
本來/etc/apache2/sites-available/redmine.conf
的內容如下:
<VirtualHost *:80> #ServerName redmine.example.com DocumentRoot /xxx/redmine/public PassengerRoot /usr/share/rvm/gems/ruby-2.7.0/gems/passenger-6.0.7 PassengerDefaultRuby /usr/share/rvm/gems/ruby-2.7.0/wrappers/ruby PassengerUser redmine <Directory /xxx/redmine/public> Allow from all Options -MultiViews Require all granted </Directory></VirtualHost>
port 80表示只能透過http訪問該頁面。需加上以下這段:
<VirtualHost *:443> #ServerName redmine.example.com DocumentRoot /xxx/redmine/public PassengerRoot /usr/share/rvm/gems/ruby-2.7.0/gems/passenger-6.0.7 PassengerDefaultRuby /usr/share/rvm/gems/ruby-2.7.0/wrappers/ruby PassengerUser redmine SSLEngine on SSLCertificateFile /etc/ssl/certs/certificate.crt SSLCertificateKeyFile /etc/ssl/private/private.key SSLCertificateChainFile /etc/ssl/certs/ca_bundle.crt <Directory /xxx/redmine/public> Allow from all Options -MultiViews Require all granted </Directory></VirtualHost>
其實大部份跟port 80那一段差不多,只是將port改為443,然後加上ssl相關的設定。(注意:做了以上改動後,要將剛剛在/etc/apache2/apache2.conf
加入的東西刪除。)
在剛剛的例子中,我們用的是apache2默認的網頁,即000-default.conf
,這裡我們要用的是redmine.conf
,所以需要停用000-default.conf
並啟用redmine.conf
:
a2dissite 000-default.confa2ensite redmine.conf
重啟apache:
service apache2 restart
出現以下錯誤:
Invalid command 'SSLEngine', perhaps misspelled or defined by a module not included in the server configurationAction 'configtest' failed.The Apache error log may have more information.
代表需使用以下指令啟用ssl module:
sudo a2enmod ssl
最後再重啟apache:
service apache2 restart
redirect http to https
做完了以上步驟,應該http://
及https://
都能成功打開,但是開啟http://
時,網址列會出現以下提示:
這說明http是不安全的,我們可以將所有http的請求都重導向至https來解決:
修改/etc/apache2/sites-available/redmine.conf
,在port 80那一段裡,DocumentRoot
之前,加上:
Redirect permanent / https://www.yourdomain.com
或:
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
然後記得要啟用rewrite這個module:
sudo a2enmod rewrite
否則會出現以下錯誤:
Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configurationAction 'configtest' failed.The Apache error log may have more information.
最後:
service apache2 restart