Django server deployment

Source: Internet
Author: User
Tags django server python mysql virtualenv django tutorial

Django server deployment

Django is the most popular full-stack web framework in Python. In the past, Django applications were deployed using Apache + mod_wsgi, but with Nginx's outstanding performance, django also has a more advanced deployment method. A common deployment solution is Nginx + Gunicorn. Next, I will introduce in detail a complete deployment process and components that meet production conditions. All these components are open-source implementations.

Prerequisites

I suppose you have a basic understanding of Linux and a host with root permissions. the server I use is Ubuntu12.04. you can also select other Linux distributions (such as CentOS and Fedora). The corresponding installation package management method is as follows:apt-getAndyum。

System update
$ sudo apt-get update$ sudo apt-get upgrade
Install MySQL
$ sudo apt-get install mysql-server

During the installation process, you will be prompted to enter the Database Password. After the installation is successful, create a new MySQL user and grant permissions.

# Log on to mysql-uroot-p as an administrator # select mysqluse mysql # create a username and password create user 'test _ user' @ 'localhost' identified by 'Password' # create a database database test_db # grant all permissions for test_user to operate test_db grant all privileges on test_db. * to test_user @ localhost identified by 'Password' # Make all operations effective flush privileges
Install virtualenv to create an independent python environment for the app

Virtualenv can create an independent python environment in the system. Multiple applications are not affected by each other, so that the dependent libraries used by different applications do not conflict with each other (for example, an application is based on Django1.5, another application can use virtualenv to create a new python environment to use Django1.6 ). of course, its installation is also very simple.

sudo apt-get install python-virtualenv
Create and activate a python environment for the app

We create an application under the/webapps directory,

$ Cd/webapps/$ virtualenv hello_djangoNew python executable in hello_django/bin/pythonInstalling Setuptools ....................... ............. done. installing Pip ...................................... ..... done. $ cd hello_django $ source bin/activate (hello_django) $ # note the hello_django before the '$' symbol. This indicates that you are already in the new python execution environment.

Now that the python environment is activated, you can install other libraries such as django in this environment.

(hello_django) $ pip install djangoDownloading/unpacking django  Downloading Django-1.6.1.tar.gz (6.6MB): 6.6MB downloaded  Running setup.py egg_info for package django    warning: no previously-included files matching '__pycache__' found under directory '*'    warning: no previously-included files matching '*.py[co]' found under directory '*'Installing collected packages: django  Running setup.py install for django    changing mode of build/scripts-2.7/django-admin.py from 644 to 755    warning: no previously-included files matching '__pycache__' found under directory '*'    warning: no previously-included files matching '*.py[co]' found under directory '*'    changing mode of /usr/local/bin/django-admin.py to 755Successfully installed djangoCleaning up...

Next, create an empty django project.

(hello_django) $ django-admin.py startproject hello

Test whether the project can run normally in the development mode.

(hello_django) $ cd hello(hello_django) $ python manage.py runserver localhost:80Validating models...0 errors foundJanuary 17, 2014 - 10:34:13Django version 1.6.1, using settings 'hello.settings'Starting development server at http://localhost:80/Quit the server with CONTROL-C.

Now you should be able to access: http: // localhost.

Configure MySQL to work with Django

Django needs to use MySQL as the backend storageMySQL-pythonDatabase adapter, but it depends on the local extension Librarypython-dev,libmysqlclient-devSo install the dependent Library first.

$ sudo apt-get install python-dev libmysqlclient-dev

InstallMySQL-python

(hello_django) $pip install mysql-python

Configure database information in settings. py

DATABASES = {    'default': {        'ENGINE': 'django.db.backends.mysql',        'NAME': 'test_db',        'USER': 'test_user',        'PASSWORD': 'password',        'HOST': 'localhost',        'PORT': '',                      # Set to empty string for default.    }}

Django initializes the database. By default, Django creates some data tables.

(hello_dango) $ python manage.py syncdb
Create system users for Applications

Although DJango has a complete security tracking record, if the application's access to server resources is restricted within its own range, it can avoid unnecessary intrusion hazards, therefore, our web application should use users with limited permissions to run this web application.

Create a user for the application namedhello, Attached to the system group namewebapps.

$ sudo groupadd --system webapps$ sudo useradd --system --gid webapps --home /webapps/hello_django hello 
Gunicorn

In the production environment, we should not use the single-threaded development server that comes with Django. Gunicorn is a good choice.

(hello_django) $ pip install gunicornDownloading/unpacking gunicorn  Downloading gunicorn-0.17.4.tar.gz (372Kb): 372Kb downloaded  Running setup.py egg_info for package gunicornInstalling collected packages: gunicorn  Running setup.py install for gunicorn    Installing gunicorn_paster script to /webapps/hello_django/bin    Installing gunicorn script to /webapps/hello_django/bin    Installing gunicorn_django script to /webapps/hello_django/binSuccessfully installed gunicornCleaning up...

After the installation is successful, you can run the following command to test whether your django application can run on gunicorn.

(hello_django) $ gunicorn hello.wsgi:application --bind 0.0.0.0:8001

Now you should be able to access the Gunicorn server from http: // localhost: 8001. After Gunicorn is installed, write a bash script to make it easier to use. Savebin/gunicorn_start.sh

#! /Bin/bashNAME = 'Hello _ app' # Application name DJANGODIR =/webapps/hello_django/hello # django project directory SOCKFILE =/webapps/hello_django/run/gunicorn. sock # use this sock to communicate with USER = hello # the user group that runs the application = webapps # The NUM_WORKERS GROUP that runs the application = 3 # The number of worker processes used by gunicorn DJANGO_SETTINGS_MODULE = hello. settings # django configuration file DJANGO_WSGI_MODULE = hello. wsgi # wsgi module echo "starting $ NAME as 'whoam'" # activate python virtual runtime environment cd $ DJANGODIRsource .. /bin/activateexport DJANGO_SETTINGS_MODULE = $ DJANGO_SETTINGS_MODULEexport PYTHONPATH = $ DJANGODIR: $ PYTHONPATH # If gunicorn. if the sock directory does not exist, create RUNDIR =$ (dirname $ SOCKFILE) test-d $ RUNDIR | mkdir-p $ RUNDIR # Start Djangoexec .. /bin/gunicorn $ {DJANGO_WSGI_MODULE }: application \ -- name $ NAME \ -- workers $ NUM_WORKERS \ -- user = $ USER -- GROUP = $ GROUP \ -- log-level = debug \ -- bind = unix: $ SOCKFILE

UserhelloYou must grant the permission to the directory of the new application.hello

$ sudo chown -R hello:users /webapps/hello_django$ sudo chmod -R g+w /webapps/hello_django$ sudo chmod u+x bin/gunicorn_start.sh

If you are not a groupusers, Use the following command:

$ sudo usermod -a -G users `whoami`

Now you can switch to the userhelloTo execute this script:

$sudo su - hello$bin/gunicorn_start.shStarting hello_app as hello2014-01-17 15:59:25 [10724] [INFO] Starting gunicorn 18.02014-01-17 15:59:25 [10724] [DEBUG] Arbiter booted2014-01-17 15:59:25 [10724] [INFO] Listening at: unix:/webapps/hello_django/run/gunicorn.sock (10724)2014-01-17 15:59:25 [10724] [INFO] Using worker: sync2014-01-17 15:59:25 [10735] [INFO] Booting worker with pid: 107352014-01-17 15:59:25 [10736] [INFO] Booting worker with pid: 107362014-01-17 15:59:25 [10737] [INFO] Booting worker with pid: 10737^C (CONTROL-C to kill Gunicorn)2014-01-17 15:59:28 [10736] [INFO] Worker exiting (pid: 10736)2014-01-17 15:59:28 [10735] [INFO] Worker exiting (pid: 10735)2014-01-17 15:59:28 [10724] [INFO] Handling signal: int2014-01-17 15:59:28 [10737] [INFO] Worker exiting (pid: 10737)2014-01-17 15:59:28 [10724] [INFO] Shutting down: Master$ exit

-- The number rule set by workers is: 2 * CPUs + 1. Therefore, set the number of processes on a single-core CPU to three.
-- The default name isgunicorn, You can usetopOrpsThe process is uniquely identified.

S Start and monitor using Supervisor

gunicorn_startThe script is now ready. We need to ensure that the system can be automatically started or restarted, because the system may terminate abnormally due to some reasons, and this task is handed over to the supervisor. Its installation is also very simple:

$ sudo apt-get insatll supervisor

After installation/etc/supervisor/conf.d/Create a configuration file under the Directory/etc/supervisor/conf.d/hello.confTo start the monitoring application.

[program:hello]command = /webapps/hello_django/bin/gunicorn_start.sh                 ; Command to start appuser = hello                                                          ; User to run asstdout_logfile = /webapps/hello_django/logs/gunicorn_supervisor.log   ; Where to write log messagesredirect_stderr = true  

Days of file storage creation:

$ mkdir -p /webapps/hello/logs$ touch /webapps/hello_django/logs/gunicorn_supervisor.log

After the configuration is complete, the supervisor reloads the configuration file.

$ sudo supervisorctl rereadhello: available$ sudo supervisorctl updatehello: added process group

You can also check the app status, start, stop, and restart.

$ sudo aupervisorctl status hellohello                RUNNING$ sudo supervisorctl stop hello hello: stopped$ sudo supervisorctl start hello hello: started$sudo supervisorctl restart hello hello:stopedhello:started

Now the application can be restarted automatically after the system is restarted or crashes for some reason.

Nginx

Configure Nginx

$ sudo apt-get install nginx$ sudo /etc/init.d/nginx start
Create an Nginx virtual server to serve Django

Each Nginx virtual server should be/etc/nginx/sites-availableFile description in the directory, in order to make it take effect, you need/etc/nginx/sites-enbledMake a symbolic connection
Create a configuration file/etc/nginx/sites-available/hello, The content is as follows:

upstream hello_app_server {  # fail_timeout=0 means we always retry an upstream even if it failed  # to return a good HTTP response (in case the Unicorn master nukes a  # single worker for timing out).  server unix:/webapps/hello_django/run/gunicorn.sock fail_timeout=0;}server {    listen   80;    server_name localhost;    client_max_body_size 4G;    access_log /webapps/hello_django/logs/nginx-access.log;    error_log /webapps/hello_django/logs/nginx-error.log;    location /static/ {        alias   /webapps/hello_django/static/;    }    location /media/ {        alias   /webapps/hello_django/media/;    }    location / {        # an HTTP header important enough to have its own Wikipedia entry:        #   http://en.wikipedia.org/wiki/X-Forwarded-For        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        # enable this if and only if you use HTTPS, this helps Rack        # set the proper protocol for doing redirects:        # proxy_set_header X-Forwarded-Proto https;        # pass the Host: header from the client right along so redirects        # can be set properly within the Rack application        proxy_set_header Host $http_host;        # we don't want nginx trying to do something clever with        # redirects, we set the Host: header above already.        proxy_redirect off;        # set "proxy_buffering off" *only* for Rainbows! when doing        # Comet/long-poll stuff.  It's also safe to set if you're        # using only serving fast clients with Unicorn + nginx.        # Otherwise you _want_ nginx to buffer responses to slow        # clients, really.        # proxy_buffering off;        # Try to serve static files from nginx, no point in making an        # *application* server like Unicorn/Rainbows! serve static files.        if (!-f $request_filename) {            proxy_pass http://hello_app_server;            break;        }    }    # Error pages    error_page 500 502 503 504 /500.html;    location = /500.html {        root /webapps/hello_django/static/;    }}

Create a symbolic link:

$ sudo ln -s /etc/nginx/sites-available/hello /etc/nginx/sites-enabled/hello

Restart Nginx:

$ sudo /etc/init.d/nginx restart

All the configurations are complete. Now you can see the welcome page of django.

Uninstall a Django Application

If you need to uninstall this project, you can follow the steps below to completely clear

Remove a virtual server from Nginxsites-enabledDirectory:

$ sudo rm /etc/nginx/sites-enabled/hello_django

Restart Nginx:

$ sudo /etc/init.d/nginx restart

If you are not planning to use this project in the future, you cansite-availableDirectory to delete the configuration file

$ sudo rm /etc/nginx/sites-available/hello_django

Stop an application with a Supervisor:

$ sudo supervisorctl stop hello 

Remove the configuration from the control Script directory of the supervisor:

$ sudo rm /etc/supervisor/conf.d/hello.conf

Finally, you can delete the entire application directory:

$ sudo rm -r  /webapps/hello_django
Summary

If you follow this tutorial step by step, the entire directory structure should be as follows:

/Webapps/hello_django/── bin <= virtualenv directory created │ ├ ── activate <= Environment activation script │ ── django-admin.py │ ── gunicorn │ ├ gunicorn_django │ ├ ── gunicorn_start.sh <= use Gunicorn to start the application script │ ── python ├ ── hello <= root directory of the project, add him to PYTHONPATH │ ── manage. py │ ── project_application_1 │ ── project_application_2 │ ── hello <= configuration directory of the Project │ ── _ init __. py │ ── settings. py <= hello. settings-settings module, Gunicorn needs to use │ ── urls. py │ ── wsgi. py <= hello. wsgi-WSGI module, gunicorn uses ├ ── include │ └ ── python2.7->/usr/include/python2.7 ── lib │ ── python2.7 ── lib64->/webapps/hello_django/lib ├ ── logs <= project days directory │ ├ ── gunicorn_supervisor.log │ ── nginx-access.log │ ── nginx-error.log ── media <= User File Upload directory ── run │ ── gunicorn. sock ── static <= static Resource Directory of the project

All the steps have been verified by yourself. If you have any questions during the configuration process, do not hesitate to leave a message for me.

Django1.8 returns the json string and the content of the json string that receives the post.

How to Use Docker components to develop a Django project?

Install Nginx + uWSGI + Django on Ubuntu Server 12.04

Deployment of Django + Nginx + uWSGI

Django tutorial

Build a Django Python MySQL Linux development environment

Django details: click here
Django's: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.