Ubuntu 18.04:多域名绑定同一IP, 配置Apache虚拟主机, 同一ip绑定多域名, 一台服务器支持多个域名和站点, Configure Apache Virtual Hosts

 

Ubuntu 18.04:多域名绑定同一IP, 配置Apache虚拟主机, 同一ip绑定多域名, Configure Apache Virtual Hosts
Ubuntu 18.04:多域名绑定同一IP, 配置Apache虚拟主机, 同一ip绑定多域名, Configure Apache Virtual Hosts

 

 

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

我的测试盒IP地址是192.168.225.22,主机名是ubuntuserver

首先,我们将看到如何在Apache Web服务器中配置基于名称的虚拟主机。

 

配置基于名称的虚拟主机

 

 1.   安装Apache Web服务器  (如果已经安装好了Apache,请略过次步)

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

$ sudo apt-get install apache2

 

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

打开您的Web浏览器并将其指向  http://IP_Address 或者 http://localhost.。您应该看到如下页面。

 

Ubuntu 18.04:多域名绑定同一IP, 配置Apache虚拟主机, 同一ip绑定多域名, Configure Apache Virtual Hosts
Ubuntu 18.04:多域名绑定同一IP, 配置Apache虚拟主机, 同一ip绑定多域名, Configure Apache Virtual Hosts

好!Apache Web服务器已启动并正常工作!!

 

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

我将创建两个虚拟主机,即 ikeepstudying1.com ikeepstudying2.com

让我们为第一个虚拟主机 ikeepstudying1.com 创建一个目录。该目录是存储我们的虚拟主机数据所必需的。

为此,请输入:

$ sudo mkdir -p /var/www/html/ikeepstudying1.com/public_html

 

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

$ sudo mkdir -p /var/www/html/ikeepstudying2.com/public_html

 

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

为此,请运行:

$ sudo chown -R $USER:$USER /var/www/html/ikeepstudying1.com/public_html
$ sudo chown -R $USER:$USER /var/www/html/ikeepstudying2.com/public_html

在这里,$ USER表示当前登录的用户。

 

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

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

 

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

我们已经创建了用于存储每个虚拟主机数据的必需目录,并设置了适当的权限。现在,该创建一些示例页面了,这些示例页面将由每个虚拟主机提供。

 

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

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

$ sudo vi /var/www/html/ikeepstudying1.com/public_html/index.html

在其中添加以下行:

<html>
 <head>
 <title>www.ikeepstudying1.com</title>
 </head>
 <body>
 <h1>Hello, This is a test page for ikeepstudying1.com website</h1>
 </body>
</html>

保存并关闭文件。

 

同样,为 ikeepstudying2.com 网站创建一个示例页面:

$ sudo vi /var/www/html/ikeepstudying2.com/public_html/index.html

在其中添加以下行:

<html>
 <head>
 <title>www.ikeepstudying2.com</title>
 </head>
 <body>
 <h1>Hello, This is a test page for ikeepstudying2.com website</h1>
 </body>
</html>

保存并关闭文件。

 

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

接下来,我们需要为每个虚拟主机创建配置文件。首先,让我们在ikeepstudying1.com网站上执行此操作。

 

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

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ikeepstudying1.com.conf
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ikeepstudying2.com.conf

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

 

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

编辑 ikeepstudying1.com.conf 文件:

$ sudo vi /etc/apache2/sites-available/ikeepstudying1.com.conf

 

编辑/修改 ServerAdminServerNameServerAlias 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@ikeepstudying1.com
   ServerName ikeepstudying1.com
   ServerAlias www.ikeepstudying1.com
   DocumentRoot /var/www/html/ikeepstudying1.com/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>

保存并关闭文件。

 

接下来,编辑 ikeepstudying2.com.conf 文件:

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

保存/关闭文件。

 

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

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

$ sudo a2dissite 000-default.conf
$ sudo a2ensite ikeepstudying1.com.conf
$ sudo a2ensite ikeepstudying2.com.conf

 

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

$ sudo systemctl restart apache2

 

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

 

6.测试虚拟主机

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

$ sudo vi /etc/hosts

 

如下所示,一一添加所有虚拟网站/域。

[...]
192.168.225.22 ikeepstudying1.com
192.168.225.22 ikeepstudying2.com
[...]

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

保存并关闭文件。

 

打开您的Web浏览器并将其指向http://ikeepstudying1.comhttp://ikeepstudying2.com

 

恭喜你!现在,您可以访问所有网站。从现在开始,您可以上传数据并从其他网站提供数据。

如您所见,我们使用相同的IP地址(即192.168.225.22)托管两个不同的网站(http://ikeepstudying1.com和http://ikeepstudying2.com)。这就是我们所说的基于名称的虚拟主机。希望这可以帮助。在下一指南中,我将向您展示如何配置基于IP的虚拟主机。在此之前,请继续关注!

 

资源: 

 

本文:Ubuntu 18.04:多域名绑定同一IP, 配置Apache虚拟主机, 同一ip绑定多域名, 一台服务器支持多个域名和站点, Configure Apache Virtual Hosts

Loading

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.