Skip to content

Instantly share code, notes, and snippets.

@lricoy
Last active December 26, 2015 04:29
Show Gist options
  • Save lricoy/7094059 to your computer and use it in GitHub Desktop.
Save lricoy/7094059 to your computer and use it in GitHub Desktop.
Ubuntu server setup (nbinx + gunicorn + supervisor + virtualenv)
adduser --gecos "" webadmin && adduser webadmin sudo
adduser --gecos "" lucas && adduser lucas sudo
#!/bin/bash
# Ubuntu locale config
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
source ~/.bashrc
# apt-get
sudo apt-get update -y
sudo apt-get upgrade -y
# Git
sudo apt-get install git -y
# Postgree
sudo apt-get install postgresql postgresql-contrib -y
sudo su - postgres <<HERE
createdb hello
psql -c "CREATE ROLE hello_django WITH ENCRYPTED PASSWORD 'hello_django'; GRANT ALL PRIVILEGES ON DATABASE hello to hello_django;"
HERE
# Virtualenv
sudo apt-get install python-virtualenv -y
sudo mkdir /webapps
cd /webapps
sudo virtualenv hello_django
cd hello_django
sudo chown -R webadmin:webadmin /webapps/
. /webapps/hello_django/bin/activate
pip install django
django-admin.py startproject hello
cd hello
#python manage.py runserver 0.0.0.0:8000
# Config Postgree to work with django
sudo apt-get install libpq-dev python-dev -y
pip install psycopg2
mv hello/settings.py hello/base_settings.py
echo "
from base_settings import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'hello',
'USER': 'hello_django',
'PASSWORD': 'hello_django',
'HOST': 'localhost',
'PORT': '',
}
}" > hello/settings.py
python manage.py syncdb
# Gunicorn
pip install gunicorn
#gunicorn_django --bind 162.243.61.69:8001
# Gunicorn config file
cd /webapps/hello_django/bin
wget https://gist.github.com/lricoy/7093909/raw/gunicorn_start
chmod u+x gunicorn_start
# Supervisor
sudo apt-get install supervisor -y
sudo apt-get install python-dev -y
pip install setproctitle
sudo wget https://gist.github.com/lricoy/7093978/raw/hello.conf
sudo mv ./hello.conf /etc/supervisor/conf.d/hello.conf
sudo mkdir -p /webapps/hello_django/logs/
sudo touch /webapps/hello_django/logs/gunicorn_supervisor.log
sudo chown -R webadmin:webadmin /webapps/
# start supervisor
sudo supervisorctl reread
sudo supervisorctl update
# Nginx
sudo apt-get install nginx -y
sudo service start nginx
# Nginx config file
wget https://gist.github.com/lricoy/7094028/raw/hello.nginxconf
sudo mv ./hello.nginxconf /etc/nginx/sites-available/hello.nginxconf
sudo ln -s /etc/nginx/sites-available/hello.nginxconf /etc/nginx/sites-enabled/hello.nginxconf
# remove default page
sudo rm /etc/nginx/sites-enabled/default
# create logs dirs
sudo mkdir -p /webapps/hello_django/logs/
sudo touch /webapps/hello_django/logs/nginx-access.log
sudo chown -R webadmin:webadmin /webapps/
# restart
sudo service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment