ScaleScaleScaleScale

Tips / Nginx


8 Essential Nginx Commands

If you are just beginning with Nginx, there are a few things that you may love to know while working with this fantastic web server. You will certainly benefit from this review of some of the basic manipulation commands to start, reload, stop, restart or check the health of your Nginx configuration.

First, you need to learn the basics of installing Nginx.  As most web servers run on Ubuntu/Debian and Centos/RHEL, you can learn from the following examples for these popular Linux distributions.

Install Nginx

On Debian/Ubuntu, you can install Nginx easily with the following command:

apt-get install nginx

On CentOS and RHEL, it’s an easy process too:

yum install nginx

or

dnf install nginx

Note: on CentOS/RHEL you must install Nginx repo, first.

1. Start Nginx

Starting Nginx is really simple if you are using init.d scripts:

service nginx start

2. Stop Nginx

This will kill all Nginx processes:

service nginx stop

On busy servers, this can take a few seconds. If you want a more aggressive kill, you can also try:

killall -9 nginx

3. Restart Nginx

The restart command does a stop and then start in one single line:

service nginx restart

Output example:

[root@server.nginxtips.com:~]service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]

4. Reload Nginx

service nginx reload

Output example:

[root@server.nginxtips.com:~]service nginx reload
Reloading nginx:                                           [  OK  ]

5. Check Nginx Configuration

This is a really useful command when you are editing your Nginx configuration files. If there is an error in your Nginx configuration, this command will let you know the exact line where the error is located:

nginx -t

or

service nginx configtest

Both will show you something like this:

[root@server.nginxtips.com:~]nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

6. Check if Nginx is Running

service nginx status
[root@server.nginxtips.com:~]service nginx status
nginx (pid  12249) is running...

Another way to check the status is:

ps -aux | grep nginx

Output example:

[root@server.nginxtips.com:~]ps -aux | grep nginx
root     12249  0.0  0.0  58456   612 ?        Ss   Sep24   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx    12251  0.0  0.0  60332  4368 ?        S    Sep24   1:58 nginx: worker process                   
nginx    12252  0.0  0.0  60500  4344 ?        S    Sep24   1:46 nginx: worker process                   
nginx    12253  0.0  0.0  61124  3960 ?        S    Sep24   1:35 nginx: worker process                   
nginx    12254  0.0  0.0  60728  4644 ?        S    Sep24   1:48 nginx: worker process                   
root     29521  0.0  0.0 103312   880 pts/0    S+   08:57   0:00 grep nginx

With ‘ps -aux | grep nginx’, you can see which Nginx processes are running right now, along with some other information (like name of the user who runs the process, CPU and memory usage, etc.)

7. Upgrade Nginx

This is the best way to restart Nginx after running an upgrade (yum upgrade nginx, for example):

service nginx upgrade

This upgrade command will start Nginx with the new upgraded binaries along with the old Nginx version. It will also move the listening sockets to the new binary files and then shut down the old binaries. This will result in a zero downtime upgrade.

8. Check Nginx Version & Compiled Modules

nginx -V

This will output the Nginx version, along with all the compiled modules. Output example:

[root@server.nginxtips.com:~]nginx -V
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --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-http_ssl_module --with-http_realip_module --with-http_addition_module --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_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

These are just the basic web server interaction commands for all those who are just starting with Nginx. It will surely help you get started on your own Nginx journey.

Popular search terms:

  • nginx commands
  • check nginx status
  • restart nginx
  • how to start nginx
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Thank you!

  • DR01D

    Right into my favorites!