Nginx is a powerful webserver used by some of the largest sites on the Internet for its capability to handle a much larger number of cerrunt connactions than Apache while also having a smaller memory footprint per connection.
You can use Nginx as a standalone web server or as a reverse proxy for other web servers such as Apache.
Before you begin following this tutorial, make sure you are logged in your server on a non-root user account with sudo privileges.
1 – Install Nginx
Nginx is available in the default Ubuntu repositories, making the installation relatively easy and straight forward.
Let`s start by updating the package index to make sure
that we have access to the most recent package listings, we will then proceed installing Nginx:
sudo apt update
sudo apt install nginx
At this point, Nginx and any required dependencies should be installed on your server.
2 – Configuring The Firewall
By default, Ubuntu 18.04 uses UFW as a firewall. You are gonna need to open port 80 for HTTP access and port 443 for HTTPS.
This is done by running a single command:
sudo ufw allow 'Nginx Full'
To verify the changes run:
sudo ufw status
The output should look something like this:
tatus: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Nginx HTTP ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Nginx HTTP (v6) ALLOW Anywhere (v6)
3 – Check The Installation
Is good practice to check if everything is running smoothly before proceeding to further configuration.
To do so run the following command:
systemctl status nginx
If everything is running fine the output should look like this:
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-02-05 17:54:50 CST; 1h 35min ago
Main PID: 11317 (nginx)
Tasks: 9 (limit: 4915)
├─11317 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─12251 nginx: worker process
An alternative way to check if Nginx is running is to enter your server IP address into your browser`s address bar:
4 – Managing The Nginx Service
Now Nginx is up and running, it`s useful to know some basic management commands.
To stop the Nginx service, run:
sudo systemctl stop nginx
To start it , run:
sudo systemctl start nginx
To restart the Nginx service, run:
sudo systemctl restart nginx
To reload Nginx (necessary anytime you make some configuration changes for it to take effect), run:
sudo systemctl reload nginx
You now have installed Nginx in your server, further tweaks may be necessary such as setting up server blocks, but apart that, you are now ready to run websites and web application trough Nginx!
If you are gonna use it to run a website, we suggest you get yourself an SSL certificate as it`s gonna give your users a more secure experience while also improve noticeably your rankings on search engines.