Ubuntu 14.04+django 1.7.1+NGINX+UWSGI Deployment Tutorial

Source: Internet
Author: User
Tags django server virtualenv
Specific environment:
Ubuntu 14.04 Python 2.7.6 Django 1.7.1 virtualenv name:test Nginx Uwsgi

Assume that the project folder is located in the/data/www/ts setting saved in the./conf

Copy the Code code as follows:


Virtualenv name = Test
Domain name = example.com

Django+uwsgi's deployment is too painful. Existing tutorials on the web seem to have compatibility issues with new versions. Finally ran to the UWSGI official online to find the tutorial finally ran through the.
However, the official website of the tutorial seems to be guided by the nature of teaching, deployment of time seems to be very detour, here to record the streamlining of content

Http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html

First, a uwsgi_params file is required and placed in the project's Conf folder. You need to point to it later. The contents of the file are as follows:

Copy the Code code as follows:


Uwsgi_param query_string $query _string;
Uwsgi_param Request_method $request _method;
Uwsgi_param Content_Type $content _type;
Uwsgi_param content_length $content _length;
Uwsgi_param Request_uri $request _uri;
Uwsgi_param path_info $document _uri;
Uwsgi_param document_root $document _root;
Uwsgi_param server_protocol $server _protocol;
Uwsgi_param HTTPS $https If_not_empty;
Uwsgi_param remote_addr $remote _addr;
Uwsgi_param Remote_port $remote _port;
Uwsgi_param server_port $server _port;
Uwsgi_param server_name $server _name;

Create a file called ts_nginx.conf, which reads as follows

Copy the Code code as follows:


# ts_nginx.conf

# The upstream component Nginx needs to connect to
Upstream Django {
# server Unix:///path/to/your/mysite/mysite.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 is served on
Listen 80;
# The domain name it would serve for
server_name. example.com; # 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/data/www/ts/media; # your Django project ' s media files-amend as required
}

location/static {
alias/data/www/ts/static; # your Django project ' s static files-amend as required
}

# Finally, send all Non-media requests to the Django server.
Location/{
Uwsgi_pass Django;
Include/data/www/ts/conf/uwsgi_params; # The Uwsgi_params file you installed
}
}

Connect this conf file to the Nginx search directory.

Copy the Code code as follows:


sudo ln-s/data/www/ts/conf/ts_nginx.conf/etc/nginx/sites-enabled/

Prerequisites: Here to set up the Django project settings inside the static files

Copy the Code code as follows:


Static_root = Os.path.join (Base_dir, "static/")
And then run

Python manage.py collectstatic

After:

Copy the Code code as follows:


Service Nginx Restart

Should be able to see
Http://example.cn:8000/media/1.gif
(Put a static file in advance)

After the BlaBla steps are nonsense, jump to here:

Copy the Code code as follows:


Configuring Uwsgi to run with a. ini file

Ts_uwsgi.ini in the project root directory

Copy the Code code as follows:


[Uwsgi]
# django-related Settings
# The base directory (full path)
ChDir =/data/www/ts
# Django ' s Wsgi file
module = Ts.wsgi
# The Virtualenv (full path)
Home =/root/.envs/test
# process-related Settings
# Master
Master = True
# Maximum number of worker processes
processes = 10
# The socket (use the full path to be safe
Socket = 127.0.0.1:8001
# ... with appropriate Permissions-may is needed
# Chmod-socket = 664
# Clear Environment on exit
Vacuum = True
# Set an environment variable
env = Django_settings_module=conf.settings
Uwsgi--ini Mysite_uwsgi.ini # The--ini option is used to specify a file

Here environment variable set env need Conf folder has init.py, otherwise conf will not be considered module

(Currently, in addition to port 80, other ports can be accessed via the address: port.) Have tested 8000,81.80 test do not know why not. To be continued tomorrow)

  • 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.