Skip to content

Instantly share code, notes, and snippets.

@sivaa
Created September 26, 2014 04:41
Show Gist options
  • Save sivaa/4f68a2db467a1bcf670b to your computer and use it in GitHub Desktop.
Save sivaa/4f68a2db467a1bcf670b to your computer and use it in GitHub Desktop.

Introduction

Create a sudo user

adduser appuser
passwd appuser
# Provide the password here
/usr/sbin/visudo
# Once it is opened, search for the line "root    ALL=(ALL)       ALL" and add the following line
appuser    ALL=(ALL)       ALL

High level overview using Centos

  • Install dev tools and compilers
  • Install Python 2.7.8
  • Install Python package manager - pip.
  • Install Python environment manager - virtualenv
  • Download source code
  • Install python middleware (uwsgi) and app requirements
  • Install nginx.

Installation

  • Install development tools.
#!shell
sudo yum -y groupinstall "Development tools"
sudo yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel readline-devel tk-devel gdbm-devel db4-devel xz-devel vim htop libevent-devel python-devel wget telnet mysql-devel postgresql-devel
  • Install Python.
#!shell
mkdir Downloads
cd Downloads/

wget http://python.org/ftp/python/2.7.8/Python-2.7.8.tar.xz
tar xf Python-2.7.8.tar.xz
cd Python-2.7.8

./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && sudo make altinstall
sudo cp /usr/lib64/python2.6/lib-dynload/_sqlite3.so /usr/local/lib/python2.7/sqlite3/
  • Install pip, virtualenv
#!shell
cd /tmp
wget https://bootstrap.pypa.io/get-pip.py
sudo /usr/local/bin/python2.7 get-pip.py
  • Add the SSH keys to repo
cd ~
ssh-keygen

cat .ssh/id_rsa.pub

# Copy the output and add it to the repo settings. 
https://bitbucket.org/<username>/fav/admin/deploy-keys


  • Pull the source code
#!shell
sudo mkdir -p /var/www

sudo chown appuser /var/www
cd /var/www
git clone [email protected]:<username>/fav.git
  • Create virtualenv
#!shell
sudo /usr/local/bin/pip install virtualenv
mkdir $HOME/.virtualenvs
virtualenv ~/.virtualenvs/fav

vim ~/.bashrc
# Add next two line to ~/.bashrc
export WORKON_HOME=$HOME/.virtualenvs
. $WORKON_HOME/fav/bin/activate
cd /var/www/fav/fav

source ~/.bashrc
  • Install app dependencies
#!shell
cd /var/www/fav
pip install -r requirements.txt
  • Setting up nginx
#!shell
sudo rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
sudo yum -y install nginx16

sudo chkconfig nginx on
sudo cp /var/www/fav/fav/server_configs/fav_nginx.conf /etc/nginx/conf.d/fav_nginx.conf
sudo chown appuser /etc/nginx/conf.d/fav_nginx.conf

# Open the above file and change the host name accordingly
vim /etc/nginx/conf.d/fav_nginx.conf

sudo service nginx restart

# Disable the firewall in the CentOS. Otherwise all the ports except 22 will be blocked
sudo service iptables save
sudo service iptables stop
sudo chkconfig iptables off

  • Start uwsgi
#!shell
pip install uwsgi
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
sudo mkdir /var/log/uwsgi
sudo touch /var/log/uwsgi/uwsgi.log
sudo chown -R appuser /var/log/uwsgi/ 
sudo chown -R appuser /var/run

sudo ln -s /var/www/fav/fav/server_configs/fav_uwsgi.ini /etc/uwsgi/vassals/
/home/appuser/.virtualenvs/fav/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --post-buffering 1 --env DJANGO_SETTINGS_MODULE=fav.production &

  • Create default groups & assign permissions
cd /var/www/fav/fav
python manage.py syncdb --settings=fav.production
python manage.py collectstatic --settings=fav.production

chmod +x restart.sh

  • Restart server with latest code
#!shell
cd /var/www/fav/fav
./restart.sh (or) production_restart.sh
  • SSL certificate
#!shell

Steps for self-signed ssl installation https://www.digitalocean.com/community/articles/how-to-create-a-ssl-certificate-on-nginx-for-centos-6

# Use the SSL only configuration
sudo cp /var/www/fav/fav/server_configs/fav_nginx_only_ssl.conf /etc/nginx/conf.d/fav_nginx.conf

# Change the server name and certificate paths accordingly.

Post installtion steps

# Change the DEBUG level to False in the production.py settings file

DEBUG = False
TEMPLATE_DEBUG = False

# Update the ALLOWED_HOSTS settings with hostname

ALLOWED_HOSTS = ['hostname']

# Change the database name in the production.py

Install supervisor to restart the server automatically

pip install supervisor
cd /var/www/fav/fav
sudo cp server_configs/supervisord.conf /etc/supervisord.conf
sudo mkdir /etc/supervisord.d/
sudo cp server_configs/supervisord  /etc/rc.d/init.d/supervisord
sudo chmod +x  /etc/rc.d/init.d/supervisord
sudo chkconfig --add supervisord
sudo chkconfig supervisord on
sudo ln -s /home/appuser/.virtualenvs/fav/bin/supervisord /usr/bin/supervisord
sudo ln -s /home/appuser/.virtualenvs/fav/bin/supervisorctl /usr/bin/supervisorctl
sudo service supervisord start
sudo cp server_configs/supervisor_fav.conf /etc/supervisord.d/supervisor_fav.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment