Use code KB4KDO0L9 to receive a 10% recurring discount on any server.
Have you ever wanted to host your website on a server? Creating a server to host your blog, CV, or full-fledged website is easier than you think.
This tutorial will show you how to install Nginx on Ubuntu 20.04 hosted on a ServerMania server. Nginx is a web server that will serve the pages from your website when someone requests them by visiting it.
For this tutorial, we will be using need the following:
- A ServerMania Hybrid or Dedicated Server
- Ubuntu 20.04 operating system
- 512MB RAM
- 2GB Disk Space
These requirements change based on the expected traffic flow to your website. For upgrading, check ServerMania’s range of offerings.
For hosting Ubuntu 20.04 on a ServerMania server, see Hosting Ubuntu on ServerMania.
Configure Ubuntu 20.04
Step 1: Log in using your root password you had set while spinning up the Ubuntu instance using the following command:
root@localhost:~# ssh root@servermania_server_ip
Alternatively, you can use SSH keys to perform a passwordless login. As a best practice, do not use the root user for performing administrative tasks.
Step 2: Instead, create a new user and grant the relevant admin privileges to that user. Use a strong password for the new user.
root@localhost:~# adduser smuser
Step 3: To grant user privileges, you need to add this user to the sudo group. Use the following command:
root@localhost:~# usermod -aG sudo smuser
Step 4: Log in using the new user using the following command:
Install Nginx (Ubuntu 20.04)
Every operating system usually comes with a package manager. With Ubuntu 20.04, you can either use apt or apt-get. As this is a new server, you first need to update the package manager.
Step 5: Update the package manager to the latest compatible version:
You can either use the apt or apt-get package manager to install Nginx.
sudo apt update
sudo apt-get update
Now, you are ready to install Ngnix. As you are logged in with a non-root user with sudo privileges, you will need to prefix every command that requires admin privileges with sudo.
Step 6: Install Ngnix web server using apt:
sudo apt install nginx
sudo apt-get install nginx
Although the service starts automatically after installing Nginx, before you can test whether Nginx is working correctly or not, you will need to configure the OS firewall to access Nginx.
Configure Firewall for Nginx
The most common tool used to define ingress and egress rules in iptables. Ubuntu 20.04 has a user-friendly wrapper over iptables, which is called uncomplicated firewall or ufw. Although it comes installed with Ubuntu 20.04 but to make sure that it is appropriately installed, install it manually. Follow the steps mentioned below to set up the firewall after installing Nginx:
Step 1: Install ufw:
sudo apt install ufw
Step 2: Check if ufw is working fine:
sudo ufw status
Step 3: Allow traffic over HTTP and recheck the status:
sudo ufw allow 'Nginx HTTP'
Step 4: Allow traffic over TCP (OpenSSH) too if you want to log into the machine again:
sudo ufw allow 'OpenSSH'
Step 5: If the status was inactive when you checked earlier, activate it:
sudo ufw enable
You will be prompted to decide whether you want to apply the changes or not. Beware that if you disable TCP traffic or add a deny OpenSSH rule in the firewall, you won’t be able to log into the machine again. Let's now discuss some important Nginx commands for administration and management.
Nginx Installation & Management
Step 1: Check the status of the Nginx server:
sudo systemctl status nginx
Step 2: You can also check if Nginx is working by accessing your website using the browser. Visit the following link:
Step 3: Use systemctl to start, stop, or restart your Nginx server:
sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl restart nginx
Step 4: The Nginx configuration file is found at /etc/nginx/nginx.conf. If you make changes to the configuration, you can either restart the Nginx server or reload the configuration file.
systemctl reload nginx # reloads Nginx configuration
Whenever possible, use the reload command instead of the restart command as it doesn’t impact your existing connections to Nginx.
Configure Server Blocks
Server blocks provide similar functionality to Apache Web Server's virtual hosts. Apache Web Server is a very popular server used most commonly in the LAMP Stack. To install the LAMP stack on ServerMania servers, see How to Quickly Setup Your Own Server.
Nginx server blocks enable you to use more than one domain with your Nginx server. If you want to set up two domains, domainone.com, and domaintwo.com on the same Nginx server, you need to go through the following steps:
Step 1: Create a directory, one for each domain:
By default, Nginx creates only a single server block that is pointed to the /var/www/html directory. This would work perfectly well if you only had one site. The html subdirectory contains all the content hosted on the website. Create the html subdirectories for both your domains.
sudo mkdir -p /var/www/domainone.com/html
sudo mkdir -p /var/www/domaintwo.com/html
Step 2: Change ownership of the directories:
Doing this will enable the user to create and edit the contents of these new directories.
sudo chown -R $user:$user /var/www/domainone.com/html
sudo chown -R $user:$user /var/www/domaintwo.com/html
Step 3: Modify permissions for both the domain directories:
This step is important to make sure that you don't need root privileges to make changes to any of the sites.
sudo chmod -R 755 /var/www
Step 4: Create sample web pages for each domain/site:
Create a landing page index.html for both domains. Create a basic HTML snippet for a welcome page for both sites.
Once the editor is open, paste the following into the editor:
<title>Welcome to ServerMania's Domain One!</title>
<h1>You have reached Nginx via Server Block #1</h1>
Save the index.html file and create a similar one for the second domain. Make sure you change the welcome message.
Step 5: Create Server Blocks:
As mentioned earlier, by default, Nginx has only one server block. You can find it here — /etc/nginx/sites-available/default. Now, because we have two sites to cater to, we’ll need two server blocks. The server blocks allow traffic on port 80. Let’s copy the default server block for our new server blocks.
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/domainone.com
Similarly, copy the default file and create the second server block too.
Step 6: Mark One of the Two Servers as Default:
You can only have a single default server with the default_server option enabled. Please remove it from one of the server blocks.
Step 7: Enable Server Blocks & Restart Nginx:
You can enable the server blocks by creating symbolic links from the server block configuration files to the sites-enabled directory.
sudo ln -s /etc/nginx/sites-available/domainone.com /etc/nginx/sites-enabled/
Once you create symbolic links for both the domains and restart Nginx, it will start serving the requests for both domains from their respective blocks received on port 80.
To test the changes, you can try visiting both domains and see if the contents of index.html are rendered correctly.
You are now ready to install Nginx on Ubuntu 20.04!