下面给你几种**在 Ubuntu 上安装 Apache(Apache HTTP Server)**的常见方法,按使用场景从简单到进阶说明。
适用于 Ubuntu 20.04 / 22.04 / 24.04 等大多数版本。
sudo apt update
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl status apache2
看到 active (running) 就说明成功了 ✅
在浏览器访问:
http://服务器IP
看到 Apache2 Ubuntu Default Page 即成功。
如果启用了防火墙,需要放行 HTTP / HTTPS:
sudo ufw allow 'Apache Full'
sudo ufw reload
查看规则:
sudo ufw status
| 作用 | 路径 |
|---|---|
| 网站根目录 | /var/www/html |
| 主配置文件 | /etc/apache2/apache2.conf |
| 虚拟主机配置 | /etc/apache2/sites-available/ |
| 启用站点 | /etc/apache2/sites-enabled/ |
| 日志 | /var/log/apache2/ |
假设你有一个站点 example.com:
sudo mkdir -p /var/www/example.com
sudo nano /var/www/example.com/index.html
写入测试内容后,创建虚拟主机:
sudo nano /etc/apache2/sites-available/example.com.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
启用站点:
sudo a2ensite example.com.conf
sudo systemctl reload apache2
sudo systemctl start apache2 # 启动
sudo systemctl stop apache2 # 停止
sudo systemctl restart apache2 # 重启
sudo systemctl reload apache2 # 平滑重载
sudo apache2ctl configtest # 检查配置语法
sudo systemctl stop apache2
sudo apt remove apache2 -y
sudo apt purge apache2 -y
如果你告诉我:
我可以给你一套完整的一键部署方案。