下面给你一份在 Debian 上配置 Apache2 的实用指南,从安装到常见配置,适合新手到日常使用。
sudo apt update
sudo apt install apache2
安装完成后,Apache 会自动启动。
查看状态:
systemctl status apache2
设置开机自启(一般默认已开启):
sudo systemctl enable apache2
Debian 下 Apache2 的重要路径:
/var/www/html/etc/apache2/apache2.conf/etc/apache2/sites-available//etc/apache2/sites-enabled//etc/apache2/mods-available//etc/apache2/ports.conf浏览器访问:
http://服务器IP
看到 Apache2 Debian Default Page 说明成功。
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R www-data:www-data /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}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</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 -d www.example.com
Certbot 会自动修改 Apache 配置并启用 443。
sudo ufw allow 80
sudo ufw allow 443
Require all grantedwww-datasudo a2enmod rewrite
并确认 AllowOverride All
sudo apache2ctl configtest
sudo systemctl reload apache2
echo "Hello Apache" | sudo tee /var/www/html/index.html
访问 http://IP 即可。
如果你有具体需求(如 PHP、反向代理、多域名、内网部署),可以告诉我,我可以给你更针对性的配置。