on my last article Installing web server on ubuntu 18, I covered all the steps required to have a nginx server running on your linux ubuntu server with Letsencrypt SSL encryption. Here I will cover how to use a bash script to Auto-renew Letsencrypt SSL certificate on Ningx.

Varnish uses port 80 and Nginx uses port 8080 but when letsencrypt try to renew, it needs the port 80 and port 8080, and that’s why you need to stop those services before renewing ssl. In order to make it automatically, we need to use bash script. here the bash script that I use: 


echo "starting to renew..."

#stop nginx and varnish
echo "stop nginx and varnish..."
service nginx stop
service varnish stop

#renew ssl
echo "letsencrypt auto renew goes..."
/opt/letsencrypt/letsencrypt-auto renew

#restart nginx and varnish
echo "restart nginx and varnish..."
service nginx restart
service varnish restart

save it in a file e.g. mybashssl.sh and put in a directory.

then edit the crontab :

crontab -e

and add this line:

0 0 1 * * /yourdir/mybashssl.sh

then save changes and that’s all. that line will make the cron check once in a month.

 also you don’t need to wait and you can check immediately in the command line: 

bash /yourdir/mybashssl.sh

then you don’t have to worry about the expire of your ssl. because is annoying when the ssl is expired and the browsers don’t let your site to show up.

I hope this article be useful for your needs.