Normally, if you install Apache as shown in the previous recipe, you can host exactly one website that is accessible as the server's IP address or the domain name Apache is running on, for example, http://192.168.1.100
or http://www.centos7.home
. Such a system is very wasteful for your server resources as you would need individual servers with Apache installed for every single domain you want to host. Name-based or virtual hosting is used to host multiple domains on the same Apache web server. If a number of different domain names have already been assigned to your Apache web server's IP address using a DNS server or through a local /etc/hosts
file, virtual hosts can be configured for every available domain name to direct the user to a specific directory on the Apache server containing the site's information. Any modern webspace provider uses this kind of virtual hosting to divide one web server's space into multiple sites. There is no limit to this system and to the number of sites to create from it as long as your web server can handle its traffic. In this recipe, we will learn how to configure name-based virtual hosting on the Apache web server.
To complete this recipe, you will require a working installation of the CentOS 7 operating system with root privileges and a console-based text editor of your choice. It is expected that your server will be using a static IP address and Apache is installed and currently running, and that you have enabled system users publishing directories in an earlier recipe. Virtual host names cannot work without previously setting up one or more domains or subdomains outside Apache.
For testing, you could set up your /etc/hosts
(see the Setting your hostname and resolving the network recipe in Chapter 2, Configuring the System) or configure some A or CNAMES in your BIND DNS server (refer to Chapter 9, Working with Domains) to use different domain names or subdomains, such as www.centos7.home
, all pointing to your Apache web server's IP address.
A common misconception is that Apache can create domain names for your Apache web server on its own. This is not true. The different domain names you want to wire to different directories using virtual hosts need to be set up in a DNS server or /etc/hosts
file to point to your Apache server's IP address before you can use them with virtual hosts.
For the purpose of this recipe we will be building some local virtual hosts with the following Apache example subdomain names: www.centos7.home
, web1.centos7.home
, web2.centos7.home
and <username>.centos7.home
for the corresponding web publishing folders /var/www/html
, /var/www/web1
, /var/www/web2
, and /home/<username>/public_html
for the domain's network name centos7.home
. These names are interchangeable and it is expected that you will want to customize this recipe based on something more appropriate to your own needs and circumstances.
vi /etc/httpd/conf.d/vhost.conf
centos7.home
value and the username <username>
to fit your own needs:<VirtualHost *:80> ServerName centos7.home ServerAlias www.centos7.home DocumentRoot /var/www/html/ </VirtualHost> <VirtualHost *:80> ServerName web1.centos7.home DocumentRoot /var/www/web1/public_html/ </VirtualHost> <VirtualHost *:80> ServerName web2.centos7.home DocumentRoot /var/www/web2/public_html/ </VirtualHost> <VirtualHost *:80> ServerName <username>.centos7.home DocumentRoot /home/<username>/public_html/ </VirtualHost>
mkdir -p /var/www/web1/public_html /var/www/web2/public_html
web1
and web2
by using our favorite text editor, as follows:echo "<html><head></head><body><p>Welcome to Web1</p></body></html>" > /var/www/web1/public_html/index.html echo "<html><head></head><body><p>Welcome to Web2</p></body></html>" > /var/www/web2/public_html/index.html
apachectl configtest && systemctl reload httpd
hosts
file of the client computer that wants to access these virtual hosts, but remember that you can also configure these subdomains in a BIND DNS server. Login to this client computer (it needs to be in the same network as our Apache server) as root and add the following lines to the /etc/hosts
file, assuming our Apache server has the IP address 192.168.1.100:192.168.1.100 www.centos7.home 192.168.1.100 centos7.home 192.168.1.100 web1.centos7.home 192.168.1.100 web2.centos7.home 192.168.1.100 john.centos7.home
<username>
with the username you defined for the virtual host): http://www.centos7.home
, http://web1.centos7.home, http://web2.centos7.home
and http://<username>.centos7.home
.The purpose of this recipe was to show you how easy it is to implement name-based virtual hosting. This technique will boost your productivity and using this approach will give you unlimited opportunities to domain-based web hosting.
So what did we learn from this experience?
We began by creating a new Apache configuration file to hold all our virtual host configuration. Remember, all files ending with the .conf
extension in the /etc/httpd/conf.d/
directory will be loaded automatically when Apache is started. Following this, we then proceeded to put in the relevant directive blocks, starting with our default server root centos7.home
and the alias www.centos7.home
. The most important option in any virtual host block is the ServerName
directive, which maps an existing domain name for our web server's IP address to a specific directory on the filesystem. Of course, there are many more settings you can include, but the previous solution provides the basic building blocks that will enable you to use it as the perfect starting point. The next step was to then create individual entries for our centos7.home
subdomains web1
, web2
, and <username>
. Remember, each virtual host supports the typical Apache directives and can be customized to suit your needs. Refer to the official Apache manual (install the YUM package httpd-manual
, then go to the location /usr/share/httpd/manual/vhosts/
) to learn more. After we created our virtual host blocks for every subdomain we wanted, we then proceeded to create the directories to hold the actual content and created a basic index.html
in each directory. In this example, our web1
and web2
content directories were added to /var/www
. This is not to imply that you cannot create these new folders in another place. In fact most production servers generally place these new directories in the home folder, as shown with our /home/<username>/public_html
example. However, if you do intend to take this approach, remember to modify the permissions and ownership, as well as SELinux labels (outside/var/www
you need to label Apache directories as httpd_sys_content_t
) of these new directories so that they can be used as they were intended. Finally, we reloaded the Apache web service so that our new settings would take immediate effect. We could then directly use the subdomain names in our browser to browse to our virtual hosts when correctly set up in /etc/hosts
on the client or on a BIND DNS server.