Django生产环境部署

开发环境

centos 7.4
django 1.11.10
nginx 1.14.0
uwsgi 2.0.16
supervisor 3.1.4

Django

Django部署步骤

  1. /data/wwwroot/创建一个文件夹用来存放Django程序, 这里假设文件夹名为django_project·
  2. 上传Django程序到/data/wwwroot/django_project目录下,这里假设文件夹名称project
  3. (可选,推荐) 在/data/wwwroot/django_project 目录下运行命令virtualenv env (py3 使用 python3.6 -m venv env )生产Python虚拟环境,以便隔离系统Python环境(env为虚拟环境生成的文件夹名称可以自定义)
  4. 启用Python虚拟环境,运行命令source /data/wwwroot/django_project/env/bin/active,启用后服务器主机名会带上(env) 字样,表示激活成功
  5. 安装Django程序所需依赖 进入上传的Django程序目录 一般开发者都会将依赖信息保存到程序目录下的requirements.txt文件里 运行命令pip install -r requirements.txt安装所需要的依赖
  6. 根据自己网站程序等实际情况修改 程序目录里的配置文件,一般文件名为settings.py在此文件中设置好数据库等相关信息,附Mysql设置,请根据实际情况修改
  7. 切换到django项目目录,使用命令python manage.py runserver 0.0.0.0:8001启动django,启动成功则可以通过浏览器访问到django的页面
  8. 创建数据库,然后初始化数据库 python manage.py migrate
  9. 启动程序 python manage.py runserver 0.0.0.0:8080
  10. 测试程序是否正常运行 浏览器上访问 ip:8080 (阿里云安全组记得先打开8080端口,测试可以用后在关闭)
  11. 配置uwsgi, 在/etc/uwsgi.d/目录下创建一个配置文件名称自定义后缀ini即可,名称为dj_project.ini的文件 ,新建配置文件后 运行命令chown uwsgi:uwsgi -R /etc/uwsgi.d/
  12. 重启uwsgi 命令systemctl restart uwsgi然后查看uwsgi是否正常运行netstat -anopt查看uwsgi端口是否启用 运行命令systemctl status uwsgi 输出结果没有明显的错误信息
  13. 配置Nginx,修改/etc/nginx/conf.d/default.conf
  14. 重启Nginx systemctl restart nginx,然后访问你的IP 或域名 (阿里云安全组需要打开80和443端口)

Django启动测试

出现问题-以普通用户soga登陆,项目提示无权限

(venvs) [soga@iZb oee]$ ls -l
-rwxr--r-- 1 root root     6 Jul 29 14:03 manage.py         # soga无文件的执行权限
<del> (venvs) [root@iZb oee]$ chown soga -R /home/oee <del> # 切换root,修改文件所属用户
(venvs) [soga@iZb oee]$ ls -l                               # 切换soga用户后,显示对该目录下文件的权限
-rwxr--r-- 1 soga root     6 Jul 29 14:03 manage.py
...

虽然django正常启动,后面误操作(修改部分系统程序文件权限给soga,导致msyql、rabbitmq等应用无法正常运行)。
给文件777的权限,chmod 777 -R /home/oee或者以root用户登陆

settings.py

设置debug=False

Uwsgi

uwsgi的配置文件一般放在/etc/uwsgi.d/

配置文件

[uwsgi]
chdir=/home/oee                # 项目目录
module=oee.wsgi                # 指定项目的application
socket=path/to/uwsgi_prj.sock        # 也可选择使用http端口
process=4                      # 进程个数
; pidfile=/home/oee/script/uwsgi.pid
; static-map=/static=/home/oee/static     # 静态文件目录
master=true                    # 启用主进程
vacuum=true                    # 自动移除unix Socket和pid文件当服务停止的时候
enable-threads=true            # 启用线程
daemonize=/home/oee/script/uwsgi.log    # 设置日志目录

出现问题: 后台出现错误提示 invalid request block size: 21573 (max 4096)...skip,问题原因是,uwsgi配置成socket访问。

更改配置文件权限

将新建的配置文件权限,转给uwsgi用户组。这样启动uwsgi后,uwsgi会按照目录下的配置文件执行程序
chown uwsgi:uwsgi -R /etc/uwsgi.d/

uwsgi启动命令

systemctl start|stop|status|restart uwsgi

若将uwsgi.ini中的socket=:8001替换为http=:8001,启动uwsgisystemctl start uwsgi则可不通过nginx代理直接访问django项目

Nginx

配置文件

uwsgi的配置文件修改/etc/nginx.d/default.conf

server {
    listen 8001;
    server_name xxx.xxx.xxx.xxx;
    root /data/wwwroot/oee;
   # http
    location / {
        include uwsgi_params;
        uwsgi_read_timeout 3600;
        uwsgi_pass /path/to/uwsgi_prj.sock;
    }
    location  ~/static/ {
        root /data/wwwroot/oee/;
    }
}

nginx启动命令

systemctl start|stop|status|restart nginx

supervisor

配置文件

supervisor 主要是管理后台进程,在生产环境中使用supervisor管理django和uwsgi等进程,在进程中断时重启

[program:oee_satrt]
command=/data/wwwroot/venv/bin/python start.py
directory=/data/wwwroot/oee/background
environment=PATH="/data/wwwroot/venv/bin"
process_name=%(program_name)s
num_procs=1
autostart=true
autorestart=true
stdout_logfile_maxbytes=40MB
redirect_stderr=true
stdout_logfile = /var/log/supervisor/oee_start.log
~                                                

supvisor启动命令

[root@iZbp153nglzx9iy0y6zgafZ ~]# supervisorctl 
oee_satrt                        RUNNING   pid 5329, uptime 1 day, 0:23:27
supervisor> status
oee_satrt                        RUNNING   pid 5329, uptime 1 day, 0:23:32

进入supvervisor还有reload、start和stop等命令

linux文件权限

uwsgi配置

阿里云镜像django部署
* http://help.websoft9.com/python-guide/djlinux/deploysite.html

猜你喜欢

转载自blog.csdn.net/soga238/article/details/81273744