How To Create Nginx Server Block on Debian/Ubuntu Linux

DigitalOcean Referral Badge
Start your VPS now with FREE $100 credit.

NGINX is a free and open-source high-performance HTTP web server, reverse proxy, HTTP cache, and load balancer. Nginx is also known or called as “Engine-X”, and in another language it’s pronounced of “EN-jin-EKS” or “ɛndʒɪnˈɛks”. The firt version of Nginx have been released to the public by Igor Sysoev in 2004, and beat the Apache web server in many aspect, especially in terms of performance, scalability and its reliability. It’s free and open-source software which released under 2-clause BSD license, so you can use, modify and distribute it freely without at any costs.

Currently Nginx is backed by big company “F5 Networks” which previously was acquired for $670 million In March 2019. The company give paid support and offer NGINX Plus as their paid software product. Last year Netcraft predicted that Nginx is ranked second and served 23.21% of the million websites behind the Apache web server which served 24.63% active website on the world.

Well, if you have currently have running virtual or phisycal server with NGINX installed inside your machine, but you don’t know what should you do with your NGINX installation. Kindly follow this guide, so we can teach you on how to create and setting up Nginx server block on Debian/Ubuntu-based distros.

PREREQUISITES:

* Virtual / Physical Servers, VPS or Cloud
* OS Distros with Debian/Ubuntu Linux
* Superuser Privileges (Root Access)
* Familiar with CLI-Based or Command-Line Only
* Gnome Terminal for Linux Desktop
* PuTTy SSH Client for Windows or Mac
* Powershell for Windows 10/11

1.) Create Document Roots or Public Folder for Your Website

READ:  What is Debian Software Package (.deb files) on Debian/Ubuntu Linux

It’s similar to the public_html directory on cPanel-WHM, but on NGINX (CLI-Only / No-GUI) you can create this public directory as you like, unlike on cPanel which is restricted.

~# mkdir -p /var/www/domainname.com/public/ /var/www/domainname.com/logs/

~# cd /var/www/domainname.com/public/

2.) Create Nginx ServerBlock (On Apache it’s Called Virtual Hosts)

On Apache web server you may know Virtual Hosts which became best practice on the web hosting industry to running more than one website on single machine with one or more than single IPv4 addresses, for example domain1.com, domain2.com, domain3.com, and more. This method allows single virtual or physical machine to share the same server resources like memory (RAM), and CPU (processor) allocations.

Virtual hosts or vhosts can run with “IP-based” method, which mean you can have different IP address for each website. It’s also called “name-based” Apache vhosts which allow the machine to served multiple domain name on each IP address. End users will not know about that, while each domain name with different IPv4 address running on the same virtual or physical server. In addition to support IP-based virtual hosts, the latter version of Apache web server also support for name-based, host-based or non-IP vhosts.

READ:  How To Install OpenResty v1.21.4.1 on CentOS Stream 8

However, virtual hosts or vhosts is still Apache methods to served their users website on the web hosting industry. You would never found it on NGINX. In fact, NGINX not using or utilize virtual host at all, altough they support reverse proxy in the front of the Apache web server. There is only “Server Blocks” which use “server_name” and listen directives to bind to the TCP sockets.

For example, you may know server_name directives on default config of NGINX like “server_name domainname.com www.domainname.com”, while the listen directives it’s tells NGINX to listen on both TCP port 80 or 443. The latter one is for SSL or HTTPS connections.

===============================================================

~# nano /etc/nginx/sites-available/domainname.com
===============================================================

# Virtual Host Configuration (Please don't alter these files manually)
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6

root /var/www/domainname.com/public/;
index index.htm index.html index.php;

# Make site accessible from public
server_name domainname.com www.domainname.com;

#access_log /var/log/nginx/domainname.com.access.log;
error_log /var/log/nginx/domainname.com.error.log;
access_log off;

location / {
try_files $uri $uri/ /index.php?$args;
}

# Fastcgi-Cache Config
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000; ## you should adjust it as well on php-fpm-pool config or pool configuration files.
include fastcgi_params;
include fastcgi.conf;
fastcgi_param HTTP_PROXY "";
fastcgi_index index.php;
# fastcgi_intercept_errors on;
}

# Static Cache Config
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|css|js|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires 360d;
add_header Access-Control-Allow-Origin "*";
}

location = /robots.txt { access_log off; log_not_found off; }
location ~ /\. { deny all; access_log off; log_not_found off; }

}

===============================================================

3.) Create Symbolic Link (Symlink) to Sites Enabled and Logs Directory

READ:  How To Install NGINX v1.22.0 Web Server on CentOS Stream 9

After that you can create symbolic link or symlink to the NGINX sites enabled and its log directories.

~# ln -s /etc/nginx/sites-available/domainname.com /etc/nginx/sites-enabled/

~# ln -s /var/log/nginx/domainname.com.access.log /var/www/domainname.com/logs/access.log

~# ln -s /var/log/nginx/domainname.com.error.log /var/www/domainname.com/logs/error.log
===============================================================

4.) Install Web Apps, eg WordPress

Later, you have active and running Server Block under Nginx web server, then you can continue to setting up web apps or CMS like WordPress or maybe Joomla, Drupal.

~# cd /var/www/domainname.com/public/
~# wget https://wordpress.org/latest.tar.gz
~# tar --strip-components=1 -xvf latest.tar.gz
~# chown -R www-data:www-data /var/www/domainname.com/

Finished, from this step you have completed on create and setting up Nginx server block on your virtual, cloud or phisycal servers. Then you can move to the next step to create database name, database user and its password.

Leave a Comment