What is Nginx?
NGINX is an fast and robust open source software that basically a web server used for reverse proxying, caching and load balancing. It also provides HTTP server capabilities and is mainly designed for maximum performance stability. It can be used as a proxy server for email (IMAP, POP3, SMTP) and also use a non-threaded and event-driven architecture.
Nginx mainly use Master/Slave architecture by supporting event-driven, asynchronous and non-blocking model.
Why Should We Use Nginx
Nginx has a robust features for web server, such as:
- Ease of installation and maintenance
- Reduces the time for users
- Improves Performance
- Load Balancing
- Offers scalability
- On the fly upgrades
Step 1. Install Nginx
The first thing you do is update your repositories to get the latest database.
sudo apt update && sudo apt upgrade -y
After the update done, you will then need to install Nginx package.
sudo apt install nginx -y
Verify Nginx version.
$ nginx -vnginx version: nginx/1.14.0 (Ubuntu)
Step 2. Adjust firewall
It is important to protect and hardening your server from abusive users. You can use a robust and powerful firewall such as
sudo ufw enable
You can list the application that your firewall knows by type below command.
$ sudo ufw app listAvailable applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
You’ll see that you know you’ll get all the available applications, Nginx Full, Nginx HTTP and Nginx HTTPS. This profile opens both port 80, the unencrypted web traffic and also the port 443 that allows the encrypted traffic. The Nginx HTTP opens only port 80 that means it only allows the unencrypted traffic and Nginx HTTPS opens to port 443 that allows the encrypted traffic.
So we are will allow Nginx Full both port 80 and 443 are allowed. We will type below command to allow both port 80 and 443.
$ sudo ufw allow 'Nginx Full'Rule addedRule added (v6)
You’ll see that a rule added for Nginx Full. The same if you want to allow only Nginx HTTP or Nginx HTTPS, you just type the same command and mention HTTP or HTTPS, so you know which traffic you will be allowed.
Step 3. Verify your server
To check the firewall status type following command.
$ sudo ufw statusStatus: activeTo Action From-- ------ ----Nginx Full ALLOW AnywhereNginx Full (v6) ALLOW Anywhere (v6)
Step 4. Manage Nginx process
systemd to manage it’s services. We will use
systemctl command as central management tool to control init system. Please refer to manpages to have deep insight and how to use systemctl.
Following command will check Nginx service.
$ sudo systemctl status Nginx● 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 Sun 2019-05-05 17:17:14 CEST; 25min ago Docs: man:nginx(8) Process: 32123 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 32107 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 32125 (nginx) Tasks: 7 (limit: 4915) CGroup: /system.slice/nginx.service ├─32125 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ├─32126 nginx: worker process ├─32128 nginx: worker process ├─32130 nginx: worker process ├─32132 nginx: worker process ├─32134 nginx: worker process └─32135 nginx: worker processMay 05 17:17:14 vmi261528.contaboserver.net systemd: Starting A high performance web server and a reverse proxy server...May 05 17:17:14 vmi261528.contaboserver.net systemd: Started A high performance web server and a reverse proxy server.
To stop Nginx service type this command.
$ sudo sytemctl stop nginx
To restart Nginx service type following command.
$ sudo sytemctl restart nginx
Nginx is free powerful web server, robust, and lightweight. If you have any question please do not hesitate to leave a comment below. Thanks.