在Ubuntu上安裝和配置Apache2時,遵循以下最佳實踐可以幫助你確保服務器的安全性、性能和可維護性:
sudo apt update
sudo apt install apache2
sudo systemctl start apache2
sudo systemctl enable apache2
在瀏覽器中輸入服務器的IP地址或域名,查看是否看到Apache2的默認歡迎頁面。
/etc/apache2/ports.conf
文件,找到 Listen 80
并修改為 Listen 5000
。/etc/apache2/apache2.conf
文件,找到 <Directory/>
配置段,修改 Options
和 AllowOverride
選項以限制訪問權限。<Directory /mnt/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
/etc/apache2/sites-available/000-default.conf
文件,添加新的虛擬主機配置。<VirtualHost *:80>
ServerName example.com
DocumentRoot /mnt/www/example.com
<Directory /mnt/www/example.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
sudo a2ensite example.com.conf
openssl
:sudo apt install openssl
sudo openssl req -x509 -newkey rsa:1024 -keyout apache.pem -out apache.pem -nodes -days 36500
/etc/apache2/sites-available/default-ssl.conf
文件,添加以下內容:SSLEngine on
SSLCertificateFile /path/to/apache.pem
SSLCertificateKeyFile /path/to/apache.pem
sudo a2enmod ssl
sudo ufw allow 'Apache Full'
event
、prefork
或 worker
MPM。新安裝的系統通常使用 worker
MPM。a2dismod
命令禁用不需要的模塊,以減少內存占用。/etc/apache2/apache2.conf
文件,根據需求調整以下參數:KeepAlive On
MaxClients 256
HostnameLookups Off
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
而不是 Allow from all
以提高安全性。遵循這些最佳實踐,你可以在Ubuntu上成功安裝、配置和優化Apache2服務器,同時確保其安全性。