在我的本地网络中,我有一个 ubuntu 服务器(我可以通过http://服务器)我已经将灯全部设置好,并创建了几个网站,当我使用服务器本身时,我可以通过以下方式访问它们:
http://localhost.test1.com和http://localhost.test2.com
在我想要访问这些网站的主机上,我编辑了 hosts 文件以将这些 URL 转发到服务器
server localhost.test1.com
server localhost.test2.com
这会将我的请求发送到服务器,但服务器仅提供默认网站。
我错过了什么?如何强制 Apache 知道我要访问哪个站点?
答案1
您需要在 apache 的配置中为每个站点设置一个虚拟服务器/etc/apache2/sites-available
。您可以通过为每个服务器创建一个新的文件 conf 文件来执行此操作,我只需复制默认配置即可。然后,您需要在每个文件中对文件的开头进行修改。
默认文件:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
您将需要更改每个虚拟主机信息和 documentRoot。
例子
<VirtualHost localhost.test1.com:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/foo/test1
最后你需要启用这些新网站
sudo a2ensite test1
注意:站点名称基于conf文件的名称。