Install nginx if not already
# Update the package repository
sudo apt update
sudo apt install -y nginx
# Verify nginx is running by
sudo systemctl status nginx.service
# if not running enabled and start
sudo systemctl enable nginx.service && sudo systemctl start nginx.service
Define the root of the sites
sudo mkdir /var/www/sitename.com
sudo mkdir /var/www/another-site.com
Add hello world HTML page inside sitename.com
, another-site.com
For sitename.com
echo '<html>
<head>
<title>Welcome to sitename.com!</title>
</head>
<body>
<h1 style="color: green;">The sitename.com is working!</h1>
</body>
</html>' | sudo tee /var/www/sitename.com/index.html
For another-site.com
echo '<html>
<head>
<title>Welcome to another-site.com!</title>
</head>
<body>
<h1 style="color: green;">The another-site.com is working!</h1>
</body>
</html>' | sudo tee /var/www/another-site.com/index.html
Configuration of the sites
for sitename.com
echo '
server {
listen 80;
listen [::]:80;
root /var/www/sitename.com;
index index.html index.htm;
server_name sitename.com www.sitename.com;
location / {
try_files $uri $uri/ =404;
}
}
' | sudo tee /etc/nginx/sites-available/sitename.com
for another-site.com
echo '
server {
listen 80;
listen [::]:80;
root /var/www/another-site.com;
index index.html index.htm;
server_name another-site.com www.another-site.com;
location / {
try_files $uri $uri/ =404;
}
}
' | sudo tee /etc/nginx/sites-available/another-site.com
Enable site configuration
#Create a symbolic link from sites-available to sites-enabled
# for sitename.com
sudo ln -s /etc/nginx/sites-available/sitename.com /etc/nginx/sites-enabled/
# for another-site.com
sudo ln -s /etc/nginx/sites-available/another-site.com /etc/nginx/sites-enabled/
sudo systemctl restart nginx
Test from Local Machine
If you are testing this in your local lab environment or virtual box, etc., add an entry to the /etc/hosts
file on your main laptop. Map the domain name (sitename.com, another-site.com) to the IP address of your Ubuntu server.
Note: If you have a real server and a domain name, you don't need the above steps. Simply add an A DNS record to your domain name provider, using the IP address of the server as the value.
Next we will look how to server PHP files