Local Subdomains under Ubuntu Linux and Apache 2.4 Tutorial

The whole process was described in this article, but there are few quirks to fix in newer Ubuntu (namely 13.10 / Apache 2.4).

  1. Every config file in sites-available has to have a .config extension now, so instead of
    sudo nano /etc/apache2/sites-available/project
    use
    sudo nano /etc/apache2/sites-available/project.config
    if upgrading fist add the extension to the /etc/apache2/sites-available/project config file, then delete /etc/apache2/sites-enabled/project and re-run “sudo a2ensite project” / restart apache.
  2. If your project root is located in your home directory you might get a persistent 403 / Forbidden error, that (weird as it is) has nothing to do with Linux permissions. To fix it add “Require all granted” directory directive. Here is how the new config file might look:
    <VirtualHost *:80>
    DocumentRoot /home/user/project
    ServerName project.localhost
    <Directory /home/user/project>
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
    </Directory>
    </VirtualHost>

So here are updated instructions for Ubuntu 13.10+ (and maybe some older versions). Make sure to replace “project” below with the actual name of your project.

  1. Edit your /etc/hosts file with superuser privileges:
    sudo nano /etc/hosts
    We catch local requests to the new subdomain here, add following line (make sure your apache actually uses 127.0.0.1 and not say 127.0.1.1):
    127.0.0.1 project.localhost
  2. Now configure the new website so that the server knows where to look for it, to do that create a new config file named after our project:
    sudo nano /etc/apache2/sites-available/project.config
    Here we set up the path to the project directory and few other options:<VirtualHost *:80>
    DocumentRoot /home/user/project
    ServerName project.localhost
    <Directory /home/user/project>
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
    </Directory>
    </VirtualHost>

    See Apache Docs to get the idea what these directives mean.
  3. Next use terminal to run the following:
    sudo a2ensite project
    a2ensite is a small utility that creates correct symlinks in sites-enabled to allow your newly configured subdomain to be served.
  4. Last thing to do is to reload Apache so that it “picks up” the new config data:
    sudo service apache2 reload

That’s it, now you can access your project under http://project.localhost/.

6 thoughts on “Local Subdomains under Ubuntu Linux and Apache 2.4 Tutorial

    • www would be another deeper subdomain, normally you can ignore it if it’s about a local project. If you want it, repeating the same process for the same folder should work

  1. Hi, I done all the steps of your tutorial but when I type sudo a2ensite there is an error: ERROR: Site does not exist!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.