Creating two websites — Nginx and Ubuntu 20.04

Mutale Mulenga
2 min readDec 22, 2020

--

Multiple Websites on One VPS

You may be like me trying to create to websites on one VPS you just bought.

Here is the code that helped me to.
#install nginx
sudo apt update
sudo apt install nginx

After Installing NginX or engine X as other’s pronounce it.
Allow nginx through the fire wall.
sudo ufw allow ‘Nginx HTTP’

You can check status as follows:

sudo ufw status

You will get the following results

Status: active

To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
#Step1.sudo mkdir -p /var/www/domain1.com/public_html
sudo mkdir -p /var/www/domain2.com/public_html
#Step2
sudo chown -R $(whoami):$(whoami) /var/www/domain1.com/public_html
sudo chown -R $(whoami):$(whoami) /var/www/domain2.com/public_html
#Step3 - Allow writting to file
sudo chmod -R 755 /var/www
#Step4 - Create an index.html file in your first domain:sudo nano /var/www/domain1.com/public_html/index.html#Step 5 - Add Html
<!DOCTYPE html>
<html>
<head>
<title>Domain1.com</title>
</head>
<body>
<h1>Welcom to Domain1.com</h1>
</body>
</html>
#Step 6 - copy the created file to the other site
sudo cp /var/www/domain1.com/public_html/index.html /var/www/domain2.com/public_html/
#Step 7 - open copied file
sudo nano /var/www/domain2.com/public_html/index.html
#Step 8 - Add Html
<!DOCTYPE html>
<html>
<head>
<title>Domain2.com</title>
</head>
<body>
<h1>Welcome to Domain2.com</h1>
</body>
</html>
#Step 9 - Create Server Block Files for Each Domainsudo nano /etc/nginx/sites-available/domain1.com
#Step 10 - add to sever block domain1.com
server {
listen 80;
listen [::]:80;
server_name domain1.com www.domain1.com;root /var/www/domain1.com/public_html;
index index.html index.php index.htm index.nginx-debian.html;
access_log /var/log/nginx/domain1.com.access.log;
error_log /var/log/nginx/domain1.com.error.log;
}
#Step 11 - Create the Second Server Block File
sudo cp /etc/nginx/sites-available/domain1.com /etc/nginx/sites-available/domain2.com
#Step 12 - Open the copied Server Block
/sudo nano etc/nginx/sites-available/domain2.com
#Step 13 - add to server block domain2.com
server {
listen 80;
listen [::]:80;
server_name domain1.com www.domain2.com;root /var/www/domain2.com/public_html;
index index.html index.php index.htm index.nginx-debian.html;
access_log /var/log/nginx/domain2.com.access.log;
error_log /var/log/nginx/domain2.com.error.log;
}
#Step 14 - Enable your Server Blocks and Restart Nginxsudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/domain2.com /etc/nginx/sites-enabled/
#Step 15 - also remove the symbolic link for the default server block.sudo rm /etc/nginx/sites-enabled/default#Step 16 - Now restart Nginx.sudo service nginx restart

For domains1 and domain2 you can replace them with your domains.
In your domains you need to point to the servers .

You can also create free domains at http://freenom.com

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Mutale Mulenga
Mutale Mulenga

Written by Mutale Mulenga

Entrepreneurship is at the centre of my heart...

No responses yet

Write a response