Most Useful Nginx Commands in Linux System

Nginx is a free, high performance and open-source HTTP and reverse proxy server. It can be used as a standalone web server, and as a reverse proxy for Apache and other web servers. It is a more flexible and lightweight program than Apache HTTP Server that's why it powers some of the largest sites on the Internet. Nginx can handle the bigger amount of connection than Apache and consuming smaller memory. 

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Nginx queries.

In this context, we shall look into the most used Nginx commands.


How to Install Nginx Server on Linux ?

Before performing this Installation procedure, ensure that you are using sudo enabled user account with any Linux distro such as Ubuntu and CentOS and Debian.

To begin, if you want to install Nginx then following commands for different distro:

$ sudo yum install epel-release && yum install nginx   #For CentOS/RHEL
$ sudo dnf install nginx                               #For Debian/Ubuntu
$ sudo apt install nginx                               #For Fedora


How to Check Nginx Version ?

You can check installed version of Nginx on your system using below command:

$ nginx -v

It will show output as below:

Output
nginx version: nginx/1.14.0 (Ubuntu)

Above command will only show version number. If you want to show version and configuration details then run below command:

$ nginx -V
Output
nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.0g  2 Nov 2017 (running with OpenSSL 1.1.1b  26 Feb 2019)
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-FIJPpj/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module


How to Check Nginx Configuration Syntax ?

It's best practice to check configuration syntax of nginx when you have made changes in configuration file. Run the below command to check:

$ sudo nginx -t

It will display output as below:

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

 

How to Start Nginx ?

It's a very simple to start nginx service. Use the below command to start it:

$ sudo systemctl start nginx

If you Nginx configuration is correct then it will start and will not display any output. If your Linux distribution have not systemd then you should use below command to start service:

$ sudo service start nginx


How to Enable Nginx Service ?

Nginx start command will start the service only for a while. If you don’t want to start service manually each time then you can enable auto-start at boot time using below command:

$ sudo systemctl enable nginx

Same as above command, if your Linux distribution have not systemd then you should use below command:

$ sudo service enable nginx


How to Stop Nginx ?

If you have requirement to stop Nginx service for any reason you can run below command:

$ sudo systemctl stop nginx

For non systemd system execute below command:

$ sudo service nginx stop


How to Restart Nginx ?

The restart option is a quick way of stopping and then starting the Nginx server.

Following is command to perform an Nginx restart for systemd and non systemd accordingly:

$ sudo systemctl restart nginx
$ sudo service restart nginx

This command is most frequently used for Nginx service.


How to Reload Nginx ?

When you have made any changes to Nginx configuration files then you need to reload Nginx service. It will load new configuration and start new process and stop all old processes.

To reload Nginx, use one of the following commands:

$ sudo systemctl reload nginx

Run below command if not systemd available on system:

$ sudo service reload nginx


How to View Nginx Service Status ?

You can check the status of Nginx service using status command. It will show the run time status information about the service.

Use below command to check status:

$ sudo systemctl status nginx      #systemd
Output
● 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 Mon 2019-04-29 04:57:52 UTC; 1 weeks 0 days ago
      Docs: man:nginx(8)
  Main PID: 9561 (nginx)
     Tasks: 2 (limit: 1152)
    CGroup: /system.slice/nginx.service
            ├─9561 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
            └─9564 nginx: worker process
 Apr 29 04:57:51
ibmimedia.com systemd[1]: Stopped A high performance web server and a reverse proxy server.
 Apr 29 04:57:51 ibmimedia.com systemd[1]: Starting A high performance web server and a reverse proxy server…
 Apr 29 04:57:52 ibmimedia.com systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
 Apr 29 04:57:52 ibmimedia.com systemd[1]: Started A high performance web server and a reverse proxy server.


[Need assistance in fixing Nginx configuration errors? We can help you. ]

This article covers most used Nginx commands. You can learn more about Nginx command line at Nginx documentation.

Nginx is one of the most popular web servers in the world. So whether you're currently using it or not, chances are, if you're a web developer chances are you'll likely come in contact with it at some point. 

Also, Nginx is well known for its simple configuration, and low resource consumption due to its high performance, it is being used to power several high-traffic sites on the web, such as GitHub, SoundCloud, Dropbox, Netflix, WordPress and many others.


To start the Nginx service, run the following command. Note that this process may fail if the configuration syntax is not OK:

$ sudo systemctl start nginx #systemd

OR

$ sudo service nginx start   #sysvinit


To enable Nginx auto-start at boot time, run the following command:

$ sudo systemctl enable nginx #systemd

OR

$ sudo service nginx enable   #sysv init


How to Show Nginx Command Help ?

To get an easy reference guide of all Nginx commands and options, use following command.

$ systemctl -h nginx

Related Posts