Please refer to update in the next comment
Here is an automation solution for nginx that works with my configuration. This solution assumes you are using CentOs/Rhel 7, have root access, and have already gone into
Virtualmin>>System Settings>>Features and Plugins>>Nginx website>>configure (in the “actions” column)
and set the
File or directory for new virtual hosts
to
/etc/nginx/conf.d/
Logged in as
root
Navigate to Webmin. Go to
Others>>File Manager>>/root/
then click
File>>Create new directory
Create two. One called
scripts
one called
sites-deleted
This can also be done in the shell with
mkdir /root/scripts && mkdir /root/sites-deleted
I also have one called
sites-created
Create a file in
scripts
Call it whatever
myscrypt.sh
or you can
nano /root/scripts/myscript.sh
Either way, paste the following code:
Please see update below
What this does:
sed -i -e ‘s/^/#/’ /etc/nginx/conf.d/$VIRTUALSERVER_DOM.conf;
comments out the entire file, thus preserving the original
this
systemctl restart php-fpm php54-php-fpm nginx;
restarts nginx and php b/c our script is going to run after the post-create restart and our changes won’t be realized otherwise pending a restart.
This
if [ “$VIRTUALSERVER_ACTION” = “DELETE_DOMAIN” ]; then
CONF_FILE_EXISTS="/etc/nginx/conf.d/${VIRTUALSERVER_DOM}.conf";
if [ -f “$CONF_FILE_EXISTS” ]; then
mv /etc/nginx/conf.d/${VIRTUALSERVER_DOM}.conf /root/deleted_domains/${VIRTUALSERVER_DOM}.conf.deleted;
fi
fi
Checks if this is a delete operation and if it is moves the .conf file to our “sites-deleted” directory (Because VM will only delete the one server block, and ignores the code blocks containing our rewrite rules, thus we are left with half a file – the original commented out code will still be in there as well, which is why I’m preserving it as a record of the state of the config at the time of deletion. It could potentially be used to rebuild the virtual host later).
Finally, navigate to
Virtualmin>>System Settings>>Actions upon server and user creation
Find the line
Command to run after making changes to a server
And input your path
/root/scripts/myscript.sh
And your done. After you’ve done some testing and made the necessary edits for your configuration. i.e. if you are running a Debian based system the paths and a few other things will be a little different, but nothing you won’t be able to work out on our own.
Apologies for any readability issues. First time with the editor.
Leave this field blank
Oh, and don’t forget to make the file executable
chmod +x /root/scripts/myscript.sh
or in File Manager right click
myscript.sh>>properties>Change permissions
Check the Execute box under Owner and Group.