Ubuntu 上配置 Apache 虚拟主机
一 准备与安装
sudo apt update && sudo apt install apache2sudo systemctl start apache2 && sudo systemctl enable apache2sudo systemctl status apache2(应看到 Active: active (running))二 创建站点目录与示例页面
sudo mkdir -p /var/www/example.comsudo chown -R $USER:$USER /var/www/example.comsudo chmod -R 755 /var/www/example.comecho "<h1>Welcome to Example.com</h1>" | sudo tee /var/www/example.com/index.html三 新建虚拟主机配置文件
sudo nano /etc/apache2/sites-available/example.com.conf<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo a2enmod rewrite。四 启用站点与生效配置
sudo a2ensite example.com.confsudo a2dissite 000-default.confsudo apache2ctl configtest(应返回 Syntax OK)sudo systemctl reload apache2(或 restart)五 本地与公网访问及常见问题
127.0.0.1 example.com www.example.com虚拟机IP example.com www.example.comsudo ufw allow 'Apache Full'sudo apt install certbot python3-certbot-apachesudo certbot --apache -d example.com -d www.example.comsudo tail -f /var/log/apache2/error.logsudo a2query -s example.com.confsudo ss -tlnp | grep ':80\|:443'sudo apache2ctl configtest