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

Resolve all problems with nginx+uwsgi deployment of Django (summary)

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

Share

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

Recently, my summer vacation writing small projects are also finished, thinking of putting them on their own Cloud Virtual Machine, originally thought that as long as you open the port to run python3 manager runserver 0.0.0.0:80 will be done, finally know that this only applies to Django development mode, only support single user access, in this case, then you have to need web server deployment. I used nginx.

nginx?

Why nginx?

First of all, I think it is small, very lightweight, easy to use, not as complex as apache, and the Internet recommends nginx deployment Django.

installation

Skip directly here, say a little Linux users recommend everyone source installation, because the command installation may be installed as a Taobao secondary development nginx, personal or recommended with the original.

uwsgi

Why do we need this?

In short,nginx is a reverse proxy server, what can it do? Listen to a port, such as 80, you can configure a reverse proxy port, such as 8000, so that all external user access to port 80 is actually requesting data on port 8000, but the user is not really communicating with port 8000, but through the bridge 80. At present, I only think that this can hide my real port. Please leave a message if you have any high opinions.

Since this is the case, then it is actually only accessible by a single user, so we need a tool that can be accessed concurrently by multiple users, so uwsgi is it.

How to install?

pip install uwsgi

profile

First let me show you the file status of my project:

FlyCold├── FlyCold│ ├── settings.py│ ├── urls.py│ └── wsgi.py├── manage.py├── SchoolBuy│ ├── admin.py│ ├── forms.py│ ├── __init__.py│ ├── models.py│ ├── urls.py│ └── views.py└── templates

Explain the following, this is a reduced directory tree, created project name FlyCold, generated FlyCold subdirectory and SchoolBuy subdirectory. My main code is in SchoolBuy, setting.py in Flycold subdirectory, and manager.py in FlyCold root directory.

After installation, a configuration file will be sent, which reads as follows

# myweb_uwsgi.ini file[uwsgi]# Django-related settingssocket = :8080#port of real service # Django project root directory (absolute path)chdir = module/home/lyt/FlyCold# wsgi.py file location in the project = FlyCold.wsgi# process-related settings# mastermaster = true#Number of processes running = 4# ... with appropriate permissions - may be needed# chmod-socket = 664# clear environment on exitvacuum = true

This.ini file can be placed anywhere, uwsgi --ini ***.ini at startup

configure nginx

Find nginx.conf and write the following

server { #Here is the port used to access listen 80; server_name localhost; charset UTF-8; #Save this log file access_log /var/log/nginx/SchoolBuy_access.log; error_log /var/log/nginx/SchoolBuy_error.log; client_max_body_size 75M; location / { include uwsgi_params; #Same as uwsgi content uwsgi_pass 127.0.0.1:8001; #Link timeout uwsgi_read_timeout 30; } }

In this way, restart your nginx, access port 80, you can see the effect.

Any more questions?

You may have noticed that static resources on your web page are inaccessible!! For example, the admin page will be particularly simple, this is because when nginx+uwsgi+Django, Django can not proxy nginx for static resources (probably). In short, this kind of thing should not be done by Django, because nginx is more capable of handling static resources. For static resources, let nginx handle it.

Generally, you will have two types of static resources: /media/and/static/. static is used to handle raw images, videos, js,css files on some websites, and Django supports such links on its own. So how to turn off Django processing files that start with/static/is very simple. Change the DEBUG value to False in setting.py, and Django will not process/static/files.

What about/media/? Generally speaking, we will save the pictures uploaded by users. When displaying them on the web page, use/media/and set them in setting.py.

MEDIA_URL = '/media/' #prefix link accessed MEDIA_ROOT = os.path.join(BASE_DIR, '../ media') #The exact location of the file

Add it to url.py

from django.conf import settingsfrom django.conf.urls.static import staticif settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This means that when DEBUG=True, the/media/file will be parsed, and the location of the file is the second parameter.

This way, when deploying to production, you just need to change DEBUG to False and Django will not handle static and media.

Collect static files

Django has a tool that collects all static files used in an application for easy nginx parsing. Specifically:

Set STATIC_ROOT = os.path.join(BASE_DIR, '../) in setting.py. collectedstatic')

The static files collected in this way are placed in the directory above. How to run this tool? python3 manager.py collectstatic

Configure nginx to parse static files

Also, nginx.conf

First, add user root at the top of the file.

Declare to let root user run nginx, otherwise access static files may prompt no permission

Next, add the following strip before the configuration file location /mentioned above

location /static/ { autoindex on; alias /root/SchoolBuyWeb/collectedstatic/; } location /media/ { autoindex on; alias /root/SchoolBuyWeb/media/; }

Note that alias after the corresponding good set of their own directory can be!

Restart nginx, it's ok now ~~

The above is all the content of this article, I hope to help everyone's study, but also hope that everyone a lot of support.

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