Ubuntu Apache 服務器 + HTTPS/SSL 配置
簡單總結了下在一個空 Linux 主機去砌 Apache Web Server 帶 SSL
安裝 Apache Web 服務器
- 升級系統
1
sudo apt update && sudo apt upgrade
- 安裝 Apache
1
sudo apt install apache2
- 防火牆允許
1
2sudo ufw allow 'Apache'
sudo ufw status - 檢查防火牆狀態
1
sudo ufw status
- 改變
網頁文件存放目錄
權限1
sudo chmod -R 755 /var/www/your_domain
- 給當前用戶改變
網頁文件存放目錄
權限1
sudo chown -R $USER:$USER /var/www/your_domain
- 添加域名配置
vi /etc/apache2/sites-available/your_domain.conf
1
2
3
4
5
6
7
8
9<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain
ServerAlias www.your_domain
Redirect permanent / https://www.yourdomain.com/
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost> - 啓動配置(啓動
a2ensit
,關閉a2dissit
)1
2
3
4sudo a2ensite your_domain.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
sudo systemctl restart apache2
配置 HTTPS/SSL
- 去 https://www.sslforfree.com/ 獲取
Let's Encrypt
證書,有ca_bundle.crt
、private.key
、certificate.crt
- 創建/修改
vi /etc/apache2/sites-available/default-ssl.conf
,加上下面的內容1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile path/certificate.crt
SSLCertificateKeyFile path/private.key
SSLCertificateChainFile path/intermediate-ca_bundle.crt
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
# BrowserMatch "MSIE [2-6]" \
# nokeepalive ssl-unclean-shutdown \
# downgrade-1.0 force-response-1.0
</VirtualHost>
</IfModule> - 防火牆配置
1
2sudo ufw allow 'Apache Full'
sudo ufw delete allow 'Apache' - 啓動
1
2
3
4
5
6sudo a2enmod ssl
sudo a2enmod headers
sudo a2ensite default-ssl
sudo a2enconf ssl-params
sudo apache2ctl configtest
sudo systemctl restart apache2
其他相關信息
Apache 相關命令
1 | sudo systemctl stop apache2 |
Apache 相關路徑
1 | /etc/apache2: The Apache configuration directory. All of the Apache configuration files reside here. |