Apache 不加载虚拟主机文件

Zlu*_*lug 2 linux apache-http-server ubuntu

我正在尝试设置 Apache2 服务器,但无法加载我的虚拟主机。或者更准确地说,只要我把它们写在000-default.conf.

为什么这不起作用?我该如何让它发挥作用?

Gia*_*968 5

通常,如果要设置单独的虚拟主机配置,您可以将它们放在以下目录中:

/etc/apache2/sites-available/
Run Code Online (Sandbox Code Playgroud)

因此,如果您有一个名为www.example.comconfig 文件名的虚拟主机,则可以这样命名www.example.com.conf

/etc/apache2/sites-available/www.example.com.conf
Run Code Online (Sandbox Code Playgroud)

然后,您将在这样的文本编辑器中打开该文件;我正在nano用作示例,但可以随意使用您喜欢的任何文本编辑器:

sudo nano /etc/apache2/sites-available/www.example.com.conf
Run Code Online (Sandbox Code Playgroud)

然后将与此类似的内容放入www.example.com.conf

<VirtualHost *:80>
  ServerAdmin admin@example.com
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example.com/
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

关闭并保存该文件,然后a2ensite像这样使用,让系统设置一个符号链接 from sites-availabletosites-enabled像这样:

sudo a2ensite www.example.com.conf
Run Code Online (Sandbox Code Playgroud)

或者——如果你愿意——你可以手动ln -s设置一个符号链接,如下所示:

ln -s /etc/apache2/sites-available/www.example.com.conf /etc/apache2/sites-enabled/www.example.com.conf
Run Code Online (Sandbox Code Playgroud)

完成后,只需像这样重新加载 Apache:

sudo service apache2 reload
Run Code Online (Sandbox Code Playgroud)

如果由于某种原因reload不起作用,只需像这样强制重启:

sudo service apache2 restart
Run Code Online (Sandbox Code Playgroud)

使用reloadbefore的好处restart是,如果配置文件有拼写错误,Apache 会报错但不会加载配置文件。这意味着您的 Apache Web 服务器仍将根据它加载的最后一个稳定配置文件启动并运行。

风险在于,如果您强制 arestart并且配置文件中有错字,Apache 将死掉,因为restart强制一个完整的服务stop后跟一个start. 如果start卡住了,服务器就会死机,直到配置错字/问题被清除。