Basic commands for managing Nginx services

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

If you are a developer or system administrator, chances are you interact with Nginx on a regular basis.

In this guide, we’ll cover the most important and used Nginx commands including starting, stopping, and restarting Nginx.

Before starting

All commands should work like sudo or root and should run on modern Linux distributions like Ubuntu 18.04, CentOS 7, and Debian 9.

Run Nginx

Getting started with Nginx is fairly simple. Just run the following command:

sudo systemctl start nginx

When successful, the command produces no output.

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

sudo service start nginx

Instead of starting the Nginx service manually, it is recommended to set it to start automatically upon system bootup:

sudo systemctl enable nginx

Nginx is shutting down

Stopping Nginx will quickly terminate all Nginx worker processes, even if there are still open connections.

To stop Nginx, run one of the following commands:

sudo systemctl stop nginx

sudo service nginx stop

Restart Nginx

The restart option is a quick way to shut down and then restart the Nginx server.

Use one of the following commands to restart Nginx:

sudo systemctl restart nginx

or

sudo service nginx restart

When changing the settings, this could be something that you may be using a lot.

You will need to reload or restart Nginx every time you make changes to its configuration.

The reload option will load and apply the new configuration, startup Worker operations New with a new configuration and by shutdown Worker operations Long.

To reload Nginx, use one of the following commands:

sudo systemctl reload nginx

or

sudo service nginx reload

Nginx Configuration Tests

Whenever you have made changes to the Nginx Server configuration files, it is best to test the configuration before restarting or reloading the service.

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

sudo nginx -t

The output will look like this.

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

If there is an error, the command will print a detailed message along with the file that caused the error

Error outputting nginx configuration

View Nginx Status

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

sudo systemctl status nginx

The output will look 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

View the Nginx version

Sometimes you may need to know your version of Nginx in order to be able to correct an issue or determine if a specific feature is available.

You can check the Nginx version by running:

sudo nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

The -V option will display the Nginx version with configuration options.

sudo nginx -V

The full version of Nginx

Conclusion

In this guide, we’ve shown you some of Nginx’s most important commands. If you want to learn more about the Nginx command line, visit the Nginx documentation

.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *