Linux Tutorials

Virtual Sites – Apache Ubuntu Windows 10

Apache is up and running. Now we need to set-up multiple sites (virtual hosts) on Apache2. Below we walk through a baseline vhost file and set-up.

Edit the apache config file, I am a huge fan of nano, but you can easily use vim or another. If you new to text editors in Linux Nano is a nice simple easy to use interface https://wiki.gentoo.org/wiki/Nano/Basics_Guide

sudo nano /etc/apache2/apache2.conf

Add the below lines to the bottom of the conf file. For more information on the AcceptFilter option please visit Apache Docs https://httpd.apache.org/docs/2.4/mod/mpm_winnt.html

If your interested in diving deepers into Apache configuration options https://httpd.apache.org/docs/2.4/sections.html

Remember to restart Apache;

Servername localhost
AcceptFilter http none
service apache2 reload

Now we need to set up virtual host files in Apache. This will allow us to future proof the set-up so we can run multiple domains and code bases from our local machine.

cd /etc/apache2/sites-available/
sudo nano sitename.local.conf

Create a virtual host file for your new domain {sitename.local} the name of the file MUST match your doamain name with a .conf on the end.

Make sure to change the locations for the documents to match the folder location on your windows machine that your hosting your web files from.

Remember when your using the Ubuntu CMD you need to use the /mnt/c/ path to access your Windows files. As long as you map them by using the /mnt/c/ path then Linux and Windows file systems will talk to eachother.

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName sitename.local
        ServerAlias *.sitename.local

        DocumentRoot /mnt/c/www/sitename.local

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

        <Directory /mnt/c/www/sitename.local>
                Options Indexes FollowSymlinks MultiViews
                AllowOverride All
#               Order allow,deny
#               Allow from all
                Require all granted
        </Directory>
</VirtualHost>

The above is a baseline vhosts file I recommended that will cover most major framework needs. However vhosts files can be fine tuned for those who are interested to have a further read and to play around https://httpd.apache.org/docs/2.4/vhosts/examples.html

Save the file and enable the virtual host in Apache.

sudo a2ensite sitename.local.conf
sudo service apache2 reload

If you get an error from Apache open the conf file and check the spelling and for any typos. Also check that the conf file has the same name as the domain its related to. Any spelling mistakes will make Apache throw and error and therefore not be able to enable the site. 95% of errors in Apache will be spelling errors so triple check. Don’t be put off by failures once you correct the conf file Apache will run as expected.

You will also need to add the domain to the hosts file of Ubuntu and Windows. Make sure you edit the file using sudo or sudo -i

sudo nano /etc/hosts
127.0.0.1         localhost
127.0.0.1         sitename.local

Check your windows host file has the correct entries. You can open Notepad as administrator and then navigate to c:\Windows\System32\drivers\etc\hosts

You can also open the hosts file from the command line but don’t forget to invoke admin privileges.

It is best to create a www folder outside of the Ubuntu root and symlink it. Have a look at mnt folder and you should see the letters of your Windows drives represented. Create a directory on the drive you want to store your web development files.

cd /mnt
ls -la 
mkdir /mnt/c/www/html

We then need to symlink the directory to our ubuntu web root. This will then mean our web root is now pointing to a folder outside of the Ubuntu subsystem. If there are any problems with the Ubuntu package and you need to reinstall the www folder will not be affected now as it lives outside of Ubuntu.

ln -s /mnt/c/www/html /var/www/html

For any further domains you want to use just follow the above template making the adjustments for the domain and document. Certain frameworks can and generally will require unique settings in the conf file so be sure to check for any of these when setting up a new framework.

The settings included above give you a baseline to start with. From here you can just modify how you need to for any specific needs for each project. If this is your first time writing a vhosts file don’t worry, the above settings will be more then enough for your needs the majority of the time.

Where does Ubuntu File system live?

I have written a blog post covering the topic of Ubuntu file location in Windows and Windows file system in Ubuntu. https://aimeemaree.com/ubuntu-files-on-windows-10/