nginx+uwsgi+django(restframework)服务器部署

来源:互联网 发布:乐器软件 编辑:程序博客网 时间:2024/05/19 07:11

django项目假设已完成

安装问题

nginx

pip install nginx
error:package nginxcore is not configured yet
解决:先安装这些缺失的包
还有一种情况版本情况高于该包之类的,也是有些相关包没有预先安装。

uwsgi

pip install uwsgi
error
no request plugin is loaded you will not be able to manage requests
解决:sudo apt-get install uwsgi-plugin-python

supervisor

pip install supervisor
sudo echo_supervisor_conf > /etc/supervisord.conf

配置

nginx.conf

项目服务应写在nginx.conf的http{}中或site-availabled/site-enabled 中的conf文件中

http{...       server {    listen 8035;    #root /usr/share/nginx/html;    index index.html index.htm index.nginx-debian.html;    client_max_body_size 75M;    server_name localhost;    charset UTF-8;    location /static    {        add_header Cache-Control private;        alias /home/zhouxin/com_tel_query/static/;    }    location /    {        include uwsgi_params;        uwsgi_pass localhost:8037;        uwsgi_read_timeout 2;    }  }include /etc/nginx/conf.d/*.conf;#include /etc/nginx/sites-enabled/*;...  } 

web_uwsgi.ini(任意取名,对应好)

[uwsgi]
plugin = python
http=:8037 #socket=:8037需对应到sock,自行延伸
chdir = /home/your/project_name/
module = com_tel_query.wsgi:application #:application必须加
master = true
processes = 4
buffer-size = 32786 #应对error:invalid request block size: 21327 (max 4096)…skip

uwsgi –ini web_uwsgi.ini 测试运行状态

admin/restframework后台css样式缺失

setting.py

STATIC_ROOT = ‘/your/path/to/static_resource/static’#或相对路径
STATIC_URL = ‘static’

cd project
mkdir static
python manage.py collectstatic #自动copy admin和restframework的css/js等文件到static
仍然缺失的情况下可尝试:
urls.py

from django.conf import settings
from django.conf.urls.static import static
urlparrerns = [
your url…
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

守护进程-supervisor

supervisord /etc/supervisor.conf

error:
Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting
解决:
find / -name supervisor.cock #找到sock文件所在目录
unlink path/supervisor.sock

后台服务持续开启:vim /etc/supervisord.conf (可另写conf文件放入相应/usr/local/bin/supervisor文件夹中,supervisord.conf 中include *.conf,可以一个服务一个conf)
在conf最底部加入:

[program:comTel]
directory=/home/zhouxin/com_tel_query #project目录
command=uwsgi –ini /home/zhouxin/com_tel_query/web_uwsgi.ini
user=root #运行的用户名
autostart=true
autorestart=true
loglevel=error
stdout_logfile=/home/zhouxin/com_tel_query/logs/uwsgi_out.log
stderr_logfile=/home/zhouxin/com_tel_query/logs/uwsgi_err.log #logs文件夹要在运行conf前建立
logfile_maxbytes=1M

一些命令

supervisorctl status #查看当前服务状态
supervisorctl reload #重载被修改过的conf文件
supervisorctl -c /etc/spervisord.conf start/stop program-name #启动/关闭

运行

/usr/local/bin/supervisord -c /etc/supervisord.conf #写全supervisord的路径而非直接supervisord 命令