25 Nginx commands that developers and administrators must master (below)

Nginx is one of the most popular web servers among Linux and BSD users because of its rich functional instruction set and superior flexibility. If you are a web developer, then you may be using Nginx or Apache server. Therefore, it is important to understand in depth how Nginx works from the command line. Fortunately, you can improve your DevOps skills by mastering some Nginx commands. Our editors have selected these frequently used commands for you and provided a comprehensive discussion for starting Linux administrators. Then please read this article to learn more about these commands.

25 Nginx commands that developers and administrators must master (below)

Nginx commands for Linux administrators

Unlike many web servers, Nginx only uses configuration files to handle server configuration. Therefore, there are few command line parameters that you can use. This is useful because administrators can quickly find the command they are looking for. Here are some widely used Nginx commands, you can use them now.

17. Find the Nginx PID

PID or process ID is a unique number used to distinguish processes on Linux and Unix-like systems. We can use the appropriate PID to send various signals to the Nginx server. This PID can be found using one of the following commands.

Find Nginx PID

18. Find log files

The log file contains a lot of valuable information for system administrators and developers. Nginx has two default log files, composed of access.log and error.log documents. These are located in / var / log and can be viewed using the following command.

25 Nginx commands that developers and administrators must master (below)

You should now see the log files mentioned above. As the name suggests, access.log contains information about visitors to your website, and error.log contains warnings / details about misconfigurations. However, you need to enable these two logs from the Nginx configuration file before you can use them.

19. Set up a virtual host

Web hosting allows server administrators to run multiple websites on a single server computer. This is often useful because you can share your computing process to run multiple sites at the same time. However, the term virtual host is usually associated with an Apache server. In the Nginx world, they are called "server blocks".

You can use this simple symbolic link to easily enable virtual hosting on the Nginx server. If you want to disable the virtual host, just delete the symbolic link.

20. View the compiled Nginx module

As you saw in the previous Nginx command, when installing the daemon, some basic modules are also installed. We can easily view these Nginx modules using the following commands.

25 Nginx commands that developers and administrators must master (below)

This command utilizes several Linux command line tools and filters out irrelevant information to display only modules. Since Nginx has many modules, this command is useful for checking which modules are compiled for the server.

21. Enable / disable Nginx service

Enabling the Nginx service allows the server to start automatically during startup. This is critical for dedicated servers because otherwise, user requests may be interrupted. We can use the following command to easily start Nginx automatically.

These simple but effective commands will ensure that server downtime is minimized. You can also disable automatic startup as needed. Just use one of the following commands.

22. Instantly upgrade Nginx

Nginx allows administrators to instantly upgrade binary files and / or configuration files. This means that your client requests will not be interrupted due to server upgrades. To do this, first, we need to find the PID of the main Nginx process. We can use simple commands that have already been demonstrated.

The new Nginx binary should be ready. Use the following command to generate a new set of Nginx master / worker processes that use new binaries

Now, use the following command to kill the auxiliary process used by the first main process.

Track it by killing the old main process.

23. Set up Nginx in Chroot Jail

Chroot Jail on the Nginx server will provide additional security protection in the event of possible intrusion. Administrators often use this technique to ensure that their servers are isolated and secure in a small part of the Linux file system. Use the following command to set up the Nginx server in Chroot Jail.

# D=/nginx
# mkdir -p $D

# mkdir -p $D/etc
# mkdir -p $D/dev
# mkdir -p $D/var
# mkdir -p $D/usr
# mkdir -p $D/usr/local/nginx
# mkdir -p $D/tmp
# chmod 1777 $D/tmp
# mkdir -p $D/var/tmp
# chmod 1777 $D/var/tmp
# mkdir -p $D/lib64

# ls -l /dev/{null,random,urandom}
# /bin/cp -farv /usr/local/nginx/* $D/usr/local/nginx

You need to run them as superuser. Now, use the following command to find the shared library.

# ldd /usr/local/nginx/sbin/nginx

As shown below, copy all libraries one by one.

# cp /lib64/libpcre.so.0 $D/lib64

You also need to copy / etc and some other directories.

# cp -fv /etc/{group,prelink.cache,services,adjtime,shells,gshadow,shadow,hosts.deny,localtime,nsswitch.conf,nscd.conf,prelink.conf,protocols,hosts,passwd,ld.so.cache,ld.so.conf,resolv.conf,host.conf} $D/etc
# cp -avr /etc/{ld.so.conf.d,prelink.conf.d} $D/etc

Your Chroot Jail is now ready to accept Nginx. Just kill the old service and use the next command to start the new service.

# /usr/sbin/chroot /nginx /usr/local/nginx/sbin/nginx -t

24. Run Nginx in Docker

Docker containers have become very popular due to their flexibility and powerful performance. You can easily create and run Nginx web servers from Docker containers. The next command will extract the official Nginx image and use the default configuration to create a server instance.

25 Nginx commands that developers and administrators must master (below)

You can use the following simple command to maintain persistent storage.

sudo docker run --name nginx-server -v /var/www:/usr/share/nginx/html:ro \

-v /var/nginx/conf:/etc/nginx:ro -P -d nginx

25. Run Nginx in LXD

LXD is hailed as the next-generation Linux container and provides a series of amazing features. You can also use Nginx through the LXD container. Take a look at the following Nginx commands for LXD.

First, we created a container called nginx-server, and then started a shell in the container. Then, we updated the package list and installed the Nginx web server in the container. The last command is just to reload the server.

to sum up

Nginx provides many surprising features, and new users are often overwhelmed by its powerful features. Fortunately, if you only know some basic Nginx commands, you can also gain great confidence. This is why we take the liberty to show you these awesome commands. We have introduced not only basic commands, but also some advanced and more powerful command-line techniques. I hope that once you get used to these commands, your mastery of Nginx skills will be improved. Please tell us your thoughts in the comment section below, and continue reading other articles from the Linux Commune for more guidance on Linux servers.

Find Nginx PID

Guess you like

Origin www.linuxidc.com/Linux/2020-04/162957.htm