If you are running multiple websites, you can set them up with same port so they consume less resources. Here’s how to setup multiple virtual hosts with same port in Apache.
How to Setup Multiple Virtual Hosts with Same Port in Apache
Here are the steps to setup multiple virtual hosts with same port in Apache.
Let’s say you want to set up 2 virtual hosts(dev.domain.com and blog.domain.com) in your Apache server running on same port 80.
1. Open Apache config file
Open Apache config file in a text editor. You will find it at one of the following locations:
$ sudo vim /etc/apache2/httpd.conf
2. Add Virtual Hosts
Add 2 virtual host blocks, one for each virtual host , as shown. They both run on port 80. The key is to use different ServerName directives and DocumentRoot directive to distinguish between the 2 virtual hosts. You can also use separate ErrorLog locations
<VirtualHost *:80> ServerName dev.domain.com DocumentRoot /home/projects/dev ErrorLog /var/log/apache2/dev-error.log </VirtualHost> <VirtualHost *:80> ServerName blog.domain.com DocumentRoot /home/projects/blog ErrorLog /var/log/apache2/blog-error.log </VirtualHost>
Please Note: If you have setup separate virtual host files for each domain in Apache, then update the <VirtualHost> block in their respective files, with the ones shown above.
3. Restart Apache web server
Restart Apache web server to apply changes
$ sudo /etc/init.d/apache2 start [Debian or Ubuntu] # sudo apachectl restart [RHEL, CentOS or Fedora]