Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash script to automatically provision LAMP sites
#!/bin/bash
# Bash script to set up local site using LAMP on Ubuntu
# Requires Apache2, MySQL, mkcert (https://github.com/FiloSottile/mkcert)
# See also sitedrop.sh https://gist.github.com/jonathanbossenger/4950e107b0004a8ee82aae8b123cce58
HOME_USER=jonathan
SSL_CERTS_DIRECTORY=/home/jonathan/ssl-certs
SITES_DIRECTORY=/home/jonathan/development/websites
SITE_NAME=$1
SITE_CONFIG_PATH=/etc/apache2/sites-available/$SITE_NAME.conf
SSL_SITE_CONFIG_PATH=/etc/apache2/sites-available/$SITE_NAME-ssl.conf
echo "Creating websites directory"
mkdir $SITES_DIRECTORY/"$SITE_NAME"
chown -R $HOME_USER:$HOME_USER $SITES_DIRECTORY/"$SITE_NAME"
echo "Setting up virtual hosts..."
VIRTUAL_HOST="<VirtualHost *:80>
ServerName $SITE_NAME.test
ServerAdmin webmaster@$SITE_NAME.test
DocumentRoot $SITES_DIRECTORY/$SITE_NAME
<Directory \"$SITES_DIRECTORY/$SITE_NAME\">
#Require local
Order allow,deny
Allow from all
AllowOverride all
# New directive needed in Apache 2.4.3:
Require all granted
</Directory>
ErrorLog \${APACHE_LOG_DIR}/$SITE_NAME-error.log
CustomLog \${APACHE_LOG_DIR}/$SITE_NAME-access.log combined
</VirtualHost>"
echo "$VIRTUAL_HOST" | sudo tee -a "$SITE_CONFIG_PATH"
SSL_VIRTUAL_HOST="<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerName $SITE_NAME.test
ServerAdmin webmaster@$SITE_NAME.test
DocumentRoot $SITES_DIRECTORY/$SITE_NAME
<Directory \"$SITES_DIRECTORY/$SITE_NAME\">
#Require local
Order allow,deny
Allow from all
AllowOverride all
# New directive needed in Apache 2.4.3:
Require all granted
</Directory>
ErrorLog \${APACHE_LOG_DIR}/$SITE_NAME-error.log
CustomLog \${APACHE_LOG_DIR}/$SITE_NAME-access.log combined
SSLEngine on
SSLCertificateFile $SSL_CERTS_DIRECTORY/$SITE_NAME.test.pem
SSLCertificateKeyFile $SSL_CERTS_DIRECTORY/$SITE_NAME.test-key.pem
<FilesMatch \"\.(cgi|shtml|phtml|php)\$\">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
</IfModule>"
echo "$SSL_VIRTUAL_HOST" | sudo tee -a "$SSL_SITE_CONFIG_PATH"
echo "Enabling virtual hosts..."
a2ensite "$SITE_NAME".conf
a2ensite "$SITE_NAME"-ssl.conf
echo "Add hosts record.."
echo "127.0.0.1 $SITE_NAME.test" >> /etc/hosts
echo "Creating database.."
mysql -uroot -ppassword --execute="CREATE DATABASE $SITE_NAME;"
echo "Add certs.."
runuser -l jonathan -c "cd $SSL_CERTS_DIRECTORY && mkcert $SITE_NAME.test"
echo "Restarting Apache..."
service apache2 restart
echo "Done."
@jonathanbossenger

This comment has been minimized.

Copy link
Owner Author

@jonathanbossenger jonathanbossenger commented Nov 8, 2019

Last piece of the puzzle, adding the mkcert steps:

Because this script is run as root (typically via sudo) I need to be able to run the following commands is my user (jonathan)

cd /home/jonathan/ssl-certs/
mkcert $SITE_NAME.test
@jonathanbossenger

This comment has been minimized.

Copy link
Owner Author

@jonathanbossenger jonathanbossenger commented Nov 8, 2019

Per @waclaw try

su - jonathan

Before the mkcert commands

@jonathanbossenger

This comment has been minimized.

Copy link
Owner Author

@jonathanbossenger jonathanbossenger commented Nov 8, 2019

Or

su -c 'cd /home/jonathan/ssl-certs/' jonathan
su -c 'mkcert $SITE_NAME.test' jonathan
@jonathanbossenger

This comment has been minimized.

Copy link
Owner Author

@jonathanbossenger jonathanbossenger commented Feb 6, 2020

Part of the mkcerts problem is that it's installed via homebrew, might be worth trying to build it from source, or install one of the pre built libraries - https://github.com/FiloSottile/mkcert/releases

@jonathanbossenger

This comment has been minimized.

Copy link
Owner Author

@jonathanbossenger jonathanbossenger commented Apr 12, 2020

Discovered runuser command solves mkcert problem.
Added site directory creation.

@haydar

This comment has been minimized.

Copy link

@haydar haydar commented Sep 19, 2020

mkdir -p should be instead of mkdir on line 19 because mkdir creates only a directory. You need a few subdirectory.

@haydar

This comment has been minimized.

Copy link

@haydar haydar commented Sep 20, 2020

Also, I had some changes for everybody to use. Maybe you could interest : My Version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.