Django UWSGI Ubuntu 14.04 in Amazon EC2


Here is complete tutorial installing and setup django for production in EC2 Ubuntu 14.04. First, we need to login into EC2, setup a new instance and launch. Then login SSH and ready for action.

1. Always get latest updates

1
sudo apt-get update && sudo apt-get upgrade -y

2. Install Java

1
2
3
4
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

3. Install Python and PostgreSQL

1
sudo apt-get install libpq-dev python-dev postgresql postgresql-contrib nginx git zip unzip build-essential python libxslt1-dev python-pip libmysqlclient-dev imagemagick uwsgi uwsgi-plugin-python uwsgi-plugin-python

4. Setup Django UWSGI

1
2
cd /etc/uwsgi/apps-enabled
sudo vim yourapp.ini

This is some example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[uwsgi]
# enable master process manager
master = true

# spawn 2 uWSGI worker processes
workers = 4

# bind to UNIX socket at /run/uwsgi/<confnamespace>/<confname>/socket
socket = /tmp/myproject.sock

# set mode of created UNIX socket
chmod-socket = 666

# place timestamps into log
log-date = true

# user identifier of uWSGI processes
uid = ubuntu

# group identifier of uWSGI processes
gid = ubuntu

; define variables to use in this script
project = myproject

; number of worker processes
processes = 2

plugins=python
; project-level logging to the logs/ folder

; django >= 1.4 project
chdir = /var/myproject
wsgi-file=/var/myproject/myproject_settings_folder/wsgi.py

; run master process as root
enable-threads = true
processes = 10

vacuum = true
;env = DJANGO_SETTINGS_MODULE=djtest.settings
;module = polatic.wsgi:application
env = DJANGO_SETTINGS_MODULE=myproject.settings
pidfile2 = /tmp/vasham.pid
;harakiri = 20 # respawn processes taking more than 20 seconds
;max-requests = 5000 # respawn processes after serving 5000 requests

REMEMBER! in Ubuntu 14.04, UWSGI WILL NOT WORKING!

Here is the quickfix:

1
2
3
4
sudo pip install uwsgi
cd /usr/bin
sudo mv uwsgi uwsgi-old
sudo ln -s /usr/local/bin/uwsgi uwsgi

4. Setup NGINX

Create a new file in “/etc/nginx/sites-enabled”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
server {                                                                  
    listen 80;                                                            
    server_name yourdomain;                                    
    access_log /var/log/nginx/access.log;                                  
    error_log /var/log/nginx/error.log;                                    
    root /yourpath/yourapp;                                                    
                                                                           
    client_body_timeout 646000;                                            
    client_header_timeout 646000;                                          
    send_timeout 64600;                                                    
    client_max_body_size 30m;                                              
    keepalive_timeout 63000;                                              
    proxy_connect_timeout 66000;                                          
    proxy_send_timeout 66000;                                              
    proxy_read_timeout 66000;                                              
                                                                           
    index  index.php index.html index.htm;                                
                                                                           
    # Check if a file exists at /var/www/domain/ for the incoming request.
    # If it doesn’t proxy to Apache/Django.                                
    try_files $uri @django;                                                
                                                                           
    location /media  {                                                    
         alias /yourpath/yourapp/media;                                        
    }                                                                      
                                                                           
    location /static {                                                    
         alias /yourpath/yourapp/static;                                        
    }                                                                      
                                                                           
    location / {                                                          
        uwsgi_pass unix:/tmp/yourapp.sock;                          
        uwsgi_read_timeout 60000;                                          
        include uwsgi_params;                                              
    }                                                                      
                                                                           
}

5. Setup PostgreSQL

Edit file “/etc/postgresql/9.3/main/pg_hba.conf”. Edit this line and change it “trust” for enable access psql without password from server.

1
2
3
4
5
6
7
# Database administrative login by Unix domain socket
local   all             postgres                                trust

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust

To create new user :

1
2
sudo -i -u postgres
createuser –interactive
1
2
3
4
5
psql
create database YOUR_DATABASE;
ALTER USER your_user WITH PASSWORD ‘somepassword’;
GRANT ALL PRIVILEGES ON DATABASE your_database TO your user;
\q

Done! Now we good to go!


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.