项目结构
/data/playback_project/
├── PlayBack
└── script
/data/playback_project/PlayBack
├── app01
├── db.sqlite3
├── manage.py
├── Middle
├── PlayBack
├── README
├── requirements.txt
├── static
├── static_all
├── templates
└── utils
一、环境配置
1. CENTOS安装PY3
#!/bin/bash cd /usr/local/src wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel lib-devel ncurses-devel tk-devel gcc make tar -xf Python-3.6.4.tgz cd Python-3.6.4 ./configure --prefix=/usr/local/python-3.6.4 make make install echo "export PATH=$PATH:/usr/local/python-3.6.4/bin" >> /etc/profile source /etc/profile
2. 安装uWSGI
pip3 freeze
pip3 install uWSGI
3. 使用uWSGI命令启动
前提,1.用python manage runserver x.x.x.x:prot 启动成功
2.pip -r requirements.txt 依赖安装完成
requirements.txt
Django==1.11
Pillow==4.3
aliyun-python-sdk-core==2.13
oss2==2.9
XlsxWriter==1.2
进入项目目录。uwsgi --http x.x.x.x:prot --file PlayBack/wsgi.py --static-map=/static=static --static-map参数尽量写,不然样式有的时候会出问题
二、NGINX整合INI文件启动项目
1. 通过uWSGI.ini文件启动
[uwsgi] #项目目录 chdir=/data/playback_project/PlayBack/ #启动uwsgi的用户名和用户组 uid=root gid=root #加载一个wsgi模块 module=PlayBack.wsgi #指定sock的文件路径 socket=/data/playback_project/script/uwsgi.sock #启用主进程 master=true #进程个数,也可以使用processes指定 workers=1 pidfile=/data/playback_project/script/uwsgi.pid #自动移除unix Socket和pid文件当服务停止的时候 vacuum=true #序列化接受的内容,如果可能的话 thunder-lock=true #启用线程 enable-threads=true #设置自中断时间 harakiri=30 #设置缓冲 post-buffering=4096 #设置日志目录 daemonize=/data/playback_project/script/uwsgi.1og #logto=/data/playback_project/script/uwsgi.log
uwsgi --ini uwsgi.ini
uwsgi --stop uwsgi.pid
2. 安装配置NGINX
yum install nginx # conf server { listen 8081; server_name domian; access_log /var/log/nginx/playexecl_access.log main; error_log /var/log/nginx/playexecl_error.log warn; charset utf-8; gzip on; gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; # 单指定项目路径uwsgi location / { rewrite ^/(.*) http://$server_name/playback/home/$1 permanent; } location ^~ /playback/ { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/data/playback_project/script/uwsgi.sock; } # 指定静态文件路径 location /static/ { alias /data/playback_project/PlayBack/static/; index index.html index.htm; } }
3. 为了防止样式丢失
我们发现,访问的时候,有些样式会丢失,往往是DJANGO ADMIN,通过下面方法收集解决此类问题
首先在项目settings.py中添加静态文件目录 STATIC_ROOT = os.path.join(BASE_DIR, "static_ all")
然后使用内置命令进行静态文件收集 python3 manage.py collectstatic --noinput
最后更改NGINX静态文件目录为 alias /data/playback_project/PlayBack/static_all/
三、重启脚本
# 指定项目目录 PROJECT_DIR="/data/playback_project/PlayBack/" #指定脚本目录在哪里 SCRIPTS_DIR="/data/playback_project/script" # 描述 DESC="playback daemon" # 名称 NAME="playback" # 脚本名称 SCRIPT_FILENAME="manage_playback.sh" #脚本名称 SCRIPTNAME=$(pwd)/$SCRIPT_FILENAME # PID PID="uwsgi.pid" # 启动函数 d_start() { # 进入到项目目录 cd $SCRIPTS_DIR # 判断这个PID是否存在 if [ ! -f $PID ]; then echo -e "\n\033[34m$NAME 项目启动中...\033[0m" # 如果不存在执行 uwsgi --ini uwsgi.ini nginx -s reload cd $PROJECT_DIR && python3 manage.py collectstatic --noinput echo -e "\n\033[32m$NAME 项目启动完成...\033[0m" exit 0 fi echo -e "\n\033[33m$NAME 项目已启动请勿重复启动\033[0m" } # 关闭函数 d_stop() { cd $SCRIPTS_DIR # 判断这个pid文件是否存在 if [ ! -f "uwsgi.pid" ]; then # 这个项目已经关闭 echo -e "\n\033[33m$NAME 项目已经关闭...\033[0m" exit 0 fi echo -e "\n\033[34m$NAME 项目关闭中...\033[0m" # 如果没有关闭 uwsgi --stop uwsgi.pid echo -e "\n\033[32m$NAME 项目关闭完成...\033[0m" } d_restart() { d_stop sleep 1 d_start } case "$1" in start) echo -e "\nstarting $DESC: $NAME" d_start ;; stop) echo -e "\nstop $DESC: $NAME" d_stop ;; restart) echo -e "\nrestart $DESC: $NAME" d_restart ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart}" exit 3 ;; esac
四、SYSTEMD
/usr/lib/systemd/system/playback.service/playback.service [Unit] #描述 Description="playback" #表示服务信息 [Service] Type=forking #注意:需要和conf配置文件中的信息一致 PIDFile=/data/playback_project/script/uwsgi.pid #启动服务的命令 ExecStart=/usr/local/python-3.6.4/bin/uwsgi --ini /data/playback_project/script/uwsgi.ini ExecStartPost=/usr/local/python-3.6.4/bin/python3 /data/playback_project/PlayBack/manage.py collectstatic --noinput #ExecStartPost=nginx -s reload #重新加载命令 ExecReload=/bin/kill -s HUP $MAINPID #停止服务的命令 ExecStop=/bin/kill -s QUIT $MAINPID #SIGQUIT信号将会毁掉systemd服务 KillSignal=SIGQUIT #意外的失败,就将重启 Restart=on-failure #重启服务之前,需要等待的秒数 RestartSec=5s #安装相关信息 [Install] #multi-user.target表明当系统以多用户方式启动时,这个服务需要被自动运行 WantedBy=multi-user.target systemctl daemon-reload playback systemctl enable playback