There are two possible ways to install nginx: you can download from the official website a pre-bluit package or you can decide to compile it by yourself and configure it as you wish. I strongly suggest first time users to use the prebuild package. However, building it by yourself could be a chance to learn about compiling and nginx 3rd party modules.
This is the only guide available on the internet that have been heavy tested, do not trust other methods because this one JUST WORKS!>
Download the key and add it to apt program keyring.
wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key
nano /etc/apt/sources.list
Add the following code at the end of the file in two different rows. Substitute "codename" with the debian distribution you have installed like "wheezy" or "Jessie".
deb http://nginx.org/packages/debian/ codename nginx
deb-src http://nginx.org/packages/debian/ codename nginx
If you want to install the mainline version append instead the following code.
deb http://nginx.org/packages/mainline/debian/ codename nginx
deb-src http://nginx.org/packages/mainline/debian/ codename nginx
For ubuntu place
deb http://nginx.org/packages/mainline/ubuntu/ codename nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ codename nginx
With this passage you install nginx with apt-get in order to get int.d and pid files. If you decide to skip this passage you will need to crate that files yourself.
sudo apt-get update
sudo apt-get install nginx
Now stop and remove nginx leaving only init.d and pid files.
sudo service nginx stop
sudo apt-get remove nginx
sudo apt-get remove nginx-common
Get rid of nginx configuration.
sudo rm -r /etc/nginx
cd /tmp
wget http://nginx.org/download/nginx-1.9.x.tar.gz
Extract it.
tar -zxvf nginx-1.9.x.tar.gz
Refresh the repositorie.
sudo apt-get update
The next command will install the required dependencies.
sudo apt-get build-dep nginx
Enter the folder containing the package downloded.
cd nginx-1.9.x
Type the following command in one row. Check that most of the code output generated by ./configure command gives "found" values. If not try to use the sudo before the command or check again your configuration for errors.
./configure ...
As there are many configuration arguments possible I suggest you to look at Nginx Docs in order to best fit your needs. If you include Naxsi rules I suggest you to put that module at the top of the ./configure command, for more info Naxsi wiki.
After having configured the installation is time to compile the package with the following command make; and install it make install.
Lastly you need to unmask the installed service.
sudo systemctl unmask nginx
Download naxsi
wget https://github.com/nbs-system/naxsi/archive/0.54.tar.gz
tar -zxvf 0.54.tar.gz
Include naxsi in the ./configure file
--add-module=/tmp/naxsi-0.54/naxsi_src
Download zlib
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
Include naxsi in the ./configure file
--with-zlib=/tmp/zlib-1.2.8
This article was last updatated the 12/3/16