下面是在 Ubuntu 上配置 Apache2的常用步骤,从安装到常见配置,适合新手和日常使用。
sudo apt update
sudo apt install apache2
安装完成后,Apache 会自动启动。
查看状态:
sudo systemctl status apache2
sudo ufw allow 'Apache Full'
验证:
sudo ufw status
浏览器访问:
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/ |
可用模块 |
/etc/apache2/mods-enabled/ |
已启用模块 |
直接修改默认页面:
sudo nano /var/www/html/index.html
或新建 PHP 文件(需安装 PHP):
sudo apt install php libapache2-mod-php
sudo mkdir -p /var/www/example.com/public_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
确保站点配置中有:
AllowOverride All
sudo apt install certbot python3-certbot-apache
sudo certbot --apache
自动续期:
sudo certbot renew --dry-run
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl reload apache2
sudo apache2ctl configtest
Require all grantedDocumentRootsudo apache2ctl configtest
sudo systemctl restart apache2
如果你有具体需求(比如:PHP / HTTPS / 多站点 / 反向代理 / Laravel / WordPress),可以直接告诉我,我给你定制配置。