VirtualHost-prettify url in local website development environment

VirtualHost-prettify url in local website development environment

Posted: 10 years ago in  Unix | Apache |


Common URL used in website development is localhost/website_folder_name that is very unfriendly. In addition, there is feature that requires input/host url in form of mywebsite.com instead of localhost/directory. Then, VirtualHost is a way to make a nice url on development environment.


Common URL used in website development is localhost/website_folder_name that is very unfriendly. In addition, there is feature that requires input/host url in form of mywebsite.com instead of localhost/directory. Then, VirtualHost is a way to make a nice url on development environment.

The guide is made with Apache2 on Ubuntu 12.04

After install Apache by

sudo apt-get install apache2

Go to "/etc/apache2/sites-available"

There is a way to modify the /etc/apache2/apache2.conf or /etc/apache2/httpd.conf, but it would not be good with multiple site development on local pc, I prefers to modify files in sites-available then enable it using a2ensite script

Clone file 000-default/default, you can name it whatever you want, e.g. mysite.com

cd /etc/apache2/sites-available
sudo cp default mysite.com

Edit it by nano editor:

sudo nano mysite.com

Input these lines:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName mysite.com
        ServerAlias www.mysite.com
        DocumentRoot /var/www/directory_name
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/directory_name/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/mysite.error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

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

ServerName and ServerAlias, choose the nice one you can think about

DocumentRoot /var/www/directory_name is website folder in Apache2.

Go to /etc/ and edit hosts

Add in 127.0.0.1 mysite.com below 127.0.0.1 localhost, something like

127.0.0.1       localhost
127.0.1.1       luan-pc
127.0.0.1       mysite.com www.mysite.com

Now restart Apache and enjoy. Type mysite.com in web browser and you are directed to your site.