How to Deploy Python 3 Django WSGI Applications Using uWSGI Web Server with Nginx on CentOS 7


I use CentOS 7 as the server operating system, and use ssh to access the server by command ssh root@192.**.***.1

Preparing Droplet for Production
In this section, we are going to prepare our virtual server for production (i.e. for deploying our application).
We primarily need:

  • updating the default operating system
  • downloading and installing Python and common Python tools (i.e. pip, virtualenv)
  • creating a virtual environment to contain the application
Updating the default operating system
yum -y update
yum install -y epel-release
Install Python 3.3
In CentOS, There is no Python3 rpm package in EPEL, we need manually install Python 3.3 and we need some pakages before installing Python.
yum install -y openssl-devel sqlite-devel bzip2-devel
yum install -y zlib zlib-devel libjpeg-devel libpng12-devel
yum install git wget curl
installation
wget http://www.python.org/ftp/python/3.3.3/Python-3.3.3.tar.xz
tar -xvf Python-3.3.3.tar.xz && cd Python-3.3.3
./configure
make && make altinstall

Install Pip and Virtualenv
wget https://bootstrap.pypa.io/get-pip.py
python3.3 get-pip.py 
pip install virtualenv

Creating a self-contained Virtual (Python) Environment
now  create a virtual environment
virtualenv --python=python3.3 --no-site-packages env
cd env && source bin/activate

Downloading and Installing uWSGI and some packages
pip install uwsgi
pip install django
pip install pytz pillow


Download our project from bitbucket or github. use Git
git clone https://Lh4cKg@bitbucket.org/Lh4cKg/neweco.git
cd neweco && python manage.py runserver ip_address:8000
visit: http://ip_address:8000 to check.
We can test it by creating a file called test.py:
# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
Run uWSGI
uwsgi --http :8000 --wsgi-file test.py

This should serve a ‘hello world’ message directly to the browser on port 8000. Visit: http://example.com:8000 to check. If so, it means the following stack of components works:
the web client <-> uWSGI <-> Python

Now we want uWSGI to do the same thing, but to run a Django site instead of the test.py module.
uwsgi --http :8000 --module mysite.wsgi



Point your browser at the server; if the site appears, it means uWSGI is able to serve your Django application from your virtualenv, and this stack operates correctly:
the web client <-> uWSGI <-> Django


Now normally we won’t have the browser speaking directly to uWSGI. That’s a job for the webserver, which will act as a go-between.
nginx installation and configuration
yum instlall -y  nginx
systemctl start nginx


And now check that the nginx is serving by visiting it in a web browser on port 80 - you should get a message from nginx: “Welcome to nginx!”. That means these components of the full stack are working together:
the web client <-> the web server  


Now create a file called neweco.conf in /etc/nginx/conf.d/, so nginx can see it .In my case put this in the file:

# the upstream component nginx needs to connect to
upstream django {
    #server unix:///home/neweco/env/neweco/neweco.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name 104.131.72.32; # substitute your machine's IP address or FQDN
    charset     utf-8;
    # max upload size
    client_max_body_size 75M;   # adjust to taste
    # Django media
    location /media  {        alias /home/neweco/env/neweco/media;  # your Django project's media files - amend as required
    }
    location /static {        alias /home/neweco/env/neweco/static2; # your Django project's static files - amend as required
    }
    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;        include     /home/neweco/env/neweco/uwsgi_params; # the uwsgi_params file you installed
    }
}




You will need the uwsgi_params file, which is available in the nginx directory of the uWSGI distribution, or from https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
Copy it into your project directory. In a moment we will tell nginx to refer to it.

Just create this script and make it executable
mkdir /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/conf.d/neweco.conf /etc/nginx/sites-enabled/



Before running nginx, you have to collect all Django static files in the static folder. First of all you have to edit neweco/settings.py adding:

STATIC_ROOT = os.path.join(BASE_DIR, "static2/")
and then run
python manage.py collectstatic
Make sure we need to comment the added line after collecting or the django project won't work.
Restart nginx to check that media files are being served correctly, add an image called media.png to the /path/to/your/project/project/media directory, then visit http://example.com:8000/media/media.png - if this works, you’ll know at least that nginx is serving files correctly.

Let’s get nginx to speak to the “hello world” test.py application.
uwsgi --socket :8001 --wsgi-file test.py
Visit: http://example.com:8000/ to check. And this is our stack:
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
Now we can deploy the django app.

uwsgi --socket :8001 --module neweco.wsgi
Create a file called neweco_uwsgi_production.ini:
# neweco_uwsgi_production.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /home/neweco/env/neweco
# Django's wsgi file
module          = neweco.wsgi
# the virtualenv (full path)
home            = /home/neweco/env

# process-related settings
master          = true
pidfile         = /tmp/neweco.pid
# maximum number of worker processes
processes       = 5
# the socket
socket          = :8001
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum           = true
# background the process

daemonize        = /home/neweco/env/neweco/neweco.log

This .ini file also makes sure that the uwsgi can serve the app even outside virtualenv.Now run uswgi using this file:
uwsgi --ini neweco_uwsgi_production.ini

Once again, test that the Django site works as expected. And we can manage uwsgi process in a more gentle way:
uwsgi --stop /tmp/neweco.pid
uwsgi --reload /tmp/neweco.pid

check uWSGI and nginx documentation
now restart nginx server
systemctl restart nginx
visit: http://ip_address/ or http://mysite_domain.com to check.
django app is successfully deployed!
SHARE

About Lasha Gogua

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment