I have a very specific script I wrote about two years ago. It runs fantastically and has never failed ... However the more websites we put into the system the script gets slower and slower (as expected) ... The cron
that runs the script every 10 mins now takes about 3 minutes to complete. I am looking to reduce that time. So let me explain what the script does.
1) Here is the MAIN LOOP -- It checks the database for all websites for paying customers:
mysql --login-path=main-data -e "SELECT user_file FROM database" | while read user_file; do
2) Inside the main loop I build the individual conf files :
p=$user_file
echo "<VirtualHost *:80>" > /etc/apache2/sites-available/$p.conf
.........
3) Still inside the main loop, I check to see if they have extra domains and create their aliases :
mysql --login-path=main-data -e "SELECT domain
FROM database
WHERE user_file = '$user_file';" | while read domain; do
echo " ServerAlias $domain" >> /etc/apache2/sites-available/$p.conf
echo " ServerAlias www.$domain" >> /etc/apache2/sites-available/$p.conf
done
4) Still inside the main loop -- I check for SSL and create that part of the conf file. NOTE that there's some openssl
config tests here that may slow things down:
mysql --login-path=main-data --skip-column-names -e"SELECT ssl FROM database
WHERE user_file = '$user_file'
AND a.primary_domain = '1'
AND b.https = '1'" | while read ssl; do
if [ $ssl = 1 ]
then
########################### START SSL TEST
crt="/var/www/liveSites/websites/$user_file/ssl/$domain.crt"
key="/var/www/liveSites/websites/$user_file/ssl/$domain.key"
key_test=$(openssl x509 -in $crt -pubkey -noout -outform pem | sha256sum 2>&1)
crt_test=$(openssl pkey -in $key -pubout -outform pem | sha256sum 2>&1)
if [ "$key_test" = "$crt_test" ]
then
echo "\n - Matched -- Cert Good - \n";
else
echo "SSL match failed for $user_file -> $domain" > /etc/apache2/websitesCron/ssl_fail.txt
cat /etc/apache2/websitesCron/ssl_fail.txt | mail -s "SSL INSTALLATION ERROR" it@mycompany.com
fi
####################### END SSL TEST
echo "<VirtualHost *:443>
ServerName $domain
ServerAlias www.$domain
DocumentRoot /var/www/liveSites/websites/$user_file/public_html
........
fi
done
5) Finally, still inside main loop I check to see if the site is enabled, if not enable it
if test -f "/etc/apache2/sites-enabled/$p.conf"; then
echo "Configuration exists \n"
else
A2ENSITE=/usr/sbin/a2ensite
${A2ENSITE} $p.conf
fi
After the main loop -- I check the Apache config, to make sure it will reload gracefully
and I check that ssl
didn't fail the openssl
comparison check:
/usr/sbin/apachectl configtest > /etc/apache2/websitesCron/configtest 2>&1
if grep "failed" /etc/apache2/websitesCron/configtest
then
# Do failed stuff stop script and turn cron off
elif grep "failed" /etc/apache2/websitesCron/ssl_fail.txt
then
# Do failed SSL stuff stop script and turn cron off
else
# gracefully reload Apache
/etc/init.d/apache2 reload
fi
It's a pretty simple script, but there's a few moving parts -- For 1,000+ websites, does it seem like 3 mins for it to run is reasonable? Should I fork? Is there logic changes that could "clean it up"?