Ubuntu14.04 + Django1.7.1 + Nginx + uwsgi deployment tutorial-Python tutorial

Source: Internet
Author: User
Tags django server virtualenv
Deployment of django + uwsgi is too painful. The existing tutorials on the Internet seem to have compatibility issues with new versions. Finally, I ran to the uwsgi official website to find the tutorial and finally ran through .. however, the tutorials on the official website seem to be of a guiding nature, and the deployment seems to be a detour. here we will record and streamline the specific content environment:
Ubuntu 14.04 Python 2.7.6 Django 1.7.1 Virtualenv name: test Nginx uwsgi

Assume that the project folder is in/data/www/ts and the settings are saved in./conf.

The code is as follows:


Virtualenv name = test
Domain name = example.com

Deployment of django + uwsgi is too painful. The existing tutorials on the Internet seem to have compatibility issues with new versions. Finally, I ran to the uwsgi official website to find the tutorial and finally ran through ..
However, the tutorials on the official website seem to be of a guiding nature, and the deployment seems to be a detour. here we will record the simplified content.

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

First, you need a uwsgi_params file and put it in the conf folder of the project. Then point to it. The file content is as follows:

The code is 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 named ts_nginx.conf with the following content:

The code is as follows:


# Ts_nginx.conf

# The upstream component nginx needs to connect
Upstream django {
# Server unix: // path/to/your/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 will be served on
Listen 80;
# The domain name it will serve
Server_name .example.com; # substitute your machine's IP address or FQDN
Charset UTF-8;

# Max upload size
Client_max_body_size 75 M; # 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 the conf file to the nginx search directory.

The code is as follows:


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

Prerequisites: set static files in settings of the django project.

The code is as follows:


STATIC_ROOT = OS. path. join (BASE_DIR, "static /")
And then run

Python manage. py collectstatic

After:

The code is as follows:


Service nginx restart

You can see
Http://example.cn: 8000/media/1.gif
(Put a static file in advance)

The subsequent blabla steps are all nonsense. skip here:

The code is as follows:


Grouping uWSGI to run with a. ini file

Ts_uwsgi.ini is in the project Root Directory

The code is 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 be 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, the environment variable setting env requires the conf folder to have init. py, otherwise the conf will not be considered as a module

(Currently, all ports except port 80 can be accessed through the address: Port. I have tested 8000 and 81.80. I don't know why. Tomorrow to be continued)

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.