温馨提示×

Ubuntu Apache怎么开启

小樊
35
2026-07-17 18:43:05
栏目: 智能运维

下面分 Ubuntu 上开启 Apache(安装并启动)常见配置检查 两部分讲,基本照着做就能跑起来。


一、安装并启动 Apache(最常见)

1️⃣ 更新软件源

sudo apt update

2️⃣ 安装 Apache

sudo apt install apache2 -y

3️⃣ 启动 Apache

sudo systemctl start apache2

4️⃣ 设置开机自启

sudo systemctl enable apache2

5️⃣ 检查状态

sudo systemctl status apache2

看到 active (running) 就说明已经开启成功。


二、测试是否成功

在浏览器里访问:

http://你的服务器IP

如果看到 Apache2 Ubuntu Default Page,说明 Apache 已正常运行。


三、常见问题排查

✅ 1. 防火墙阻拦(很常见)

如果访问不了,先检查防火墙:

sudo ufw status

如果启用了,放行 HTTP/HTTPS:

sudo ufw allow 80
sudo ufw allow 443

✅ 2. 端口被占用

查看谁占用了 80 端口:

sudo ss -lntp | grep 80

✅ 3. 重启 / 停止 Apache

sudo systemctl restart apache2
sudo systemctl stop apache2

四、Apache 默认目录说明

项目 路径
网站根目录 /var/www/html
主配置文件 /etc/apache2/apache2.conf
虚拟主机配置 /etc/apache2/sites-available/
日志 /var/log/apache2/

五、开启 rewrite(如果你要用伪静态)

sudo a2enmod rewrite
sudo systemctl restart apache2

如果你愿意,可以告诉我:

  • Ubuntu 版本(20.04 / 22.04 / 24.04)
  • 本地虚拟机 / 云服务器 / WSL
  • 是否要 配置 HTTPS / 多站点 / PHP

我可以直接给你一套完整配置方案 ✅

0