shazow's stack (nginx+uwsgi+postgresql+more)

by shazow
38 deployments · 9 still active · last rev. 3 years ago

Nginx, uWSGI for Python via supervisor.

Optional: PostgreSQL, JPEG/PNG/Freetype PIL fixes.

Create and deploy multiple projects with create_project.sh

(Note: Ubuntu 12.04 LTS support is experimental.)

Compatible with: No distros currently supported
						#!/bin/bash
# Nginx + uwsgi + Python [ + PIL + PostgreSQL (Unstable) ]
# Written by Andrey Petrov (andrey.petrov@shazow.net)
#
# <UDF name="hostname" default="Mynode" label="Server Hostname">
#
# <UDF name="username" label="Your Login Username" example="Your personal login account for the server." default=""/>
#
# <UDF name="password" Label="Your Login Password" example="Also used for the Deploy user."/>
# <UDF name="ssh_key" label="Your SSH Public Key" example="Also used for the Deploy user."/>
#
# <UDF name="do_deploy" label="Create a deploy user and setup nginx + uwsgi?" oneof="Yes,No", default="Yes" />
# <UDF name="deploy_username" Label="Deploy Username" default="deploy" example="This user will contain the virtualenv, Git repository, and own the uWSGI process that serves our code. It will share the same ssh key and password as your login."/>
#
# <UDF name="do_pil" label="Include Python Imaging Library?" oneof="Yes,No", default="Yes" example="With Freetype, PNG, and JPEG support."/>
#
# <UDF name="do_postgresql" label="Include PostgreSQL?" oneof="Yes,No", default="Yes" example="Latest version from ppa:pitti/postgresql backports (9.1a at time of writing)" />
# <UDF name="postgresql_database" Label="PostgreSQL Database" default="" />
# <UDF name="postgresql_user" Label="PostgreSQL Username" default="" />
# <UDF name="postgresql_password" Label="PostgreSQL Password" default="" />
#
# <UDF name="notify_email" Label="Email to notify when ready" default="" />

# FIXME: Make sure all input variables are contained in "$FOO"


source <ssinclude StackScriptID="1"> ## StackScript Bash Library {
    system_update
    postfix_install_loopback_only
## }


source <ssinclude StackScriptID="123"> ## lib-system-ubuntu {
    # Set hostname
    if [ "$HOSTNAME" ]; then
        system_update_hostname "$HOSTNAME"
    fi

    # Add users
    if [ "$USERNAME" ]; then
        system_add_user "$USERNAME" "$PASSWORD" "sudo"
        system_user_add_ssh_key "$USERNAME" "$SSH_KEY"
    fi

    if [ "$DO_DEPLOY" == "Yes" ]; then
        system_add_user "$DEPLOY_USERNAME" "$PASSWORD" "sudo"
        system_user_add_ssh_key "$DEPLOY_USERNAME" "$SSH_KEY"
    fi

    # SSH
    system_sshd_passwordauthentication "no"
    system_sshd_permitrootlogin "no"
    system_sshd_pubkeyauthentication "yes"
## }


# Essentials
aptitude -y install python-software-properties
aptitude -y install build-essential python-dev git-core mailutils


# Install PostgreSQL?
if [ "$DO_POSTGRESQL" == "Yes" ]; then
    add-apt-repository ppa:pitti/postgresql -y
    aptitude update
    aptitude -y install postgresql postgresql-client python-psycopg2

    if [ "$POSTGRESQL_USER" -a "$POSTGRESQL_DATABASE" ]; then
        # Create postgres user
        echo "CREATE ROLE $POSTGRESQL_USER WITH LOGIN ENCRYPTED PASSWORD '$POSTGRESQL_PASSWORD';" | sudo -u postgres psql

        # Create postgres database
        sudo -u postgres createdb --owner "$POSTGRESQL_USER" "$POSTGRESQL_DATABASE"
    fi
fi


# Python environment
aptitude -y install python-setuptools
easy_install pip
pip install virtualenv

## Install supervisor
pip install meld3 # TODO: Get rid of this once the Py27 Meld3 bugfix is in the Ubuntu mainline.
aptitude -y install supervisor


# Install PIL?
if [ "$DO_PIL" == "Yes" ]; then
    aptitude -y install libfreetype6-dev libjpeg62-dev libpng12-dev

    # FIXME: Make this compatible with 64bit?
    ln -s /usr/lib/i386-linux-gnu/{libfreetype.so,libz.so,libjpeg.so} /usr/lib/

    pip install PIL
fi

# Install Nginx + uWSGI
if [ "$DO_DEPLOY" == "Yes" ]; then
    add-apt-repository ppa:uwsgi/release -y
    aptitude update
    aptitude -y install nginx uwsgi-python

    # Nginx-related: Move the default nginx config name so that it doesn't take
    # priority over our other configurations.
    mv /etc/nginx/sites-enabled/{default,99_default}

    # Fetch project deployment-target setup script
    cd /home/$DEPLOY_USERNAME/
    sudo -u $DEPLOY_USERNAME wget "https://gist.github.com/raw/1210041/2259630889a6a2d6323fbb0aff13d5a560d4bb13/create_project.sh"
    sudo -u $DEPLOY_USERNAME chmod +x create_project.sh
fi


# Notify email
if [ "$NOTIFY_EMAIL" ]; then
    IP_ADDRESS="$(system_primary_ip)"
    mail -s "Linode Stackscript Deployed" $NOTIFY_EMAIL << EOF
Your server is deployed at $IP_ADDRESS.

If you specified the deploy option, you can create a deploy target by ssh'ing in and running:

    ./create_project.sh PROJECT_NAME [DOMAIN]

Happy pushing!
EOF
fi;