Python3.4+uWSGI for Django Website in VirtualEnv (CentOS)

Install Python 3.x

In CentOS, There is no Python3 rpm package in EPEL, RPMFusion, we must install Python 3.x by source.

Download Python 3.4.0, and extract it.

wget https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tar.xz
tar -xvf Python-3.4.0.tar.xz
cd Python-3.4.0

switch to Python-3.4.0, compile&install Python 3.4.0 (You are expected have installed gcc, make and sqlite-devel)

./configure
make
make install

As the result, you can see the path of Python 3.4.0 by command which python3.4

Thanks to Python 3.4.0, we needn’t install PIP.

Install virtualenvwrapper&Django

You can install virtualenvwrapper by PIP.(Require root privileges)

pip install virtualenvwrapper

Don’t forget add two lines to file .bashrc:

VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.4
source /usr/local/bin/virtualenvwrapper.sh
Let it take effect:
source ~/.bashrc

Then, create a New VirtualEnv, and install Django:

mkvirtualenv django_test
pip install django

Let’s create a new Website for test (in virtualenv django_test):

django-admin.py startproject proj
cd proj
python manage.py runserver

Visit port 8000, you can see the welcome page.

Install uWSGI

Download it and extract it:

wget http://projects.unbit.it/downloads/uwsgi-2.0.3.tar.gz
tar -xvf uwsgi-2.0.3.tar.gz
cd uwsgi-2.0.3

Compile it ( before compile, make sure you are in virtualenv django_test with Python 3.4.0 ):

python uwsgiconfig.py --build

Test it (with Django Website proj):

/path/to/uwsgi/uwsgi --http :8000 --chdir /path/to/proj --wsgi-file proj/wsgi.py -H $HOME/.virtualenvs/django_test

Then visit the port 8000, the uWSGI is also worked!

Let’s prepare uWSGI for nginx!

move uwsgi to system path (I cannot find a better way to install uWSGI):

mv /path/to/uwsgi/ /usr/local
#that is, you have folder /usr/local/uwsgi-2.0.3

and create the Directory /usr/etc/uwsgi and the file /usr/etc/uwsgi/django.ini:

mkdir -p /usr/local/etc/uwsgi
cat > /usr/local/etc/uwsgi/django.ini << EOF
[uwsgi]
socket = /tmp/django.sock
pidfile = /tmp/django.pid
chmod-socket = 666
limit-as = 256
processes = 6
max-request = 2000
memory-report = true
enable-threads = true

wsgi-file=proj/wsgi.py

virtualenv=/root/.virtualenvs/django_test
chdir = /path/to/proj

daemonize=/var/log/uwsgi/proj.log
EOF

execute uWSGI:

/usr/local/uwsgi-2.0.3/uwsgi /usr/local/etc/uwsgi/django.ini

Install Nginx

You can install nginx by yum (Require EPEL):

yum install nginx -y

Then, edit /etc/nginx/conf.d/virtual.conf :

server {
    listen       80;
    server_name  _;

    location / {
        include     uwsgi_params;
        uwsgi_pass  unix:///tmp/django.sock;
    }
}

And start nginx:

service nginx start

Then, visit the port 80, you can see the welcome page of Django.

2 thoughts on “Python3.4+uWSGI for Django Website in VirtualEnv (CentOS)”

  1. Its like you read my mind! You appear to know a lot about this,
    like you wrote the book in it or something. I think that you can do with a few pics
    to drive the message home a little bit, but instead of that,
    this is excellent blog. A fantastic read. I will certainly
    be back.

Leave a Reply

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

61 − 57 =