Woah, that's a long title.
Today I wanted to share the little shell script I use for setting up local development sites in a one-liner in the terminal. Be advised that I don't usually write shell scripts, so if you are a pro, and I have made some obvious mistakes, please feel free to give improvements in the comment section. Also, while i have been writing this post, i noticed that Klausi also has a kick ass tutorial and shell script on his blog. Be sure to read that as well, since the article is much more thorough than mine. But since my script is for those even more lazy, I decided to post it anyway.
The idea I had was pretty simple. I constantly have the need to set up a fresh, working copy of drupal in various versions with various modules, i.e for bug testing, prototyping, or just testing a patch on a fresh install. While drush make and installation profiles are both awesome tools for a lot of things, I wanted to install Drupal without making make files and writing .info files, and at the same time generate the virtual host and edit my hosts file. And why not also download and enable the modules i need. For a while I used just
(site-install) on a specified directory for flushing and starting over, but part since I have little experience in writing shell scripts (I said that, right?), I thought, what the hey, let's give that a go. Fun to learn new stuff. On my computer the script is called vhost, but since this is not a describing name, and the script is all about being lazy, let's call it lazy-d for the rest of the article (lazy-d for lazy drupal. Also, it is kind of catchy).
So the basic usage is
For example:
This will download drupal 7 to example.dev in your www directory, create a database and user called example.dev, install Drupal with the site name example.dev, edit your hosts file so you can navigate to example.dev in your webbrowser, and download and enable views, ctools admin_menu and coffee modules.
Running the script like this:
will do the same, but with no modules (so Drupal 7 is default, as you might understand).
You can also use it to download Drupal 8, but the drush site install does not work at the moment (it used to when I wrote the script a couple of weeks back though). Drupal 6 is fully supported.
The script has been tested on ubuntu 12.04 and mac os x 10.7. On mac you may have to do some tweaking (depending on whether you have MAMP or XAMPP or whatever) to the mysql variable at the top. And probably do something else with the a2ensite and apache2ctl restart as well (again, depending on your setup).
The script obviously requires apache, php, mysql and drush (git if downloading drupal 8).
OK, to the code:
All done, indeed. Pretty straight forward code. Known issues: will drop existing database if you specify one that exists. But why would you do that? Don't you know what you are doing?
To finish this off, here is an animated gif called "alien computer":
Sam•Wednesday, May 9th 2012 (about 13 years ago)
Why not use Aegir for local development? http://drupal.org/project/aegir-up
jibran•Wednesday, May 9th 2012 (about 13 years ago)
Nice script. Always wanted to make one but too lazy like this script.
Just little correction.
$DRUSH site-install --db-url=mysql://$1:$1@localhost/$1 --site-name=$1 --account-pass=admin
if [ -n "$3" ]; then
$DRUSH dl $3
$DRUSH en $3
fi
Little suggestion
# Create Drush Alies
DRUSHRC=$DRUSH/../aliases.drushrc.php
echo "" >> $DRUSHRC
echo "\$aliases['$1'] = array (" >> $DRUSHRC
echo " 'root' => '$WWW_ROOT/$DOMAIN'," >>$DRUSHRC
echo " 'uri' => 'http://$1'," >>$DRUSHRC
echo ");" >>$DRUSHRC
echo "" >> $DRUSHRC
eiriksm•Wednesday, May 9th 2012 (about 13 years ago)
@sam: That is also a good alternative. But personally I don't feel like it's right for me, given its dependencies and overhead. Also, when I tried it, it was not nearly as smooth as one might hope for. This script on the other hand is very lightweight (i mean, i already have a LAMP stack, why should I download a 4G VM to develop), and works smoothly for me. Also, as I said, I felt like writing it myself. Always fun. But aegir-up sure is cool, though!
@jibran: Nice suggestion! I'll probably add that to the post later.
eiriksm•Wednesday, May 9th 2012 (about 13 years ago)
Just updated with jibran's suggestion. But added the DRUSHRC variable in the config block, since it theoretically can be different on different setups (or you may want automated aliases separated for some reason). Also added closing and opening php tag, since that most likely will be needed, and a check that the file actually exists. Thanks for the suggestion!
Owen Barton•Thursday, May 10th 2012 (about 13 years ago)
You might also be interested in the new quick-drupal command in Drush 5 (which has been the stable release for a few weeks now), which does a very similar thing in what I think is perhaps an even quicker/lazier/flexier way that lazy-d. One nice benefit is that it doesn't need root access (in fact it doesn't even need Apache or MySQL running) and everything (including the database) is contained in a single directory, for easy clean up. The only potential catch is you need either the php-cgi binary, or php 5.4 installed - on many systems this is already installed, or easy, but in some OS X based stacks it can take a little work.
To see it in action, you may want to look at my screencast at http://civicactions.com/blog/2011/oct/04/drush_screencast_core_quick_dru...
eiriksm•Friday, May 11th 2012 (about 13 years ago)
Cool! Did not know that! Every command with either "quick" or "lazy" in it is a good idea :)