Skip to content

Instantly share code, notes, and snippets.

@jonathanbossenger
Last active March 22, 2022 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonathanbossenger/4950e107b0004a8ee82aae8b123cce58 to your computer and use it in GitHub Desktop.
Save jonathanbossenger/4950e107b0004a8ee82aae8b123cce58 to your computer and use it in GitHub Desktop.
Bash script to automatically decommission LAMP sites
#!/bin/bash
# Bash script to set drop local site created by sitesetup script
# See https://gist.github.com/jonathanbossenger/2dc5d5a00e20d63bd84844af89b1bbb4
SSL_CERTS_DIRECTORY=/home/jonathan/ssl-certs
SITES_DIRECTORY=/home/jonathan/development/websites
SITE_NAME=$1
MYSQL_DATABASE=$(echo $SITE_NAME | sed 's/[^a-zA-Z0-9]//g')
SITE_CONFIG_PATH=/etc/apache2/sites-available/$SITE_NAME.conf
SSL_SITE_CONFIG_PATH=/etc/apache2/sites-available/$SITE_NAME-ssl.conf
echo "Deleteing websites directory"
rm -rf $SITES_DIRECTORY/"$SITE_NAME"
echo "Disabling virtual hosts..."
a2dissite "$SITE_NAME".conf
a2dissite "$SITE_NAME"-ssl.conf
echo "Deleting virtual hosts..."
rm -rf "$SITE_CONFIG_PATH"
rm -rf "$SSL_SITE_CONFIG_PATH"
echo "Remove hosts record.."
sed -i "/$SITE_NAME.test/d" /etc/hosts
echo "Deleting database.."
mysql -uroot -ppassword --execute="DROP DATABASE $MYSQL_DATABASE;"
echo "Deleting certs.."
rm -rf $SSL_CERTS_DIRECTORY/"$SITE_NAME"*
echo "Restarting Apache..."
service apache2 restart
echo "Done"
@haydar
Copy link

haydar commented Sep 24, 2020

There is the wrong code on line 19. It is sites-available not the sites-enabled.

I added a few features. My updated script;

https://gist.github.com/haydar/4ecd18b88a2e216d6e5776b9b6aed162

@jonathanbossenger
Copy link
Author

Add the possibility to autofill site name, based on ~/development/websites/ directory structure.

@jonathanbossenger
Copy link
Author

Updated to use MYSQL_DATABASE variable, in case site name has a hyphen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment