Skip to content

Instantly share code, notes, and snippets.

@eiriksm
Created July 27, 2014 16:46
Show Gist options
  • Save eiriksm/d78b651cd6e740d009af to your computer and use it in GitHub Desktop.
Save eiriksm/d78b651cd6e740d009af to your computer and use it in GitHub Desktop.
virtual host ++ drupal
#!/bin/bash
# Configuration
VHOST_CONF=/etc/apache2/sites-available/
ROOT_UID=0
WWW_ROOT=/var/www
MYSQL=`which mysql`
DRUSH=`which drush`
# If you don't use drush aliases, uncomment this.
DRUSHRC=/home/MYUSERNAME/.drush/aliases.drushrc.php
TMP_DIR=/var/www/tempis
SERVER_ADMIN=me@mycompany.com
LOG_DIR=/var/log
# Check if we are running as root
if [ "$UID" -ne "$ROOT_UID" ]; then
echo “You must be root to run this script.”
exit
fi
# Check that a domain name is provided
if [ -n "$1" ]; then
DOMAIN=$1
else
echo “You must provide a name for this site”
echo -n “Run this script like $0 mycoolsite.test.”
exit
fi
# Downloading the correct Drupal version.
cd $WWW_ROOT
if [ -n "$2" ]; then
VERSION=$2
else
VERSION=7
fi
case $VERSION in
7) $DRUSH dl --destination=$TMP_DIR
;;
8) git clone --recursive --branch 8.x http://git.drupal.org/project/drupal.git $1
;;
6) $DRUSH dl drupal-6 --destination=$TMP_DIR
;;
esac
# Move the folder to the correct place. Drupal 8 was cloned directly to the correct place.
if [ "$VERSION" -ne "8" ]; then
cd $TMP_DIR
mv drupal* $WWW_ROOT/$DOMAIN
rm -rf $TMP_DIR
fi
# Create the database and user.
Q0="DROP DATABASE IF EXISTS \`$1\`;"
Q1="CREATE DATABASE IF NOT EXISTS \`$1\`;"
Q2="GRANT ALL ON \`$1\`.* TO '$1'@'localhost' IDENTIFIED BY '$1';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q0}${Q1}${Q2}${Q3}"
# If you want to keep you password in the file, uncomment the following line and edit the password.
# $MYSQL -uroot --password=Mypassword -e "$SQL"
# If you have no password, uncomment the following line to skip the password prompt.
# $MYSQL -uroot -e "$SQL"
# Else, this prompts for password. If you have uncommented any of the above,
# comment this out, please.
echo "Need the MYSQL root password"
$MYSQL -uroot -p -e "$SQL"
echo "<VirtualHost *:80>" >> $VHOST_CONF/$DOMAIN
echo " ServerAdmin $SERVER_ADMIN" >> $VHOST_CONF/$DOMAIN
echo " DocumentRoot $WWW_ROOT/$DOMAIN" >> $VHOST_CONF/$DOMAIN
echo " ServerName $DOMAIN" >> $VHOST_CONF/$DOMAIN
echo " ErrorLog $LOG_DIR/$DOMAIN-error_log" >> $VHOST_CONF/$DOMAIN
echo " CustomLog $LOG_DIR/$DOMAIN-access_log common" >> $VHOST_CONF/$DOMAIN
echo "" >> $VHOST_CONF/$DOMAIN
echo " <Directory \"$WWW_ROOT/$DOMAIN\">" >> $VHOST_CONF/$DOMAIN
echo " AllowOverride All" >> $VHOST_CONF/$DOMAIN
echo " </Directory>" >> $VHOST_CONF/$DOMAIN
echo "</VirtualHost>" >> $VHOST_CONF/$DOMAIN
echo "127.0.0.1 $DOMAIN" >> /etc/hosts
# This will enable the virtual host in apache. If you are on mac, just include
# the vhost directory in the apache config, and uncomment the follwing line.
a2ensite $DOMAIN
# If you are on mac, check if this will really restart your MAMP/XAMPP apache.
# Or just uncomment and restart with the pretty GUI manually.
apache2ctl restart
cd $WWW_ROOT/$DOMAIN
drush si --db-url=mysql://$1:$1@localhost/$1 --site-name=$1 --account-pass=admin
if [ -n "$3" ]; then
drush dl $3
drush en $3
fi
# Create Drush Aliases
if [ -f $DRUSHRC ] && [ -n "$DRUSHRC" ]; then
echo "" >> $DRUSHRC
echo "<?php" >> $DRUSHRC
echo "\$aliases['$1'] = array (" >> $DRUSHRC
echo " 'root' => '$WWW_ROOT/$DOMAIN'," >>$DRUSHRC
echo " 'uri' => 'http://$1'," >>$DRUSHRC
echo ");" >>$DRUSHRC
echo "?>" >> $DRUSHRC
else
echo "No drush alias written."
fi
echo “Done. Navigate to http://$DOMAIN to see your new site. User/pass: admin.”
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment