如何在Ubuntu 18.04 LTS中配置Apache虚拟主机
什么是apache虚拟主机?
虚拟主机术语是指运行多个的方法,例如host1.theitroad.com,host2.theitroad.com,或者www.domain1.com,www.domain2.com等,在一个系统上。
Apache中有两种类型的虚拟托管,即基于IP的虚拟托管和基于名称的虚拟主机。
使用基于IP的虚拟主机,我们可以在同一系统上托管多个或者域,但每个/域都有不同的IP地址。
使用基于名称的虚拟主机,我们可以在同一IP地址上托管多个/域。
如果要从单个物理服务器或者VPS托管多个和域,则虚拟托管可能很有用。
希望我们获得Apache虚拟主机的基本思想。
今天,我们将看到如何在Ubuntu 18.04 LTS中配置Apache虚拟主机。
在Ubuntu 18.04 LTS中配置Apache虚拟主机
我的测试盒IP地址是192.168.225.22,主机名是ubuntuserver。
首先,我们将看到如何在Apache Web服务器中配置基于名称的虚拟主机。
配置基于名称的虚拟主机
1.安装Apache WebServer
确保已安装Apache WebServer。
要在Ubuntu上安装它,请运行:
$sudo apt-get install apache2
安装Apache后,通过在浏览器中浏览Apache测试页面,测试是否正常工作。
打开Web浏览器并将其指向http://ip_address或者http://localhost。
我们应该看到如下页面。
好的! apache webserver是启动和工作!!
2.为每个主机创建Web目录
我要创建两个虚拟主机,即theitroad1.lan和onItoad2.lan。
让我们为First Virtual Host OnItoad1.lan创建一个目录。
存储虚拟主机的数据需要此目录。
为此,请输入:
$sudo mkdir -p /var/www/html/theitroad1.lan/public_html
同样,为第二个虚拟主机OnItoad2.Lan创建一个目录,如下所示。
$sudo mkdir -p /var/www/html/theitroad2.lan/public_html
以上两个目录由root用户拥有。
我们需要将所有权更改为常规用户。
为此,运行:
$sudo chown -R $USER:$USER /var/www/html/theitroad1.lan/public_html
$sudo chown -R $USER:$USER /var/www/html/theitroad2.lan/public_html
其中$用户指的是当前登录的用户。
接下来,将读取权限设置为Apache根目录:/var/www/html /使用命令:
$sudo chmod -R 755 /var/www/html/
我们这样做是因为我们已经为每个虚拟主机创建了一个单独的目录,用于存储其数据。
因此,我们使Apache根目录仅为除root用户以外的所有用户只读。
我们创建了用于存储每个虚拟主机的数据的所需目录,请设置正确的权限。
现在,是时候创建一些将从每个虚拟主机服务的示例页面了。
3.为每个主机创建演示网页
让我们为OniToad1.lan站点创建一个示例页面。
为此,运行:
$sudo vi /var/www/html/theitroad1.lan/public_html/index.html
添加以下行:
<html> <head> <title>www.theitroad.lan</title> </head> <body> <h1>Hello, This is a test page for theitroad1.lan website</h1> </body> </html>
保存并关闭文件。
同样,为OniToad2.lan站点创建一个示例页面:
$sudo vi /var/www/html/theitroad2.lan/public_html/index.html
添加以下行:
<html> <head> <title>www.theitroad.lan</title> </head> <body> <h1>Hello, This is a test page for theitroad2.lan website</h1> </body> </html>
保存并关闭文件。
4.为每个主机创建配置文件
接下来,我们需要为每个虚拟主机创建配置文件。
首先,让我们为OniToad1.Lan执行此操作。
将称为000-default.conf内容的默认虚拟主机文件复制到如下所示的新虚拟主机文件。
$sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/theitroad1.lan.conf
$sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/theitroad2.lan.conf
请注意,我们必须在最后使用.conf扩展名为所有配置文件。
否则它将无法正常工作。
现在,修改配置文件以与我们的虚拟主机匹配。
编辑onitad.lan1.conf文件:
$sudo vi /etc/apache2/sites-available/theitroad1.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 Hyman@theitroad ServerName theitroad1.lan ServerAlias www.theitroad1.lan DocumentRoot /var/www/html/theitroad1.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>
保存并关闭文件。
接下来,编辑onitoad2.lan.conf文件:
$sudo vi /etc/apache2/sites-available/theitroad2.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 Hyman@theitroad ServerName theitroad2.lan ServerAlias www.theitroad2.lan DocumentRoot /var/www/html/theitroad2.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>
保存/关闭文件。
5.启用虚拟主机配置文件
进行必要的更改后,禁用默认虚拟主机配置文件:000.default.conf,并启用所有新创建的虚拟主机配置文件,如下所示。
$sudo a2dissite 000-default.conf
$sudo a2ensite theitroad1.lan.conf
$sudo a2ensite theitroad2.lan.conf
重新启动Apache Web服务器以生效更改。
$sudo systemctl restart apache2
我们在Apache中成功配置了虚拟主机。
让我们继续检查他们是否正在工作。
6.测试虚拟主机
在任何编辑器中打开/etc/hosts文件:
$sudo vi /etc/hosts
将所有虚拟/域一个逐个添加一个。
[...] 192.168.225.22 theitroad1.lan 192.168.225.22 theitroad2.lan [...]
请注意,如果要从任何远程系统访问虚拟主机,则必须在每个远程系统/etc/hosts文件中添加上述行。
保存并关闭文件。
打开Web浏览器并将其指向http://theitroad1.lan或者http://theitroad2.lan。
从现在开始,我们可以上传数据并从不同的服务。
正如我们所知,我们使用了同一IP地址(即192.168.225.222)的主机两种不同的(http://theitroad1.lan和http:///theitroad2.lan)。
这是我们称之为名称的虚拟托管。