UPDATE: RightScale has released version 13.2 of their server templates. I have updated the scripts to reflect the various changes and bug fixes in the new templates.
RightScale has released a beta server template for Django which makes it incredibly easy to get a server up and running on EC2. One drawback with this server template is that it is configured to work with a load balancer which isn’t always necessary.
To create a custom server template that doesn’t require a load balancer, start with the Base ServerTemplate for Linux (current version is 13.2) (be sure to use the Chef-based template rather the RightScript-based version). Then add the necessary scripts so that your boot and operational configs look like the following:
Now you’ve got a server that runs Apache, mod_wsgi and Django 1.4. It will also pull your code down from GIT or SVN and will install any PIP packages from your requirements.txt file (I prefer to install this stuff as part of the initial server config as shown below, or via a controlled server upgrade rather than automatically via requirements.txt).
But what about missing or outdated software? The following is a sample script that installs and upgrades a whole bunch of stuff. It also reconfigs Apache and runs compress. This was added as the last script in the template’s boot scripts. (Note – this script assumes you’re using the Ubuntu RightImage)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# | |
# Test for a reboot, if this is a reboot just skip this script. | |
# | |
if test "$RS_REBOOT" = "true" ; then | |
echo "Skip software update on reboot." | |
logger -t RightScale "Software update, skipped on a reboot." | |
exit 0 | |
fi | |
# | |
# install and upgrade a bunch of stuff | |
# | |
if [ $RS_DISTRO = ubuntu ]; then | |
# fix sftp issue | |
perl -p -i -e "s/\/usr\/libexec/\/usr\/lib/g" /etc/ssh/sshd_config | |
service ssh restart | |
# upgrade pip | |
yes | pip install –upgrade pip | |
# after updating pip, need to relink it so do_update_code works | |
ln -s /usr/local/bin/pip /usr/bin/pip | |
# install this for pip | |
apt-get install -y libyaml-dev | |
# install these for compressor | |
apt-get install -y default-jre | |
apt-get install -y npm | |
npm install -g less | |
npm install -g coffee-script | |
gem install sass | |
# link these so compress can find them | |
ln -s /usr/local/bin/lessc /usr/bin/lessc | |
ln -s /usr/local/bin/sass /usr/bin/sass | |
ln -s /usr/local/bin/coffee /usr/bin/coffee | |
# install Django beta | |
yes | pip uninstall Django | |
wget https://www.djangoproject.com/download/1.5b2/tarball/ | |
mv index.html Django-1.5b2.tar.gz | |
tar xzvf Django-1.5b2.tar.gz | |
cd Django-1.5b2 | |
python setup.py install | |
cd ../ | |
rm -rf Django-1.5b2 | |
rm Django-1.5b2.tar.gz | |
# install pip packages | |
yes | pip install –upgrade distribute | |
yes | pip install –upgrade MySQL-python | |
yes | pip install django-compressor | |
yes | pip install django-appconf | |
yes | pip install versiontools | |
yes | pip install –upgrade simplejson | |
yes | pip install "BeautifulSoup<4.0" | |
STATIC_DEPS=true | |
yes | sudo pip install lxml | |
yes | pip install odict | |
yes | pip install ply | |
yes | pip install html5lib | |
yes | pip install httplib2 | |
yes | pip install slimit | |
yes | pip install boto | |
yes | pip install oauth2 | |
yes | pip install python-openid | |
yes | pip install PyYAML | |
yes | pip install pytz | |
yes | pip install py-bcrypt | |
yes | pip install yolk | |
# cleanup simplejson (fixes yolk) | |
rm /usr/lib/python2.7/dist-packages/simplejson-2.3.2.egg-info | |
# remove generated wsgi.py in site root | |
rm /home/webapps/$APPLICATION/wsgi.py | |
# update apache config | |
perl -p -i -e "s/$APPLICATION\/wsgi.py/$APPLICATION\/conf\/wsgi.py/g" /etc/apache2/sites-available/http-80-localhost.vhost.conf | |
# copy and compress assets | |
python /home/webapps/$APPLICATION/manage.py collectstatic –noinput -i css -i js | |
python /home/webapps/$APPLICATION/manage.py compress | |
# restart apache | |
service apache2 restart | |
fi | |
logger -t RightScale "Software update completed." |