Apache2: Smarter Virtual Host Configuration for Ubuntu 14.04

Configure Your site smartly:

There are only one step if you configure once (first configuration is little lengthy but not hard). This will be as simple as:

Create a directory for the site’s files. Something like:
/var/www/html/local/{your project directory} (for my case)

And you are done! Your URL will be http://your_project_directory.local

Let see how can we configure for this. There are only 2 step out from LAMP installation.

Enable vhost_alias_module

sudo a2enmodule vhost_alias_modul

Create a site configuration

sudo vim /etc/apache2/sites-available/local.conf

add this configuration in to this file and save the file

<VirtualHost *:80>
UseCanonicalName Off
ServerAlias *.local
VirtualDocumentRoot "/var/www/html/local/%-2"

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

Now enable site configuration

sudo a2ensites local.conf

Everything is done except dynamic DNS, I mean I want to use wild card like *.local. So that any domain .local will point on my host pc. dnsmask is the most easiest solution for this simple purpose. Install dnsmask and add wild card for your domain.

sudo apt-get install dnsmasq

Now edit its configuration:

sudo vim /etc/dnsmasq.conf

Add this line at the end of this file

"address=/.local/127.0.0.1"

Now restart dnsmasq

sudo service dnsmasq restart

Your configuration is done. This settings is only for *.local URLs.

To add a new site in local environment, just put your project directory in the /var/www/html/local/ and browse with URL like http://directoryname.local, cheers..


In VirtualDocumentRoot I used a dynamic URI to point the dynamic %-2 returns the 2nd part of the URL from the last.

You can use any pattern you like, Please refer here for more details of the URI pattern those are supported by Apache.

Advertisements

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s