配置 Apache2 網站
373 字
2 分鐘
配置 Apache2 網站
正常來說,Apache的網站配置檔會在「/etc/apache2/sites-available」之下,因此可以為每個網站建立不同的配置檔。
配置HTTP (80 port)
編輯「/etc/apache2/sites-available/<your.domain.name>.conf」
# 配置網站資訊 ServerName ServerAdmin admin@localhost DocumentRoot /var/www/
# 配置LOG資訊 ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # 代理設定 # ProxyPass / http://localhost:8080/ # 導到http # ProxyPass / unix:/path/to/project/uwsgi/uwsgi.sock # 導到uwsgi
# [可選] 配置Rewrite資訊(需要啟用mod_rewrite) #ReDirect any HTTP request to HTTPS RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]有些情況要啟用模組
若有啟用RewriteEngine,要啟用模組
sudo a2enmod mod_rewrite若有啟用proxy,要啟用模組
# PROXYsudo a2enmod proxy
# PROXY到HTTPsudo a2enmod proxy_balancersudo a2enmod proxy_http
# PROXY到uWSGIsudo a2enmod proxy_uwsgi若有啟用ssl,要啟用模組
sudo a2enmod ssl配置HTTPS (SSL 443 port)
編輯「/etc/apache2/sites-available/<your.domain.name>-ssl.conf」
# 開啟SSL SSLEngine On SSLCertificateFile /path/to/you_domain_ssl_cert/cert.pem SSLCertificateKeyFile /path/to/you_domain_ssl_cert/privkey.pem SSLCertificateChainFile /path/to/you_domain_ssl_cert/fullchain.pem # 網站設定 ServerName ServerAlias DocumentRoot /var/www/ # 網站日誌 ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined編輯「/etc/apache2/ports.conf」打開apache監聽port
Listen 80
Listen 443
Listen 443打開防火牆
ufw allow 443sudo ufw reload配置目錄存取規則
配置目錄設定,編輯「/etc/apache2/apache2.conf」
> Options Indexes FollowSymLinks # 若想禁止存取檔案,改為Options FollowSymLinks AllowOverride All Require all granted啟用配置
查看已啟用的conf檔
ls -la /etc/apache2/sites-enabled啟用網站配置並重置伺服器(ssl和非ssl若有多個conf檔,多個都要啟用)
# 啟用網站配置sudo a2ensitesudo a2ensite -ssl
# 重新載入Apache2配置sudo systemctl reload apache2# 重啟Apache2sudo systemctl restart apache2# 停止Apache2sudo systemctl stop apache2# 啟動Apache2sudo systemctl start apache2文章分享
如果這篇文章對你有幫助,歡迎分享給更多人!
配置 Apache2 網站
https://linziyou.info/posts/2022-12-01-配置-apache2-網站/ 最後更新於 2022-12-01,距今已過 1187 天
部分內容可能已過時