CheatSheet for Nginx Commands

The CheatSheet is one of the quick reference guides, allowing Linux administrator or developer to easily find the right commands whenever needed.

This Nginx cheatsheet contains information about Nginx service commands, log file location, configuration file location, and syntax test commands.

What’s Nginx?

Nginx (pronounced “engine X”). NGINX is a free, open-source, high-performance HTTP web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache.

NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

NGNX is used by millions of websites, including major companies such as Airbnb, Box, Dropbox, Netflix, Tumbler and WordPress.

How to Install Nginx on Linux

Nginx can be easily installed on Linux systems from the distribution official repository.

If you are using Enterprise Linux like Red Hat, CentOS, you need to enable the EPEL repository to install Nginx.

For RHEL/CentOS 6 systems, run the below command

$ sudo yum install -y nginx

For RHEL/CentOS 7 systems, use the yum command to install Nginx.

$ sudo yum install -y nginx

For RHEL/CentOS 8 and Fedora systems, use the dnf command to install Nginx.

$ sudo dnf install -y nginx

For Debian based systems, use the apt command or apt-get command to install Nginx.

$ sudo apt install nginx
or
$ sudo apt-get install nginx

For openSUSE systems, use the zypper command to install Nginx.

$ sudo zypper install -y nginx

For Arch Linux systems, use the pacman command to install Nginx.

$ sudo pacman -S nginx

How to Check Nginx Version on Linux

Run the command below to check the version of Nginx installed on your Linux system. You can get a different version in the output, depending on your distribution and installation method.

$ nginx -v

nginx version: nginx/1.18.0

Run the below command to verify the installed Nginx version, compiler version and configured parameters.

$ nginx -V
or
$ 2>&1 nginx -V | tr -- - '\n' | grep _module

nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

How to Find Syntax Errors in the Nginx Configuration File

You may need to check for syntax errors in the Nginx configuration file when you make changes. This will help prevent unexpected errors. Also, it prevents your website from going down.

$ sudo nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If you want to check for a specific nginx configuration file instead of the default, use the following command.

$ sudo nginx -t -c /path/to/the/file

How to Start Nginx Service on Linux

Use the below commands to start Nginx server in Linux.

For SysVinit systems.

# service nginx start
or
# /etc/init.d/nginx start

For systemd systems.

# systemctl start nginx.service
or
# systemctl start nginx

How to Stop Nginx Service on Linux

Use the below commands to stop Nginx server in Linux.

For SysVinit systems.

# service nginx stop
or
# /etc/init.d/nginx stop

For systemd systems.

# systemctl stop nginx.service
or
# systemctl stop nginx

How to Restart Nginx Service on Linux

Use the below commands to restart Nginx server in Linux.

For SysVinit systems.

# service nginx restart 
or
# /etc/init.d/nginx restart 

For systemd systems.

# systemctl restart nginx.service
or
# systemctl restart nginx

How to Reload Nginx Service on Linux

Use the following command to reload the Nginx server on Linux. Changes made to the configuration file will not be used until you run “reload” or “restart”.

For SysVinit systems.

# service nginx reload 
or
# /etc/init.d/nginx reload 

For systemd systems.

# systemctl reload nginx.service
or
# systemctl reload nginx

How to Status Nginx Service on Linux

Use the below commands to status Nginx server in Linux.

For SysVinit systems.

# service nginx status 
or
# /etc/init.d/nginx status 

For systemd systems.

# systemctl status nginx.service
or
# systemctl status nginx

● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-04-25 00:41:28 UTC; 1 weeks 0 days ago
     Docs: http://nginx.org/en/docs/
 Main PID: 12338 (nginx)
   CGroup: /system.slice/nginx.service
           ├─  570 nginx: worker process
           ├─ 2327 nginx: worker process
           ├─12338 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─12341 nginx: cache manager process

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

How to Enable Nginx Service On Boot in Linux

Use the below commands to enable Nginx service on boot in Linux.

For SysVinit systems.

# chkconfig nginx on

For systemd systems.

# systemctl enable nginx.service
or
# systemctl enable nginx

Important Nginx configuration files for Red Hat based systems.

+------------------------------------------+-----------------------------------------------+
|   File Path                              |        Description                            |
+------------------------------------------+-----------------------------------------------+
| /etc/nginx/nginx.conf                    |  Global config file                           |
| /usr/share/nginx/html                    |  Default document root directory              |
| /etc/nginx/conf.d/default                |  Sample config file for virtual host          |
| /usr/share/nginx/[Site_Name]             |  Create a separate directory for each domains |
| /etc/nginx/conf.d/[Site_Name.com.conf]   |  Store config file for other virtual hosts    |
+------------------------------------------+-----------------------------------------------+

Important Nginx Configuration files for Debian/Ubuntu based systems.

+---------------------------------------------------+-----------------------------------------------+
|   File Path                                       |        Description                            |
+---------------------------------------------------+-----------------------------------------------+
| /etc/nginx/nginx.conf                             |  Global config file                           |
| /var/www/html                                     |  Default document root directory              |
| /etc/nginx/sites-available/default                |  Sample config file for virtual host          |
| /var/www/[Site_Name]                              |  Create a separate directory for each domains |
| /etc/nginx/sites-available/[Site_Name.com.conf]   |  Store config file for other virtual hosts    |
| /etc/nginx/sites-enabled/[Site_Name.com.conf]     |  Activated config files can be found          |
+---------------------------------------------------+-----------------------------------------------+

You need to create a symlink or symbolic link to enable a new site for Debian-based systems whereas you need to remove the symlink to disable it.

$ sudo ln -s /etc/nginx/sites-available/xyz.com /etc/nginx/sites-enabled/xyz.com

Log files can be found in the following location. If you’re having trouble viewing logs, use the log Viewer application for better viewing.

/var/log/nginx/access.log
/var/log/nginx/error.log

You can control the nginx daemon by sending the signal to an nginx master process with the help of the “-s” option.

$ sudo nginx -s stop     #Fast shutdown
$ sudo nginx -s quit     #Graceful shutdown
$ sudo nginx -s reload   #To reloading the configuration file
$ sudo nginx -s reopen   #To reopening the log files

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...