-
-
Save airstrike/3b513e3c24e5c6f91f1fb8f71947a033 to your computer and use it in GitHub Desktop.
Setting up a Python / Django / uwsgi / nginx / psql stack on a Ubuntu 17.04 Codeanywhere container (Code Anywhere)
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 | |
# this assumes you have already upgraded to zesty (17.04) | |
export PROJECT_NAME=atlantis | |
# general clean up | |
echo "export PS1=\"\[\e[00;38m\]\u@\h\[\e[0m\]\[\e[00;00m\]:\[\e[0m\]\[\e[01;35m\]\w\[\e[0m\]\[\e[00;37m\] \$ \"" >> ~/.profile | |
echo "export TERM=screen-256color" >> ~/.profile | |
source ~/.profile | |
sudo apt-get install dialog -y | |
sudo apt-get update -y | |
sudo apt-get upgrade -y | |
sudo locale-gen en_US en_US.UTF-8 | |
sudo update-locale LANG=en_US.UTF-8 | |
export LC_CTYPE="en_US.UTF-8" | |
export LC_MESSAGES="en_US.UTF-8" | |
export LC_ALL="en_US.UTF-8" | |
mkdir ~/projects | |
sudo ln -s ~/projects /projects | |
ln -s ~/projects ~/workspace/projects | |
mkdir ~/.virtualenvs | |
sudo ln -s ~/.virtualenvs ~/virtual | |
sudo ln -s ~/.virtualenvs /virtual | |
ln -s ~/.virtualenvs ~/workspace/virtual | |
# set up the python environment and additional tools | optional: python3-pip python3.4-venv build-essentials | |
# sudo apt-get install python3.6 python3.6-dev python-dev -y | |
mkdir ~/downloads | |
cd ~/downloads | |
sudo apt-get install software-properties-common -y | |
sudo add-apt-repository ppa:jonathonf/python-3.6 -y | |
sudo apt-get update | |
sudo apt-get install python python3-dev build-essential -y --force-yes | |
sudo apt-get install python3.6 -y | |
sudo apt-get install python3.6-dev -y | |
sudo apt-get install python3.6-venv -y | |
wget https://bootstrap.pypa.io/get-pip.py | |
sudo python3.6 get-pip.py | |
sudo ln -s /usr/bin/python3.6 /usr/local/bin/python3 | |
sudo ln -s /usr/local/bin/pip /usr/local/bin/pip3 | |
# set up virtual environments | |
sudo -H pip install virtualenvwrapper | |
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3" >> ~/.profile | |
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.profile | |
source ~/.profile | |
# install postgresql | |
sudo apt-get install postgresql-9.6 postgresql-client-9.6 postgresql-contrib-9.6 -y | |
sudo apt-get install postgresql-server-dev-9.6 -y | |
PSQL="sudo -u postgres psql -c" | |
$PSQL " | |
ALTER ROLE postgres WITH ENCRYPTED PASSWORD '$PROJECT_NAME'; | |
CREATE USER $PROJECT_NAME WITH ENCRYPTED PASSWORD '$PROJECT_NAME'; ALTER ROLE $PROJECT_NAME WITH SUPERUSER; | |
" | |
sudo -u postgres psql -c "update pg_database set datallowconn = TRUE where datname = 'template0';" | |
sudo -u postgres psql -d template0 -c "update pg_database set datistemplate = FALSE where datname = 'template1';" | |
sudo -u postgres psql -c" drop database template1;" | |
sudo -u postgres psql -c "create database template1 with template = template0 encoding = 'UTF8';" | |
sudo -u postgres psql -c "update pg_database set datistemplate = TRUE where datname = 'template1';" | |
sudo -u postgres psql -d template1 -c "update pg_database set datallowconn = FALSE where datname = 'template0';" | |
# install nginx | |
sudo apt-get install nginx -y | |
ln -s /etc/nginx ~/workspace/nginx | |
sudo rm -rf /etc/nginx/sites-enabled/* | |
sudo chmod 666 /etc/nginx/nginx.conf | |
sudo cat << EOT > /etc/nginx/nginx.conf | |
user www-data; | |
worker_processes 4; | |
pid /run/nginx.pid; | |
events { | |
worker_connections 768; | |
} | |
http { | |
sendfile on; | |
tcp_nopush on; | |
tcp_nodelay on; | |
keepalive_timeout 65; | |
types_hash_max_size 2048; | |
include /etc/nginx/mime.types; | |
default_type application/octet-stream; | |
access_log /var/log/nginx/access.log; | |
error_log /var/log/nginx/error.log; | |
gzip on; | |
gzip_disable "msie6"; | |
include /projects/*/nginx.conf; | |
} | |
EOT | |
sudo chmod 644 /etc/nginx/nginx.conf | |
sudo touch /etc/nginx/uwsgi_params | |
sudo chmod 666 /etc/nginx/uwsgi_params | |
sudo cat << EOT > /etc/nginx/uwsgi_params | |
uwsgi_param QUERY_STRING \$query_string; | |
uwsgi_param REQUEST_METHOD \$request_method; | |
uwsgi_param CONTENT_TYPE \$content_type; | |
uwsgi_param CONTENT_LENGTH \$content_length; | |
uwsgi_param REQUEST_URI \$request_uri; | |
uwsgi_param PATH_INFO \$document_uri; | |
uwsgi_param DOCUMENT_ROOT \$document_root; | |
uwsgi_param SERVER_PROTOCOL \$server_protocol; | |
uwsgi_param UWSGI_SCHEME \$scheme; | |
uwsgi_param REMOTE_ADDR \$remote_addr; | |
uwsgi_param REMOTE_PORT \$remote_port; | |
uwsgi_param SERVER_PORT \$server_port; | |
uwsgi_param SERVER_NAME \$server_name; | |
EOT | |
sudo chmod 644 /etc/nginx/uwsgi_params | |
# install uwsgi | |
sudo apt-get install build-essential autoconf uuid-dev libcap-dev libpcre3-dev openssl | |
sudo apt-get install uwsgi uwsgi-src | |
cd ~ | |
PYTHON=python3.6 uwsgi --build-plugin "/usr/src/uwsgi/plugins/python python36" | |
curl -O https://projects.unbit.it/downloads/uwsgi-2.0.14.tar.gz | |
tar zxvf uwsgi-2.0.14.tar.gz | |
cd uwsgi-2.0.14 | |
sudo apt-get install -y | |
make PROFILE=nolang | |
PYTHON=python3.6 ./uwsgi --build-plugin "plugins/python python36" | |
sudo mkdir /usr/local/lib/uwsgi/ | |
sudo cp python*_plugin.so /usr/local/lib/uwsgi/ | |
sudo cp uwsgi /usr/local/bin/uwsgi | |
sudo touch /var/log/uwsgi.log | |
# create template uwsgi app | |
cat << EOT > /projects/uwsgi | |
uwsgi: | |
chdir: /projects/%n | |
master: true | |
threads: 20 | |
socket: /run/%n.sock | |
chmod-socket: 664 | |
chown-socket: www-data:www-data | |
env: DJANGO_SETTINGS_MODULE=%n.settings | |
module: django.core.handlers.wsgi:WSGIHandler() | |
harakiri: 30 | |
uid: www-data | |
gid: www-data | |
app: wsgi | |
module: %n.wsgi:application | |
plugins-dir: /usr/local/lib/uwsgi | |
plugin: python36 | |
venv: /virtual/%n | |
logformat: %[[1;34m{%(status)} %(ctime) %(method) %(uri) %[[1;33m%(addr) %[[1;32m%(uagent) %[[0;0m | |
EOT | |
# create upstart job for uwsgi | |
sudo touch /etc/init/uwsgi.conf | |
sudo chmod 666 /etc/init/uwsgi.conf | |
sudo cat << EOT > /etc/init/uwsgi.conf | |
description "uWSGI Emperor" | |
start on (filesystem and net-device-up IFACE=lo) | |
stop on runlevel [!2345] | |
respawn | |
exec sudo /usr/local/bin/uwsgi --emperor /projects --emperor-nofollow --emperor-pidfile /run/emperor.pid --logto /var/log/uwsgi.log | |
EOT | |
sudo chmod 644 /etc/init/uwsgi.conf | |
# create sample project | |
$PSQL " | |
CREATE DATABASE $PROJECT_NAME WITH ENCODING 'UTF-8' OWNER $PROJECT_NAME; | |
" | |
mkvirtualenv $PROJECT_NAME | |
workon $PROJECT_NAME | |
pip install django psycopg2 | |
cd ~/projects | |
django-admin.py startproject $PROJECT_NAME | |
cd $PROJECT_NAME | |
git init . | |
cat << EOT > .gitignore | |
*.pyc | |
*.swp | |
static/ | |
logs/ | |
EOT | |
mkdir logs | |
touch logs/error.log | |
touch logs/access.log | |
echo "# Welcome to ${PROJECT_NAME}!" > README.md | |
ln -s /projects/uwsgi /projects/$PROJECT_NAME.yml | |
cat << EOT > /projects/$PROJECT_NAME/nginx.conf | |
server { | |
listen 80; | |
listen [::]:80; | |
server_name \$hostname $PROJECT_NAME-YOURNAME.codeanyapp.com; | |
root /projects/$PROJECT_NAME; | |
error_log /projects/$PROJECT_NAME/logs/error.log; | |
access_log /projects/$PROJECT_NAME/logs/access.log; | |
location / { | |
include uwsgi_params; | |
uwsgi_pass unix://run/$PROJECT_NAME.sock; | |
} | |
location -^/(img|js|css)/ { | |
root /projects/$PROJECT_NAME/static; | |
expires 30d; | |
} | |
location = /favicon.ico { | |
log_not_found off; | |
} | |
location /static { | |
alias /projects/$PROJECT_NAME/static; | |
} | |
location /robots.txt { | |
alias /projects/$PROJECT_NAME/robots.txt; | |
} | |
} | |
EOT | |
# create project logs for nginx | |
sudo touch /projects/$PROJECT_NAME/logs/access.log | |
sudo touch /projects/$PROJECT_NAME/logs/error.log | |
git add -A | |
git commit -m "Initial commit" | |
# set up a decent dev environment | |
sudo apt-get install tmux -y | |
touch ~/.bash_aliases | |
ln -s ~/.bash_aliases ~/.aliases | |
cat << EOT > ~/.bash_aliases | |
alias cls="clear" | |
alias td='tmux attach -t dev' | |
alias tp='tmux attach -t production' | |
alias tl='tmux list-sessions' | |
alias tn='tmux new -s \$*' | |
alias ta='tmux attach -t \$*' | |
diskspaceleft() { | |
df -H | grep 'dev\/' | awk '{print \$1 " " \$4 " (" \$5 ")" }' | |
} | |
alias ureload='sudo kill -HUP \`cat /run/emperor.pid\`' | |
EOT | |
source ~/.aliases |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment