How to host multiple websites on one VPS

how to host multiple websites on one vps
In this tutorial, we are going to provide you with step-by-step instructions on how to host multiple websites on one single IP address with Apache or Nginx on an Ubuntu VPS or a CentOS 7 VPS. Hosting multiple domains/subdomains on a VPS is fairly easy using virtual hosts in Apache or using server blocks in Nginx.

Apache Virtual Hosts

Virtual hosts in Apache allow users to run multiple websites off of one IP address as well as fine-tune settings for each website.

In order to configure Virtual Hosts in Apache to host multiple domains/subdomains, log in to your server via SSH and install Apache:

Ubuntu or Debian:

apt-get update
apt-get install apache2

CentOS or Fedora:

yum update

yum install httpd

Create a backup of Apache configuration, then set up virtual host directives for your websites:

Ubuntu or Debian:

Edit the main Apache configuration file (/etc/apache2/apache2.conf) and uncomment the following line if it is not already done so (# are comments in Apache conf files):

vi /etc/apache2/apache2.conf
IncludeOptional sites-enabled/*.conf

CentOS or Fedora:

Edit the main Apache configuration file (/etc/httpd/conf/httpd.conf) and uncomment the following line:

vi /etc/httpd/conf/httpd.conf
NameVirtualHost *:80

Please note, the ‘NameVirtualHost’ directive is already enabled by default on Ubuntu 16.04 and CentOS 7.

Create virtual hosts in Apache for each domain/subdomain. For example, create virtual hosts for domain1.com and domain2.com :

Ubuntu or Debian:

vi /etc/apache2/sites-available/domain1.conf

<VirtualHost *:80>
DocumentRoot “/var/www/html/domain1”
ServerName domain1.com
ServerAlias www.domain1.com

# enter other directives here, e.g. :

<Directory /var/www/html/domain1/>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all

</Directory>
ErrorLog /var/log/apache2/domain1.com-error_log
CustomLog /var/log/apache2/domain2.com-access_log common
</VirtualHost>

vi /etc/apache2/sites-available/domain2.conf

<VirtualHost *:80>
DocumentRoot “/var/www/html/domain2”
ServerName domain2.com
ServerAlias www.domain2.com

# enter other directives here
</VirtualHost>

Enable ‘domain1.conf’ and ‘domain2.conf’ configurations in Apache using:

ln -s /etc/apache2/sites-available/domain1.conf /etc/apache2/sites-enabled/domain1.conf
ln -s /etc/apache2/sites-available/domain2.conf /etc/apache2/sites-enabled/domain2.conf

Or, use the a2ensite command to enable the ‘domain1.conf’ and ‘domain2.conf’ configurations in Apache:

sudo a2ensite domain1.conf
sudo a2ensite domain2.conf

Restart Apache for the changes to take effect:

service apache2 restart

CentOS or Fedora:
Edit the main Apache configuration file (/etc/httpd/conf/httpd.conf) and add virtual hosts at the end:

vi /etc/httpd/conf/httpd.conf

<VirtualHost *:80>
DocumentRoot “/var/www/html/domain1”
ServerName domain1.com
ServerAlias www.domain1.com
# enter other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot “/var/www/html/domain2”
ServerName domain2.com
ServerAlias www.domain2.com
# enter other directives here
</VirtualHost>

Restart Apache for the changes to take effect:

service httpd restart

Create /var/www/html/domain1 and /var/www/html/domain2 directories, then upload your websites to them.

All website files have to be readable by the web server, so set a proper ownership:

Ubuntu or Debian:

chown www-data:www-data -R /var/www/html/domain*

CentOS or Fedora:

chown apache:apache -R /var/www/html/domain*

Nginx Server Blocks

In order to configure server blocks in nginx to host multiple domains/subdomains using a single IP address, log in to your server via SSH and install nginx:

Ubuntu or Debian:

apt-get update
apt-get install nginx

CentOS or Fedora:

yum update
yum install nginx

Create a backup of the nginx configuration, then set up a server block for the first website:

Ubuntu or Debian:

Create a new nginx configuration for the first domain:

vi /etc/nginx/sites-available/domain1.conf

Add the following lines to it:

server {
    listen 80;

    server_name domain1.com;

    root /var/www/html/domain1.com;
    index  index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

# add other directives here;

}

CentOS or Fedora:

Run the following commands:

mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

Add the following lines to the main nginx configuration file (/etc/nginx/nginx.conf) at the end of HTTP block:

vi /etc/nginx/nginx.conf
include /etc/nginx/sites-enabled/*.conf;

Create a new nginx configuration file for the first website:

vi /etc/nginx/sites-available/domain1.com
server {
    listen       80;
    server_name  domain1.com;
    root   /var/www/html/domain1.com/;
    index  index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

# add other directives here;

}

Enable ‘domain1.conf’ configurations in nginx using:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/

Restart nginx for the changes to take effect:

service nginx restart

Upload each website to the root directory specified in the nginx server block, for example:

mkdir -p /var/www/html/domain1

Set a proper ownership of website files, so they can be accessible by the nginx web server (e.g. www-data):

chown -R www-data:www-data /var/www/html/domain1

That is it. Repeat the same procedure for each additional website.

Please note, for each domain or subdomain you want to host on your server, you need to create an A record that points to your server’s IP address and once the DNS changes fully propagate throughout the Internet, your website visitors should be able to access your websites using a web browser.


Of course, you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to set up multiple websites on one single IP address with Apache or Nginx for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

1 thought on “How to host multiple websites on one VPS”

Leave a Comment