Deploy Django project based on nginx+uwsgi under centos7

One: Basic environment introduction:

  • Centos: 7.8 (cat /etc/redhat-release to check the version number)
  • Python: 3.9.5 (python -V to view the version number)
  • Django: 4.2 (django-admin --version to view the version number)
  • Uwsgi: 2.0.21 (uwsgi --version to view the version number)
  • Nginx: 1.20.1 (nginx -v view version number)
  • mysql-community-common-8.0 (can be installed by yourself)

Two: deployment environment installation configuration:

1. Basic dependent environment installation
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel 
ncurses-devel sqlite-devel readline-devel tk-devel
 gcc make gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
2. Install wegt, vim, unzip and other necessary commands
yum -y install wget vim unzip
3. Install python and pip (or python multi-version management tool pyenv, etc.)
## 1.下载自己需要版本的python版本包
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tar.xz
 
## 2.新建python3目录
mkdir /usr/local/python3
 
## 3.安装Python3压缩包,进入解压目录,指定安装目录,安装Python3
tar -xvf  Python-3.9.5.tar.xz
cd Python-3.9.5
./configure --prefix=/usr/local/python3
make && make install
 
## 4.安装Python3时,会自动安装pip,如果没有就手动安装
yum -y install python-pip
 
## 5.pip配置(更换pip的源)
(1)编辑pip.conf
[root@localhost ~]# cd ~ 
[root@localhost ~]# mkdir .pip 
[root@localhost ~]# vim pip.conf
 
 
 
[global] 
index-url = https://mirrors.aliyun.com/pypi/simple/ 
[install] 
trusted-host=mirrors.aliyun.com
 

 
(2) 安装依赖安装好pip
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum install python-pip
[root@localhost ~]# pip install --upgrade pip
 
## 6.升级下pip
pip install --upgrade pip
 
 
## 7.创建软链接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
 
 
## 输入python3测试是否安装成功
python -V
4. Install nginx
## 1.yum方式安装nginx
yum -y install nginx
 
## 2.查看nginx版本
nginx -v
 
## 3.启动nginx
systemctl status nginx  ## 查看nginx状态
 
systemctl start nginx   ## 开启nginx服务
 
systemctl stop nginx     ## 关闭nginx服务
5. Install uwsgi
## 1.安装uwsgi
pip3 install uwsgi
 
## 2.建立软链接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
 
## 3.查看uwsgi版本
uwsgi --version

Three: Deploy the django project:

1. Upload the project and install the project modules and start the test
## 1.pip导出项目依赖包
pip freeze > requirements.txt

## 2.上传文件到服务器
 
## 3.解压项目
unzaip tman.zip
 
## 4.pip安装项目依赖模块
pip3 install -r requirements.txt
 
## 5.通过python3 manage.py runserver运行一下项目,如果能正常启动则进行下一步,不能正常运行往上检查。
这里需要注意使用python3来执行manage.py,否则会出现报错情况!
2. Configure the uwsgi.ini configuration file in the project

(1) In the directory where settings.py is located vim new file project name.ini

insert image description here

(2) Configure the project name.ini file

[uwsgi]
master=true
chdir=/var/tman
module=tman.wsgi
py-autoreload=1
lazy-apps=true  
socket=127.0.0.1:8000
processes=4
# pid文件,用于脚本启动,停止
pidfile=uwsgi.pid
buffer-size=32768
daemonize=uwsgi.log
log-maxsize = 5000000
vacuum = true
disable-logging = true

(3) Check the settings.py configuration and collect the static files of the project.
Generally, this is the configuration. Mine is a Vue-based front-end and back-end separation project:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'dist/static'),
]

STATIC_URL = 'static/'

url.py configuration:

from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/v1/', include('tadmin.urls')),
    path('', TemplateView.as_view(template_name='index.html'))
]

Non-front-end and back-end separation projects can use the following commands to collect static files:

python manage.py collectstatic

Based on vue, use the following command for front-end packaging:

npm run build

(4) Start the test and view the log

## 在当前目录启动
uwsgi -i tman.ini
 
## 启动之后查看进程
ps -ef | grep uwsgi

insert image description here
View process
insert image description here
View log
insert image description here

3. Configure nginx
vim /etc/nginx/nginx.conf

nginx.conf configuration

 server {
    
    
        listen       80;
        listen       [::]:80;
        server_name 192.168.75.188;
        charset utf-8;

        location /static/ {
    
    
          alias /var/tman/dist/static/;
          index index.html index.htm;
        }
  
        location / {
    
    
          include uwsgi_params;
          uwsgi_pass 127.0.0.1:8000;
        }
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
    
    
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    
    
        }
    }

(4) Start nginx

[root@localhost tman]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost tman]# nginx -s reload

(5) Access itemshttp://192.168.75.188/#/

insert image description here

Four: Remarks

After restarting the project, you only need to restart uwsgi and then restart nginx. Remember to turn off the debug mode of django in the official environment to prevent the error source code from being seen.

## 项目重启
killall -9  uwsgi
uwsgi -i tman.ini
nginx -s reload

uwsgi operation

uwsgi --ini uwsgi.ini
# 重启uswgi
uwsgi --reload uwsgi.pid
# 停止uwsgi
uwsgi --stop uwsgi.pid

5. Deployment error:

(1) pip3 install mysqlclient==2.1.1 error

/bin/sh: mysql_config: command not found
/bin/sh: mariadb_config: command not found
/bin/sh: mysql_config: command not found

Solution:

yum -y  install mysql-devel
Error: MariaDB-compat conflicts with 1:mariadb-libs-5.5.68-1.el7.x86_64
Error: Package: 1:mariadb-devel-5.5.68-1.el7.x86_64 (base)
           Requires: mariadb-libs(x86-64) = 1:5.5.68-1.el7
           Installed: MariaDB-compat-10.4.25-1.el7.centos.x86_64 (@mariadb)
               mariadb-libs(x86-64) = 1:10.1.48-1.el7.centos
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

Solution:

yum remove MariaDB-common

(2)open() "/root/tman/dist/static/js/vendor.652814051b5133caa1e9.js" failed (13: Permission denied)

Reason, nginx directory permission problem, I put the project under root, resulting in insufficient nginx permissions, the solution is to move the project to other directories, such as /var, etc.

Guess you like

Origin blog.csdn.net/weixin_43883625/article/details/130202135