How to Set Up Apache Virtual Hosts for Development on Ubuntu

There are a many tutorials that explain how to set up virtual hosts for a production server on Apache. It’s not that different for a development enviornment, but there aren’t very many tutorials on how to do this.

The main difference is instead of pointing the DNS of a domain to your server’s IP Address you need to map your /etc/hosts file from a local domain you selected to a local loopback address (the most common of these being 127.0.0.1).

This tutorial assumes that you have a LAMP stack installed on your Linux Desktop. If you do not I will have a tutorial up on that soon. Alright, let’s begin.

The first thing you want to do is go to your sites-available folder.

cd /etc/apache2/sites-available

This folder will contain the config files for all of the virtual sites you have created. If you have not created any, you should have a file 000-default.conf. Let’s copy that folder and name it newsite.conf.

sudo cp 000-default.conf newsite.conf

Let’s take a look at that config file

sudo nano newsite.conf

We have the following text inside (comments removed):

   
<VirtualHost *:80>
   
        ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

     
</VirtualHost>


We’re going to edit this information, then I will explain it below.

   
<VirtualHost *:80>
   
        ServerName newsite.local

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/newsite/public_html

        ErrorLog /var/www/html/newsite/logs/error.log
        CustomLog /var/www/html/newsite/logs/access.log combined

     
</VirtualHost>


ServerName refers to the domain name you will select. For a production environment this will be the domain name you selected. For a development environment it will be a domain name you select. You can use almost any TLD, but I tend to use .local. Note that the TLD .dev will not work.

DocumentRoot refers to the directory that will contain the index page of the site. I usually place the document root in a public_html file inside of a site’s directory. That way I can place DB connection files inside of the site name folder. This is a security measure – say PHP is misconfigured and Apache is just loading PHP code. A hacker can load any file and see the code. Placing the DB connection file outside of the server ensures they won’t be able to get the connection information.

ErrorLog refers to the location of the Apache error log for this site. The CustomLog refers to the access log. Both of these are stored in a log file inside of the logs folder in the newsite directory.

Next we need to create the directory.

cd
mkdir -p /var/www/html/newsite/logs
mkdir -p /var/www/html/newsite/public_html

Once the directory exists we can add newsite.local (the url we selected) to the /etc/hosts file. /etc/hosts maps domain names to IP addresses. We will map it to 127.0.0.1.

sudo nano /etc/hosts
127.0.0.1    newsite.local

Save the file and close it.

Finally we want to enable the site with Apache.

sudo a2ensite newsite.conf
sudo service apache2 reload

If you haven’t had any typos in your configuration files you should have a new virtual site working.

Published by burnedfaceless

Brian Abbott is studying IT and Music Composition at Georgia Southern University.

Leave a comment

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