在 Ubuntu 上配置 Apache2 一般包括:安装、启动、配置虚拟主机、设置防火墙、以及常见目录说明。下面我按从零开始给你一份常用、实用的配置流程。
sudo apt update
sudo apt install apache2 -y
安装完成后,Apache 会自动启动。
sudo systemctl start apache2 # 启动
sudo systemctl stop apache2 # 停止
sudo systemctl restart apache2 # 重启
sudo systemctl reload apache2 # 平滑重载配置
sudo systemctl status apache2 # 查看状态
设置开机自启(一般默认已开启):
sudo systemctl enable apache2
查看状态:
sudo ufw status
允许 HTTP / HTTPS:
sudo ufw allow 'Apache Full'
浏览器访问:
http://服务器IP
看到 Apache2 Ubuntu Default Page 即成功。
| 说明 | 路径 |
|---|---|
| 网站根目录 | /var/www/html |
| 主配置文件 | /etc/apache2/apache2.conf |
| 虚拟主机配置 | /etc/apache2/sites-available/ |
| 启用虚拟主机 | /etc/apache2/sites-enabled/ |
| 模块配置 | /etc/apache2/mods-available/ |
| 日志 | /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 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
ServerName example.comsudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com
apache2ctl configtest
tail -f /var/log/apache2/error.log
如果你愿意,可以告诉我:
我可以帮你给你一份完全针对你场景的 Apache 配置。