10 Most Used Nginx Commands Every Linux User Must Know

[‘

n

Nginx (pronounced Engine x) is a free, open-source, high-performance, scalable, reliable, full-featured and popular HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server.

n

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.

n

Read Also: 3 Useful Hacks Every Linux User Must Know

n

In this guide, we will explain some of the most commonly used Nginx service management commands that, as a developer or system administrator, you should keep at your fingertips. We will show commands for both Systemd and SysVinit.

n

All of these following list of Nginx popular commands must be executed as a root or sudo user and should work on any modern Linux distribution such as CentOS, RHEL, Debian, Ubuntu and Fedora.

n

Install Nginx Server

n

To install Nginx web server, use your default distribution package manager as shown.

n

$ sudo yum install epel-release && yum install nginx   [On CentOS/RHEL]rn$ sudo dnf install nginx                               [On Fedora]rn$ sudo apt install nginx                               [On Debian/Ubuntu]rn

n

Check Nginx Version

n

To check the version of Nginx web server installed on your Linux system, run the following command.

n

$ nginx -vrnrnnginx version: nginx/1.12.2rn

n

The above command simply displays the version number. If you want to view version and configure options then use the -V flag as shown.

n

$ nginx -Vrn

n

Show Nginx, Compiler and Configuration Parameters

n

nginx version: nginx/1.12.2rnbuilt by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) rnbuilt with OpenSSL 1.0.2k-fips  26 Jan 2017rnTLS SNI support enabledrnconfigure arguments: --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'rn

n

Check Nginx Configuration Syntax

n

Before you actually start the Nginx service, you can check whether its configuration syntax is correct. This is especially useful if you have made changes or added a new configuration to the existing configuration structure.

n

To test the Nginx configuration, run the following command.

n

$ sudo nginx -trnrnnginx: the configuration file /etc/nginx/nginx.conf syntax is okrnnginx: configuration file /etc/nginx/nginx.conf test is successfulrn

n

You can test the Nginx configuration, dump it and exit using the -T flag as shown.

n

$ sudo nginx -Trn

n

Show Nginx Configuration Settings

n

nginx: the configuration file /etc/nginx/nginx.conf syntax is okrnnginx: configuration file /etc/nginx/nginx.conf test is successfulrn# configuration file /etc/nginx/nginx.conf:rn# For more information on configuration, see:rn#   * Official English Documentation: http://nginx.org/en/docs/rn#   * Official Russian Documentation: http://nginx.org/ru/docs/rnrnuser nginx;rnworker_processes auto;rnerror_log /var/log/nginx/error.log;rnpid /run/nginx.pid;rnrn# Load dynamic modules. See /usr/share/nginx/README.dynamic.rninclude /usr/share/nginx/modules/*.conf;rnrnevents {rn    worker_connections 1024;rn}rnrnhttp {rn    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" 'rn                      '$status $body_bytes_sent "$http_referer" 'rn                      '"$http_user_agent" "$http_x_forwarded_for"';rnrn    access_log  /var/log/nginx/access.log  main;rnrn    sendfile            on;rn    tcp_nopush          on;rn    tcp_nodelay         on;rn    keepalive_timeout   65;rn    types_hash_max_size 2048;rnrn    include             /etc/nginx/mime.types;rn    default_type        application/octet-stream;rnrn    # Load modular configuration files from the /etc/nginx/conf.d directory.rn    # See http://nginx.org/en/docs/ngx_core_module.html#includern    # for more information.rn    include /etc/nginx/conf.d/*.conf;rnrn    server {rn        listen       80 default_server;rn        listen       [::]:80 default_server;rn        server_name  _;rn        root         /usr/share/nginx/html;rnrn        # Load configuration files for the default server block.rn        include /etc/nginx/default.d/*.conf;rnrn        location / {rn        }rnrn        error_page 404 /404.html;rn            location = /40x.html {rn        }rnrn        error_page 500 502 503 504 /50x.html;rn            location = /50x.html {rn        }rn    }rnrn....rn

n

Start Nginx Service

n

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

n

$ sudo systemctl start nginx #systemdrnORrn$ sudo service nginx start   #sysvinitrn

n

Enable Nginx Service

n

The previous command only starts the service for the meantime, to enable it auto-start at boot time, run the following command.

n

$ sudo systemctl enable nginx #systemdrnORrn$ sudo service nginx enable   #sysv initrn

n

Restart Nginx Service

n

To restart the Nginx service, an action which will stop and then start the service.

n

$ sudo systemctl restart nginx #systemdrnORrn$ sudo service nginx restart   #sysv initrn

n

View Nginx Service Status

n

You can check the Nginx service’s status as follows. This command shows the run time status information about the service.

n

$ sudo systemctl status nginx #systemdrnORrn$ sudo service nginx status   #sysvinitrn

n

Show Nginx Status Information

n

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.rn[[emailxa0protected] ~]# systemctl status nginxrn● nginx.service - The nginx HTTP and reverse proxy serverrn   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)rn   Active: active (running) since Tue 2019-03-05 05:27:15 EST; 2min 59s agorn Main PID: 31515 (nginx)rn   CGroup: /system.slice/nginx.servicern           ├─31515 nginx: master process /usr/sbin/nginxrn           └─31516 nginx: worker processrnrnMar 05 05:27:15 tecmint.com systemd[1]: Starting The nginx HTTP and reverse proxy server...rnMar 05 05:27:15 tecmint.com nginx[31509]: nginx: the configuration file /etc/nginx/nginx.conf syntax is okrnMar 05 05:27:15 tecmint.com nginx[31509]: nginx: configuration file /etc/nginx/nginx.conf test is successfulrnMar 05 05:27:15 tecmint.com systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argumentrnMar 05 05:27:15 tecmint.com systemd[1]: Started The nginx HTTP and reverse proxy server.rn

n

Reload Nginx Service

n

To tell Nginx to reload its configuration, use the following command.

n

$ sudo systemctl reload nginx #systemdrnORrn$ sudo service nginx reload   #sysvinitrn

n

Stop Nginx Service

n

If you want to stop the Nginx service for once reason or the other, use the following command.

n

$ sudo systemctl stop nginx #systemdrnORrn$ sudo service nginx stop   #sysvinitrn

n

Show Nginx Command Help

n

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

n

$ systemctl -h nginxrn

n

Nginx Help Commands and Options

n

systemctl [OPTIONS...] {COMMAND} ...rnrnQuery or send control commands to the systemd manager.rnrn  -h --help           Show this helprn     --version        Show package versionrn     --system         Connect to system managerrn  -H --host=[[emailxa0protected]]HOSTrn                      Operate on remote hostrn  -M --machine=CONTAINERrn                      Operate on local containerrn  -t --type=TYPE      List units of a particular typern     --state=STATE    List units with particular LOAD or SUB or ACTIVE statern  -p --property=NAME  Show only properties by this namern  -a --all            Show all loaded units/properties, including dead/emptyrn                      ones. To list all units installed on the system, usern                      the 'list-unit-files' command instead.rn  -l --full           Don't ellipsize unit names on outputrn  -r --recursive      Show unit list of host and local containersrn     --reverse        Show reverse dependencies with 'list-dependencies'rn     --job-mode=MODE  Specify how to deal with already queued jobs, whenrn                      queueing a new jobrn     --show-types     When showing sockets, explicitly show their typern  -i --ignore-inhibitorsrn...rn

n

You might also like to read these following Nginx related articles.

n

    n

  1. The Ultimate Guide to Secure, Harden and Improve Performance of Nginx Web Server
  2. n

  3. Amplify – NGINX Monitoring Made Easy
  4. n

  5. ngxtop – Monitor Nginx Log Files in Real Time in Linux
  6. n

  7. How to Install Nginx with Virtual Hosts and SSL Certificate
  8. n

  9. How to Hide Nginx Server Version in Linux
  10. n

n

That’s all for now! In this guide, we have explained some of the most commonly used Nginx service management commands that you should know, including starting, enabling, restarting and stopping Nginx. If you have any additions or questions to ask, use the feedback form below.

n

‘]