Nginx is among the most popular webservers in the world. It is considered more resource-friendly than its direct competitor Apache and it is fairly easy to setup (tutorial). It can be setup as a reverse-proxy in front of Apache as well, which is a very powerful setup that allows you to use all of the features and power of Apache, while benefitting from the speed of Nginx. Here, NGINX will be configured as a single, stand-alone server.
The basic idea of configuring NGINX is that you write server-blocks in a defined location on the server. These text-blocks are automatically included in the main NGINX-configuration file. Server blocks have different configurations for plain html, php-supported html, WordPress websites or Python-Django driven websites.
Requirements
After completing the basic configuration NGINX should already be running. To check the status do:
$ sudo systemctl status nginx
Nginx server blocks
Server blocks are small text-snippets that are included in the main nginx configuration-file. The server block text-files are located at etc/nginx/sites-available/.
You can create a new file in this location with:
$ sudo nano webdomain.com
Plain HTML
The most basic setup looks like:
server { listen 80; root /home/testuser/public_html; index index.html; server_name website.com www.website.com; location / { try_files $uri $uri/ =404; } }
root refers to the location on the server.
index refers to the file that will be opened when the domainname is called.
server_name refers to the name of the webdomain according to the DNS A-records. Make sure to make a record with and without the www-prefix, if you want to secure http with Certbot.
After saving the text-file in the subdirectory /etc/nginx/sites-available/, the server block will be enabled by creating a symbolic link in the subdirectory /etc/nginx/sites-enabled/. Next, you can test the nginx configuration file for syntax errors and reload the file.
$ sudo ln -s /etc/nginx/sites-available/mydomain.com /etc/nginx/sites-enabled/ $ sudo nginx -t $ sudo systemctl reload nginx
You should now have an active website that connects the webdomain to the root location of your webcontent on the server. http://webdomain.com should display your index.html content. Return to the main page for next steps.
HTML with php
To use php, a fastcgi-php.conf snippet is included in the NGINX-config file and the request is refered to /var/run/php/php7.2-fpm.sock. At the index directive, index.php is added as a file to be opened when the domainname is called.
As a security measure, the location directive /etc/nginx/sites-available/~ /\.ht {deny all;} directive is added, to prohibt access to any .htacces content if present (when combined with Apache).
server { listen 80; root /home/testuser/public_html; index index.html index.php; server_name website.com www.website.com; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; } location ~ /\.ht { deny all; } }
The configuration will take effect after creating a symbolic link in the sites-enabled directory, testing the configuration and reloading the nginx configuration file.
$ sudo ln -s /etc/nginx/sites-available/mydomain.com /etc/nginx/sites-enabled/ $ sudo nginx -t $ sudo systemctl reload nginx
You should now have an active website that connects the webdomain to the root location of your webcontent on the server. http://webdomain.com should display your index.php content. Return to the main page for next steps.
Some basic NGINX commands
Nginx is configured by default, to start automatically when the server boots. To disable and reenable nginx startup at server boot use:
$ sudo systemctl disable nginx $ sudo systemctl enable nginx
To stop, start, stop and then start NGINX again use:
$ sudo systemctl stop nginx $ sudo systemctl start nginx $ sudo systemctl restart nginx
To reload the NGINX configuration-file, after changes use:
$ sudo systemctl reload nginx