Maojui

Apache 安裝 | Linux

2022-04-22

Apache 安裝

用 apt install 安裝

1
2
3
sudo apt update
sudo apt upgrade -y
sudo apt install apache2 -y

設定防火牆

開啟防火牆

1
sudo ufw enable

設定

1
2
3
4
5
6
7
8
# 可以看有誰可以開
sudo ufw app list

# 拜託別把 ssh 關掉,你會回不去 =口=||
sudo ufw allow "OpenSSH"

sudo ufw allow "Apache Full"
sudo ufw status # 看防火牆狀態

看 Apache2 狀態

1
systemctl status apache2

開啟 HTTPS

1
2
3
apt-get install openssl
a2enmod ssl # 開啟 Apache2 套件: Mod_ssl
a2enmod rewrite # 開啟 Apache2 套件: Mod_rewrite

編輯設定檔: apache2.conf

1
vim /etc/apache2/apache2.conf

再檔案最後面加上,下方內容: 告訴 Apache2 要改掉哪個資料夾

1
2
3
<Directory /var/www/html>
AllowOverride All
</Directory>

製造 Private key

1
2
3
mkdir /etc/apache2/certificate
cd /etc/apache2/certificate
openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out apache-certificate.crt -keyout apache.key

設定 /etc/apache2/sites-enabled/000-default.conf

1
vim /etc/apache2/sites-enabled/000-default.conf

定義規則

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</virtualhost>
<VirtualHost *:443>
ServerAdmin maojui@localhost
ServerName maojui.me
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/certificate/apache-certificate.crt
SSLCertificateKeyFile /etc/apache2/certificate/apache.key
</VirtualHost>

重新啟動

1
service apache2 restart