Deploying Django on production
There are many ways in which Django can be deployed, and the way to adopt NGINX+UWSGI is one of the more common ones.
Uwsgi Introduction
UWSGI is a Web server that implements protocols such as WSGI Protocol, UWSGI, HTTP, and so on. The role of Httpuwsgimodule in Nginx is to exchange with the UWSGI server.
Be aware of the distinction between the three concepts of WSGI/UWSGI/UWSGI.
- WSGI is a Web server gateway interface. It is a specification of a Web server (such as a server such as NGINX,UWSGI) that communicates with a Web application, such as a program written with the Flask framework.
- Uwsgi is a line protocol rather than a communication protocol, which is commonly used in data communication between UWSGI servers and other network servers.
- UWSGI is a Web server that implements the two Protocols of Uwsgi and WSGI.
- The UWSGI protocol is a UWSGI server-owned protocol used to define the type of transmission information (type of information), each UWSGI packet the first 4byte for the transport information type description, which is two things compared to WSGI.
Uwsgi performance is very high
The main features of UWSGI are as follows
- Ultra-fast Performance
- Low memory footprint (about half of Mod_wsgi measured as apache2)
- Multi-app Management (finally don't have to worry about which port the next app is better-.-)
- Detailed logging capabilities (can be used to analyze app performance and bottlenecks)
- Highly customizable (memory size limit, service Restart after a certain number of times, etc.)
All in all, UWGI is a good deployment, as the Uwsgi author boasts:
If you are searching for a simple wsgi-only server, uWSGI is not for you, but if you are building a real (production-ready) app that need to be rock-solid, fast and easy to distribute/optimize for various load-average, you will pathetically and morbidly fall in love (we hope) with uWSGI.
UWSGI Installation and use
1234 |
# Install the latest stable release: pip install uwsgi # ... or if you want to install the latest LTS (long term support) release, pip install https: / / projects.unbit.it / downloads / uwsgi - lts.tar.gz |
Basic test
Create a file called test.py
:
12345 |
# test.py def application (env, start_response): start_response ( ' OK ' ' Content-type ' ' text/html ' return [b " Hello World " ] # python3 #return [" Hello World "] # Python2 |
Run
1 |
uwsgi - - http : 8000 - - wsgi - file test.py |
Start Django with Uwsgi
1 |
uwsgi - - http : 8000 - - module mysite.wsgi |
You can write parameters to the configuration file.
1234567891011121314151617181920 |
[email protected]
-
ubuntu:~
/
uwsgi
-
test$ more crazye
-
uwsgi.ini
[uwsgi]
http
=
:
9000
#the local unix socket file than commnuincate to Nginx
socket
=
127.0
.
0.1
:
8001
# the base directory (full path)
chdir
=
/
home
/
alex
/
CrazyEye
# Django‘s wsgi file
wsgi
-
file
=
CrazyEye
/
wsgi.py
# maximum number of worker processes
processes
=
4
#thread numbers startched in each worker process
threads
=
2
#monitor uwsgi status
stats
=
127.0
.
0.1
:
9191
# clear environment on exit
vacuum
=
true
|
Start
1 |
/ usr / local / bin / uwsgi crazye - uwsgi.ini |
Nginx Installation Use
12 |
sudo apt - get install nginx sudo / etc / init.d / nginx start # start nginx |
Generate Nginx configuration files for your project
You'll need the uwsgi_params
file, which is available in the nginx
directory of the UWSGI distribution, or from Https://github. com/Nginx/nginx/blob/master/conf/uwsgi_params
Copy it into your project directory. In a moment we'll tell the Nginx to refer to it.
Now create a file called Mysite_nginx.conf, and put the This in it:
12345678910111213141516171819202122232425262728293031323334 |
# mysite_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 will be served on
listen
8000
;
# the domain name it will 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
/
path
/
to
/
your
/
mysite
/
media;
# your Django project‘s media files - amend as required
}
location
/
static {
alias
/
path
/
to
/
your
/
mysite
/
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
/
path
/
to
/
your
/
mysite
/
uwsgi_params;
# the uwsgi_params file you installed
}
}
|
This conf file tells -serve up media and static files from the filesystem, as well as handle requests that req Uire Django ' s intervention. For a large deployment it's considered good practice to let one server handle Static/media files, and another handle Djan Go applications, but for now, this would do just fine.
Symlink to this file from/etc/nginx/sites-enabled so nginx can see it:
1 |
sudo ln - s ~ / path / to / your / mysite / mysite_nginx.conf / etc / nginx / sites - enabled / |
Deploying static Files
Before running Nginx, you had to collect all Django static files in the static folder. first of all you have to edit mysite/settings.py adding:
1 |
STATIC_ROOT = os.path.join(BASE_DIR, "static/" ) |
And then run
1 |
python manage.py collectstatic |
When you start Nginx and Uwsgi, your Django project can achieve high concurrency.
Https://www.cnblogs.com/chenice/p/6921727.html
54025761
Deploy to the Cloud
Https://www.jianshu.com/p/55c3fc8ea9b0
Deploying Django Blogs with Nginx and Gunicorn
https://www.zmrenwu.com/post/20/
Python Django Production Environment deployment