Ubuntu 14.04 + Django 1.7.1 + Nginx + uwsgi deployment tutorial, djangong.pdf
Specific 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.
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe 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.
Copy codeThe Code is as follows:
STATIC_ROOT = OS. path. join (BASE_DIR, "static /")
And then run
Python manage. py collectstatic
After:
Copy codeThe 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:
Copy codeThe Code is as follows:
Grouping uWSGI to run with a. ini file
Ts_uwsgi.ini is in the project root directory
Copy codeThe 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)