Virtual host configuration in apache for multiple websites

 In Apache

If you are running more than one website on a single apache webserver, most likely you’ll have to use the virtual host technology. A virtual host is basically one of the websites running on the same machine. (You can have more virtual hosts like: website.com, myblog.co.uk, etc). For this, you need to have a config file in /etc/apache2/sites-available/ in case you use one single file for all the websites.

Some people are happy to use one single file for all the virtual hosts but we like to have a separate file for each website so we can have more control of each one.

So we have created a template file named template.conf in /etc/apache2/sites-available (see below) and put all the general configuration in.

#http

        # 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

        ServerName www.sitename.com
        ServerAlias *.sitename.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/sitename

        # 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 warn

        ErrorLog ${APACHE_LOG_DIR}/sitename-error.log
        CustomLog ${APACHE_LOG_DIR}/sitename-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

        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
        </Directory>
</VirtualHost>

#Https
#<VirtualHost *:443>
#        ServerName www.sitename.com
#        ServerAlias *.sitename.com
#        ServerAdmin webmaster@localhost
#        DocumentRoot /var/www/html/sitename
#        #Logging
#        LogLevel warn
#        ErrorLog ${APACHE_LOG_DIR}/sitename-error.log
#        CustomLog ${APACHE_LOG_DIR}/sitename-access.log combined
#        # SSL Section
#        SSLEngine on
#        #   SSLCertificateFile directive is needed.
#        SSLCertificateFile      /etc/ssl/customercerts/sitename.com.pem
#        SSLCertificateKeyFile /etc/ssl/customercerts/sitename.com.key
#        SSLCertificateChainFile /etc/ssl/certs/GlobalSign_DomainSSL_Root_CA.pem
#        <Directory /var/www/html/sitename>
#        Options -Indexes +FollowSymLinks +MultiViews
#        AllowOverride All
#        Require all granted
#        </Directory>
#</VirtualHost>

You can also download the file from below and copy it to your virtual host in /etc/apache2/sites-available

template.conf (218 downloads)

 

From now on, all we need to do when we create a new website is to replicate this file and replace the generic statements with the name of the new website. In this file, the generic statement is sitename  sometimes you might need to change the suffix too from .com to .co.uk or .org, etc).

To replicate (copy) this file we need to be in /sites-available:

cd /etc/apache2/sites-available

At this point, if you type ll and press Enter you’ll see a list with all your config files.

Duplicate the template.conf file and replace the bits of text accordingly:

cp template.conf mywebsite.conf

Now you have a copy of template.conf file named mywebsite.conf

As an example, if I need to create a new website called www.mywebsite.com the file should look like the example below.

If you are on the linux command line, you can use vi to edit your file (you need to be in the sites-available directory):

sudo vi mywebsite.conf

Once you file is open press Escape, colon (:) and type: %s /sitename/mywebsite/g

Your command should look like this:

:%s /sitename/mywebsite/g

Before

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

        ServerName www.sitename.com
        ServerAlias *.sitename.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/sitename

        # 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 warn

        ErrorLog ${APACHE_LOG_DIR}/sitename-error.log
        CustomLog ${APACHE_LOG_DIR}/sitename-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
        <Directory /var/www/html/sitename>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
         </Directory>
</VirtualHost>

#Https
#<VirtualHost *:443>
#        ServerName www.sitename.com
#        ServerAlias *.sitename.com
#        ServerAdmin webmaster@localhost
#        DocumentRoot /var/www/html/sitename
#        #Logging
#        LogLevel warn
#        ErrorLog ${APACHE_LOG_DIR}/sitename-error.log
#        CustomLog ${APACHE_LOG_DIR}/sitename-access.log combined
#        # SSL Section
#        SSLEngine on
#        #   SSLCertificateFile directive is needed.
#        SSLCertificateFile      /etc/ssl/customercerts/sitename.com.pem
#        SSLCertificateKeyFile /etc/ssl/customercerts/sitename.com.key
#        SSLCertificateChainFile /etc/ssl/certs/GlobalSign_DomainSSL_Root_CA.pem
#        <Directory /var/www/html/sitename>
#        Options -Indexes +FollowSymLinks +MultiViews
#        AllowOverride All
#        Require all granted
#        </Directory>
#</VirtualHost>

After

http
<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
 
        ServerName www.sitename.com
        ServerAlias *.sitename.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/sitename
 
        # 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 warn
 
        ErrorLog ${APACHE_LOG_DIR}/sitename-error.log
        CustomLog ${APACHE_LOG_DIR}/sitename-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
        <Directory /var/www/html/sitename>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
         </Directory>
</VirtualHost>
 
#Https
#<VirtualHost *:443>
#        ServerName www.sitename.com
#        ServerAlias *.sitename.com
#        ServerAdmin webmaster@localhost
#        DocumentRoot /var/www/html/sitename
#        #Logging
#        LogLevel warn
#        ErrorLog ${APACHE_LOG_DIR}/sitename-error.log
#        CustomLog ${APACHE_LOG_DIR}/sitename-access.log combined
#        # SSL Section
#        SSLEngine on
#        #   SSLCertificateFile directive is needed.
#        SSLCertificateFile      /etc/ssl/customercerts/sitename.com.pem
#        SSLCertificateKeyFile /etc/ssl/customercerts/sitename.com.key
#        SSLCertificateChainFile /etc/ssl/certs/GlobalSign_DomainSSL_Root_CA.pem
#        <Directory /var/www/html/sitename>
#        Options -Indexes +FollowSymLinks +MultiViews
#        AllowOverride All
#        Require all granted
#        </Directory>
#</VirtualHost>

As you can see at the bottom of the file you’ve got HTTPS section which is commented with #.  You can  uncomment these lines once you have a valid certificate configured and put in place.

Now if you’re going to your browser and type www.mywebsite.com, you’ll be redirected by apache to you website.

All done.

Leave a Comment

Contact Us

Call us on: 01332 890460 or Send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt