NGINX Installation on Debian
I like lighttpd, is easy in resources and to configure; but there are other players like NGINX which is a great alternative with tons of resources available on the internet and a great community and documentation.
This is the first of 3 posts where I will be:
- Installing, Configuring and Securing NGINX (this)
- Compiling, Installing and Configuring the GeoIP2 module
- Installing and Configuring CertBot
Let’s start with some clarifications, I installed this as a test and to have great performance, the main installation was as a proxy for my python projects, and a few sites again just for test. Also, I have to spend a few weeks tweaking and hardening the configuration to make it robust for production, to get A+ on the test from SSLLabs at https://www.ssllabs.com/ssltest/.
You can also check the NGINX tool from Digital Ocean at https://www.digitalocean.com/community/tools/nginx, this tool will basically achieve a big part of the configuration and leave you a stable system to build on top.
Installing NGINX:
We will install the latest stable distribution from the NGINX repositories for Debian, this will give you a lean system and you will need to create some helpers like directories for the modules, sites and database.
If you just need what comes with Debian, trigger the installation with sudo apt update
then sudo apt install nginx
.
If like me, you want the latest Stable version from the NGINX repository, then the first thing is to add the NGINX official repository to apt
, for that let’s create a file nginx.list
in /etc/apt/sources.list.d
as:
|
|
Add the following content:
|
|
<CODENAME>
should be your Debian distribution, e.g. buster (or whatever you get from lsb_release -cs
) then save the file.
!!! NOTE: There are 2 branches available in the repository, one is the Mainline (Includes the latest features, it is reliable, but it may include some bugs) and Stable (Doesn’t include all of the latest features, but has critical bug fixes and is recommend for production servers). To access the Mainline change the url to https://nginx.org/packages/mainline/debian/.
To add the keys for the repositories:
|
|
or
|
|
After that, update apt database with:
|
|
Now you are ready to install the latest version, issue the following command:
|
|
You can check the available versions with apt show nginx
.
To prioritise the mantainer repository over the distribution one, yuo need to create the preference file in /etc/apt/preferences.d/
with the name 99nginx
for example, and add the following content:
|
|
By now you have installed the latest stable version of NGINX and it should be a lean installation. You can visit the server’s ip address using your browser to check that. It will not have any of the directories that have the Debian package distribution, only the following:
|
|
Some useful commands
If you want to check if NGINX is running, use:
|
|
To start the service you can use:
|
|
To stop NGINX use:
|
|
Configuring NGINX:
Initially, we will start with the configuration generated by the Digital Ocean Tool for NGINX, then we will tweak it. But first, we need to create some files and directories and get some values from the system.
As we installed NGINX from the project’s repositories, we only have a basic configuration to render a basic page, which you can see if the NINGX is running and you visit the server address. Also, we don’t have the directories to arrange the files, so let’s start with that.
This installation should have a modules directory where will be all modules for the installation, but let’s create a directory for the modules that will be enabled:
|
|
Now let’s create folders for the site’s configurations following the same pattern (available and enabled)
|
|
And finally a folder for snippets of configuration that will be later used by CertBot (letsencrypt), General directives and Security directives.
|
|
The main configuration file for NGINX is nginx.conf
located in /etc/nginx
, open your favourite editor and copy what you got from the Digital Ocean Tool for NGINX for the nginx.conf
file, it should be similar to the following:
|
|
Now let’s tweak what we have:
- For
worker_processes
you can leaveauto
or use the output ofgrep processor /proc/cpuinfo | wc -l
- For
worker_connections
you should use the output ofulimit -n
. and finally generate the Diffie-Hellman keys withopenssl dhparam -out /etc/nginx/dhparam.pem 4096
. !!! If you are using a fresh installation you probably will need to install openssl and its dependencies.
The first server-block default
The first server-block configured is the default and should be in the file default under /etc/nginx/conf.d
, we need to move it from the to the sites-available
directory and then create a symbolic link to it from sites-enabled
, let’s do that:
|
|
The default.conf
file should look like:
|
|
Checking the configuration
To check that all config and the loaded modules all is correct, you can issue the following command:
|
|
If there is any error or missing reference to a file, you will get an error meessage.
Checking NGINX build details
Sometime to add a module, you need to check if the installed version of NGINX actually support or has been compiled with support for the module you want to add, for that you can use the command:
|
|
The output should be similar to this:
|
|
In this output, you can see that the module http_realip_module
has been included as a static module, so you just need to include the configuration snipped in the config file and it will work.
Securing NGINX:
For this task we will rely on ufw
for simplicity of management of the iptables
.
To install let’s issue the following command:
|
|
Now, let’s configure the basic rules to allow incoming and outgoing communication in both HTTP and HTTPS:
|
|
The firewall is ready, if you are accessing your Debian via ssh, add also sudo ufw allow SSH
.
sudo ufw allow 2222/tcp
)Once that is complete you can proceed to activate the firewall with:
|
|
G