如何在Ubuntu 18.04 LTS中配置Apache虚拟主机

  • 如何在Ubuntu 18.04 LTS中配置Apache虚拟主机已关闭评论
  • 165 views
  • A+
所属分类:linux

如何在Ubuntu 18.04 LTS中配置Apache虚拟主机

什么是Apache虚拟主机?

虚拟主机术语是指在单个系统上运行多个网站(如host1.domain.com,host2.domain.com或www.domain1.com,www.domain2.com等)的方法。Apache中有两种类型的虚拟主机,即基于IP的虚拟主机和基于域名的虚拟主机。使用基于IP的虚拟主机,您可以在同一系统上托管多个网站或域,但每个网站/域具有不同的IP地址。使用基于域名的虚拟主机,您可以在同一IP地址上托管多个网站/域。如果要从单个物理服务器或VPS托管多个网站和域,虚拟主机可能很有用。

希望您掌握Apache虚拟主机的基本概念。今天,我们将看到如何在Ubuntu 18.04 LTS中配置Apache虚拟主机。我的测试IP地址是192.168.225.22,主机名是ubuntuserver

在Ubuntu 18.04 LTS中配置Apache虚拟主机

首先,我们将了解如何在Apache webserver中配置基于域名的虚拟主机。

配置基于域名的虚拟主机

1.安装Apache Web服务器 

确保已安装Apache Web服务器。要在Ubuntu上安装它,请运行:

$ sudo apt-get install apache2

安装apache后,通过浏览浏览器中的apache测试页面来测试它是否正常工作。

打开Web浏览器并将其指向  http://IP_Addresshttp://localhost。你应该看到如下页面。

如何在Ubuntu 18.04 LTS中配置Apache虚拟主机

好!Apache webserver正常运行!!

2.为每个主机创建Web目录

我将创建两个虚拟主机,即bonechnix1.lanbonechnix2.lan

让我们为第一个虚拟主机bonechnix1.lan创建一个目录。存储虚拟主机的数据需要此目录。

为此,请输入:

$ sudo mkdir -p /var/www/html/ostechnix1.lan/public_html

同样,为第二个虚拟主机bonechnix2.lan创建一个目录,如下所示。

$ sudo mkdir -p /var/www/html/ostechnix2.lan/public_html

以上两个目录由root用户拥有。我们需要将所有权更改为普通用户。

为此,请运行:

$ sudo chown -R $USER:$USER /var/www/html/ostechnix1.lan/public_html
$ sudo chown -R $USER:$USER /var/www/html/ostechnix2.lan/public_html

这里,$ USER引用当前登录的用户。

接下来,设置对Apache根目录的读取权限,即/ var / www / html / using命令:

$ sudo chmod -R 755 /var/www/html/

我们这样做是因为我们已经为每个虚拟主机创建了一个单独的目录来存储他们的数据。所以我们将apache根目录设置为除root用户以外的所有用户的只读。

我们已经创建了所需的目录来存储每个虚拟主机的数据,设置适当的权限。现在,是时候创建一些将从每个虚拟主机提供的示例页面。

3.为每个主机创建演示网页

让我们为ostechnix1.lan网站创建一个示例页面。为此,请运行:

$ sudo vi /var/www/html/ostechnix1.lan/public_html/index.html

在其中添加以下行:

<html>
 <head>
 <title>www.ostechnix.lan</title>
 </head>
 <body>
 <h1>Hello, This is a test page for ostechnix1.lan website</h1>
 </body>
</html>

保存并关闭文件。

同样,为ostechnix2.lan站点创建一个示例页面:

$ sudo vi /var/www/html/ostechnix2.lan/public_html/index.html

在其中添加以下行:

<html>
 <head>
 <title>www.ostechnix.lan</title>
 </head>
 <body>
 <h1>Hello, This is a test page for ostechnix2.lan website</h1>
 </body>
</html>

保存并关闭文件。

4.为每个主机创建配置文件

接下来,我们需要为每个虚拟主机创建配置文件。首先,让我们为ostechnix1.lan网站做这个。

将名为000-default.conf  内容的默认虚拟主机文件复制到新的虚拟主机文件,如下所示。

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ostechnix1.lan.conf
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ostechnix2.lan.conf

请注意,您必须在结尾处保存所有带.conf扩展名的配置文件,否则将无法正常工作。

现在,修改配置文件以与我们的虚拟主机匹配。

编辑ostechnix.lan1.conf文件:

$ sudo vi /etc/apache2/sites-available/ostechnix1.lan.conf

编辑/修改ServerAdmin,ServerName,ServerAlias和DocumentRoot值与虚拟主机匹配。

<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@ostechnix1.lan
   ServerName ostechnix1.lan
   ServerAlias www.ostechnix1.lan
   DocumentRoot /var/www/html/ostechnix1.lan/public_html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
 # error, crit, alert, emerg.
 # It is also possible to configure the loglevel for particular
 # modules, e.g.
 #LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
 # enabled or disabled at a global level, it is possible to
 # include a line for only one particular virtual host. For example the
 # following line enables the CGI configuration for this host only
 # after it has been globally disabled with "a2disconf".
 #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

如何在Ubuntu 18.04 LTS中配置Apache虚拟主机

保存并关闭文件。

接下来,编辑ostechnix2.lan.conf文件:

$ sudo vi /etc/apache2/sites-available/ostechnix2.lan.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@ostechnix2.lan
    ServerName ostechnix2.lan
    ServerAlias www.ostechnix2.lan
    DocumentRoot /var/www/html/ostechnix2.lan/public_html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
 # error, crit, alert, emerg.
 # It is also possible to configure the loglevel for particular
 # modules, e.g.
 #LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
 # enabled or disabled at a global level, it is possible to
 # include a line for only one particular virtual host. For example the
 # following line enables the CGI configuration for this host only
 # after it has been globally disabled with "a2disconf".
 #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

如何在Ubuntu 18.04 LTS中配置Apache虚拟主机

保存/关闭文件。

5.启用虚拟主机配置文件

进行必要的更改后,禁用默认的虚拟主机配置文件,即000.default.conf,并启用所有新创建的虚拟主机配置文件,如下所示。

$ sudo a2dissite 000-default.conf
$ sudo a2ensite ostechnix1.lan.conf
$ sudo a2ensite ostechnix2.lan.conf

重启apache Web服务器以使更改生效。

$ sudo systemctl restart apache2

而已。我们已经在Apache中成功配置了虚拟主机。让我们继续检查它们是否正常工作。

6.测试虚拟主机

在任何编辑器中打开/ etc / hosts文件:

$ sudo vi /etc/hosts

像下面一个一个地添加所有虚拟网站/域名。

[...]
192.168.225.22   ostechnix1.lan
192.168.225.22   ostechnix2.lan
[...]

请注意,如果要从任何远程系统访问虚拟主机,则必须在每个远程系统的/ etc / hosts文件中添加以上行。

保存并关闭文件。

打开Web浏览器并将其指向http://ostechnix1.lanhttp://ostechnix2.lan

ostechnix1.lan测试页:

如何在Ubuntu 18.04 LTS中配置Apache虚拟主机

ostechnix2.lan测试页:

如何在Ubuntu 18.04 LTS中配置Apache虚拟主机

恭喜!您现在可以访问您的所有网站。从现在开始,您可以上传数据并从不同的网站提供服务。

正如您所注意到的,我们使用相同的IP地址(192.168.225.22)来托管两个不同的网站(http://ostechnix1.lan和http://ostechnix2.lan)。这就是我们所说的基于域名的虚拟主机。

参考资料: 

 

 

 

 

 

 

 

 

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar