虚拟主机是Apache最强大且常用的功能之一。
虚拟主机(AKA vhosts)让我们允许我们在单台计算机上托管多个网站。
这有2种大的优势:
- 我们知道只需要机器的一个IP地址,而不是每个网站需要一个IP地址。
- 如果它只托管单个网站,可能不会使用大量机器容量。因此,在一台机器上具有多个网站将更好地利用机器的计算能力
有几种方法可以设置vhosts,但我们将通过一个典型的最常见方式之一来创建vhosts。
在我们的示例中,我们有2个网站,我们希望在我们的盒子上主持,它们是:
- example.com.
- example.net.
首先要首先需要创建每个网站的内容目录,我们将选择:
$ mkdir /var/www/example_com $ mkdir /var/www/example_net $ chown apache:apache /var/www/example_com $ chown apache:apache /var/www/example_net $ ll -Z /var/www/ | grep example drwxr-xr-x. apache apache unconfined_u:object_r:httpd_sys_content_t:s0 example_com drwxr-xr-x. apache apache unconfined_u:object_r:httpd_sys_content_t:s0 example_net
现在让我们为每个网站创建一个虚拟主页:
$ echo 'hello example.com' > /var/www/example_com/index.html $ chown apache:apache /var/www/example_com/index.html $ echo 'hello example.net' > /var/www/example_net/index.html $ chown apache:apache /var/www/example_net/index.html $ ls -lZ /var/www/example_com/index.html -rw-r--r--. apache apache unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/example_com/index.html $ ls -lZ /var/www/example_net/index.html -rw-r--r--. apache apache unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/example_net/index.html
现在我们创建了vhost配置文件,它们是:
$ ll /etc/httpd/conf.d | grep example -rw-r--r--. 1 apache apache 243 Mar 5 19:11 example_com.conf -rw-r--r--. 1 apache apache 242 Mar 5 19:10 example_net.conf $ ll -Z /etc/httpd/conf.d | grep example -rw-r--r--. apache apache unconfined_u:object_r:httpd_config_t:s0 example_com.conf -rw-r--r--. apache apache unconfined_u:object_r:httpd_config_t:s0 example_net.conf $ cat /etc/httpd/conf.d/example_com.conf <VirtualHost *:80> ServerName www.example.com ServerAlias example.com DocumentRoot /var/www/example_com ErrorLog /var/log/httpd/example_com_error.log CustomLog /var/log/httpd/example_com_access.log combined $ cat /etc/httpd/conf.d/example_net.conf <VirtualHost *:80> ServerName www.example.net ServerAlias example.net DocumentRoot /var/www/example_net ErrorLog /var/log/httpd/example_net_error.log CustomLog /var/log/httpd/example_net_access.log combined
我们可以使用这样的apachect测试这些新配置文件的语法:
$ apachectl configtest Syntax OK
我们还可以检查httpd是否知道我们的新vhosts:
$ httpd -D DUMP_VHOSTS VirtualHost configuration: *:80 is a NameVirtualHost default server www.example.com (/etc/httpd/conf.d/example_com.conf:1) port 80 namevhost www.example.com (/etc/httpd/conf.d/example_com.conf:1) alias example.com port 80 namevhost www.example.net (/etc/httpd/conf.d/example_net.conf:1) alias example.net *:443 webserver.local (/etc/httpd/conf.d/ssl.conf:56)
由于这些是用于测试目的的伪网站,因此我们没有公共DNS条目为example.com和example.net。
因此,我们需要在本地使用/ etc / hosts文件进行DNS解析。
在我们的示例中,我们的Box的IP地址是'10.0.5.10',因此我们添加了以下行:
$ cat /etc/hosts 127.0.0.1 webserver.local webserver 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.0.5.10 example.com 10.0.5.10 example.net
现在我们重新加载配置:
$ systemctl restart httpd
最后,我们可以测试它是否有效:
[root@webserver conf.d]# curl http://example.com hello example.com [root@webserver conf.d]# curl http://example.net hello example.net
日期:2020-07-07 20:57:13 来源:oir作者:oir