在 Debian 上用 Apache 配置虚拟主机(VirtualHost)一般分为以下几步,以基于域名的虚拟主机为例(最常见)。
sudo apt update
sudo apt install apache2
假设你要建两个站点:
site1.example.comsite2.example.comsudo mkdir -p /var/www/site1
sudo mkdir -p /var/www/site2
echo "<h1>site1</h1>" | sudo tee /var/www/site1/index.html
echo "<h1>site2</h1>" | sudo tee /var/www/site2/index.html
Apache 虚拟主机文件放在:
/etc/apache2/sites-available/
sudo nano /etc/apache2/sites-available/site1.conf
内容:
<VirtualHost *:80>
ServerName site1.example.com
DocumentRoot /var/www/site1
<Directory /var/www/site1>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/site1_error.log
CustomLog ${APACHE_LOG_DIR}/site1_access.log combined
</VirtualHost>
sudo nano /etc/apache2/sites-available/site2.conf
<VirtualHost *:80>
ServerName site2.example.com
DocumentRoot /var/www/site2
<Directory /var/www/site2>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/site2_error.log
CustomLog ${APACHE_LOG_DIR}/site2_access.log combined
</VirtualHost>
sudo a2ensite site1.conf
sudo a2ensite site2.conf
禁用默认站点(可选):
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
sudo systemctl restart apache2
修改本地 hosts 文件(Windows / macOS / Linux 客户端):
你的服务器IP site1.example.com
你的服务器IP site2.example.com
浏览器访问:
http://site1.example.com
http://site2.example.com
sudo apt install certbot python3-certbot-apache
sudo certbot --apache
<VirtualHost *:8080>
并确保在 ports.conf 中监听:
Listen 8080
如果你需要:
可以继续问我。