Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
Share on:

Host Multiple Domains on One Server or IP with Apache or Nginx

multiple site hosting
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

Let’s learn how to configure Apache and Nginx to host multiple websites.

Hosting sites on the Cloud server gives much flexibility compared to shared hosting. You can install whatever you like, scale up or down, and configure for better performance and cost-optimization.

If you are running multiple business sites but don’t want to host them separately, you can take advantage of Virtual Server in web servers. Let’s take a look at this scenario.

I have the following two sites which I would like to point to my one cloud VM.

  • lab.geekflare.com
  • gf.dev

Before implementation, let’s understand how it works.

The concept to have multiple websites on a single web server instance is called Virtual Server. It is defined in the configuration file along with the URL. When a request is made to a defined URL, the webserver would serve the traffic from the respective Document Root.

Configure Virtual Host in Apache to host multiple domains

  • Login into Apache HTTP Server
  • Go to the apache conf location. ( in default installation – you will find it here /etc/httpd/conf/httpd.conf)
  • Take a backup of httpd.conf file
  • Create a VirtualHost container like below I have done for two domains.
<VirtualHost *:80>
ServerAdmin mail@example.com
   DocumentRoot /opt/htdocs/lab
   ServerName lab.geekflare.com
   ErrorLog logs/lab.geekflare.com-error_log
   CustomLog logs/lab.geekflare.com-access_log common
</VirtualHost>

<VirtualHost *:80>
   ServerAdmin mail@xyz.com
   DocumentRoot /opt/htdocs/gf-dev
   ServerName gf.dev
   ErrorLog logs/gf.dev-error_log
   CustomLog logs/gf.dev-access_log common
</VirtualHost>

Note: Change the value for ServerAdmin, DocumentRoot, ServerName, ErrorLog, CustomLog based on your requirement.

  • Restart Apache HTTP and test both URLs.

Configure Virtual Host in Nginx to host multiple domains

  • Login to Nginx server
  • Go to the location where you’ve installed Nginx. (in default location – you will find it here /etc/nginx/)

There are multiple ways to achieve this. Either you can modify nginx.conf file or custom file if you have any. You may also create a custom.conf under conf.d folder. Choose what works for you and take a backup if modifying an existing file.

  • Create a server block for both URL’s as I have shown below.
server {
listen 80;
   root /opt/htdocs/lab;
index index.html index.htm;
   server_name lab.geekflare.com;
   location / {
       try_files $uri $uri/ =404;
   }
}

server {
   listen 80;
   root /opt/htdocs/gf-dev;
   index index.html index.htm;
   server_name gf.dev;
   location / {
       try_files $uri $uri/ =404;
   }
}

Note: Change the value for root, server_name based on your requirement.

  • Restart Nginx and test both URL’s

Don’t forget to update the DNS record of your URL to map to server IP. Once done, you should be able to access the URLs which are getting served through the above configuration you made.

Conclusion

That was the quick guide to hosting multiple domains in a single web server instance like Apache or Nginx. Once your website is live, don’t forget to test it against security vulnerabilities.

Thanks to our Sponsors
More great readings on Apache HTTP
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • Semrush is an all-in-one digital marketing solution with more than 50 tools in SEO, social media, and content marketing.
    Try Semrush
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder