在 Ubuntu 上安装 Apache2 非常简单,通常只需几步。下面按**最常见场景(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) 就说明成功。
如果启用了 UFW 防火墙,需放行 HTTP/HTTPS:
sudo ufw allow 'Apache Full'
或只放行 80 端口:
sudo ufw allow 80
在浏览器访问:
http://服务器IP
或本机:
http://localhost
看到 Apache2 Ubuntu Default Page 即成功。
/var/www/html
/etc/apache2/
sudo systemctl restart apache2 # 重启
sudo systemctl stop apache2 # 停止
sudo systemctl reload apache2 # 平滑重载
如果你要搭 Web 环境:
sudo apt install php libapache2-mod-php php-mysql -y
sudo systemctl restart apache2
如果你告诉我:
我可以给你更针对性的配置示例。