NGINX restarts on cPanel servers

Today we’ll fix NGINX restarts on cPanel servers.

When running NGINX on a cPanel server, it often requires that you put a restart of NGINX inside your httpd init script to make NGINX and Apache restart at same time to get the changes updated.

The problem people often will find is that cPanel from time to time revert this change to the httpd init script, and this will result in that NGINX doesn’t get restarted when Apache does. So what I did, was to write a small fix for this:

#!/bin/bash

for file in /usr/local/apache/bin/apachectl /etc/init.d/httpd;
do
    if [[ -z $(grep "/etc/init.d/nginx" $file) ]];then
        sed -i "s/$HTTPD -k .*/\\0\\n\\/etc\\/init.d\\/nginx \$ARGV/g" $file
    fi
done;

So what the script does, is a small for loop with a list of files, it checks if the files contain /etc/init.d/nginx – if it doesn’t contain that string, it will go into the file, and look for $HTTPD -k .* which is located twice in the files. And then right under that matched line, it will append:

/etc/init.d/nginx $ARGV

You can then implement some checking if you want, but very basic, this script will work. Put it in a cron and run it every 4 hours or whatever, if it contains the nginx part in the file, it won’t do anything. But it will make sure that the files will not be reverted by cPanel at some point.