下面给你一个常见、实用的 Debian + Apache 配置指南,从安装到常用配置,适合新手和日常服务器使用。
sudo apt update
sudo apt install apache2
安装完成后,Apache 会自动启动。
查看状态:
sudo systemctl status apache2
常用命令:
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl enable apache2 # 开机自启
sudo ufw allow 80 # HTTP
sudo ufw allow 443 # HTTPS
sudo ufw reload
/etc/apache2/
├── apache2.conf # 主配置文件
├── ports.conf # 端口配置
├── sites-available/ # 可用站点配置
├── sites-enabled/ # 已启用站点(软链接)
├── mods-available/
├── mods-enabled/
├── conf-available/
└── conf-enabled/
网站根目录默认:
/var/www/html
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com
sudo nano /etc/apache2/sites-available/example.com.conf
示例配置:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo a2ensite example.com.conf
sudo systemctl reload apache2
禁用默认站点(可选):
sudo a2dissite 000-default.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
.htaccess 才能生效(前提是 AllowOverride All)。
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
证书会自动续期。
sudo a2enmod ssl
sudo a2enmod rewrite
sudo a2enmod headers
sudo a2dismod xxx
sudo apache2ctl configtest
看到:
Syntax OK
说明配置没问题。
Require all grantedrewriteAllowOverride 不是 Allsudo netstat -tlnp | grep 80
你可以直接告诉我:
我可以给你更精确的配置示例。