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.

Raspberry Pi – PHP permission File Problems Solved!

I recently installed php, but when I was unable to save files, I was very stuck, and that’s why I’m posting it here, I want to tell anyone who Googles this problem to find this solution.

 

The problem was when I used an fopen and fsave within php, the file would always ‘die’ as in the open or die within php. The solution is below

Firstly I looked for the Environment user in phpinfo() in my case it was
[box type=”shadow”]www-data[/box]
and then I simply typed this
[box type=”shadow”]chmod 777 -R www-data /var/www[/box]
that, in its simplicity is the solution, but I am thankful that I have found the solution!

To create a phpinfo you need to create a file in your root and insert the following code

and save it as phpinfo.php or whatever you choose, I saved it in my root but you can save it wherever you want, then I browsed to the environments section and that gave me the necessary information for me to create the solution.

Why this works

The problem was that php did not have enough permissions to write to the folder. by giving the user www-data full permission i.e. 777 it can now read write and execute without a problem!

here is what php looks like when you do phpinfo

phpinfo() user sample on Raspberry Pi
phpinfo() user sample on Raspberry Pi

you can check a file exits and is editable with this code http://stackoverflow.com/a/13297218

Now you’ve finished you can celebrate! here is a song (this is mainly so i can test embedding :P)

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