create named virtualhost for local development

Named virtual host to remove localhost

We web developers use localhost quite frequently on development. It is like our main domain in development. We always check the website using localhost as the domain in the URL. It makes development quite simple. But with this process, we come across a few issues which will occur on production and we can easily handle those in development, without waiting for those issues to produce on a production

When we perform development on a local machine the URI which we use to access the website usually is our this type (http://localhost/DOCUMENT_ROOT_of_website1). Now if we are working on more than one website then this URI changes with respect to those sites like (http://localhost/DOCUMENT_ROOT_of_websiten). This is not the same as we access the site from production. Due to this the URI segment changes and the images which appear on the production site suddenly become unavailable on the local machine(if they are not coming from CDN). Please comment if want to know about CDN.

Nowadays we are extensively using React, Angular, VueJs, and microservices for development. This helped in separating data display with data processing. We can host services on another domain and the front end on another domain. Using API we call data from services to the front end. Due to this we normally face cross-origin-policy issues in the development and the reason for this is the same-origin policy. Due to this error API call gives an error and we come to a discussion that API call is happening through postman(Rest API client) and not through code

If we can remove this localhost from URI then these issues can be solved on the local machine only instead of facing them on production.

we are considering LAMP(Linux, Apache, MySQL, PHP) as a development environment. On /etc/hosts file make entry of domain as shown below

127.0.0.1 website1.com

Instead of website1.com, you can give a domain name you want to give to your website.

For ubuntu

Create file website2.com.conf at /etc/apache2/sites-available/

Add below entry in this file.

<VirtualHost *:80>

# Admin email, Server Name (domain name) and any aliases

ServerAdmin webadmin@website1.com

ServerName website1.com

ServerAlias www.website1.com

# Index file and Document Root (where the public files are located)

DirectoryIndex index.html

DocumentRoot /home/demo/public_html/website1.com/public

# Custom log file locations

LogLevel warn

ErrorLog /var/log/apache2/error-website.com.log

CustomLog /var/log/apache2/access-website.com.log combined

</VirtualHost>

Change DocumentRoot as per the location of your index.php file or as per the entry location of your website.

Enable this site/virtualHost using the below command

Sudo a2ensite website1.com

For CentOS

Add below entry in file /etc/httpd/conf/httpd.conf

<VirtualHost *:80>

# Admin email, Server Name (domain name) and any aliases

ServerAdmin webadmin@website1.com

ServerName website1.com

ServerAlias www.website1.com

# Index file and Document Root (where the public files are located)

DirectoryIndex index.html

DocumentRoot /home/demo/public_html/website1.com/public

# Custom log file locations

LogLevel warn

ErrorLog /var/log/httpd/error-website.com.log

CustomLog /var/log/httpd/access-website.com.log combined

</VirtualHost>

Change DocumentRoot as per the location of your index.php file or as per the entry location of your website.

Restart apache using command

Sudo apachectl restart

This makes development smooth as we get a production-like URL. This also helps to solve issues like an image not found and cross-origin-policy issues. if want to discuss more the development environment set-up then please comment

Tejbahadur Singh

Always learner enthusiast finding solutions of technical issues that exist. Web, Blockchain, cybersecurity, web/app security researcher. I am a nerd who can be found reading books, consuming online contents and reading on internet.

2 thoughts on “create named virtualhost for local development


  1. Spot on with this write-up, I really think this website needs
    a great deal more attention. I’ll probably be returning to read more, thanks for the info!

  2. Great site. A lot of useful info here. I’m sending it to a few pals ans also sharing in delicious.

    And naturally, thanks for your effort!

Leave a Reply

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