How to Setup Multiple Domains on Same IP in Apache


setup multiple domains on same ip in apache

If you run multiple websites and want to save on server costs, you try running them on the same server or IP. Here’s how to setup multiple domains on same IP in Apache web server.

 

How to Setup Multiple Domains on Same IP in Apache

Here are the steps to setup multiple domains on same IP in Apache.

Let’s say you want to set up 2 domains(www.domain1.com and www.domain2.com) in your Apache server running on same IP and 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:

  • /etc/apache2/httpd.conf
  • /etc/apache2/apache2.conf
  • /etc/httpd/httpd.conf
  • /etc/httpd/conf/httpd.conf

 


$ sudo vim /etc/apache2/httpd.conf

 

 

2. Add Virtual Hosts

We will use NameVirtualHost and ServerAlias directive to run both virtual hosts on same ip and port.

Add the NameVirtualHost directive.

Next, 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, ServerAlias and DocumentRoot directives to distinguish between the 2 virtual hosts. You can also use separate ErrorLog locations if you want.

 


NameVirtualHost *:80

<VirtualHost *:80>
 ServerName www.domain1.com
 DocumentRoot /var/www/html/domain1
 ErrorLog /var/log/apache2/domain1-error.log
</VirtualHost>

<VirtualHost *:80>
 ServerName www.domain2.com
 DocumentRoot /var/www/html/domain2
 ErrorLog /var/log/apache2/domain2-error.log
</VirtualHost>

 

The key is to specify different ServerName and DocumentRoot in each virtual host.

 

If the above code doesn’t work for you, or if you want to serve both websites from same DocumentRoot, you can try using ServerAlias directive


NameVirtualHost *:80

<VirtualHost *:80>
 ServerName www.domain1.com
 ServerAlias www.domain2.com
 DocumentRoot /var/www/html/domain1
 ErrorLog /var/log/apache2/domain1-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]

 

That’s it! Now both domains www.domain1.com and www.domain2.com will serve from the same IP

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!