django deployment-in-aws

25
Django Deployment Best Practices Presenter: Premananda Das, Mindfire Solutions Date: 05/02/2015

Upload: mindfire-solutions

Post on 15-Jul-2015

263 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Django Deployment-in-AWS

Django Deployment Best Practices

Presenter: Premananda Das, Mindfire SolutionsDate: 05/02/2015

Page 2: Django Deployment-in-AWS

About me

Connect Me:Facebook: https://www.facebook.com/premanandadLinkedin: https://in.linkedin.com/pub/premananda-das/1a/353/454Twitter: https://twitter.com/premanandaGoogle+: https://plus.google.com/117290494758487835690

Contact Me:Email: [email protected] / [email protected]: mfsi_prem

Zend PHP Certified and Zend Framework Certified developer.

Skills: PHP, Python, Zend Framework, CodeIgniter, CakePHP, Django,JavaScript, jQuery,Mysql, Postgresql, AWS.

Presenter: Premananda Das, Mindfire Solutions

Page 3: Django Deployment-in-AWS

Agenda

Environment setup

Server configuration

Tools

Demo: Deploying in AWS

Q&A

Presenter: Premananda Das, Mindfire Solutions

Page 4: Django Deployment-in-AWS

Environment setup

pip

Virtualenv and Virtualenvwrapper

Django setup

requirements.txt

git

Supervisord

Presenter: Premananda Das, Mindfire Solutions

Page 5: Django Deployment-in-AWS

Environment setup > pip

Its a tool for installing Python packages from PyPI.Install: sudo apt-get install python-pip

Example:$ pip install PackageName # latest version$ pip install PackageName ==1.0.4 # specific version$ pip install PackageName >=1.0.4' # minimum version$ pip uninstall PackageName

Presenter: Premananda Das, Mindfire Solutions

Page 6: Django Deployment-in-AWS

Environment setup > Virtualenv

Presenter: Premananda Das, Mindfire Solutions

A tool to create isolated Python environments.$ pip install virtualenv

Example:Create Environment: $ virtualenv venvActivate Environment: $ source venv/bin/activate

Install Packages: $ pip install requestsDeactivate Environment: $ deactivate

Virtualenvwrapper is a set of extensions to virtualenv tool. It provides commands to make it easy to work with virtualenv.

Create virtual environment: mkvirtualenv mynewenv List virtual environment: lsvirtualenvRemove a virtual environment: rmvirtualenv myenv

Page 7: Django Deployment-in-AWS

Environment setup > Django

Presenter: Premananda Das, Mindfire Solutions

Install: $ pip install djangoNew project: $ django-admin.py startproject myproject

1. Keep the app name concise - simple and one word.

2. Use flake8 to check coding convention - PEP8.

3. django-dotenv - avoid using local_settings.py.

4. Error handling and logging: logger - error and exception with log rotate.

5. DEBUG = False.

6. DB log error and analyse slow queries.

7. django-debug-toolbar for debugging.

8. Don’t reinvent the wheel.

Page 8: Django Deployment-in-AWS

Environment setup > requirements.txt

Presenter: Premananda Das, Mindfire Solutions

$ pip freeze > requirements.txt$ pip install -r requirements.txt

Page 9: Django Deployment-in-AWS

Environment setup > git

Presenter: Premananda Das, Mindfire Solutions

Ubuntu: sudo apt-get install git

1. Working directory must be kept clean.2. All files should be committed to git.3. Server configs can be kept in git.4. Don’t keep any application and database password in git.5. Follow process: Gitflow Workflow

Page 10: Django Deployment-in-AWS

Environment setup > git > Gitflow Workflow

Presenter: Premananda Das, Mindfire Solutions

Page 11: Django Deployment-in-AWS

Environment setup > Supervisord

Presenter: Premananda Das, Mindfire Solutions

A Process Control System

Install:pip: pip install supervisor —pre Ubuntu: sudo apt-get install supervisor

Config:echo_supervisord_conf > /etc/supervisord.conf

$ sudo supervisorctl reload$ sudo supervisorctl restart uwsgi

Page 12: Django Deployment-in-AWS

Environment setup > Supervisord

Presenter: Premananda Das, Mindfire Solutions

#/etc/supervisor/conf.d/uwsgi.conf

[program:uwsgi]command=/home/ubuntu/.virtualenvs/venv/bin/uwsgi --ini /home/ubuntu/myproject/system/uwsgi/uwsgi.inidirectory=/home/ubuntu/myprojectuser=www-dataautostart=trueautorestart=trueredirect_stderr=truestopsignal=QUITstopasgroup=truekillasgroup=true

Page 13: Django Deployment-in-AWS

Server configuration> Nginx> uWSGI

Presenter: Premananda Das, Mindfire Solutions

The world’s busiest websites use

Page 14: Django Deployment-in-AWS

Server configuration:

Presenter: Premananda Das, Mindfire Solutions

Page 15: Django Deployment-in-AWS

Server configuration > uWSGI

Presenter: Premananda Das, Mindfire Solutions

Install: $ pip install uwsgi

Supervisord conf:[program:uwsgi]command=/home/ubuntu/.virtualenvs/venv/bin/uwsgi --ini /home/ubuntu/myproject/system/uwsgi.inidirectory=/home/ubuntu/myprojectuser=www-dataautostart=trueautorestart=trueredirect_stderr=truestopsignal=QUITstopasgroup=truekillasgroup=true

Page 16: Django Deployment-in-AWS

Server configuration > uWSGI

Presenter: Premananda Das, Mindfire Solutions

[uwsgi]chdir = /home/ubuntu/myprojectmodule = myproject.wsgi# the virtualenv (full path)home = /home/ubuntu/.virtualenvs/venv/#logger = file:/home/ubuntu/myproject/log/uwsgi.log

# process-related settingsmaster = true# maximum number of worker processesprocesses = 3# the socket (use the full path to be safe)socket = /tmp/myproject.sockchmod-socket = 666# clear environment on exitvacuum = true

Page 17: Django Deployment-in-AWS

Server configuration > Nginx

Presenter: Premananda Das, Mindfire Solutions

Install: sudo apt-get install nginx

Process:Start: sudo service nginx startStop: sudo service nginx stopCheck Status: sudo service nginx statusRestart: sudo service nginx restart

Server Config:/etc/nginx/sites-enabled/web-http.conf

server {listen 80;server_name myserver.com;return 301 https://$server_name$request_uri;

}

Page 18: Django Deployment-in-AWS

Server configuration > nginx

Presenter: Premananda Das, Mindfire Solutions

/etc/nginx/sites-enabled/web-https.conf

server {listen 443;server_name myserver.com;root /home/ubuntu/myproject/static;client_max_body_size 4G;

access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;

ssl on;ssl_certificate /etc/nginx/ssl/certs/bundle.crt;ssl_certificate_key /etc/nginx/ssl/certs/myserver_com.key;ssl_session_timeout 5m;ssl_protocols SSLv3 TLSv1;ssl_ciphers

ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;ssl_prefer_server_ciphers on;

Continued…

Page 19: Django Deployment-in-AWS

Server configuration > nginx

Presenter: Premananda Das, Mindfire Solutions

location / {uwsgi_pass django;include /home/ubuntu/myproject/system/uwsgi/uwsgi_params;

}

location /static/ {alias /home/ubuntu/myproject/myproject/static/;

}}

upstream django {server unix:///tmp/guinness.sock; # for a file socket

}

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 HTTPS $https if_not_empty;uwsgi_param REMOTE_ADDR $remote_addr;uwsgi_param REMOTE_PORT $remote_port;uwsgi_param SERVER_PORT $server_port;uwsgi_param SERVER_NAME $server_name;

Page 20: Django Deployment-in-AWS

Tools

Presenter: Premananda Das, Mindfire Solutions

1. New Relic: Server and application monitoring

2. Sentry: Gives insight into the application errors

3. Jenkins for CI, unittest

4. Fabric: Application deployment or system administration tasks

over SSH.

Page 21: Django Deployment-in-AWS

Demo > Deploying in AWS

Presenter: Premananda Das, Mindfire Solutions

Page 22: Django Deployment-in-AWS

Any Questions?

Presenter: Premananda Das, Mindfire Solutions

Page 23: Django Deployment-in-AWS

References Link

Presenter: Premananda Das, Mindfire Solutions

pip: https://pip.pypa.io/en/latest/reference/pip_install.html

git: https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-

workflow

virtualenvwrapper: http://virtualenvwrapper.readthedocs.org/en/latest/

uwsgi: http://uwsgi-

docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html

Supervisord: http://supervisord.org/installing.html

nginx: http://nginx.com/

uWSGI+nginx: https://www.digitalocean.com/community/tutorials/how-to-

deploy-python-wsgi-applications-using-uwsgi-web-server-with-nginx

Sentry: https://getsentry.com/welcome/

New Relic: http://newrelic.com/

Page 24: Django Deployment-in-AWS

Presenter: Premananda Das, Mindfire Solutions

Thank You !!

Page 25: Django Deployment-in-AWS

www.mindfiresolutions.com

https://www.facebook.com/MindfireSolutions

http://www.linkedin.com/company/mindfire-solutions

http://twitter.com/mindfires