2-virtual

------

 Virtual Hosts on CentOS 7

our example we have 2 websites that we want to host on our box,

To start with we first need to create a content directory of each website, we’ll choose:

$ 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 

Now let’s create a dummy home page for each website:

$ 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

Now we create the vhost config files, they are:

$ 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     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     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

We can test the syntax of these new config files using apachectl like this:

$ apachectl configtest Syntax OK

We can also check whether httpd is aware of our new vhosts like this:

$ 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)

Since these are dummy websites for testing purposes, we don’t have public dns entries for example.com and example.net. So we need to do the dns resolution locally using /etc/hosts file. In our example, our box’s IP address is ‘10.0.5.10’, so we add in the following lines:

$ 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.com10.0.5.10   example.net

Now we reload the configurations:

$ systemctl restart httpd

Finally we can test to see if this has worked:

[root@webserver conf.d]# curl http://example.com hello example.com [root@webserver conf.d]# curl http://example.net hello example.net

Success!

----