If you are running multiple websites, then it might be a good idea to run them on the same server to save costs and resources. You can easily do that with the help of virtual hosts. Here’s how to setup multiple virtual hosts with different port in Apache.
How to Setup Multiple Virtual Hosts with Different Port in Apache
Here’s how to setup multiple virtual hosts with different 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 different ports, 9000 and 9001 respectively.
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 *:9000> ServerName dev.domain.com DocumentRoot /home/projects/dev ErrorLog /var/log/apache2/dev-error.log </VirtualHost> <VirtualHost *:9001> ServerName blog.domain.com DocumentRoot /home/projects/blog ErrorLog /var/log/apache2/blog-error.log </VirtualHost>
The key is to specify different port names, ServerName and DocumentRoot in each virtual host.
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]