Nginx Commands You Should Know
3 min read
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.
In this guide, we will go over the most important and frequently used Nginx commands, including starting, stopping, and restarting Nginx.
Before You Begin
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 start nginx
Instead of manually starting the Nginx service, it is recommended to set it to start on system boot:
sudo systemctl enable 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 nginx
sudo service stop 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 nginx
sudo service restart nginx
This is the command that you will probably use the most frequently.
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 nginx
sudo service reload nginx
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 - 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 2019-04-21 13:57:01 PDT; 5min ago Docs: man:nginx(8) Process: 4491 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS) Process: 4502 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 4492 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 4504 (nginx) Tasks: 3 (limit: 2319) CGroup: /system.slice/nginx.service |-4504 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; |-4516 nginx: worker process `-4517 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.14.0 (Ubuntu)
-V option displays the Nginx version along with the configure option.
sudo nginx -V
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.
If you have any questions or feedback, feel free to leave a comment.