Nginx + uwsgi or fastcgi deploy the Django project, nginxdjango

Source: Internet
Author: User

Nginx + uwsgi or fastcgi deploy the Django project, nginxdjango
Nginx + uwsgi

Install the C compiler and Python environment in ubuntu:

sudo apt-get install build-essential python-dev

Use pip to install uwsgi:

pip install uwsgi

Nginx Configuration:

You can set a configuration file for the site separately:

sudo vim /etc/nginx/sites-enabled/mysite

You can also directly set it in nginx. conf:

sudo vim /etc/nginx/nginx.conf

Settings:

Server {listen 80; # listen for ipv4; this line is default and implied # listen [:]: 80 default listen 6only = on; # listen for ipv6 server_name site domain name; location/{uwsgi_pass 127.0.0.1: 8080; include uwsgi_params;} # Set the root directory of the static files required for the website application, you need to put the static files of admin and third-party libraries used, such as restframework, in this directory location ~ /Static/{root/home/user/mysite/; # Project address # root html; # index index.html index.htm; break;} # Set the location of the root directory of the media file ~ /Media/{root/home/user/mysite/; # root html; # index index.html index.htm; break ;}}

When setting up a localhost server on your computer, do not overwrite the configuration in/etc/nginx/sites-enabled/default. You 'd better comment it out.

Then set uwsgi:

Create the file myfile. ini in the django project:

[Uwsgi] socket = 127.0.0.1: 8080 # Same as uwsgi_pass in nginx configuration chdir =/home/user/mysite/# Project address wsgi-file = mysite/wsgi. pyprocesses = 4 threads = 2 stats = 127.0.0.1: 9191

Here, wsgi-file is an automatically created file of django (version 1.4 or later) project, which contains the following content:

import osos.environ.setdefault("DJANGO_SETTINGS_MODULE", "weixian.settings")from django.core.wsgi import get_wsgi_applicationapplication = get_wsgi_application()

If this file is not available in the django version, you can create it yourself, or set env, module, and pythonpath in myfile. ini:

[uwsgi]socket = 127.0.0.1:8080chdir = /home/user/mysite/pythonpath = ..env = DJANGO_SETTINGS_MODULE=mysite.settingsmodule = django.core.handlers.wsgi:WSGIHandler()processes = 4threads = 2stats = 127.0.0.1:9191

Restart nginx by Configuration:

/usr/sbin/nginx -s reload

Or:

killall -9 nginxnginx -c /etc/nginx/nginx.conf

Start uwsgi:

uwsgi myfile.ini

The INI file can also be set using an xml file.

Add xml support:

sudo apt-get install libxml2-dev

Configure myfile. xml

<uwsgi>    <socket>127.0.0.1:8080</socket>    <chdir>/home/user/mysite/</chdir>    <module>mysite/wsgi</module></uwsgi>

Start:

uwsgi -x myfile.xml
Nginx + fastcgi

Fastcgi needs to install flup:

wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gztar zxvf flup-1.0.2.tar.gzcd flup-1.0.2python setup.py install

Or:

sudo apt-get install  python-flup

Nginx Configuration:

Server {listen 80; server_name localhost; # Set the location ~ /Static/{root/home/user/mysite/; # root html; # index index.html index.htm; break ;}# set the media root directory location ~ /Media/{root/home/user/mysite/; # root html; # index index.html index.htm; break;} # host and port to fastcgi server location/{fastcgi_pass 127.0.0.1: 8080; fastcgi_param PATH_INFO $ response; required REQUEST_METHOD $ request_method; fastcgi_param QUERY_STRING $ query_string; fastcgi_param CONTENT_TYPE $ content_type; required CONTENT_LENGTH $ content_length; required pas S_header Authorization; fastcgi_intercept_errors off;} # Set the browser cache time for these image format files to 30 days, and the css/js cache time to 1 hour location ~. * \. (Gif | jpg | jpeg | png | bmp | swf) $ {expires 30d;} location ~. * \. (Js | css )? $ {Expires 1 h ;}}

Restart nginx and then start fcgi:

python manage.py runfcgi host=127.0.0.1 port=8080 --settings=mysitesettings

OK.

To update the django project,

ps -ef |grep fcgi 

Find the master process number kill and restart fcgi.


After uwsgi is added to/etc/rclocal, python's Django does not exist.

The difference between running in shell and automatic running mainly lies in the environment variables such as PATH. Do you want to check?

When we use nginx and uwsgi to assume django, is nginx working on a reverse proxy server or simply transmitting the uwsgi protocol?

The uwsgi protocol should be transmitted to the uwsgi server.
 

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.