Connect with us

Website Tutorials

Nginx Basics: From Nginx Restart To Configuration Checks

Published

on

Nginx Basics: From Nginx Restart To Configuration Checks

Nginx (pronounced as “engine-x”) is a popular web server and reverse proxy server. The Nginx web server has multiple versions, though this guide focuses on the open-source version. This post will look into how to manage Nginx using the systemctl command on a traditional install and the case of a cPanel installation, focused from a command line perspective.

Managing Nginx Using Systemctl On A Standard Install

This section covers how to confirm that Nginx is installed, starting, stopping, restarting as well as how to restart the service on failure.

Confirming That Nginx Is Installed?

First, to confirm that Nginx is installed, use the following command.

type -p nginx

Start, Enable, And Restart Nginx

With these are all though systemctl commands replace the second word in the command with the action we want. See the following commands:

To enable Nginx to start on boot

Advertisement
sudo systemctl enable nginx

To start Nginx a single time:

sudo systemctl start nginx

After making changing or adding new configuration use, the following to reload Nginx:

sudo systemctl reload nginx

To restart the Nginx server:

sudo systemctl restart nginx

Extra: Automatically Restart Nginx Using Systemctl

For public-facing web servers, you may want to have your web server restart on failure to minimize potential downtime caused by such an issue. Though this is not fix to what caused the service to fail, this will give you a bit more time to do a thorough investigation.

We can do this by editing the systemd unit file.

Advertisement

First, confirm the location of the unit file using the below command

systemctl status nginx | grep "Loaded"

On my test server, the file is located at /usr/lib/systemd/system/nginx.service

We will add the following two lines using your preferred text editor make sure to use sudo or root privileges.

Restart=on-failure
RestartSec=3s

So the file will look something like this

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
LimitNOFILE=8192
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
# Added to restart on failure
Restart=on-failure
RestartSec=3s

[Install]
WantedBy=multi-user.target

Save and close out of the file.

Advertisement

Then run the following command, so systemd reloads the changes:

sudo systemctl daemon-reload

Managing Ngnix With Reverse Proxy On Cpanel

This section covers working with the Ngnix proxy provided by cPanel the package called ea-nginx. At the time of writing this is the only method (which is non experimental) that cPanel supports.

Note: These tasks require root user account access.

How Do I Know My Server Is Using Cpanel?

The command below will check for /usr/local/cpanel/cpanel as this will only exist on cPanel servers.

if [ -x /usr/local/cpanel/cpanel ]; then
echo "cPanel is installed on this server."
else
echo "cPanel is not installed on this server."
fi

If you aren’t sure if Nginx is installed you can check using the following command:

Advertisement
yum list installed ea-nginx

If the command returns results then that confirms that ea-nginx (“NGINX with Reverse Proxy”) is installed.

Restarting Ngnix On Cpanel

To restart services in cPanel use the cPanel provided restart scripts, as they interact with chkservd (cPanel’s service watchdog).

The command would be:

/usr/local/cpanel/scripts/restartsrv_nginx

If Nginx is installed and the above command does not bring Nginx online, the next step is to ensure it isn’t disabled. The WHM API command below enables the service and monitoring.

whmapi1 configureservice service=nginx enabled=1 monitored=1

If both the above fail, you can try to reinstall ea-nginx.

Advertisement
yum reinstall ea-nginx

If you have any additional issues please refer to the official docs.

Checking Configuration files

To Check the configuration files for a syntax error use the following command:

nginx -t

If the command returns that the syntax is ok and the test was successful, then everything is good.

If there is an issue it will point you to the configuration file that is causing it so you can investigate the configuration file further.

Conclusion

In this post, we have discussed that Nginx has multiple versions and that this post concerns the open-source version. From there split off into a cPanel and traditional installation section which goes over how to check that Nginx is installed and basic service management from restart and end on how to check the configuration syntax.

Advertisement

Resources

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.