Follow @devcrafter91

How to install Nginx on Debian 9 (Stretch)

Fri Apr 12 2019 nginx debian sysadmin linux


Nginx is an open source web server, a reverse proxy or a load balancer and it's responsible for serving the highest traffic sites like Cloudflare, MaxCDN, Wordpress. Recently Nginx got acquired by F5 for $670M dollars (source).

The following article was written and tested on a standard Linux Debian 9 Stretch box and the current mainline version of Nginx is 1.15.10.

Step 1 - Installation (Debian 9)

Install required dependencies first.

apt-get install -y curl gnupg2 ca-certificates

Add the official mainline repository from Nginx

echo "deb stretch nginx" | \
tee /etc/apt/sources.list.d/nginx.list && \
curl -fsSL |  apt-key add -

Add the keys and update reps.

apt-key fingerprint ABF5BD827BD9BF62 && \
apt-get update

Install the latest mainline Nginx version.

apt-get install -y nginx

Step 2 - Configure

Depending on the version or the repository sometimes configuration may be different and installed on different paths. Run nginx -V to grab the required paths needed for configuring Nginx. Example output:

configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf

Now based on the previous output run cat /etc/nginx/nginx.conf to check out where directives are located and which are loaded.

Example output:

user  nginx;
worker_processes  1;

http {
    include /etc/nginx/conf.d/*.conf;

Every valid directive/block file with the extension .conf located at /etc/nginx/conf.d/ will be loaded every time Nginx start. THe master configuration is located at /etc/nginx/nginx.conf and can be useful for pre-default settings that will take effect on every directive. Adding directives can be done easily just by creating .conf files, restart the server using systemctl restart nginx.service. Always run nginx -t before restarting Nginx to make sure the configuration is valid.

Example: /etc/nginx/conf.d/my-test.conf

server {
  listen 1337;
  location / {
    add_header content-type text/plain;
    return 200 "hello world";