diff options
author | Mark Longair <mhl@pobox.com> | 2012-09-27 21:06:10 +0100 |
---|---|---|
committer | Mark Longair <mhl@pobox.com> | 2012-09-27 21:07:30 +0100 |
commit | faae328530ec403eb864a96cc1b0af734dc82f19 (patch) | |
tree | 9c2b763997c1d705a8f530e1cc6def301ee438f3 | |
parent | 312c3b14fd1683973baca6dcf6b4f95e888064ab (diff) |
Switch install script to use nginx
This commit also reorganizes the files in conf/
-rwxr-xr-x | bin/install-as-user | 60 | ||||
-rwxr-xr-x | bin/pre-install-as-root | 193 | ||||
-rw-r--r-- | conf/apache-vhost.conf.example | 36 | ||||
-rw-r--r-- | conf/crontab.example | 28 | ||||
-rw-r--r-- | conf/httpd.conf-example | 38 | ||||
-rw-r--r-- | conf/nginx.conf.example | 49 | ||||
-rwxr-xr-x | conf/sysvinit-catalyst-fastcgi.example | 53 |
7 files changed, 289 insertions, 168 deletions
diff --git a/bin/install-as-user b/bin/install-as-user index ecd2d99b6..c5e68764b 100755 --- a/bin/install-as-user +++ b/bin/install-as-user @@ -1,10 +1,22 @@ #!/bin/sh set -e +set -x + +DEFAULT_SERVER=false +if [ x"$1" = x"--default" ] +then + DEFAULT_SERVER=true + shift +fi if [ $# -ne 2 ] then - echo "Usage: $0 <UNIX-USER> <HOST>" + cat >&2 <<EOUSAGE +Usage: $0 [--default] <UNIX-USER> <HOST> +--default means to install as the default site for this server, +rather than a virtualhost for HOST. +EOUSAGE exit 1 fi @@ -35,7 +47,12 @@ then exit 1 fi -FMS_DIRECTORY="/var/www/$HOST" +if [ $DEFAULT_SERVER = true ] +then + FMS_DIRECTORY="/var/www/fixmystreet" +else + FMS_DIRECTORY="/var/www/$HOST" +fi FMS_REPOSITORY="$FMS_DIRECTORY/fixmystreet" FMS_LINK_DESTINATION="$HOME/fixmystreet" @@ -48,32 +65,12 @@ TEMPORARY_CRONTAB=$(mktemp) echo crontab file is $TEMPORARY_CRONTAB -cat > $TEMPORARY_CRONTAB <<EOF -# Timed tasks for FixMyStreet. -# -# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. -# Email: matthew@mysociety.org. WWW: http://www.mysociety.org/ - -PATH=/usr/local/bin:/usr/bin:/bin +cp "$FMS_REPOSITORY"/conf/crontab.example "$TEMPORARY_CRONTAB" -5,10,15,20,25,30,35,40,45,50,55 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_REPOSITORY/send-reports.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-reports" || echo "stalled?" -0 0-8,10,11,13,14,16,17,19-23 * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/send-reports.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-reports" || echo "stalled?" -0 9,12,15,18 * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/send-reports.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-reports --verbose" || echo "stalled?" -2 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/send-alerts.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-alerts" || echo "stalled?" -0,30 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/send-questionnaires.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-questionnaires" || echo "stalled?" -5,10,15,20,25,30,35,40,45,50,55 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/send-comments.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-comments" || echo "stalled?" -5,10,15,20,25,30,35,40,45,50,55 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/fetch-comments.lock" "$FMS_REPOSITORY/bin/cron-wrapper fetch-comments" || echo "stalled?" -0,30 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/open311-populate-service-list.lock" "$FMS_REPOSITORY/bin/cron-wrapper open311-populate-service-list" || echo "stalled?" - -# Once an hour, update the all reports stats -13 * * * * "$FMS_REPOSITORY/bin/cron-wrapper" update-all-reports - -# Once a day on all servers -39 2 * * * "$FMS_REPOSITORY/bin/problems-filed-graph" -43 2 * * * "$FMS_REPOSITORY/bin/problem-creation-graph" -00 8 * * * "$FMS_REPOSITORY/bin/check-for-zombies" $UNIX_USER - -EOF +sed -i \ + -e 's,$FMS,'"$FMS_REPOSITORY,g" \ + -e 's,$LOCK_DIR,'"$FMS_DIRECTORY,g" \ + "$TEMPORARY_CRONTAB" crontab $TEMPORARY_CRONTAB @@ -106,6 +103,15 @@ sed -r \ -e "s,^( *CONTACT_EMAIL:).*,\\1 'help@$HOST'," \ conf/general.yml-example > conf/general.yml +# Create the database if it doesn't exist: +if ! psql -l | egrep "^ *$DB_NAME *\|" > /dev/null +then + createdb --owner "$UNIX_USER" "$DB_NAME" + echo 'CREATE LANGUAGE plpgsql;' | psql -U "$UNIX_USER" "$DB_NAME" || true + psql -U "$UNIX_USER" "$DB_NAME" < "$FMS_REPOSITORY"/db/schema.sql + psql -U "$UNIX_USER" "$DB_NAME" < "$FMS_REPOSITORY"/db/alert_types.sql +fi + # Install the required Perl modules - this may take a very long time: cd "$FMS_REPOSITORY" diff --git a/bin/pre-install-as-root b/bin/pre-install-as-root index d0615e370..e460c0e7a 100755 --- a/bin/pre-install-as-root +++ b/bin/pre-install-as-root @@ -12,17 +12,53 @@ # named virtualhost. set -e +set -x -if [ $# -ne 2 ] +DEFAULT_SERVER=false +if [ x"$1" = x"--default" ] then - echo "Usage: $0 <UNIX-USER> <HOST>" + DEFAULT_SERVER=true + shift +fi + +# If we're not running on an EC2 instance, an empty body is returned +# by this request: +EC2_HOSTNAME=`curl -s http://169.254.169.254/latest/meta-data/public-hostname` + +usage_and_exit() { + cat >&2 <<EOUSAGE +Usage: $0 [--default] <UNIX-USER> [HOST] +HOST is only optional if you are running this on an EC2 instance. +--default means to install as the default site for this server, +rather than a virtualhost for HOST. +EOUSAGE exit 1 +} + +if [ $# = 1 ] +then + if [ x = x$EC2_HOSTNAME ] + then + usage_and_exit + else + echo "setting host" + HOST="$EC2_HOSTNAME" + fi +elif [ $# = 2 ] +then + HOST="$2" +else + usage_and_exit fi UNIX_USER="$1" -HOST="$2" -FMS_DIRECTORY="/var/www/$HOST" +if [ $DEFAULT_SERVER = true ] +then + FMS_DIRECTORY="/var/www/fixmystreet" +else + FMS_DIRECTORY="/var/www/$HOST" +fi FMS_REPOSITORY="$FMS_DIRECTORY/fixmystreet" REPOSITORY_URL=git://github.com/mysociety/fixmystreet.git @@ -65,22 +101,6 @@ set_locale() { export LANG="en_GB.UTF-8" } -move_default_virtualhosts() { - # If there are any occurences of /var/www or /var/www/ in - # /etc/apache2/sites-available/(default|default-ssl) change them - # to /var/www/default and /var/www/default/ respectively: - for name in default default-ssl - do - ORIGINAL=/etc/apache2/sites-available/$name - sed -i -r \ - -e 's,(/var/www/)([^A-Za-z0-9]|$),\1default/\2,g' \ - -e 's,(/var/www)([^/A-Za-z0-9]|$),\1/default\2,g' \ - $ORIGINAL - done - mkdir -p /var/www/default - cp /var/www/index.html /var/www/default -} - add_unix_user() { # Create the required user if it doesn't already exist: if id "$1" 2> /dev/null > /dev/null @@ -121,9 +141,6 @@ deb-src http://security.debian.org/ squeeze/updates main non-free # Debian Backports deb http://backports.debian.org/debian-backports squeeze-backports main contrib non-free deb-src http://backports.debian.org/debian-backports squeeze-backports main contrib non-free - -# mySociety repository -deb http://debian.mysociety.org squeeze main EOF else echo Unsupport distribution and version combination $DISTRIBUTION $VERSION @@ -157,96 +174,51 @@ clone_or_update_repository() { fi } -install_apache() { - # Make sure that Apache is installed: - apt-get install -y apache2-mpm-worker libapache2-mod-fastcgi apache2-suexec - - # Actually enable the suexec wrapper: - sed -i -r 's/^( *)#( *FastCgiWrapper.*)/\1\2/' /etc/apache2/mods-available/fastcgi.conf - - # Since this may be run on an EC2 instance with very low memory, - # limit the number of FastCGI processes to 2: - if ! egrep '^ *FastCgiConfig -maxClassProcesses' /etc/apache2/mods-available/fastcgi.conf - then - sed '/<\/IfModule>/i\ - FastCgiConfig -maxClassProcesses 2 - -' /etc/apache2/mods-available/fastcgi.conf - fi - - /etc/init.d/apache2 restart -} - -install_website_packages() { - PACKAGES_FILE="$1/conf/packages.debian-squeeze" - xargs -a "$PACKAGES_FILE" apt-get -y install +install_nginx() { + apt-get install -y nginx libfcgi-procmanager-perl } -add_website_to_apache() { +add_website_to_nginx() { UNIX_USER="$1" HOST="$2" REPOSITORY="$3" - + SITE="$HOST" + if [ $DEFAULT_SERVER = true ] + then + SITE=default + fi + SITE_FILENAME=/etc/nginx/sites-available/"$SITE" + SITE_LINK=/etc/nginx/sites-enabled/"$SITE" + cp $FMS_REPOSITORY/conf/nginx.conf.example $SITE_FILENAME + sed -i "s,/var/www/fixmystreet,$FMS_DIRECTORY," $SITE_FILENAME + if [ $DEFAULT_SERVER = false ] + then + sed -i 's/listen 80/# listen 80/' $SITE_FILENAME + sed "/listen 80/i\ + server_name $HOST; +" /etc/apache2/mods-available/fastcgi.conf + fi + ln -nsf "$SITE_FILENAME" "$SITE_LINK" LOG_DIRECTORY="$(readlink -f $REPOSITORY/../logs)" mkdir -p "$LOG_DIRECTORY" chown -R "$UNIX_USER"."$UNIX_USER" "$LOG_DIRECTORY" + /etc/init.d/nginx restart +} - APACHE_CONFIG_FILE=$REPOSITORY/conf/httpd.conf - - cp $APACHE_CONFIG_FILE-example $APACHE_CONFIG_FILE - - cat > /etc/apache2/sites-available/"$HOST" <<EOF -<VirtualHost *:80> - ServerName $HOST - DocumentRoot $REPOSITORY/web/ - - # Pull in the specific config - Include $APACHE_CONFIG_FILE - - SuexecUserGroup $UNIX_USER $UNIX_USER - - <Directory $REPOSITORY/web> - # You also need to enable cgi files to run as CGI scripts. For example: - # on production servers these are run under fastcgi - Options +ExecCGI - AddHandler fastcgi-script .cgi - AllowOverride None - </Directory> - - <Location /admin> - # - # WARNING - enable auth here on production machine - # - </Location> - - Alias /admin/ $REPOSITORY/web-admin/ - - # Set up commonlib jslib directory - Alias /jslib/ $REPOSITORY/commonlib/jslib/ - <Location /jslib> - AddOutputFilter DEFLATE js - Header append Cache-Control "no-transform" - </Location> - - LogLevel info - ErrorLog $LOG_DIRECTORY/error.log - CustomLog $LOG_DIRECTORY/access.log combined - -</VirtualHost> -EOF - - move_default_virtualhosts - - a2ensite $HOST - - a2enmod rewrite - a2enmod proxy_http - a2enmod expires - a2enmod headers - a2enmod suexec - a2enmod fastcgi +install_sysvinit_script() { + SYSVINIT_NAME=fms-catalyst-fastcgi + SYSVINIT_FILENAME=/etc/init.d/$SYSVINIT_NAME + cp $FMS_REPOSITORY/conf/sysvinit-catalyst-fastcgi.example $SYSVINIT_FILENAME + sed -i "s,/var/www/fixmystreet,$FMS_DIRECTORY,g" $SYSVINIT_FILENAME + sed -i "s/^ *USER=.*/USER=$UNIX_USER/" $SYSVINIT_FILENAME + chmod a+rx $SYSVINIT_FILENAME + update-rc.d fms-catalyst-fastcgi start 20 2 3 4 5 . stop 20 0 1 6 . + /etc/init.d/$SYSVINIT_NAME restart +} - /etc/init.d/apache2 restart +install_website_packages() { + PACKAGES_FILE="$1/conf/packages.debian-squeeze" + xargs -a "$PACKAGES_FILE" apt-get -y install } generate_locales @@ -263,11 +235,20 @@ clone_or_update_repository $FMS_REPOSITORY chown -R "$UNIX_USER"."$UNIX_USER" "$FMS_DIRECTORY" -install_apache +install_nginx + install_website_packages "$FMS_REPOSITORY" +add_website_to_nginx "$UNIX_USER" "$HOST" "$FMS_REPOSITORY" + add_postgresql_user "$UNIX_USER" -add_website_to_apache "$UNIX_USER" "$HOST" "$FMS_REPOSITORY" +DEFAULT_PARAMETER='' +if [ $DEFAULT_SERVER = true ] +then + DEFAULT_PARAMETER='--default' +fi + +su -l -c "$FMS_REPOSITORY/bin/install-as-user $DEFAULT_PARAMETER '$UNIX_USER' '$HOST'" "$UNIX_USER" -su -l -c "$FMS_REPOSITORY/bin/install-as-user '$UNIX_USER' '$HOST'" "$UNIX_USER" +install_sysvinit_script diff --git a/conf/apache-vhost.conf.example b/conf/apache-vhost.conf.example new file mode 100644 index 000000000..de53a5b6c --- /dev/null +++ b/conf/apache-vhost.conf.example @@ -0,0 +1,36 @@ +# An example Apache virtualhost configuration file. + +# Copyright (c) 2011 UK Citizens Online Democracy. All rights reserved. +# Email: team@mysociety.org +# WWW: http://www.mysociety.org + +<VirtualHost *:80> + ServerName fixmystreet.yourservername + DocumentRoot /home/yourname/fixmystreet/web/ + + # Pull in the specific config + Include /home/yourname/fixmystreet/conf/httpd.conf + + <Directory /home/yourname/fixmystreet/web> + # You also need to enable cgi files to run as CGI scripts. For example: + # on production servers these are run under fastcgi + Options +ExecCGI + AddHandler cgi-script .cgi + AllowOverride None + </Directory> + + <Location /admin> + # + # WARNING - enable auth here on production machine + # + </Location> + + Alias /admin/ /home/yourname/fixmystreet/web-admin/ + + Alias /jslib/ /home/yourname/fixmystreet/commonlib/jslib/ + <Location /jslib> + AddOutputFilter DEFLATE js + Header append Cache-Control "no-transform" + </Location> + +</VirtualHost> diff --git a/conf/crontab.example b/conf/crontab.example new file mode 100644 index 000000000..f33e949e8 --- /dev/null +++ b/conf/crontab.example @@ -0,0 +1,28 @@ +# Timed tasks for FixMyStreet. + +# This is an example crontab that you may want to use as a basis for +# one on your own server. You should replace $FMS with the path to the +# clone of the FixMyStreet repository that you are using. You should +# also replace $LOCK_DIR with a writeable directory for the lock files. + +# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. +# Email: matthew@mysociety.org. WWW: http://www.mysociety.org/ + +PATH=/usr/local/bin:/usr/bin:/bin + +5,10,15,20,25,30,35,40,45,50,55 * * * * "$FMS/commonlib/bin/run-with-lockfile.sh" -n "$FMS/send-reports.lock" "$FMS/bin/cron-wrapper send-reports" || echo "stalled?" +0 0-8,10,11,13,14,16,17,19-23 * * * "$FMS/commonlib/bin/run-with-lockfile.sh" -n "$LOCK_DIR/send-reports.lock" "$FMS/bin/cron-wrapper send-reports" || echo "stalled?" +0 9,12,15,18 * * * "$FMS/commonlib/bin/run-with-lockfile.sh" -n "$LOCK_DIR/send-reports.lock" "$FMS/bin/cron-wrapper send-reports --verbose" || echo "stalled?" +2 * * * * "$FMS/commonlib/bin/run-with-lockfile.sh" -n "$LOCK_DIR/send-alerts.lock" "$FMS/bin/cron-wrapper send-alerts" || echo "stalled?" +0,30 * * * * "$FMS/commonlib/bin/run-with-lockfile.sh" -n "$LOCK_DIR/send-questionnaires.lock" "$FMS/bin/cron-wrapper send-questionnaires" || echo "stalled?" +5,10,15,20,25,30,35,40,45,50,55 * * * * "$FMS/commonlib/bin/run-with-lockfile.sh" -n "$LOCK_DIR/send-comments.lock" "$FMS/bin/cron-wrapper send-comments" || echo "stalled?" +5,10,15,20,25,30,35,40,45,50,55 * * * * "$FMS/commonlib/bin/run-with-lockfile.sh" -n "$LOCK_DIR/fetch-comments.lock" "$FMS/bin/cron-wrapper fetch-comments" || echo "stalled?" +0,30 * * * * "$FMS/commonlib/bin/run-with-lockfile.sh" -n "$LOCK_DIR/open311-populate-service-list.lock" "$FMS/bin/cron-wrapper open311-populate-service-list" || echo "stalled?" + +# Once an hour, update the all reports stats +13 * * * * "$FMS/bin/cron-wrapper" update-all-reports + +# Once a day on all servers +39 2 * * * "$FMS/bin/problems-filed-graph" +43 2 * * * "$FMS/bin/problem-creation-graph" +00 8 * * * "$FMS/bin/check-for-zombies" $UNIX_USER diff --git a/conf/httpd.conf-example b/conf/httpd.conf-example index 2cced0537..5d0fead38 100644 --- a/conf/httpd.conf-example +++ b/conf/httpd.conf-example @@ -1,41 +1,9 @@ # Apache configuration for FixMyStreet. # -# Add lines something like this to your /etc/apache2/sites-enabled/fixmystreet - -# replacing '/home/yourname/fixmystreet' with the path to your install +# This file should be included in an Apache <VirtualHost> section. An +# example of such a virtualhost configuration file can be found in the +# file apache-vhost.conf.example in this directory. # -# # FixMyStreet -# <VirtualHost *:80> -# ServerName fixmystreet.yourservername -# DocumentRoot /home/yourname/fixmystreet/web/ -# -# # Pull in the specific config -# Include /home/yourname/fixmystreet/conf/httpd.conf -# -# <Directory /home/yourname/fixmystreet/web> -# # You also need to enable cgi files to run as CGI scripts. For example: -# # on production servers these are run under fastcgi -# Options +ExecCGI -# AddHandler cgi-script .cgi -# AllowOverride None -# </Directory> -# -# <Location /admin> -# # -# # WARNING - enable auth here on production machine -# # -# </Location> -# -# Alias /admin/ /home/yourname/fixmystreet/web-admin/ -# -# Alias /jslib/ /home/yourname/fixmystreet/commonlib/jslib/ -# <Location /jslib> -# AddOutputFilter DEFLATE js -# Header append Cache-Control "no-transform" -# </Location> -# -# </VirtualHost> -# -# # Copyright (c) 2011 UK Citizens Online Democracy. All rights reserved. # Email: team@mysociety.org # WWW: http://www.mysociety.org diff --git a/conf/nginx.conf.example b/conf/nginx.conf.example new file mode 100644 index 000000000..253024ca6 --- /dev/null +++ b/conf/nginx.conf.example @@ -0,0 +1,49 @@ +# An example configuration for running FixMyStreet under nginx. You +# will also need to set up the FixMyStreet Catalyst FastCGI backend. +# An example sysvinit script to help with this is shown given in the file +# sysvinit-catalyst-fastcgi.example in this directory. + +# Copyright (c) 2011 UK Citizens Online Democracy. All rights reserved. +# Email: team@mysociety.org +# WWW: http://www.mysociety.org + +server { + + access_log /var/www/fixmystreet/logs/access.log; + error_log /var/www/fixmystreet/logs/error.log; + + listen 80; + root /var/www/fixmystreet/fixmystreet/web; + error_page 503 /down.html; + + # These rewrite rules are ported from the Apache configuration in + # conf/httpd.conf + + rewrite ^/rss/council/([0-9]+)$ /rss/reports/$1 permanent; + rewrite ^/report$ /reports permanent; + rewrite '^/{/rss/(.*)}$' /rss/$1 permanent; + rewrite '^/reports/{/rss/(.*)}$' /rss/$1 permanent; + rewrite ^/alerts/?$ /alert permanent; + + location /mapit { + proxy_pass http://mapit.mysociety.org/; + proxy_set_header X-Real-IP $remote_addr; + } + + location / { + if (-f $document_root/down.html) { + return 503; + } + try_files $uri @catalyst; + } + location /down.html { + internal; + } + + location @catalyst { + include /etc/nginx/fastcgi_params; + fastcgi_param PATH_INFO $fastcgi_script_name; + fastcgi_param SCRIPT_NAME ''; + fastcgi_pass 127.0.0.1:9000; + } +} diff --git a/conf/sysvinit-catalyst-fastcgi.example b/conf/sysvinit-catalyst-fastcgi.example new file mode 100755 index 000000000..d8e165a2b --- /dev/null +++ b/conf/sysvinit-catalyst-fastcgi.example @@ -0,0 +1,53 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: application-catalyst-fixmystreet +# Required-Start: $local_fs $network +# Required-Stop: $local_fs $network +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Starts the FastCGI app server for the "FixMyStreet" site +# Description: The FastCGI application server for the "FixMyStreet" site +### END INIT INFO + +# This example sysvinit script is based on the helpful example here: +# http://richard.wallman.org.uk/2010/02/howto-deploy-a-catalyst-application-using-fastcgi-and-nginx/ + +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +SITE_HOME=/var/www/fixmystreet +NAME=fms-catalyst-fastcgi +DESC="FixMyStreet app server" +USER=fms + +echo $DAEMON +test -f $DAEMON || exit 0 + +set -e + +start_daemon() { + su -l -c "$SITE_HOME/fixmystreet/bin/cron-wrapper $SITE_HOME/fixmystreet/web/fixmystreet_app_fastcgi.cgi -d -l :9000 -n 2" $USER +} + +stop_daemon() { + pkill -f perl-fcgi -u $USER || true +} + +case "$1" in + start) + start_daemon + ;; + stop) + stop_daemon + ;; + reload|restart|force-reload) + stop_daemon + sleep 5 + start_daemon + ;; + *) + N=/etc/init.d/$NAME + echo "Usage: $N {start|stop|reload|restart|force-reload}" >&2 + exit 1 + ;; +esac + +exit 0 |