Friday, December 17, 2021

How To Run Multiple Websites Using Nginx Webserver On Ubuntu 18.04 LTS

 Sometimes it may be wise to run multiple websites or blogs on a single server. For example, running one website or blog per server may not be the best way to utilize your server resources. One server, one website? No way.. just not efficient.
That’s why there’s a feature called Virtual Hosts on Apache2 and Server Blocks on Nginx. Virtual hosts or server blocks are feature on these web servers that allow multiple websites and blogs to be hosted on a single server. If a server has enough resources, why wastes those resources by only hosting a single website?
This brief tutorial is going to show you how to enable server blocks on Nginx web server to host multiple blogs.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Our Server Specification:-
Os : Ubuntu 18.04 LTS Bionic Beaver
Hostname:- www.example.com
IP Address of Server:-192.168.213.232
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
lsb_release -a ; getconf LONG_BIT ; hostname -I ; hostname

apt-get install nginx -y
systemctl restart nginx ; systemctl enable nginx
mkdir -p /var/www/html/yourdomain.com/public_html ; mkdir -p /var/www/html/myexample.com/public_html
gedit /var/www/html/yourdomain.com/public_html/index.html &>/dev/null
<html>
<head>
<title>This is the yourdomain.com domain</title>
</head>
<body>
<p>This is yourdomain.com</p>
</body>
</html>

gedit /var/www/html/myexample.com/public_html/index.html &>/dev/null
<html>
<head>
<title>This is the myexample.com domain</title>
</head>
<body>
<p>This is myexample.com</p>
</body>
</html>

chown www-data:www-data /var/www/html ; chmod -R 755 /var/www/html
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/yourdomain.com
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/myexample.com

gedit  /etc/nginx/sites-available/yourdomain.com &>/dev/null
server {
        listen 80;
        listen [::]:80;

        root /var/www/html/yourdomain.com/public_html;
        index index.html index.htm index.nginx-debian.html;

        server_name www.yourdomain.com;

        location / {
                try_files $uri $uri/ =404;
        }
}
gedit /etc/nginx/sites-available/myexample.com &>/dev/null
server {
        listen 80;
        listen [::]:80;

        root /var/www/html/myexample.com/public_html;
        index index.html index.htm index.nginx-debian.html;

        server_name www.myexample.com;

        location / {
                try_files $uri $uri/ =404;
        }
}
nginx -t ; rm /etc/nginx/sites-enabled/default
ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/myexample.com /etc/nginx/sites-enabled/

systemctl restart nginx ; systemctl enable nginx
nano /etc/hosts
192.168.213.232    www.yourdomain.com
192.168.213.232    www.myexample.com
hostname -I

http://www.myexample.com
http://www.yourdomain.com
Optional steps
sudo nano /etc/nginx/nginx.conf
server_names_hash_bucket_size 64;
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Optional steps
sudo nano /etc/nginx/nginx.conf
server_names_hash_bucket_size 64;

No comments:

Post a Comment