Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Step over the pit of deploying Django projects based on uWSGI on Nginx

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

First talk about their relationship, Nginx and uWSGI are Web servers, Nginx is responsible for static content, uWSGI is responsible for dynamic content such as Python, the two work together to provide Web services to improve efficiency and load balancing. UWSGI implements several protocols, such as the WSGI,HTTP protocol, as well as its own uwsgi protocol. For more information about uWSGI and uwsgi protocols, please see here. Similar to fastcgi, the process of request and response is as follows:

Request > Nginx > uWSGI > Django > uWSGI > Nginx > Response

The request is first handed over to Nginx, and if it is static, it is handled by itself, and if it is dynamic, it is handed over to the uWSGI server. The uWSGI server handles the Python code of the entire Django project, responding to the request and returning the original route, but unlike fastcgi, Nginx, uWSGI and Django can be deployed independently and then integrated. So let's start with Django, where the server environment is Ubuntu 16.10.

1. Project that deploys Django

Install Python with versions 2.7 and 3.5 of Python and Django,Ubuntu, and install the corresponding Django version. Note that different versions of Python have corresponding commands in Ubuntu.

Www@cloud-vm-ub01:~$ python-- versionPython 2.7.12 / www. Python3-- versionPython 3.5.2 / www. Pip-Vpip 9.0.1 from / home/wisesoe/.local/lib/python2.7/site-packages (python2.7) www@cloud-vm-ub01:~$ pip3-Vpip 9.0.1 from / home/wisesoe/.local/lib/python3.5/site-packages (python3.5) pip3 install django

Copy the completed Django project pro (pro is the Django project name) to the server. Here, copy to the path of the www user (www is the server login user name). Finally, the relative path is ~ / work/project/pro, and the absolute path is / home/www/project/pro.

Go to the above directory and use Django's built-in server to test to see if the pro project is working properly.

Python3. / manage.py runserver 127.0.0.1:8080

two。 Deploy uWSGI server

Install uWSGI through pip.

Pip3 install uwsgi

To test whether uWSGI is normal, create a Python file uwsgi_test.py for testing in the ~ / work/project/pro directory

Def application (env, start_response): start_response ('200 OK', [(' Content-Type', 'text/html')]) # return [' Hello world'] # Python2 return [b'Hello world'] # Python3

Under the pro project path, run uWSGI based on HTTP protocol. If uWSGI installation is normal, you can access port 9090 in the browser and see the word Hello world.

Uwsgi-- http 127.0.0.1 wsgi-file uwsgi_test.py

Next, start uWSGI to load the Django project. Here you still use the HTTP protocol. Replace the wsgi-file parameter pointing to the specific Python file with the-module parameter pointing to the Django project. The value of the parameter pro.wsgi points to the ~ / work/project/pro/pro/wsgi.py module. If it is normal, you can open the project in the browser http://127.0.0.1:9090 port, but there is a problem with the static file path, but it doesn't matter.

Www@cloud-vm-ub01:~/work/project/pro$ uwsgi-- http 127.0.0.1 module pro.wsgi

For the configuration of uWSGI server, it is very troublesome to add a lot of parameters such as the above command. You can write it as a configuration file. Create a configuration file uwsgi.ini in ~ / work/project/pro, and the comment-out parameter is temporarily ignored. The previous version of Django 1.4 needs to configure parameters such as env,pythonpath, which is no longer discussed here.

The http parameter is used for the above tests, and the socket parameter is required to interact with Nginx. Even if the TCP protocol is used, both WSGI and uwsgi protocols are above the TCP protocol. The socket parameter can also be configured as a network address, such as socket=127.0.0.1:7070, but if Nginx and uWSGI are on the same server, you can use the socket file. The purpose of chmod-socket is to dynamically configure the permissions of the socket file because the socket file is recreated each time uWSGI starts.

[uwsgi] http=127.0.0.1:8000#socket=/home/www/work/project/pro/nginx_uwsgi.socketchdir=/home/www/work/project/pro/#chmod-socket=664master=trueprocesses=4threads=2module=pro.wsgi#wsgi-file=uwsgi_test.py#stats=127.0.0.1:9000

You can also start uWSGI to load the Djiango project with the following command

Uwsgi-ini uwsgi.ini

3. Deploy Nginx server

Install Nginx through apt

Sudo apt install nginx

Nginx can be controlled by the following command. After installing and starting Nginx normally, you can see the welcome page of Nginx through http://127.0.0.1:80

Sudo service nginx startsudo service nginx stopsudo service nginx restart

Next, modify the configuration Nginx configuration to interact with the uWSGI server. The main configuration files of Nginx are in the / etc/nginx/nginx.conf and sites-enabled folders, nginx.conf is the global setting, and the ones in the sites-enabled folder can be configured for different sites. There is a default default configuration file, which is actually a soft link to the default file in the sites-available folder. Sites-avaliable is like a repository, but only those in sites-enabled are valid. We can delete the default of sites-enabled, then cp a default of sites-available to sites-enabled and rename it to nginx-pro, and cp / etc/nginx/uwsgi_params ~ / work/project/pro in case the nginx-pro configuration file calls it.

# nginx-pro

Upstream django {server unix:///home/wisesoe/Work/Project/Python/duty/nginx_uwsgi.sock; # file socket # server 127.0.0.1 file socket 7070; # TCP socket} server {listen 80 default_server; listen [:]: 80 default_server; root / var/www/html; index index.html index.htm index.nginx-debian.html; server_name 127.0.0.1 # IP or FQDN location / static {alias / home/www/work/project/pro/static;} location / {uwsgi_pass django; include / home/www/work/project/pro/uwsgi_params; # try_files $uri $uri/ = 404;}}

Uwsgi_params files are parameters passed from Nginx to uWSGI. Uwsgi_pass means that dynamic content requests are passed to uWSGI through a upstream named django, which uses the file socket method, which is consistent with the previous socket parameter configuration in uwsgi.ini.

4. Nginx permission problem

All the above configuration is complete, but there is also an important permission issue. If you start uWSGI and Nginx (two terminal windows are required below, because the uwsgi command will occupy one), an error will be reported.

Uwsgi-ini uwsgi.inisudo service nginx restart

You will see the word Permission denied in / var/log/nginx/error.log, which means that you do not have read and write permission to the home/www/work/project/pro/nginx_uwsgi.socket file, that is, the user running the Nginx worker process needs read and write permission to the socket file.

The user of the worker process running Nginx is configured in / etc/nginx/nginx.conf, which is the value www-data of user, but looking at / etc/group finds that www-data is a user group

User www-data;worker_processes auto;pid / run/nginx.pid;events {worker_connections 768; # multi_accept on;}

We can add www users to this user group

Usermod-G www-data www

You can also change the user group of the socket file and its parent directory pro to www-data and grant read and write permissions to that user group

Chown: www-data ~ / home/work/project/prochown: www-data ~ / home/work/project/pro/nginx_uwsgi.socketchmod g+rw ~ / home/work/project/pro/nginx_uwsgi.socket

5.Nginx and Django static file processing

The Django project can be opened normally, but there is still a problem with the static file reference path. Django can correctly handle the static file path during Django development, but Nginx can not find the static file path after deployment.

Check the nginx-pro file in the Nginx configuration folder sites-enabled to make sure that the default try_files is deleted or commented out, otherwise Nginx will check to see if the static file exists.

Django has special tools to centralize the static files of Django.

Now add StATIC_ROOT to the Settings file of Django to centralize all the static files under this path

STATIC_ROOT = os.path.join (BASE_DIR, "static/")

Execute a command

Python3. / manage.py collectstatic

In this way, all the static files in the foreground and background of Django will be concentrated in static under the project folder pro, and one of the configurations of nginx-pro, location / static, will allow Nginx to deal with static content.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report