如何连接到多个虚拟站点之一(apache 2.4)

如何连接到多个虚拟站点之一(apache 2.4)

如果我创建一个站点,我可以通过 http://localhost 访问它...

但是如果我在同一台计算机上使用 apache 创建多个虚拟站点,http://localhost 将无法工作。

我如何访问它?(在同一台计算机上,又名本地主机)?

答案1

要创建虚拟站点,请尝试以下命令;只需将“newsite”替换为您站点的名称:

笔记

  • 在 Ubuntu 20.04 上测试,使用 Apache 2.4.41 和 Firefox 95.0。
  • 所有命令均从主 ( ~/) 目录运行。
  • index.html您必须在主目录中创建自己的文件。
# Add the new site to the default Apache directory
sudo mkdir --parents /var/www/newsite

# Create your web page and place it in the directory:
sudo cp ~/index.html /var/www/newsite/index.html
sudo chmod 755 /var/www/newsite/index.html

# Copy and modify a virtual host configuration file
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/newsite.conf
sudo sed --in-place "s/webmaster@localhost/webmaster@newsite/g" /etc/apache2/sites-available/newsite.conf
sudo sed --in-place "s/DocumentRoot \/var\/www\/html/DocumentRoot \/var\/www\/newsite/g" /etc/apache2/sites-available/newsite.conf
sudo sed --in-place "/webmaster@newsite/ a ServerName newsite" /etc/apache2/sites-available/newsite.conf

# Enable the new virtual host file
sudo a2ensite newsite.conf

# Modify hosts file
sudo sed --in-place "\$a127\.0\.0\.1 newsite" /etc/hosts

# Restart Apache
sudo systemctl reload apache2

# Open the website
xdg-open http://newsite

输出:

在此处输入图片描述

您还可以将其转换为 shell 脚本。

答案2

您可以在/etc/hosts

127.0.0.1 localhost site1 site2 site3

在 Apache 配置中配置适当的VirtualHostssite1、site2、site3,并以http://site1/http://site2/、 的身份访问它们http://site3/

答案3

创建虚拟站点时,您将使用 VirtualHost 容器内的 ServerName 之类的指令来区分该虚拟站点。类似这样的内容: <VirtualHost *:80> ServerName my.best.server.biz ....... my.best.server.biz 的 DNS 名称应解析为您的 apache 服务器的 IP,在您的情况下可能是 127.0.0.1。为此,您很可能会在 hosts 文件中添加条目,如上一个答案中指出的那样。

相关内容