1. Overview

Nginx is a lightweight, reliable web server that is open-source and free. We can configure Nginx to use as a load balancer, proxy server, and even a mail proxy.

While working with any server, a few common operations are performed. This includes starting, stopping, and restarting the server.

In this tutorial, we’ll discuss the steps to manage these operations on Linux for the Nginx server.

Before we move further, let’s first find out different ways to install Nginx on Linux.

2. Understanding System Managers in Linux

A System manager is one of the building blocks of Linux. It runs using the PID 1 and is responsible for starting the entire Linux system. systemd, init, and upstart are the three most widely used system managers in Linux. System managers provide the capability to run and manage executables by creating services. Here, we define an executable as a file or binary containing a set of instructions that can run on Linux.

In general, there are three ways to run an executable in Linux.

Firstly, we can use the systemd service manager that manages all the services present inside the/lib/systemd directory. The latest versions of CentOS, Debian, Fedora, OpenSUSE, Redhat, and Ubuntu has systemd enabled by default.

On older versions of Ubuntu, CentOS, and other Linux distributions, there are chances that we might not find the systemd. As an alternative, a traditional init system manages all the services. The init service scripts are present inside the /etc/init.d directory.

At last, there are cases when none of the systemd or init is available on the machine or if it’s present but not accessible to the user. We can still run the binary on such devices by launching it as a process. This is a naive approach as we need to manage this process ourselves. On system reboots, we need to relaunch this process.

2.1. Determine System Manager on Linux

In order to detect which system manager is present on a Linux machine, we can use the following command:

$ sudo ls -l /proc/1/exe
lrwxrwxrwx. 1 root root 0 Apr  8 11:10 /proc/1/exe -> /usr/lib/systemd/systemd

Here, we check the symbolic link of the first executable on a Linux machine. In this case, it is linked to systemd. For init systems, we’ll get an output similar to this:

$ sudo ls -l /proc/1/exe
lrwxrwxrwx 1 root root 0 Apr  8 11:17 /proc/1/exe -> /sbin/init

So far, we have learned how to run an executable on Linux. Let’s now learn how to manage the Nginx server specifically in each environment.

3. Nginx Running as a systemd Service

Let’s first find out if there is any Nginx service that is managed by systemctl:

$ sudo ls -lah /lib/systemd/system/nginx.service
-rw-r--r--. 1 root root 616 Oct  7  2019 /lib/systemd/system/nginx.service

Here, we looked for the nginx.service file inside the /lib/systemd/system directory using the ls command. In case if Nginx service is not available, we’ll get an output similar to this:

$ sudo ls /lib/systemd/system/nginx.service
ls: cannot access '/lib/systemd/system/nginx.service': No such file or directory

We can start the Nginx service using the following command:

$ sudo systemctl start nginx

Let’s now get the status of the Nginx server using the systemctl command:

$ sudo systemctl status nginx

If the server is running, we’ll get the status of “active (running)” in the output. The output would include errors if the server failed to start due to any reason. We can also access the <IP>:<PORT> to verify that the server is up and running.

Stopping the Nginx server is as simple as starting it:

$ sudo systemctl stop nginx

If we check the status now, it will be marked as “inactive (dead)”.

We can also restart the server when there are any configuration changes:

$ sudo systemctl restart nginx

systemctl supports other commands like reload, isolate, kill, etc., to provide more control over the service. We can get a complete list by running the systemctl –help command.

4. Nginx Running as a SysVinit Service

So far, we have learned to manage the Nginx service using systemctl. Let’s now perform the same operations using the init service. Before that, let us find out if the Nginx service exists on the machine or not:

$ sudo ls -lah /etc/init.d/nginx
-rwxr-xr-x. 1 root root 4.2K Apr 13  2021 /etc/init.d/nginx

The output clearly shows that the service file for Nginx exists. This service file is nothing but a bash script. In order to start the Nginx server using the init service, we’ll use the start option as follows:

$ sudo /etc/init.d/nginx start

Let’s now check the status of the Nginx server:

$ /etc/init.d/nginx status
[ ok ] nginx is running.

We can also restart the Nginx server by passing the restart option. It will remove all the old Nginx processes, reload the config file and launch new Nginx processes:

$ sudo /etc/init.d/nginx restart

There are a bunch of other options to manage the Nginx service. We can get the usage using the -h option:

$ sudo /etc/init.d/nginx -h
Usage: /etc/init.d/nginx {start|stop|status|restart|reload|force-reload|upgrade|configtest|check-reload}

5. Nginx Running as a Process

Both systemd and init are service managers in Linux. Hence, they ease out the process to manage the running process. But we can also run the Nginx server directly as a process without using any service manager. It is worth noting that when we run the Nginx using any of the service managers, it will implicitly run the Nginx process using the service script.

In order to run Nginx as a process, we first need to find the installation path of Nginx:

$ sudo which nginx
/usr/sbin/nginx

which is a Linux utility that returns the full path of the executable, which we pass as an argument. Here the Nginx executables are present at /usr/sbin/nginx.

Now, when we run this executable, the Nginx server will start:

$ sudo /usr/sbin/nginx

If we view the systemd or init service file, we’ll notice that the same executable, /usr/sbin/nginx, is triggered from both the services. The service manager provides us with an extra layer of abstraction to handle the processes.

Now in order to stop the Nginx process, we’ll use the killall command:

$ sudo killall nginx

killall is a Linux command that kills the processes by their name. The above command will remove all the Nginx processes from the machine.

At last, restarting a process is nothing but killing all the existing processes and rerunning them.

6. Conclusion

In this tutorial, we have learned to start, stop and restart the Nginx server in Linux.

There are three ways to run any executable in Linux, i.e., using systemd, init, and as a process.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.