Shares 21

Configure Apache/Nginx to host multiple websites

If you are on dedicated or VPS like DigitalOcean or any other server and planning to host multiple websites on a single server then here is how you can do it either with Apache HTTP or Nginx web server.

This assumes you have already mapped DNS to your host, like following two domains I have mapped to DigitalOcean.

  1. bestflare.com
  2. usefulread.com

Before you implement this, to give you an idea how it works. The concept to have multiple websites on a single instance is called Virtual Server. Virtual server configuration is defined within web server configuration and based on server/IP address, a request is getting forwarded to respective Document Root.

Configure Virtual Host in Apache to host multiple domains

  • Login into Apache HTTP Server
  • Go to 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 [email protected]
   DocumentRoot /opt/htdocs/bestflare
   ServerName bestflare.com
   ErrorLog logs/bestflare.com-error_log
   CustomLog logs/bestflare.com-access_log common
</VirtualHost>
<VirtualHost *:80>
   ServerAdmin [email protected]
   DocumentRoot /opt/htdocs/usefulread
   ServerName usefulread.com
   ErrorLog logs/usefulread.com-error_log
   CustomLog logs/usefulread.com-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 into Nginx server
  • Go to virtual.conf location (in default location – you will find it here /etc/nginx/conf.d/virtual.conf)
  • Take a backup of virtual.conf
  • Create server block for both URL’s as I have shown below.
server {
listen 80;
   root /opt/htdocs/bestflare;
index index.html index.htm;
   server_name bestflare.com;
   location / {
       try_files $uri $uri/ =404;
   }
}
server {
   listen 80;
   root /opt/htdocs/usefulread;
   index index.html index.htm;
   server_name usefulread.com;
   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

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

Shares 21

Reader Interactions

Comments

Comments

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