Quickly Setup A Local Drupal Site Programatically

I finally got around to writing a script to quickly set up a Drupal development site on my laptop. Actually I ran across this post which gave me the push I needed to get it done. I used a lot of the same code, just tweaked it to match my environment.

Setting up a development site is not really difficult but it involves the same steps each time. You have to create a database, download Drupal, set file permissions, create a virtual host, enable the site, etc.

This little script allows me to type "sudo createsite test.com" and then all I have to do is bring up my browser and go to http://dev.test.com/install.php and I'm ready to roll.

This is only a first draft and there are several things I will surely change in due time but for now, it works great and gets the job done quickly.

There are a couple of things to point out, however. First, I have a copy of Drupal 6.20 sitting in my /home/twooten/public_html directory, which is where my development sites also reside. I also have a handful of modules I use over and over already in the correct sites/all/modules folder. These are things such as Devel, CCK, Views, Administration Menu, etc. Also I really love using the Framework theme as my starter theme so I have a copy of that in the sites/all/themes folder.

The second thing I want to point out is the fact that I have the text of the Apache virtual host file actually in the script. I'm sure there is another (better??) way of doing this but hey, it works fine. If you can suggest a better way then feel free to leave a comment.

There are lots of enhancements that could be made to it, such as using Drush to download Drupal or the modules and themes. I also should add some code to write the alias directive into my aliases.drushrc.php file for the new site. I will get around to that eventually.

#!/bin/sh
# Usage: sudo ./createsite.sh sitename.com

ARGUMENTS=1
if [ $# -ne $ARGUMENTS ]
then
echo "Usage: sudo ./createsite sitename.com"
exit;
fi

dbpass=MY-DATABASE-PASSWORD
dbname=`echo $1 | tr -d '.com'`

echo "Setting up dev.$1"
echo "Setup required files/directories"
mkdir /home/twooten/public_html/$1
cp -R /home/twooten/public_html/drupal-6.20/* /home/twooten/public_html/$1
cp /home/twooten/public_html/drupal-6.20/.htaccess /home/twooten/public_html/$1/.htaccess
mkdir /home/twooten/public_html/$1/sites/default/files

echo "Set permissions"
chmod -R 755 /home/twooten/public_html/$1
chown -R twooten /home/twooten/public_html/$1
chmod -R 777 /home/twooten/public_html/$1/sites/default/files

echo "Create database reload mysql privileges"
mysql -uroot -p$dbpass -e "DROP DATABASE IF EXISTS $dbname;"
mysql -uroot -p$dbpass -e "CREATE DATABASE IF NOT EXISTS $dbname;"
mysql -uroot -p$dbpass -e "GRANT USAGE ON $dbname.* TO 'twooten'@'localhost' IDENTIFIED BY '$dbpass';"
mysql -uroot -p$dbpass -e "GRANT ALL PRIVILEGES ON $dbname.* TO 'twooten'@'localhost';"
mysql -uroot -p$dbpass -e "flush privileges;"

echo "Config Drupal settings.php"
echo "\$db_url = 'mysql://root:$dbpass@localhost/$dbname';" >> /home/twooten/public_html/$1/sites/default/settings.php
chmod 777 /home/twooten/public_html/$1/sites/default/settings.php

echo "Set up Apache config file"
echo "" > /etc/apache2/sites-available/dev.$1

cat <<EOF > /etc/apache2/sites-available/dev.$1
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
ServerName dev.$1
DocumentRoot /home/twooten/public_html/$1/
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /home/twooten/public_html/$1/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>
EOF

echo "Enable Apache site"
a2ensite dev.$1

echo "Add domain to host file"
echo "127.0.0.1 dev.$1" >>/etc/hosts

echo "Restart apache"
/etc/init.d/apache2 force-reload

echo "Done"

Feel free to tell me how you would change this.

Contact Me

Feel free to contact me.

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.