Install Nginx-Naxsi Ubuntu 16.04


Install Nginx-Naxsi on Ubuntu 16.04

Understanding Nginx and Naxsi

Nginx is a high-performance web server which can also be used as a reverse proxy or load balancer. Naxsi is a Web Application Firewall (WAF) designed to provide protection against malicious requests. This tutorial will show you how to install and configure Nginx with Naxsi on an Ubuntu 16.04 server.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. To check if you have sudo access run the command below:

$ sudo -l

You should see a message similar to this one:

User YOUR_USERNAME may run the following commands on YOUR_SERVER_NAME:
(ALL : ALL) ALL

Installing Nginx

In order to install Nginx, the process is very simple. First, we need to update our package list:

$ sudo apt-get update

Then, we can install Nginx by running the following command:

$ sudo apt-get install nginx

Once installation is completed, you can check the status of the Nginx service using the following command:

$ sudo systemctl status nginx

The output should be similar to 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 Mon 2019-02-25 15:47:24 UTC; 2min 57s ago
Main PID: 1200 (nginx)
Tasks: 2 (limit: 1154)
CGroup: /system.slice/nginx.service
├─1200 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─1202 nginx: worker process

Installing Naxsi

The process of installing Naxsi is very simple. First, we need to move to the opt directory:

$ cd /opt

Then, we need to download the Naxsi source code:

$ sudo wget https://github.com/nbs-system/naxsi/archive/master.zip

Now, we need to unzip the source code:

$ sudo unzip master.zip

We also need to move to the unzipped directory and install the Naxsi dependencies:

$ cd naxsi-master
$ sudo apt-get install libpcre3-dev
$ sudo apt-get install libssl-dev

Then, we need to configure and install Naxsi:

$ ./configure --with-naxsi_src=naxsi_src
$ sudo make install

Configuring Nginx with Naxsi

Now that Nginx and Naxsi are installed on your Ubuntu 16.04 server, let’s configure Nginx to use Naxsi. The first step is to create a basic Nginx configuration file:

$ sudo nano /etc/nginx/conf.d/naxsi.conf

Then, add the following lines to the file:

include "/etc/naxsi/naxsi_core.rules";

server {

listen 80;
server_name example.com;
root /var/www/example.com;

location / {
include /etc/naxsi/naxsi_custom.rules;
try_files $uri $uri/ = 404;
}

}

This configuration defines a virtual host that listens on port 80 and serves content from the /var/www/example.com directory. The Naxsi_core.rules and Naxsi_custom.rules files are included in the configuration and will be used to filter out malicious requests.

Testing the Configuration

The next step is to test the Nginx configuration file for syntax errors:

$ sudo nginx -t

If the configuration is valid, you should see an output similar to this:

 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If the configuration is not valid, you will need to fix the errors and test again. Once the configuration is valid, restart the Nginx service for the changes to take effect:

$ sudo systemctl restart nginx

Conclusion

Congratulations! You have successfully installed and configured Nginx with Naxsi on your Ubuntu server. As long as you have configured your firewall rules appropriately, your server should now be protected from malicious requests.

FAQs

  • What is Nginx?
  • Nginx is a high-performance web server which can also be used as a reverse proxy or load balancer.
  • What is Naxsi?
  • Naxsi is a Web Application Firewall (WAF) designed to provide protection against malicious requests.
  • What is the command to restart Nginx?
  • The command to restart Nginx is sudo systemctl restart nginx.

Thank you for reading this article. Please consider reading some of our other articles for more tips and advice.