[Django] Deploying Django on Linux (nginx+uwsgi)

1. Description

The process of using uwsgi to deploy Django projects on Linux is not difficult, mainly the writing of configuration files, especially nginx configuration files. This article deploys Django projects through uwsgi and nginx on Ubuntu20.04

2. Installation environment

The installation environment mainly includes Nginx, Python, MySQL, and Redis, which can be installed according to your actual situation

2.1 install nginx

Most distributions of Linux support the direct installation of nginx through the package manager. If not, you can only go online to find nginx by compiling the source code. I am installing on Ubuntu 20.04 here, and I can install nginx directly through the apt command

sudo apt update
sudo apt install nginx

After the installation is complete, it will automatically run nginx, visit the IP of the machine ( http://localhostalso available), if you see Welcome to nginx!a page prompt, it means the installation is successful

2.2 install python

Since I use Ubuntu20.04 already comes with Python3.8, so I use Python3.8 directly here, if you need other versions, you can refer to the previous article【
Linux】Linux system to install Python3 and pip3

I just need to install another pip here

sudo apt install python3-pip

After that, install the library you need for the project yourself, at least run the Django project with runnerser

2.3 install mysql

To install MySQL, please refer to
【mysql】Ubuntu18 install MySQL, forget root password

2.4 install redis

If your project needs to install Redis, you can refer to
[redis] redis introduction, installation, and connection to the specified library

3. Collect static files

Open the Django configuration file (usually settings.py), change DEBUGto False, and then STATIC_ROOTwrite the path to collect static files. You can use an absolute path to put it in any authorized location, but it will overwrite existing files, so it is recommended that the path be different STATICFILES_DIRSfrom , here I am/home/pan/productions/static

DEBUG = False
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = '/home/pan/productions/static'

After modifying the configuration file, you can collectstaticcollect static files. This operation is to copy a copy of the static files used in the Django project to the specified folder. The purpose is to facilitate the nginx proxy

python3 manage.py collectstatic

If there is no error, open STATIC_ROOTthe path and you should be able to see the collected static files

4. Use uwsgi to start

4.1 Install uwsgi

First you need to install uwsgi, just use pip to install it

sudo pip install uwsgi

After installation, execute it in the terminal and uwsgiif there is output, it will be fine.

4.2 uwsgi configuration file

Create a new ini file, for example uwsgi.ini, the following is how to write my uwsgi.iniconfiguration file, manage.pyjust put the file in the directory

[uwsgi]
# 启动项目的IP端口,可以使用IP端口也可以使用本地文件
socket = 127.0.0.1:8000
# socket = /path/to/your/mysite/mysite.sock
# 项目目录(一般是manage.py所在目录)
chdir = /home/pan/productions/baiduwp_python/baiduwp_python
# 指定项目里的wsgi.py,绝对路径和相对路径都行
wsgi-file = baiduwp_python/wsgi.py
# 进程数
processes = 1
# 线程数
threads = 2
# 主程序
master = True
# 存放pid文件路径
pidfile = uwsgi.pid
# 存放日志文件路径
daemonize = uwsgi.log
# 缓存大小,如果提示缓存不够就需要改大一点
buffer-size=4095
# 虚拟环境路径(如果使用Python虚拟环境的话)
# virtualenv=/root/.virtualenvs/django_env
# 静态文件的路径,我们已经让nginx处理,所以注释掉
# static-map = /static=/home/pan/productions/static

Note that you need to check wsgi.pywhether the configuration file read by the Django project is correct, because this file is automatically generated by Django. If your project structure has changed, you need to update it manually

4.3 Start uwsgi

The basic commands of uwsgi, executed in the terminal

uwsgi --ini uwsgi.ini  # 启动uwsgi
uwsgi --stop uwsgi.pid  # 关闭uwsgi
uwsgi --reload uwsgi.pid  # 重启uwsgi

Among them uwsgi.iniis the startup file using uwsgi, just write it according to the above case. After startup, uwsgi.pida file will be generated to save the corresponding ID. When closing uwsgi, you need to specify the file

5. Use nginx proxy

The above is to start the Django project in the form of socket through uwsgi. This protocol browser cannot be accessed directly, so it needs to use nginx to forward the request to Django

We want to modify the configuration file of nginx. If you want to monitor port 80, you can directly modify the default configuration file of nginx. Generally, but /etc/nginx/sites-enabled/defaultfor the convenience of management, I will /etc/nginx/conf.dcreate a new configuration file here. It is more convenient to have a configuration file for each project. post management

sudo vim /etc/nginx/conf.d/django_8000.conf

The configuration is as follows

# 这种写法比较方便配合负载均衡
upstream backend {
    
    
	# 这里的IP端口即Django项目的IP端口
    server 127.0.0.1:8000;
    # 如果uwsgi使用的本地文件,此处写法参考
    # server unix:/path/to/your/mysite/mysite.sock;
}
server {
    
    
	# nginx监听端口,即在浏览器访问时的端口,我这里是监听8080端口
    listen 8080;
    autoindex on;
    location /static {
    
    
        alias /home/pan/productions/static;
    }
    location / {
    
    
        include uwsgi_params;
        uwsgi_pass backend;
    }
}

After saving the nginx configuration file, you need to reload the nginx configuration

sudo nginx -t
sudo nginx -s reload

Now open the browser to access http://localhost:8080should be able to access normally

Generally speaking, if the Django project and nginx are on the same server, the sock file method will be more efficient than the IP port, but when using the sock file, pay attention to whether you have read and write permissions

Guess you like

Origin blog.csdn.net/qq_39147299/article/details/131517356