Deploying Django projects on Ubuntu systems based on Nginx and UWSGI

Source: Internet
Author: User
Tags django server virtualenv

1, Nginx
1.1 Installation

sudo apt-get install Nginx
1.2 Start, stop, and restart
Sudo/etc/init.d/nginx start
Sudo/etc/init.d/nginx stop
Sudo/etc/init.d/nginx restart
Or
sudo service nginx start
sudo service nginx stop
sudo service nginx restart
2, UWSGI installation
Using Python pip installation is the simplest:
Apt-get Install Python-dev #不安装这个, the following installation may fail
Pip Install Uwsgi
3, based on UWSGI and Nginx deployment Django
3.1 Basic Tests
3.1.1 Test Uwsgi is normal

Create the test.py file in the root directory of the Django project and add the following source code:
# test.py
DEF application (env, Start_response):
Start_response (' K OK ', [(' Content-type ', ' text/html ')])
return ["Hello World"] # Python2
#return [B "Hello World"] # Python3
Then, Run Uwsgi:
Uwsgi--http:8000--wsgi-file test.py
Parameter meaning:
http:8000: Using the HTTP protocol, Port 8000
Wsgi-file test.py: Loading the specified file test.py
Open the URL below and the browser should show Hello World
http://example.com:8000
If the display is correct, the following 3 links are unobstructed:
The Web client <-> uwsgi <-> Python
3.1.2 Test if the Django project is normal
First, make sure that project itself is normal:
Python manage.py runserver 0.0.0.0:8000
If no problem, use UWSGI to pull project together:
Uwsgi--http:8000--module Mysite.wsgi
Module MYSITE.WSGI: Loading WSGI module
If project is pulled up properly, the following links are available:
The Web client <-> uwsgi <-> Django
configuration and testing of 3.2 nginx
After installing Nginx, if the http://hostname can be opened normally, the following link is unobstructed:
The Web client <-> the Web server
3.2.1 Add Nginx configuration
Copy the Uwsgi_params file to the project folder. Uwsgi_params files can also be downloaded from this page under the/etc/nginx/directory.
Create the file mysite_nginx.conf under the project folder and fill in and modify the following:
# 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 is served on
Listen 8000;
# 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/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 configuration file tells Nginx to pull media and static files from the file system as a service, while the corresponding Django request
Create a connection to this file under the/etc/nginx/sites-enabled directory so that Nginx can use it:
sudo ln-s ~/path/to/your/mysite/mysite_nginx.conf/etc/nginx/sites-enabled/
3.2.2 Deploying a static file
In the Django setting file, add the following line:
Static_root = Os.path.join (Base_dir, "static/")
Then run:
Python manage.py collectstatic
3.2.3 Test Nginx
First restart the Nginx service:
Sudo/etc/init.d/nginx restart
Then check to see if the media file has been pulled up properly:
Add the file meida.png under directory/path/to/your/project/project/media directory, and then access Http://example.com:8000/media/media.png, Perform the next Test after success.
3.2.4 Nginx and Uwsgi and test.py
Execute the code below to test if Nginx can display hello on the page, world
Uwsgi--socket:8001--wsgi-file test.py
To access http://example.com:8000, if Hello World is displayed, the following links are unobstructed:
The Web client <-> the Web server <-> the socket <-> uwsgi <-> Python
4. Replace TCP port with a UNIX socket
Make the following changes to mysite_nginx.conf:
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)
Restart Nginx, and run Uwsgi here
Uwsgi--socket mysite.sock--wsgi-file test.py
Open the http://example.com:8000/and see if it's successful.
If not successful:
Check for Nginx error log (/var/log/nginx/error.log). If the error is as follows:
Connect () to Unix:///path/to/your/mysite/mysite.sock failed (13:permission
Denied
Add socket permissions to run again:
Uwsgi--socket mysite.sock--wsgi-file test.py--chmod-socket=666 # (very permissive)
Or
Uwsgi--socket mysite.sock--wsgi-file test.py--chmod-socket=664 # (more sensible)
5. Running the Django application with USWGI and Nginx (running a Django application with USWGI and Nginx)
If everything above appears normal, the following command can pull up the Django application
Uwsgi--socket mysite.sock--module mysite.wsgi--chmod-socket=664
5.1. Configuring UWSGI to run with a. ini file (use. ini files to configure UWSGI to start the project)
Running Django application every time you run the above command is a real hassle, and using an. ini file can simplify your work by doing the following:
In the application directory, create the file Mysite_uwsgi.ini, fill in and modify the following content:
# Mysite_uwsgi.ini File
[Uwsgi]

# django-related Settings
# The base directory (full path)
ChDir =/path/to/your/project
# Django ' s Wsgi file
module = Project.wsgi
# The Virtualenv (full path)
Home =/path/to/virtualenv

# process-related Settings
# Master
Master = True
# Maximum number of worker processes
processes = 10
# The socket (use the full path to be safe
Socket =/path/to/your/project/mysite.sock
# ... with appropriate Permissions-may is needed
# Chmod-socket = 664
# Clear Environment on exit
Vacuum = True
Now you can pull up the Django application as long as you execute the following command:
Uwsgi--ini Mysite_uwsgi.ini # The--ini option is used to specify a file
5.2. Make Uwsgi startup time the system boots (let Uwsgi self-boot when booting)
Edit the file/etc/rc.local, add the following to this line of code before exit 0:
/usr/local/bin/uwsgi--socket/path/to/mysite.sock--module/path/to/mysite.wsgi--chmod-socket=666


6, several media choose to use Nginx+uwsgi

Digital media technology using NGINX+UWSGI to deploy the Django project is a fancy to its following features:
1. Support High concurrency;
2. Facilitate the management of multi-process, the use of multi-core advantages;
3. Improve performance because the UWSGI protocol has advantages over the WSGI protocol;
4. Security, client access to the Web server needs to go through the reverse proxy server first. This prevents external programs from directly attacking the Web server;
5. Load balancing, the reverse proxy Server can dynamically submit HTTP requests to different Web servers depending on the load of the Web server, provided that there are multiple Web servers;
6. Improve the IO performance of the Web server. The data of an HTTP request, from the client to the server, it takes time, for example, n seconds, if passed directly to the Web server, the Web server needs to let a process block n seconds to receive IO, which will degrade the performance of the Web server. If you use a reverse proxy server, you can improve the performance of your Web server by having the reverse proxy server receive the full HTTP request before sending the request to the Web server. There are still some requests for static files that can be handled directly by the reverse proxy, without having to go through the Web server.

 


Deploying Django projects on Ubuntu systems based on Nginx and UWSGI

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.