Cyberithub

Top 12 Nginx Commands Every Linux Admin Should Know

In this article, I will take you through top 10 Nginx commands which is important for every Linux Admin using Nginx Server. As per Nginx Wiki, NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

NGINX is one of a handful of servers written to address the C10K problem. Unlike traditional servers, NGINX doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load. Even if you don’t expect to handle thousands of simultaneous requests, you can still benefit from NGINX’s high-performance and small memory footprint. NGINX scales in all directions: from the smallest VPS all the way up to large clusters of servers.

What is Nginx

Nginx is an Open Source web server and uses a non-threaded, event driven architecture.

Top 12 Nginx Commands Every Linux Admin Should Know 1

Nginx Commands

It is worth mentioning here that I have created and used a user test in this article which is having sudo access to run all below mentioned commands. You can refer to Add User to Sudoers on Ubuntu article to check how to add user into sudoers file. You can also create any user and provide sudo access to run all the below nginx commands as per your requirement.

Also Read: How to Update or Upgrade CentOS 

1. Install Nginx on Ubuntu

To install Nginx on Ubuntu, you need to use sudo apt-get install nginx command. This command will check the availability of Nginx package in the repository and then download and install it.

test@localhost:~$ sudo apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done

..........................................................................................

2. Check Nginx Version

To check nginx version, you need to use sudo nginx -v command

test@localhost:~$ sudo nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

3. Test Nginx default Config

To test Nginx default config, use sudo nginx -t or sudo service nginx configtest nginx commands. It is very important to test your configuration before starting or restarting nginx test config to avoid any unnecessary error.

test@localhost:~$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

OR

test@localhost:~$ sudo service nginx configtest
* Testing nginx configuration [ OK ]

4. Start Nginx Service

To start Nginx Service, you need to use sudo systemctl start nginx or sudo service nginx start command.

test@localhost:~$ sudo systemctl start nginx

OR

test@localhost:~$ sudo service nginx start

5. Check Nginx Status

To check nginx status after doing any changes, you need to use sudo systemctl status nginx or sudo service nginx status command.

test@localhost:~$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-01-04 04:29:30 IST; 1 day 4h ago
Docs: man:nginx(8)
Main PID: 4642 (nginx)
Tasks: 2 (limit: 2304)
CGroup: /system.slice/nginx.service
├─4642 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─8163 nginx: worker process

Jan 04 04:29:30 localhost systemd[1]: Starting A high performance web server and a reverse proxy server...
Jan 04 04:29:30 localhost systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jan 04 04:29:30 localhost systemd[1]: Started A high performance web server and a reverse proxy server.
Jan 04 04:30:05 localhost systemd[1]: Reloading A high performance web server and a reverse proxy server.
Jan 04 04:30:05 localhost systemd[1]: Reloaded A high performance web server and a reverse proxy server.
Jan 05 07:42:40 localhost systemd[1]: Reloading A high performance web server and a reverse proxy server.
Jan 05 07:42:40 localhost systemd[1]: Reloaded A high performance web server and a reverse proxy server.

OR

test@localhost:~$ sudo service nginx status
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2020-01-05 10:00:36 IST; 44s ago
Docs: man:nginx(8)
Process: 32673 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 308 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 307 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 309 (nginx)
Tasks: 2 (limit: 2304)
CGroup: /system.slice/nginx.service
├─309 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─310 nginx: worker process

Jan 05 10:00:36 localhost systemd[1]: Starting A high performance web server and a reverse proxy server...
Jan 05 10:00:36 localhost systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jan 05 10:00:36 localhost systemd[1]: Started A high performance web server and a reverse proxy server.

6. Stop Nginx Service

To stop nginx service, you need to use sudo systemctl stop nginx or sudo service nginx stop command.

test@localhost:~$ sudo systemctl stop nginx

OR

test@localhost:~$ sudo service nginx stop

NOTE:

If stopping nginx services does not kill nginx processes, then you need to manually kill the processes using killall nginx command or if you want to kill all the processes forcefully, then you need to use killall -9 nginx command. If you want to kill the processes forcefully by process Id, then you need to use kill -9 <PID> command.

7. How to Restart Nginx Service

To restart nginx service, you need to use sudo systemctl restart nginx command or sudo service restart nginx.

test@localhost:~$ sudo systemctl restart nginx
test@localhost:~$ sudo service nginx restart

8. How to Perform Nginx Test Reload

To reload nginx service, you need to use sudo systemctl reload nginx command or sudo service reload nginx command.

test@localhost:~$ sudo systemctl reload nginx
test@localhost:~$ sudo service nginx reload

9. Check Nginx Configuration

You can check nginx configuration using sudo nginx -T command. This nginx commands will show all the current configuration in detail and exit out.

test@localhost:~$ sudo nginx -T
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream

..............................................................................................................

10. Log Rotate Nginx Files

If you want to log rotate Nginx files, you can use sudo service nginx rotate command.

test@localhost:~$ sudo service nginx rotate
* Re-opening nginx log files nginx [ OK ]

11. Upgrade Nginx 

To upgrade nginx service, you need to use sudo service nginx upgrade command.

test@localhost:~$ sudo service nginx upgrade
* Upgrading binary nginx [ OK ]

12. Show Nginx Help

If you want to see all the supported options with nginx commands, you need to use sudo nginx -h command.

test@localhost:~$ sudo nginx -h
nginx version: nginx/1.14.0 (Ubuntu)
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/share/nginx/)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file

 

Popular Searches

  • Nginx Commands
  • Restart Nginx
  • Nginx Config
  • What is Nginx
  • Nginx Tutorial
  • How to restart Nginx
  • Nginx Config File

Also Read: How to get complete dependencies List of RPM

Leave a Comment