Install and Setup NGINX on Ubuntu 18.04 server
In this tutorial, you’ll learn how to install and set up NGNIX on your Ubuntu 18.04 server and a little tutorial on how you can use it.
What is NGINX
NGINX is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server, reverse proxy, caching, load balancing, media streaming, and more. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.
Prerequisites
So, I’m assuming that you have successfully logged into your Ubuntu 18.04 server. And know some basic Linux commands.
If you don't have a server then you can create your first server by creating your account via this affiliate link that will provide you with $200 credit for 2 months to experiment with their resources.
Now Let’s install NGINX.
1- Installing Nginx
As Nginx is available in Ubuntu’s default repositories, you install it from these repositories using the “apt” packaging system.
Since this may be your first interaction with the apt
packaging system in this session, update the local package index so that you have access to the most recent package listings. Afterward, you can install nginx
:
After running this command, apt will install Nginx and any required dependencies to your server.
2- Adjusting the Firewall
Ok so, before testing Nginx, the firewall software needs to allow access to the service. Nginx registers itself as a service with ufw upon installation.Thus, making it straightforward to allow Nginx access.
First we will list the application configurations that ufw knows how to work with by typing the following:
Your output should be a list of the application profiles:
This list displays three profiles available for Nginx:
- Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
- Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
- Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)
It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Since you haven’t configured SSL for your server yet in this guide, you’ll only need to allow traffic on port 80.
You can enable this by typing the following:
Then, verify the change:
You should receive a list of HTTP traffic allowed in the output:
Now that you’ve added the appropriate firewall rule, you can check that your web server is running and able to serve content correctly.
3- Checking your Web Server
After the installation, Ubuntu 18.04 starts Nginx. So, the web server should already be up and running.
You can check this one out, with the “systemd” init system to make sure the service is running:
This output shows that the service has started successfully. However, the best way to test this is to actually request a page from Nginx.
You can access the default Nginx landing page to confirm that the software is running properly by navigating to your server’s IP address. If you do not know your server’s IP address, you can get it a few different ways.
Try typing the following at your server’s command prompt:
You will receive a few lines. You can try each in your web browser to confirm if they work.
An alternative is running the following command, which should generate your public IP address as identified from another location on the internet:
When you have your server’s IP address, enter it into your browser’s address bar:
You should receive the default Nginx landing page:
This page is included with Nginx to verify that the server is running correctly.
4- Managing the Nginx Process
Now that you have your web server up and running, let’s review some basic management commands.
To stop your web server, type the following:
To start the web server when it is stopped, type the following:
To stop and then start the service again, type the following:
If you are simply making configuration changes, you can often reload Nginx without dropping connections instead of restarting it. To do this, type the following:
By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing the following:
To re-enable the service to start up at boot, you can type the following:
Nginx should now start automatically when the server boots again.
Conclusion
Now, I have shown you how you can install NGINX and some useful commands to operate it. I wish you a happy coding journey😊✌️.
And, if you have any questions or remarks, please leave a comment below and if you really liked my work then you can buy me a coffee.