Step one.
sudo apt-get update
sudo apt-get upgrade
Update first.
The mainstream deployment of Django: nginx+uwsgi+django
Step 2, Install nginx
sudo apt-get install nginx
Install nginx. If you need to install the latest nginx, you need to download the source package from the official website for manual compilation.
The approximate file structure of nginx.
1. Configuration file: /etc/nginx
2. Program: /usr/sbin/nginx
3. Log: /var/log/nginx/access.log - error.log
Step 3, Install uwsgi
sudo apt-get install python3-dev
sudo apt-get install python3-pip
sudo pip3 install uwsgi (before this step, you can replace the PIP source to improve download speed.Create a pip.conf write under ~/.pip
[global]
trusted-host = pypi.douban.com
index-url = http://pypi.douban.com/simple)
Uwsgi is a web server that implements WSGI, uwsgi, http and other protocols.The role of HttpUwsgiModule in Nginx is to exchange with the uWSGI server.
The general flow is client <=>nginx<=>uwsgi<=>Django.Static requests are handled by Nginx itself.Non-static requests are passed to Django via uwsgi and processed by Django to complete a WEB request.
Create a Django test project, django-admin startproject mysite, cd mysite, Python management.py startapp demo1.
Step 4, Test uwsgi
Create a new test file in the mysite directory, nano test.py.
Write in:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return ["Hello World"]
Function:
uwsgi --http :8001 --plugin python --wsgi-file test.py
Access is normal.
Step 5, Test Django
python manage.py runserver 0.0.0.0:8002
Access is normal.
Connect Django and uwsgi.
uwsgi --http:8001 --plugin python --module mysite.wsgi
Access is normal.
Step 6, Configure uwsgi
uwsgi supports booting through multiple profiles, using the ini profile approach here.
New uwsgi:nano uwsgi.ini
# mysite_uwsgi.ini file [uwsgi] socket = 127.0.0.1:3400 # Django-related settings # the django project directory (full path) chdir = /home/ubuntu/mysite # Django's wsgi file module = mysite.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 2 threads = 2 max-requests = 6000 # ... with appropriate permissions - may be needed chmod-socket = 664 # clear environment on exit vacuum = true
Error accessing, invalid request block size: 21573 (max 4096)...skip.
The reason is that the url address exceeds 4096 characters, because we started with a socket, changed the socket of the configuration file to http, or modified the buffer-size.
(It is recommended that you do not make any changes, change to http for testing, and return to socket when nginx is connected)
daemonize = /home/ubuntu/mysite/uwsgi.log
Add this code to the uwsgi.ini file at regular runtime and the access log will be output to uwsgi.log in the background
django is now accessible.
Step 7, Configure nginx
Modify nginx's default profile/etc/nginx/sites-enabled/default
server { # the port your site will be served on listen 80; # the domain name it will serve for server_name 127.0.0.1; # 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 /home/ubuntu/mysite/media; # your Django project's media files - amend as required } location /static { alias /home/ubuntu/mysite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { include uwsgi_params; # the uwsgi_params file you installed uwsgi_pass 127.0.0.1:8001;#This is consistent with the uwsgi configuration file } }
Remember to modify the configuration of uwsgi.ini during the test.
Step 8, Run
Restart nginx and run uwsgi.
Be accomplished
For the time being, continue to supplement the knowledge of nginx, django, uwsgi.
Most of the configurations come from Baidu Search, so we won't paste each source one by one.Life is short.