Setting up virtual hosts

It's very common for a single organization to host multiple sites. Each of these sites can live on their own server or virtual machine, but that's not very practical. Running just one site per server is very expensive and not very efficient. The concept of virtual hosts is that multiple sites can live on one web server, which saves infrastructure. Of course, it's always possible that you may have a website that generates so much traffic that sharing it with other high-traffic sites may not be a good idea, but when this is not the case, virtual hosts are recommended.

As mentioned before, /var/www is the default location where Apache looks for files to serve. If you're hosting multiple sites on one server, you would want to create a separate directory for each of them. For example, if you are hosting a website for a company named tryadtech.com and another for linuxpros.com, you could create the following directory structure:

/var/www/tryadtech.com/html
/var/www/linuxpros.com/html

In this example, I created directories several levels deep, so you can use the -p flag with mkdir in order to create these directories and their parents.

This way, each site gets their own directory so you can keep their content separate. Everyone will need to read these files, so we'll need to adjust permissions:

# chmod 755 -R /var/www/<nameofsite>

To create a virtual host, we'll need to create a configuration file from it. On Debian, there is a default configuration you can use as a starting point should you choose (I'll detail the configuration I use in the next section, so using this file is not required). If you wish, you can start with the following file:

/etc/apache2/sites-available/000-default.conf

This file serves as a good reference point for creating configuration for a virtual host. If you choose to use it, copy it to the directory you've created for your virtual host:

# cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/tryadtech.com.conf

On CentOS, the /etc/apache2/sites-available directory doesn't even exist, so go ahead and create that. In order to tell Apache to load sites from this directory, we'll need to add the following line to the bottom of the /etc/httpd/conf/httpd.conf file:

IncludeOptional sites-available/*.conf

Now, here's an example virtual host configuration file. I've saved it as /etc/apache2/sites-available/tryadtech.com.conf on my Debian test system, but on CentOS just replace apache2 with httpd. I took this example file from the 000-default.conf file I mentioned previously, removing the commented lines for the sake of brevity. The first bold line was not originally present, and the second was modified:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName tryadtech.com
        DocumentRoot /var/www/tryadtech.com/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

As you can see here, we're calling out an html directory underneath the tryadtech.com directory. To develop your site, you would place your site's files into the html directory, and after restarting Apache, you should be able to access that directory from a web browser.

So, how does the Apache server know which directory to send visitors to? Notice the ServerName line that I added to the configuration file. In this line, I'm calling out a specific domain name that the files within this virtual host belong to. This requires that you already have DNS set up and pointing to this IP. For example, your DNS entry at your domain name registrar would be pointing each of these two virtual hosts to the same IP address. When a request comes in via the tryadtech.com domain, Apache should serve the user files from the /var/www/tryadtech.com/html directory. If you configure another virtual host and domain name, the same would apply to that domain as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset