Nginx, pronounced as “Engine-x”, is a popular high-performance Linux web server that is also used as a reverse proxy and load balancer. In particular, it connects multiple internet resources through HTTP.
The primary job of Nginx is to store, process, and then deliver website content to users. To access a website, a web browser makes a request through HTTP. The request is received by the web server, which then gets approved, and the webserver (in this case, Nginx) sends the required material, using HTTP, to the browser.
Performance is optimized with Nginx since it uses less memory processing web requests with a single thread, and each request is carried over simultaneously and seamlessly. If you don’t know how to install and configure Nginx on Ubuntu, read this guide, as we will be explaining every step for doing it.
So, without further ado, let’s get started!
Step 1. Installing the Nginx Package.
The Nginx package is already available in Ubuntu’s default repositories. This will save you the time and hassle of needing to download the Nginx package first for installation and configuration.
You can install the package directly using the “apt” package system. Before that, you should first update your packages so you can access the latest versions.
Follow these steps to successfully install the Nginx package on your system:
- First, open the Command Terminal on your system.
- Once opened, type the following command:
$ sudo apt update
- Once the updates are finished, type the following command to install the Nginx package:
$ sudo apt install nginx
- Lastly, verify the presence of Nginx using this command:
$ nginx -v
With this, you are done with the first step. Next, we move on to the configuration phase.
Step 2. Starting the Nginx Service.
After Nginx has been installed, you need to know the different commands used to either launch, enable/disable or check the status of the service. We will be going through each command.
Follow these steps:
- First, open the Terminal.
- Next, type this command to first check the status of Nginx:
$ sudo systemctl status nginx
If you get active or running on output, Nginx is working.
- In case Nginx isn’t running, use this command to launch it:
$ sudo systemctl start nginx
- To stop Nginx, this command is used:
$ sudo systemctl stop nginx
- Afterward, to automatically launch Nginx on system startup, use this command:
$ sudo systemctl enable nginx
- To prevent Nginx from launching at startup, you can use this command:
$ sudo systemctl disable nginx
These are the few important commands used to launch and disable Nginx.
Step 3. Adjusting the Firewall for Nginx.
Before you test out the Nginx service, you must adjust your firewall setting to direct safe internet traffic to your server. There are three profiles available with Nginx:
- Nginx HTTP: This profile uses port 80 for normal and unencrypted web traffic.
- Nginx HTTPS: This profile uses port 443 for TLS/SSL encrypted traffic.
- Nginx Full: This profile uses both ports 80 and 443 to manage internet traffic.
Experts who know what they are doing can choose any profile available. For beginners, it’s best to use Nginx HTTP.
Follow these steps to choose your preferred profile:
- Open the Terminal.
- Type the following command in the Terminal:
$ sudo ufw app list
You’ll be given this list as your output:
- To choose the ‘HTTP‘ profile, use this command:
$ sudo ufw allow 'Nginx HTTP'
With this, you’re now aware of how to configure your firewall settings.
Step 4. Accessing the Nginx landing page.
$ status command should ensure that your web server is working, but there is another way to be sure of it.
To do this, access the Nginx landing page using your web server’s IP address. If you can access it, then the software is working properly. If you don’t know the IP address of your web server, you can use the ‘icanhazip.com‘ tool to find it out.
Follow these steps to access your Nginx landing page:
- First, open the Command Terminal.
- Once opened, type in this command to find the IP address of your server:
$ curl -4 icanhazip.com
When you get the IP, open any web browser and type it in the address bar. This should lead you to the Nginx landing page.
With this, you have now completed the basic steps on how to install and configure Nginx on Ubuntu.
Setting Up Server Blocks.
Nginx enables you to set up different domains on a single web server. This feature is known as “server blocks”. In Ubuntu, there is one server block enabled by default. This server block functions out of the “/var/www/html” directory. The directory works fine for a single website but for multiple sites, it can become a mess.
To create your domain, you will make another directory within “/var/www” that will host it.
Follow these steps to set up your domain on Nginx:
- First, open the Terminal.
- Next, create a directory using the following command:
$ sudo mkdir -p /var/www/domain_name/html
In it, “domain_name” is the title you will give to your domain.
- With that done, give ownership of the domain using the
$USERcommand by typing the following:
$ sudo chown -R $USER:$USER /var/www/domain_name/html
To ensure the owner has permission to read, write, and execute the files, while others can only read and execute the files, use this command:
$ sudo chmod -R 755 /var/www/domain_name
- To create a sample HTML page, use the following command:
$ nano /var/www/domain_name/html/sample.html
- Upon opening, write the text as shown in the image:
- Now you need to make a new configuration file so that Nginx can host this domain correctly:
$ sudo nano /etc/nginx/sites-available/domain_name
- Make the following edits:
- Next, create a link to enable the file:
$ sudo ln -s /etc/nginx/sites-available/domain_name/etc/nginx/sites-enabled/
- After this, you need to test to make sure you have made no errors:
$ sudo nginx -t
- Lastly, if there are no errors, restart Nginx to apply your changes:
$ sudo systemctl restart nginx
With this, you have made two server blocks and you can access your site by opening a web browser and typing “http://domain_name” in the address bar.
Furthermore, if you want to make your Ubuntu device more secure, learning how to add fingerprint login in Ubuntu and other Linux distros should help you out.
That ends this guide on how to install and configure Nginx on Ubuntu. We hope that we helped you understand each step and you were able to execute them successfully. If you have any queries or doubts, let us know in the comment section down below.
If this guide helped you, please share it. 🙂