14 April 2016

How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS

Well this has been tested on Ubuntu 14.04 LTS, but it could work on your linux distro too.

Anyway, here is a quick tutorial for using Apache’s virtual hosts to organise sites to be developed locally.

Note: If you are working with a framework like CodeIgniter and need to modify URLs, you’ll need to activate mod_rewrite. See https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite for more details. tldr;

sudo a2enmod rewrite

Create the folder. I’m using example.dev as the test site. Note: I’ve used .dev because I have a plugin that forces the browser to request sites that support https to change the url from, for example http://example.com to https://example.com This will then take you to the online web page instead of the local one.

sudo mkdir -p /var/www/example.dev/public_html

Allow current user privileges to that folder

sudo chown -R $USER:$USER /var/www/example.dev/public_html

Create a test page

nano /var/www/example.dev/public_html/index.html

and add for example,

<html>
  <head>
    <title>This is example.dev</title>
  </head>
  <body>
    <h1>Success! We are running example.dev on virtual hosts!</h1>
  </body>
</html>

Make a copy of your default virtual host file

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.dev.conf

Edit the copy you just created

sudo nano /etc/apache2/sites-available/example.dev.conf

Change the following lines to point to example.dev

<VirtualHost *:80>
    ServerAdmin admin@example.dev
    ServerName example.dev
    ServerAlias www.example.dev
    DocumentRoot /var/www/example.dev/public_html
</VirtualHost>

Enable the virtual host file with the a2ensite tool

sudo a2ensite example.dev.conf

Restart apache

sudo service apache2 restart

Edit your local hosts file

sudo nano /etc/hosts

Add the line

127.0.0.1 example.dev

Test it. Open a browser and type

http://example.dev

Remember, this will now send you your local site example.dev instead of the actual example.dev online (if such a site exists).

You can delete or comment those local host entries after you’ve finished developing your site.

Update

I recently discovered ubuntu disables the functionality of the .htaccess file by default. If you intend to use it during local development, you will need to add this to your example.dev.conf file.

<Directory /var/www/example.dev/public_html >
    # AllowOverride All allows using .htaccess
    AllowOverride All
</Directory>

so the /etc/apache2/sites-available/example.dev.conf will look like this

<VirtualHost *:80>
    ServerName example.dev
    ServerAlias www.example.dev
    ServerAdmin admin@example.dev
    DocumentRoot /var/www/example.dev/public_html

    <Directory /var/www/example.dev/public_html >
        # AllowOverride All allows using .htaccess
        AllowOverride All
    </Directory>
</VirtualHost>

Update 2

If you want to process url routes make further changes

<Directory /var/www/example.dev/public_html >
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all      
</Directory>

No comments:

Post a Comment