在 Debian 系统上配置 Apache(通常指 Apache HTTP Server,即 apache2)可以分为几个常见步骤:安装、基本配置、虚拟主机、常用模块和权限设置。下面给你一个实用、循序渐进的配置指南。
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 reload apache2 # 重新加载配置
如果启用了 ufw:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
验证:
sudo ufw status
浏览器访问:
http://服务器IP
看到 Apache2 Debian Default Page 即成功。
| 路径 | 说明 |
|---|---|
/var/www/html |
默认网站根目录 |
/etc/apache2/apache2.conf |
主配置文件 |
/etc/apache2/sites-available/ |
虚拟主机配置 |
/etc/apache2/sites-enabled/ |
已启用的虚拟主机 |
/etc/apache2/mods-available/ |
可用模块 |
/etc/apache2/mods-enabled/ |
已启用模块 |
/var/log/apache2/ |
日志目录 |
sudo mkdir -p /var/www/example.com/public_html
echo "<h1>example.com</h1>" | sudo tee /var/www/example.com/public_html/index.html
sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www
sudo nano /etc/apache2/sites-available/example.com.conf
内容示例:
<VirtualHost *:80>
ServerAdmin webmaster@example.com
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 reload apache2
sudo a2enmod ssl
sudo systemctl reload apache2
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
证书会自动续期。
编辑:
sudo nano /etc/apache2/ports.conf
Listen 8080
虚拟主机中也要改:
<VirtualHost *:8080>
sudo tail -f /var/log/apache2/error.log
sudo apache2ctl configtest
apt install apache2
mkdir /var/www/example.com
a2ensite example.com.conf
systemctl reload apache2
如果你有具体需求(如 PHP、反向代理、多站点、Debian 版本、内网部署),可以告诉我,我可以给你更针对性的配置方案。