nginx Command in Linux | Explained

Nginx is a powerful web server that can serve dynamic and static content. In Linux, it provides enhanced security, speed, and stability compared with other Apache web servers. Other than that, It is also faster than Apache and can scale beyond what Apache can do.

It’s well known for its caching and low resource usage, making it a popular choice with modern websites. Installing Nginx on Linux OS. The content of this article revolves around the key points mentioned below.

Let’s get into the Nginx installation.

Installing Nginx on Linux

There are three different versions of nginx available, which are as follows

  • Minimal Version of Nginx: It does not contain many modules.
  • Full Version of Nginx: It contains lots of third-party modules as well as the core modules
  • Extra Features version of Nginx: It has some additional and third-party modules which are missing in the first two.

You can install any of these three versions on Debian-based distros by running the commands mentioned below:

$ sudo apt install nginx                #Minimal Version of nginx
$ sudo apt install nginx-full           #Full Version of nginx
$ sudo apt install nginx-extras         #Extra Features Version of nginx

To install it on Centos or Fedora you need to type:

$ sudo yum install nginx
$ sudo yum install nginx-full
$ sudo yum install nginx-extras

Performing Different Nginx Services

The nginx provides different services that can be performed and to find the list of all those services you need to type:

$ service nginx -?

Let’s perform some of the available services and see what it does.

Start Nginx Services

If you want to start nginx services, then you can do that by typing:

$ sudo service nginx start

Stop Nginx Services

Similarly, you can stop nginx services by typing:

$ sudo service nginx stop

Restart Nginx Services

Restarting the nginx services can be done by typing:

$ sudo service nginx restart

Similarly, you can perform other services as well by following the same syntax.

Check Nginx Status

The next thing you need to confirm after starting the nginx is to check its status, whether it’s activated or not, by typing the below command:

$ service nginx status

You can see in the above image that it has an active status which means that it is now fully functional. You can also do the same thing by typing another command mentioned below:

$ sudo /etc/init.d/nginx status

Check Configuration File Status

if you want to check the status of configuration files, then you can do that by typing the below command:

$ sudo nginx -t

Same thing can be done by utilizing another command which is mentioned below:

$ sudo service nginx configtest

Check Nginx Version

If you want to check the nginx version that you are currently using then you can do that by typing:

$ sudo service nginx -V

What to Do if Nginx is not Active?

There are two main ways that you should try if the nginx is not activated even if you run the above commands. It might be possible that due to any reason the nginx is disabled so in such case, you can type the below command to enable it:

$ sudo systemctl enable nginx

Now if it is still not working then the next thing you can do is to allow it through the firewall as well which you can do by typing:

$ sudo ufw allow ‘NGINX FULL’

That’s all from the nginx command in Linux.

Conclusion

Nginx is a powerful web server that can be used to serve dynamic and static content with enhanced security, speed, and stability. It’s faster than Apache and can scale beyond what Apache can do. This article contains some of the most useful commands that are needed to use nginx along with its installation and troubleshooting solution.