django源码本地调试

1先把项目在本地调通:

(1)pip install django-bootstrap

(2)pip install -r requirements.txt

python -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

 (3)python manage.py runserver

2在云服务器中安装mysql,nginx,python

3将项目上传至服务器

4进入项目后安装依赖和requirements.txt

cd wl2020
yum install -y mysql-devel
python -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
python manage.py runserver

 5安装百度api:


(1)安装解压缩包:yum install unzip -y
(2)由于地址识别用到百度语言处理基础技术,所以下载pythonSDK并上传至项目目录:
https://ai.baidu.com/file/579AD3FFD8C84F198EF1C223ED7FEA4B

(3)由于文字识别用到了百度,所以下载pythonSDK并上传至项目目录:
https://ai.baidu.com/file/1880CDB0B7C44A13AFCD7424FA18731D

(4)解压pythonSDK:
unzip aip-python-sdk-4.15.7.zip
unzip aip-python-sdk-4.15.12.zip

(5)安装pythonSDK:python setup.py install

 6安装uwsgi和supervisor:

[root@instance-atfaffll ~]# python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple uwsgi
[root@instance-atfaffll ~]# python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple supervisor
[root@instance-atfaffll ~]# ln -s /usr/local/python38/bin/uwsgi /usr/bin/uwsgi
[root@instance-atfaffll ~]# ln -s /usr/local/python38/bin/supervisord /usr/bin/supervisord
[root@instance-atfaffll ~]# ln -s /usr/local/python38/bin/supervisorctl /usr/bin/supervisorctl
[root@instance-atfaffll ~]# uwsgi
[root@instance-atfaffll ~]# supervisord -c /etc/supervisord.conf #第一次运行要指定配置文件,配置文件在下面第8条
[root@instance-atfaffll ~]# supervisorctl

 7在项目目录中编写uwsgi配置文件:

[root@instance-atfaffll ~]# cd wl2020
[root@instance-atfaffll wl2020]# vim uwsgi.ini
[root@instance-atfaffll wl2020]# cat uwsgi.ini
[uwsgi]
chdir = /root/wl2020
module = wl2020.wsgi:application
master = true
processes = 1
socket = 0.0.0.0:8888
chmod-socket = 644
vacuum = true
logto = /root/wl2020/test.log
pidfile = /root/wl2020/uwsgi2.pid

 8生成supervisor的配置文件并进行简单的配置:

[root@instance-atfaffll bin]# cd ~
[root@instance-atfaffll ~]# ln -s /usr/local/python38/bin/echo_supervisord_conf /usr/bin/echo_supervisord_conf
[root@instance-atfaffll ~]# echo_supervisord_conf >/etc/supervisord.conf
[root@instance-atfaffll ~]# vim /etc/supervisord.conf
[root@instance-atfaffll ~]# tail -n2 /etc/supervisord.conf
[include]
files = /etc/supervisord.d/*.ini

 9在supervisor的include目录中配置uwsgi启动配置文件:

[root@instance-atfaffll ~]# mkdir /etc/supervisord.d
[root@instance-atfaffll ~]# cd /etc/supervisord.d
[root@instance-atfaffll supervisord.d]# vim uwsgi.ini 
[root@instance-atfaffll supervisor]# cat uwsgi.ini

[program:uwsgi]
command=uwsgi --ini /root/wl2020/uwsgi.ini ; 程序启动命令
autostart=true ; 在supervisord启动的时候也自动启动
startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3 ; 启动失败自动重试次数,默认是3
user=root ; 用哪个用户启动进程,默认是root
priority=999 ; 进程启动优先级,默认999,值小的优先启动
redirect_stderr=true ; 把stderr重定向到stdout,默认false
stdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默认50MB
stdout_logfile_backups = 20 ; stdout 日志文件备份数,默认是10
;stdout_logfile=/root/wl2021/supervisor.log ; 日志保存路径
;stderr_logfile=/root/wl2021/supervisor_err.log ; 错误日志保存路径

 10启动:

[root@instance-atfaffll wl2020]# supervisorctl reload
Restarted supervisord

[root@instance-atfaffll ~]# supervisord -c /etc/supervisord.conf
[root@instance-atfaffll ~]# ps -ef |grep supervisor
root       26610   21315  0 16:33 pts/0    00:00:00 /usr/bin/python /usr/bin/supervisorctl
root       28385       1  0 18:49 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root       28390   21315  0 18:49 pts/0    00:00:00 grep --color=auto supervisor
[root@instance-atfaffll wl2020]# supervisorctl reload
Restarted supervisord
[root@instance-atfaffll wl2020]# ps -ef |grep uwsgi
root 42293 42045 4 10:19 ? 00:00:00 uwsgi --ini /root/wl2020/uwsgi.ini
root 42295 42293 0 10:19 ? 00:00:00 uwsgi --ini /root/wl2020/uwsgi.ini
root 42300 41468 0 10:19 pts/0 00:00:00 grep --color=auto uwsgi

 11配置supervisor开机自启:

[root@instance-atfaffll ~]# cd /usr/lib/systemd/system
[root@instance-atfaffll system]# vim supervisord.service
[root@instance-atfaffll system]# cat supervisord.service
[Unit] 
Description=Supervisor daemon

[Service] 
Type=forking 
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf 
ExecStop=/usr/bin/supervisorctl shutdown 
ExecReload=/usr/bin/supervisorctl reload 
KillMode=process 
Restart=on-failure 
RestartSec=42s

[Install] 
WantedBy=multi-user.target

12默认的supervisor管理命令:

启动

supervisord -c /etc/supervisord.conf 
1
加入自启动

systemctl enable supervisord
1
服务信息查看

[root@Master src]# ps -ef|grep supervisor
root     29019     1  0 14:42 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root     29110  8863  0 14:43 pts/1    00:00:00 grep --color=auto supervisor
1
2
3
停止supervisor

[root@Master src]# supervisorctl shutdown
Shut down
1
2
重新加载

[root@Master src]# supervisorctl reload
Restarted supervisord
1
2
新建配置文件目录,最下面的配置路径,默认为

files = relative/directory/*.ini
1
可更改

files = /etc/supervisor/*.ini
1
mkdir -p /etc/supervisor/

 13使用systemctl方式启动supervisor

[root@instance-atfaffll ~]# ps -ef|grep supervisord
root       42045       1  0 10:10 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root       42657   41468  0 10:44 pts/0    00:00:00 grep --color=auto supervisord
[root@instance-atfaffll ~]# kill -s SIGTERM 42045
[root@instance-atfaffll ~]# systemctl start supervisord.service
[root@instance-atfaffll ~]# systemctl status supervisord.service
● supervisord.service - Supervisor daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-06-29 10:46:03 CST; 5min ago
  Process: 28377 ExecStop=/usr/bin/supervisorctl shutdown (code=exited, status=1/FAILURE)
  Process: 42685 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
 Main PID: 42687 (supervisord)
    Tasks: 3 (limit: 6012)
   Memory: 42.1M
   CGroup: /system.slice/supervisord.service
           ├─42687 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
           ├─42688 uwsgi --ini /root/wl2020/uwsgi.ini
           └─42690 uwsgi --ini /root/wl2020/uwsgi.ini

Jun 29 10:46:03 instance-atfaffll systemd[1]: Starting Supervisor daemon...
Jun 29 10:46:03 instance-atfaffll systemd[1]: Started Supervisor daemon.
[root@instance-atfaffll ~]# 

[root@instance-atfaffll ~]# ps -ef|grep uwsgi
root 42688 42687 0 10:46 ? 00:00:00 uwsgi --ini /root/wl2020/uwsgi.ini
root 42690 42688 0 10:46 ? 00:00:00 uwsgi --ini /root/wl2020/uwsgi.ini
root 42926 41468 0 11:05 pts/0 00:00:00 grep --color=auto uwsgi
[root@instance-atfaffll ~]#

 

 14加入自启动:

[root@instance-atfaffll ~]# systemctl enable supervisord
Created symlink /etc/systemd/system/multi-user.target.wants/supervisord.service → /usr/lib/systemd/system/supervisord.service.
[root@instance-atfaffll ~]# 

 15在supervisor里加入nginx配置文件:

[root@instance-atfaffll ~]# cd /etc/supervisord.d
[root@instance-atfaffll supervisord.d]# ls
uwsgi.ini
[root@instance-atfaffll supervisord.d]# vim nginx.ini
[root@instance-atfaffll supervisord.d]# supervisorctl reload
Restarted supervisord
[root@instance-atfaffll supervisord.d]# ps -ef|grep nginx
root       43927   43729  0 12:11 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx -g daemon off;
www        43929   43927  0 12:11 ?        00:00:00 nginx: worker process
root       43936   41468  0 12:12 pts/0    00:00:00 grep --color=auto nginx
[root@instance-atfaffll supervisord.d]# cat nginx.ini
[program: nginx]
command=/application/nginx/sbin/nginx  -g 'daemon off;'  # 修改自己的路径 
autorestart=true ;
autostart=true ;
stderr_logfile=/application/nginx/logs/error.log ;  #这是nginx日志路径,web会用到
stdout_logfile=/application/nginx/logs/stdout.log ;
environment=ASPNETCORE_ENVIRONMENT=Production ;
user=root ; #启动用户
stopsignal=INT
startsecs=10 ;
startretries=5 ;
stopasgroup=true

 测试:

[root@instance-atfaffll supervisord.d]# supervisorctl reload
Restarted supervisord
[root@instance-atfaffll supervisord.d]# systemctl stop supervisord.service
[root@instance-atfaffll supervisord.d]# ps -ef |grep nginx
root       44030   41468  0 12:19 pts/0    00:00:00 grep --color=auto nginx
[root@instance-atfaffll supervisord.d]# ps -ef|grep uwsgi
root       44038   41468  0 12:19 pts/0    00:00:00 grep --color=auto uwsgi
[root@instance-atfaffll supervisord.d]# systemctl start supervisord.service
[root@instance-atfaffll supervisord.d]# ps -ef|grep uwsgi
root       44061   44059  8 12:20 ?        00:00:00 uwsgi --ini /root/wl2020/uwsgi.ini
root       44064   44061  0 12:20 ?        00:00:00 uwsgi --ini /root/wl2020/uwsgi.ini
root       44068   41468  0 12:20 pts/0    00:00:00 grep --color=auto uwsgi
[root@instance-atfaffll supervisord.d]# ps -ef |grep nginx
root       44060   44059  0 12:20 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx -g daemon off;
www        44062   44060  0 12:20 ?        00:00:00 nginx: worker process
root       44072   41468  0 12:20 pts/0    00:00:00 grep --color=auto nginx
[root@instance-atfaffll supervisord.d]# 

 16编写nginx配置文件:

[root@instance-atfaffll ~]# cd /application/nginx/conf/extra
[root@instance-atfaffll extra]# ls
01_www.conf
[root@instance-atfaffll extra]# cat 01_www.conf
server {
        listen       80;
        server_name  www.tangyingjie.com tangyingjie.com;

        location / {
            root   html/www;
            index  index.html index.htm;
        }

        location /kuaidi {
        include /application/nginx/conf/uwsgi_params;
        uwsgi_pass 127.0.0.1:8888;
#    uwsgi_param UWSGI_SCRIPT wl2020.wsgi; 
#       uwsgi_param UWSGI_CHDIR /root/wl2020/;
        client_max_body_size 35m;

    uwsgi_send_timeout 120s;
        uwsgi_read_timeout 120s;
        }

    location /static/ {    
        expires 30d;
        autoindex on;
        add_header Cache-Control private;
        alias /root/wl2021/wl2020/static/;    
        }

    }

 17备注二期配置uwsgi和djiango的方法:

#编写uwsgi配置文件
vim uwsgi.ini 
[uwsgi]
chdir = /root/wl2020
module = wl2020.wsgi:application
master = true
processes = 4
socket = 0.0.0.0:8888
chmod-socket = 644
vacuum = true
pidfile = /root/wl2020/uwsgi2.pid
daemonize = /root/wl2020/test.log
static-map = /static=/root/wl2020/static
log-maxsize = 50000000

#启动uwsgi
uwsgi -d --ini uwsgi.ini
#配置https:
vim /application/nginx/conf/extra/02_https.conf 
location /kuaidi {
        add_header X-Frame-Options ALLOWALL;
        include /application/nginx/conf/uwsgi_params;
        uwsgi_pass 127.0.0.1:8888;
        #uwsgi_param UWSGI_SCRIPT wl2020.wsgi;
        #uwsgi_param UWSGI_CHDIR /root/wl/app/src/wl2020;
        client_max_body_size 35m;

        uwsgi_send_timeout 120s;
        uwsgi_read_timeout 120s;
    }

#重启nginx
systemctl restart nginx
#重启django:
pkill -9 uwsgi
cd /root
uwsgi -d --ini uwsgi.ini 

 

posted @ 2021-06-28 10:09  linuxTang  阅读(33)  评论(0)    收藏  举报