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

Deployment method of django2.2+Daphne+nginx+supervisor production environment

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "deployment method of django2.2+Daphne+nginx+supervisor production environment". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In the previous project, webscoket is used to realize real-time message notification. We separately run a message push service project, which uses django-channels to implement websocket. The project uses asgi protocol, not the original wsgi protocol. When running the project, the prompt is also different from that of wsgi, as shown in the following figure:

The officially recommended asgi server is daphne to handle websocket requests. Here's how to deploy daphne and supervisor.

1. Install and deploy daphne

Daphne was already installed with django-channels when it was installed.

Create a new asgi.py file in the root directory of the project, at the same level as the wsgi file, as follows:

Import osimport djangofrom channels.routing import get_default_applicationos.environ.setdefault ("DJANGO_SETTINGS_MODULE", "project name .settings") django.setup () application = get_default_application ()

two。 Test that the daphne is configured correctly:

Enter the following command to see if the asgi service starts normally:

Daphne-b 0.0.0.0-p 8008 project name. Asgi:application

On the websocket online test site, test whether websocket can establish the link correctly

3. Configure supervisor

The first step is to enter the command pip install supervisor at the terminal to install supervisor

Execute echo_supervisord_conf > / etc/supervisord.conf to generate the configuration file for supervisor

Edit the configuration file and enter the following:

[program:supervisor project name] directory=/dqcy_message # project directory command=daphne-b 0.0.0.0-p 8008 code project name. Asgi:application # startup command autostart=trueautorestart=truestdout_logfile=/tmp/websocket.log # log redirect _ stderr=true

Start the supervisor command: supervisord-c / etc/supervisord.conf

Start or stop the supervisor project service:

Supervisorctl start supervisor Project name supervisorctl stop supervisor Project name

4. Configure nginx

Use nginx to forward websocket to expand the load balancing in the later stage.

Server {listen 80; ip address accessed by the server_name browser

Location / async {proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"

Proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $server_name; proxy_pass http://127.0.0.1:8008/async; # websocket startup port number and routing prefix, which should be consistent with the path configured by routing in the project}

This is the end of the content of "deployment method for django2.2+Daphne+nginx+supervisor production Environment". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 229

*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

Internet Technology

Wechat

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

12
Report