How to host multiple websites on one VPS

In this tutorial, we are going to show you step by step instructions on how to how to host multiple websites on the same IP with Apache or Nginx on Ubuntu or CentOS 7. Hosting multiple domains / subdomains on a VPS using Apache virtual hosts or using server blocks in Nginx is pretty straightforward.

Apache virtual hosts

Apache virtual hosts allow users to run multiple websites from the same IP address, along with fine-tuning options for each website. Creation of websites to order in web studio “SeoGain”… SeoGain.ru web studio – professional services for the creation of professional websites, development and promotion of Internet projects of any complexity.

To configure Apache virtual hosts to host multiple domains / subdomains, log into 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

Back up your Apache configurations and then set the virtual host directives for your sites:

Ubuntu or Debian:

Modify the main Apache configuration file (/etc/apache2/apache2.conf) and uncomment the following line, if not already done (# comments on Apache conf files):

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

CentOS or Fedora:

Modify 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 that the ‘NameVirtualHost’ directive is already enabled by default in Ubuntu 16.04 and CentOS 7.

Creation of virtual hosts in Apache for each domain / subdomain. For example, you can create virtual hosts for domain1.ru and domain2.ru:

Ubuntu or Debian:

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

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

# 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.ru-error_log
CustomLog /var/log/apache2/domain2.ru-access_log common
</VirtualHost>

# VI /etc/apache2/sites-available/domain2.conf

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

# enter other directives here
</VirtualHost>

Activate the ‘domain1.conf’ and ‘domain2.conf’ configuration in Apache with:

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: change the Apache config 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.ru
ServerAlias www.domain1.ru
# enter other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot “/var/www/html/domain2”
ServerName domain2.ru
ServerAlias www.domain2.ru
# enter other directives here
</VirtualHost>

Restart Apache for the changes to take effect:

service httpd restart

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

All site files must be readable by the web server, so set the correct property:

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 block

To set up server blocks in Nginx to host multiple domains / subdomains with a single IP, login 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

Back up your Nginx configuration and then create 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:

server {
    listen 80;

    server_name domain1.ru;

    root /var/www/html/domain1.ru;
    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 config file (/etc/nginx/nginx.conf) at the end of the HTTP block:

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

Create a new Nginx config file for the first website:

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

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

# add other directives here;

}

enable configuration ‘domain1.conf’ in nginx with:

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 the proper owner of the website files so that they can be accessed by the Nginx web server (e.g. www-data):

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

All. Repeat the same procedure for each additional website.

Please note that for every domain or subdomain that you want to host on your server, you need to create a record that points to the IP address of your server and after changing the DNS, it will fully propagate throughout the Internet and your website visitors should be able to receive access to your sites using a web browser.

Sidebar