总结在linux环境下部署django遇到的问题
安装gunicorn
这里我是使用python虚拟环境的,先用python -m venv venv创建虚拟环境
进入虚拟环境
1
| source venv/bin/activate
|
然后安装gunicorn
在项目更目录创建gunicorn.py
1 2 3 4 5 6 7 8 9 10
| workers = 4 threads = 2 bind = '127.0.0.1:8000' worker_class = 'gevent' worker_connections = 2000 pythonpath= '你的python虚拟目录路径' pidfile = '/var/run/gunicorn.pid' accesslog = '/xxx/gunicorn_acess.log' errorlog = '/xxx/gunicorn_error.log' loglevel = 'warning'
|
注意事项,如果work_class使用的是gevent模式,需要安装gevent的python依赖包,pip install gevent安装依赖包,pidfile建议不要设置在/tmp目录,这样会被linux清除掉,可以在/var/run目录先新建一个gunicorn.pid
安装supervisor
编辑config文件
新建一个supervisord.conf文件,在文件填入以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| [unix_http_server] file=/var/run/supervisor.sock ; the path to the socket file chmod=777 ; socket file mode (default 0700) ;chown=nobody:nogroup ; socket file uid:gid owner ;username=user ; default is no username (open server) ;password=123 ; default is no password (open server)
[inet_http_server] port=127.0.0.1:9001 # 这个必须设置 ;username=test 这个是supervisor的用户名 ;password=123456 这个是supervisor的密码
[program:test_django] # test_django表示项目的名称,随便命名 command=项目目录/venv/bin/gunicorn -c 项目目录/gunicorn.py test_django.wsgi:application # 这里我是用的事虚拟环境目录 directory=项目目录 startsecs=0 stopwaitsecs=0 autostart=true autorestart=true stdout_logfile=项目访问日志 # /.../test_django_access.log stderr_logfile=项目错误日志 # /.../test_django_error.log
|
unix_http_server配置注意,file是服务启动的入口文件,建议不要放到/temp目录下,建议放到别的目录,放到tmp目录会被linux识别为临时文件会被清理掉,chmod建议改为777,否则会启动失败
注意事项,test_django.wsgi:application这个的意思是,test_django是项目的名称,要跟项目的项目名称对应上,后面的wsgi.application这个是django框架的入口驱动,不需要改动
配置nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| server { listen 80; server_name xxx.xxx.com xxx.xxx.com;
location /static/ { # 项目的静态文件使用nginx处理 alias /xxx/test_django/static/; }
location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }
access_log /xxx/test_django.log; }
|
然后使用nginx -s reload热重启nginx
启动supervisor
使用supervisord -c supervisord.conf启动项目
停止supervis
使用supervisorctl管理项目
查看项目运行状态
重启,启动,停止项目
1
| supervisorctl start|stop|restart project_name
|
在Linux下安装mysqlclient
安装mysql-devel
安装mysqlclient
最后更新时间: