下面给你一份在 Debian 上配置 Apache2 的实用指南,从安装到常见配置,循序渐进。
sudo apt update
sudo apt install apache2
安装完成后,Apache 会自动启动。
查看状态:
sudo systemctl status apache2
设置开机自启:
sudo systemctl enable apache2
sudo ufw allow 'Apache Full'
或只开放 80/443:
sudo ufw allow 80
sudo ufw allow 443
重要目录:
/var/www/html:默认网站根目录/etc/apache2/:主配置目录
apache2.conf:主配置文件sites-available/:可用站点配置sites-enabled/:已启用站点(软链接)mods-available/ / mods-enabled/:模块ports.conf:端口配置编辑默认站点:
sudo nano /etc/apache2/sites-available/000-default.conf
示例:
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html
ServerName example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
测试配置:
sudo apache2ctl configtest
重载 Apache:
sudo systemctl reload apache2
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com
sudo nano /etc/apache2/sites-available/example.com.conf
示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
sudo a2enmod rewrite
sudo a2enmod ssl
sudo systemctl restart apache2
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com
自动续期测试:
sudo certbot renew --dry-run
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl reload apache2
sudo apache2ctl -t
sudo tail -f /var/log/apache2/error.log
如果你有具体需求(如 PHP、反向代理、WordPress、局域网部署),可以告诉我,我给你更针对性的配置。