Definitely not this. Virtualmin calls certbot, and then does stuff with the resulting certs, not the other way around.
Virtualmin has pre and post hooks, though, so you can write scripts in whatever language you like (including bash shell scripts) to run before or after changes. Renewing certs is a change, and the hook runs. To know if it’s a TLS cert change, you’d check for SSL_DOMAIN.
So, you could base your post script on that, and then configure Virtualmin to run it in Virtualmin Configuration->Actions upon server and user creation->Command to run after making changes to a server
#!/bin/bash
if [ "$VIRTUALSERVER_ACTION" = "SSL_DOMAIN" ] | [ "$VIRTUALSERVER_ACTION" = "CREATE_DOMAIN" ]; then
/usr/bin/cat "/home/$VIRTUALSERVER_DOM/ssl.combined" "/home/$VIRTUALSERVER_DOM/ssl.key" > "/etc/haproxy/certs/$VIRTUALSERVER_DOM.pem"
/usr/bin/systemctl restart haproxy.service
fi
Or:
#!/bin/bash
if [ "$VIRTUALSERVER_ACTION" = "SSL_DOMAIN" ] | [ "$VIRTUALSERVER_ACTION" = "CREATE_DOMAIN" ]; then
/usr/bin/cp -f "/home/$VIRTUALSERVER_DOM/ssl.everything" "/etc/haproxy/certs/$VIRTUALSERVER_DOM.pem"
/usr/bin/systemctl restart haproxy.service
fi
Upload it to my usual scripts directory, and then in System Settings > Virtualmin Configuration put the path . /home/mymaindomain.com/scripts/copy-cert-to-haproxy.sh into Command to run after making changes to a server?
If so that’s much easier than I thought it would be!
You need a shebang (e.g. #!/bin/sh or #!/bin/bash or whatever), but otherwise looks fine.
I’d probably also recommend wrapping anything with a variable in double quotes. I need to fix that example. I don’t know who wrote it, but I try to make all my examples pass shellcheck, so it couldn’t have been me. See some of the other examples for better practice in terms of using variables in shell scripts. (And, use shellcheck to get good advice about safer and more reliable shell scripting.)
Thanks Joe, I’ve edited my post above yours with the updated snippet - do you mean like that?
Unfortunately it did not work. I created a new test.domain.com virtualserver and Virtualmin set it up as usual, it fetched the letsencrypt script and ran copy-cert-to-haproxy.sh - however the resulting file is empty:
-rw-r--r-- 1 root root 0 Dec 25 03:20 test.domain.com.pem
I changed System Settings > Virtualmin Configuration > Command to run after making changes to a server? to bash /home/mymaindomain.com/scripts/copy-cert-to-haproxy.sh (added bash to the start) as I was getting permission denied without it, but something is still amiss.
Are you still getting permission denied, or something else?
You need to make the file executable, if you haven’t already. You can test your script by setting the variables you use in the script before you run it. e.g.:
speaking in general, is it possible to adjust the software package to look in /etc/letsencrypt/live/$DOMAIN/ and directly use the certs from that location?
Nice idea, however HAProxy only allows you to specify a directory, so they’d all need to in there (for each domain) plus I’d still need to restart HAProxy. Think the inbuilt hooks Joe mentioned is probably the best option for this case
if [ "$VIRTUALSERVER_ACTION" = "SSL_DOMAIN" ]; then
/usr/bin/cat "/home/$VIRTUALSERVER_DOM/ssl.combined" "/home/$VIRTUALSERVER_DOM/ssl.key" > "/etc/haproxy/certs/$VIRTUALSERVER_DOM.pem"
/usr/bin/systemctl restart haproxy.service
fi
And it works when I do this via terminal:
But not when I create a new test.domain.com - Virtualmin reports it has created everything successfully but nothing gets copied over to /etc/haproxy/certs/ - the domain creation page does not report a fault:
Creating administration group test.domain.com ..
.. done
Creating administration user test.domain.com ..
.. done
Creating aliases for administration user ..
.. done
Adding administration user to groups ..
.. done
Creating home directory ..
.. done
Creating mailbox for administration user ..
.. done
Adding new DNS zone ..
.. done
Adding new virtual website ..
.. done
Adding webserver user apache to server's group ..
.. done
Performing other Apache configuration ..
.. done
Creating SSL certificate and private key ..
.. done
Adding new SSL virtual website ..
.. done
Saving server details ..
.. done
Re-starting DNS server ..
.. done
Applying web server configuration ..
.. done
Re-starting Webmin ..
.. done
Re-starting Usermin ..
.. done
Requesting a certificate for test.domain.com, www.test.domain.com from Let's Encrypt ..
.. request was successful!
Creating initial website index page ..
.. done
Applying web server configuration ..
.. done
Re-starting Webmin ..
.. done
Re-starting Usermin ..
.. done
In the field for Command to run after making changes to a server I have tried both:
bash /home/mydomain/scripts/copy-cert-to-haproxy.sh
as well as without bash at the start: /home/mydomain/scripts/copy-cert-to-haproxy.sh
But neither of these seem to work.
Summary: testing via the command line works, but not when virtualmin creates the account. Any ideas what to try next?
I am guessing there are no variables available to the script, as previously I had taken out the if [ "$VIRTUALSERVER_ACTION" = "SSL_DOMAIN" ]; then conditional and that’s when it was giving me an empty file. Either that or $VIRTUALSERVER_ACTION" = "SSL_DOMAIN" is not being set on domain creation?
Only other thing I can think of is the command to run the script might need to be different, so far I have tried all of the below:
I know that bash /home/mymaindomain.com/scripts/copy-cert-to-haproxy.sh definitely runs the script as when I removed the condition that’s when I was getting an empty file - so I think it’s either the variable isn’t available or the correct one (SSL_DOMAIN) is not being set on domain creation?
So, check all the variables you’re expecting by logging the env when it runs and then exit. Modify your script to have this at the beginning, just after the shebang:
Then see what shows up in that log. Make sure all the variables are set how you think. (There will probably be other unrelated stuff in there, but you can search for the variables you want.)
The problem was VIRTUALSERVER_ACTION=CREATE_DOMAIN so I changed the conditional to:
if [ "$VIRTUALSERVER_ACTION" = "SSL_DOMAIN" ] | [ "$VIRTUALSERVER_ACTION" = "CREATE_DOMAIN" ]; then
If VIRTUALSERVER_ACTION is always SSL_DOMAIN on cert renewal then the updated script should ensure it will always works - or do I need to look for other possible variants for VIRTUALSERVER_ACTION as well?
If we go to Virtualmin (domain/account) > Server Configuration > SSL Certificate and request or renew a certificate should this also trigger the Command to run after making changes to a server action?
Asking because I just did that and it did not. It’s renewed the certificate but not copied it over to /etc/haproxy/certs/ - so doesn’t appear to have run the Command to run after making changes to a server.
certbot certificates (to view certs)
sudo certbot delete --cert-name domain.tld (to remove the cert I want Virtualmin to handle)
domain.tld > Edit Virtual Server > Check: Apache SSL website enabled? > Save (and when that did not work I did the below)
domain.tld/Server Configuration > SSL Certificate > Request Certificate
Yes it should do, although until the most recent 7.30.x series release of Virtualmin there was a bug where post-domain actions weren’t run in this case.
Will post-actions still run (even pre 7.30.x) after the virtualmin renewal script runs? (i.e if Automatically renew certificate? has been checked in Virtualmin > Server Configuration > SSL Certificate > Let's Encrypt) or will it not work at even for that for anything prior to 7.30?