Nginx Commands You Should Know

Nginx Commands You Should Know

In this guide, we will go over the most important and frequently used Nginx commands, including starting, stopping, and restarting Nginx.

Nginx pronounced “engine x” is a free, open-source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet. It can be used as a standalone web server, and as a reverse proxy for Apache and other web servers.

If you are a developer or system administrator, chances are that you’re dealing with Nginx on a regular basis.

 

Before You Begin

We’re assuming that you are logged in as root or user with sudo privileges. The commands in uide this gshould work on any modern Linux distribution like Ubuntu 18.04 and CentOS 8 and Debian 10 .

 

Starting Nginx

Starting Nginx is pretty simple. Just run the following command:

sudo systemctl start nginx

On success, the command doesn’t produce any output.

If you are running a Linux distribution without systemd to start Nginx type:

sudo service nginx start

Instead of manually starting the Nginx service, it is recommended to set it to start on system boot:

sudo systemctl enable nginx

 

Stopping Nginx

Stopping Nginx quickly shuts down all Nginx worker processes even if there are open connections.

To stop Nginx, run one of the following commands:

sudo systemctl stop nginxsudo service nginx stop 

 

Restarting Nginx

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

Use one of the following commands to perform an Nginx restart :

sudo systemctl restart nginxsudo service nginx restart 

This is the command that you will probably use the most frequently.

 

Reloading Nginx

You need to reload or restart Nginx whenever you make changes to its configuration.

The reload command loads the new configuration, starts new worker processes with the new configuration, and gracefully shuts down old worker processes.

To reload Nginx, use one of the following commands:

sudo systemctl reload nginxsudo service nginx reload 

 

Testing Nginx Configuration

Whenever you make changes to the Nginx server’s configuration file, it is a good idea to test the configuration before restarting or reloading the service.

Use the following command to test the Nginx configuration for any syntax or system errors:

sudo nginx -t

The output will look like below:

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

If there are any errors, the command prints a detailed message.

 

Viewing Nginx Status

To check the status of the Nginx service, use the following command:

sudo systemctl status nginx

The output will look something like this:

● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-05-15 16:51:44 EEST; 1min 34s ago
  Process: 31181 ExecReload=/usr/sbin/nginx -s reload (code=exited, status=0/SUCCESS)
  Process: 31162 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 31159 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 31157 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 31164 (nginx)
   CGroup: /system.slice/nginx.service
           ├─31164 nginx: master process /usr/sbin/nginx
           ├─31182 nginx: worker process
           ├─31183 nginx: worker process
           ├─31184 nginx: worker process
           ├─31185 nginx: worker process
           ├─31186 nginx: worker process
           ├─31187 nginx: worker process
           ├─31188 nginx: worker process
           └─31189 nginx: worker process
...

 

Checking Nginx Version

Sometimes you may need to know the version of your Nginx so you can debug an issue or determine whether a certain feature is available.

You can check your Nginx version by running:

sudo nginx -v
nginx version: nginx/1.20.1 (CentOs7)

The -V option displays the Nginx version along with the configure option.

sudo nginx -V

 

Conclusion

In this guide, we have shown you some of the most essential Nginx commands. If you want to learn more about the Nginx command line options, visit the Nginx documentation .