Adding a Virtual Host to Apache

Preamble

The beauty of hosting your own web server and having root access to your machine is the ability to run whatever you want on it. One great example of this is running virtual hosts in apache, which allows you to run multiple websites on the same server. What you can run as a virtual host is fairly unlimited so long as the clients can resolve the address to your web server, be it URL, IP address or even special use domain names if you so desire (though the last one I do not recommend).

When setting up Virtual Hosts it is important to realize that as websites will run on a single server, If one website goes down, its very likely that the others will go with it. Additionally, when a server is not configured correctly or securely when a client connects to the server they may not see what you intend or may see the default apache page.

Place your website’s files in the folder /var/www/root/infinityflame.co.uk replacing my domain name with yours and setting the correct permission on the directory.

Method

There are multiple ways of configuring apache virtual hosts, such as IP-Based Virtual Hosts, which allow you to apply different rules to connections based on the originating or outgoing interface. And the most popular and most used method of Name-Based Virtual Hosts.

Adding a virtual host to apache is very simple once you get used to it. In the folder/etc/apache2, there are the following files, or there should be; sites-enabled, sites-available, mods-enabled, mods-available, conf-enabled and conf-available. The folder for adding virtual hosts in is sites-available. You can also add .conf files to sites-enabled, but in doing so using a2ensite and a2dissite (commands used to enable and disable virtual hosts) no longer work as intended.

Navigate to  the folder/etc/apache2/sites-available then make a file called the name of your first website, such as infinityflame.co.uk.conf or dashboard.infinityflame.co.uk.conf as an example, you can technically call these files whatever you desire, but as you add more websites this folder can become cluttered and hard to navigate if not named efficiently. You can make these files using the touch command or nano. Once you have created the file, open it with your desired text editor and enter the following, replacing the necessary options with the ones you need.

<VirtualHost *:80>

DocumentRoot /var/www/root/infinityflame.co.uk
<Directory "/var/www/root/infinityflame.co.uk">
Options FollowSymLinks
AllowOverride All
allow from all
Options +Indexes
</Directory>
ServerName infinityflame.co.uk
ServerAdmin aidan@infinityflame.co.uk
ServerAlias www.infinityflame.co.uk

CustomLog /var/log/apache2/infinityflame.co.uk-access.log combined
ErrorLog /var/log/apache2/infinityflame.co.uk-error.log

</VirtualHost>

Once you have replaced the options with the ones you used, such as the DocumentRoot, ServerName, ServerAdmin, ServerAlias, CustomLog and ErrorLog. You can save it and start to enable it by doing a2ensite infinityflame.co.uk.conf and then service apache2 reload to reload apache, you should then be able to navigate to your website in a browser, so long as the registered address is pointing to the correct server.

Adding More Virtual Hosts

Once you have added one, its time to add another, then enable that one using the same command, you should now be able to navigate to the appropriate website from the browser automatically. If the server doesn’t have an appropriate virtual host server name or a user connects via an IP Address that hasn’t been defined they will connect to the first one defined in the output of apachectl -S. Additionally, if a user connects to a port that isn’t 80 they will not be able to connect to the Apache server as the server won’t be listening on the port.

Note that there should now be symlinks to the websites you added in the sites-enabled folder that go back to the files we made earlier.

Adding SSL

As websites move to secure areas it is important to have SSL on most modern websites. Fear not, although this requires some extra configuration it is nothing that we can’t handle. Open your website’s configuration file again and add a new virtual host below,

<VirtualHost *:443>

SSLEngine on
SSLProtocol all -SSLv2 -SSLv3
SSLHonorCipherOrder on
SSLCipherSuite "ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS"
SSLCertificateFile /etc/letsencrypt/live/infinityflame.co.uk/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/infinityflame.co.uk/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/infinityflame.co.uk/fullchain.pem
DocumentRoot /var/www/root/infinityflame.co.uk
<Directory "/var/www/root/infinityflame.co.uk">
Options FollowSymLinks
AllowOverride All
allow from all
Options +Indexes
</Directory>
ServerName infinityflame.co.uk
ServerAdmin aidan@infinityflame.co.uk
ServerAlias www.infinityflame.co.uk

CustomLog /var/log/apache2/infinityflame.co.uk-access.log combined
ErrorLog /var/log/apache2/infinityflame.co.uk-error.log

</VirtualHost>

Replacing the configuration options relevant to the ones you use, as I use LetsEncrypt’s certbot the configuration is made easier by having a standard location for all of the Certificate and Key files that you can copy if you do too, replacing the domain with your own.

Removing or Disabling a Website

To remove a website or disable it use a2dissite infinityflame.co.uk.conf and then reload apache using service apache2 reload. This removes the symlink from sites-enabled so that the website is not loaded on startup.

Bigger, Better, Faster, Stronger?

Were back! and were faster than ever, well over 140% faster to be precise, out with Xampp on windows, in with Ubuntu Webmin!

Wait… Webmin isn’t that taboo? Well, yes, it is but its got loads of features and is really easy to do otherwise time consuming tasks that could take minutes and turn that into a couple of seconds, for example, adding a virtual host to apache2 would require FTP and a bit of nifty .txt editing, but with webmin, its a couple of clicks away, practical in the long run, but it does have it’s drawbacks.

Webmin with Bootstrap 3 theme.

 

Plus webmin is easy to use and quick to configure, so Ill be doing a couple tutorials on that soon enough!

Adding Hosts to Godaddy

GoDaddy is a great Domain Registrar,  I have GoDaddy as my Domain Registrar.

When using domains and Subdomains you may want to have multiple websites on the same server, here is how to add hosts to your GoDaddy account.

 

First Browse to Godaddy and Login, It may be slightly different for you as I live in the UK.

First Click on My Account, then Visit My Account… You should see this page, Expand Domains.

Godaddy Main

On the domain you would like to Add a Host to, Click Launch. You should See this Page….

Godaddy Main 2

Please note some of my sensitive information has been removed…

To add a subdomain Click Host Names (Manage) In there click Add Hostname. The Hostname will be the Subdomain Name, The IP address will be the IP address of the web server(Or Application Server). Then Click Add.

Please note that it can take up to 48 Hours for a DNS Host to update through the Internet.

To Set the Domain Record, Click DNS Zone File, Click Add Record, Select A (Host), Enter the Host that will be the Domain or Subdomain, such as www (By Default this is already configured, so click edit rather than add record.) Then Click Points to, The IP address will be the IP address of the web server(Or Application Server), Then set The Time To Live (TTL), If you are unsure what to do, leave it as default. Then Click Add.

Please note that it can take up to 48 Hours for a DNS Host to update through the Internet.

 

You have now configured a new host!

 

If you want to add another, repeat the process.

How to Configure Apache to use Virtual Hosts

Using Apache is Good, but for one website is limiting its potential you can create multiple websites, domains and sub-domains with Apache, see below.

 

 

First you need to stop your server, you can do this on lunix by entering
[box type=”shadow”] $ sudo /etc/init.d/apache2 stop [/box]

into your terminal, this allows you to make changes to files in your server without breaking it, for windows you need to stop Apache using Xammp, see below.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Now Enable Mod_log_config browse here

[box type=”shadow”]xamppapacheconf[/box]

and in httpd.conf remove the # from this line

[box type=”shadow”]#LoadModule log_config_module modules/mod_log_config.so[/box]

to look like this

[box type=”shadow”]LoadModule log_config_module modules/mod_log_config.so[/box]

 

next using an FTP program or other method, go to your website’s root directory such as

[box type=”shadow”]/home/*/public_html/cgi-bin/[/box]

 

Or for windows

[box type=”shadow”]C:xampphtdocs[/box]

This is the base of your web server, and probably where your Index.html is located, create two folders, Domains and Subdomains, this is where your domains will be stored. See below

Domains, Subdomains

 

 

 

 

 

 

 

In Domains, Place the dome of your website, replacing dots with hyphens like so

Domains, if

 

 

 

(Also add any other websites that you plan to host on the server)

Then copy the data that was in your root of the server into the domain folder on the server like this

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

then paste  into your domain folder like this

 

Paste

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

So your Server should now have the ‘root’ of the server in

[box type=”shadow”]WEBROOTDomainsinfinityflame-co-ukindex.html[/box]

or subsequent folders in Linux.

now you need to redirect traffic from your website to the correct folder, this is done with virtual Hosts’s, and is also how you add sub domains see below

browse to your Apache installation folder and go to httpd-vhosts.conf

[box type=”shadow”]apacheconfextrahttpd-vhosts.conf[/box]

 

and at the bottom add the following code replaceing my domain, with yours.

now test the website, start Apache and browse to your domain, you should see your site as you did before, you have now configured your website to use Virtual Hosts

now you need to add other domains and subdomains, See Here