Skip to content

Instantly share code, notes, and snippets.

@ivanrvpereira
Last active August 29, 2015 14:08
Show Gist options
  • Save ivanrvpereira/8220f4ad9e7283d3a583 to your computer and use it in GitHub Desktop.
Save ivanrvpereira/8220f4ad9e7283d3a583 to your computer and use it in GitHub Desktop.
Simple script to automate debian + nginx + uwsgi emperor vassals + mysql
#!/bin/sh
# Author: Ivan Pereira
# Useful guide: http://bradenmacdonald.com/blog/2013/hosting-django-apps-ubuntu-nginx-uwsgi
# Mysql: needs to have .my.cnf on home folder with credentials
read -p "name of app? " APPNAME
echo $APPNAME
useradd -d /home/$APPNAME -G www-data -m -U -s /bin/bash $APPNAME
H="/home/$APPNAME"
echo $H
su - $APPNAME -c "mkdir $H/.ssh && chmod 700 $H/.ssh && touch $H/.ssh/authorized_keys"
su - $APPNAME -c "mkdir $H/uwsgi && ln -s /etc/uwsgi/$APPNAME.ini $H/uwsgi/config"
su - $APPNAME -c "virtualenv --no-site-packages $H/venv"
echo "[uwsgi]\\nsocket = $H/uwsgi/socket\\nchmod-socket = 664\\nmaster = true\\nprocesses = 2\\nvirtualenv = $H/app/venv\\npythonpath = $H/app/\\nmodule = $APPNAME.wsgi:application\\npidfile2 = $H/uwsgi/pid\\ndaemonize = $H/uwsgi/log" > /etc/uwsgi/$APPNAME.tmp
chown $APPNAME:www-data /etc/uwsgi/$APPNAME.tmp
echo "\n\nEdit and copy file to start"
echo "mv /etc/uwsgi/$APPNAME.tmp /etc/uwsgi/$APPNAME.ini\n\n"
echo "Creating /etc/nginx/sites-enabled/$APPNAME"
read -p "Domain? " DOMAIN
cat <<EOT >> /etc/nginx/sites-available/$APPNAME
server {
server_name $DOMAIN;
# Set up django static file serving:
location /static {
alias $H/app/public/media/;
}
location /media {
alias $H/app/public/media/;
}
# pass all non-static requests to uWSGI:
location / {
uwsgi_pass unix://$H/uwsgi/socket;
include uwsgi_params;
}
}
EOT
echo "linking nginx..."
ln -s /etc/nginx/sites-available/$APPNAME /etc/nginx/sites-enabled/
echo "Do a nginx -s reload\n"
read -p "Create database? y/n " CREATEDB
if [ "$CREATEDB" = "y" ]; then
PASS=$(openssl rand -base64 20)
echo "Mysql pass: $PASS"
mysql -e "CREATE USER '$APPNAME'@'localhost' IDENTIFIED BY '$PASS'"
mysql -e "CREATE DATABASE $APPNAME"
mysql -e "GRANT ALL PRIVILEGES ON $APPNAME.* TO $APPNAME@localhost IDENTIFIED BY '$PASS'"
fi
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment