0.部署Django
1、可能遇到rsa 问题 ,重装rsa包=4.0
1、安装nginx
配置如下:
server {
listen 8000;
# listen [::]:80 default_server;
server_name 10.0.60.55;
#root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
include /etc/nginx/uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
uwsgi_pass 127.0.0.1:9000; #
}
# 指定静态文件路径
location /static/ {
alias /home/wwwroot/baicloud/static/;
}
# 指定rest_framework_swagger静态文件路径
location /static/rest_framework_swagger/ {
alias /usr/lib/python2.7/site-packages/rest_framework_swagger/static/rest_framework_swagger/;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
2、安装uwsgi
sudo pip install uwsgi --upgrade
使用 uwsgi 运行项目
uwsgi --http :8001 --chdir /path/to/project --home=/path/to/env --module project.wsgi
这样就可以跑了,--home 指定virtualenv 路径,如果没有可以去掉。project.wsgi 指的是 project/wsgi.py 文件
如果提示端口已经被占用:
probably another instance of uWSGI is running on the same address (:8002).
bind(): Address already in use [core/socket.c line 764]
这时可以把相关的进程 kill 掉:
按照端口进行查询:
lsof -i :8002
可以查出:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
uwsgi 2208 tu 4u IPv4 0x53492abadb5c9659 0t0 TCP *:teradataordbms (LISTEN)
uwsgi 2209 tu 4u IPv4 0x53492abadb5c9659 0t0 TCP *:teradataordbms (LISTEN)
这时根据 PID 可以用下面的命令 kill 掉相关程序:
sudo kill -9 2208 2209
按照程序名称查询:
ps aux | grep uwsgi
配置uwsgi
[uwsgi]
socket = 0.0.0.0:9000
chdir = /home/wwwroot/baicloud/
module=baicloud.wsgi:application
pidfile=/home/wwwroot/uwsgi.pid
processes = 2
max-requests = 5000
threads = 4
master = true
#daemonize = /var/log/baicloud_uwsgi.log
log-maxsize = 5000000
disable-logging = true
vacuum = true
使用supervisor来管理进程
1、pip install supervisor
安装完成之后我们可以使用下面的命令来查看supervisor的一个默认配置模板:
echo_supervisord_conf
然而上面命令输出的结果仅是一个模板,我们需要使用下面的命令在/etc目录下生成配置文件:
echo_supervisord_conf > /etc/supervisord.conf
之后我们可以使用vim查看该文件的内容
vim /etc/supervisord.conf
2修改配置文件
我们可以看到,该配置文件内容一大堆,但是我们目前仅需要知道并改文件末尾的这个部分就可以了,这是默认写法
;[include]
;files = relative/directory/*.ini
分号;后面的是注释内容,我们首先把它去掉,然后将files的值改为:
[include]
files = /etc/supervisor/*.ini
include可以理解为包含,它的意思是将/etc/supervisor/目录下的,所有以.conf结尾的文件包含进来,/etc/下的supervisor/文件夹默认是不存在的,我们需要手动创建:
cd
/etc
mkdir supervisor
现在我们就创建一个uwsgi.ini文件:
vim /etc/supervisor/uwsgi.ini
[program:uwsgi]
command=uwsgi --ini /home/wwwroot/baicloud/uwsgi.ini
autostart = true ; 在 supervisord 启动的时候也自动启动
stopsignal=QUIT
user=root
startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了
startretries = 3 ; 启动失败自动重试次数,默认是 3
autorestart = true ; 程序异常退出后自动重启
redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile = /home/log/baicloud_stdout.log
stderr_logfile = /home/log/baicloud_err.log
4.启动Supervisor
在启动之前我们还需要完成两件事情:
4.1修改uwsgi配置
之前我们在uwsgi的配置文件中设置了一个日志文件保存位置的参数:
daemonize = /home/mysite_uwsgi/mysite.log
但是这个参数的配置与Supervisor的日志输出配置是有冲突的,需要注释掉它,因为在前面加个#:
#daemonize = /home/mysite_uwsgi/mysite.log
4.2干掉uwsgi进程
在使用Supervisor启动uwsgi之前,我们需要先把uwsgi正在运行的进程都干掉,如果你之前没有运行uwsgi,这一步可以跳过:
ps -aux | grep uwsgi |awk '{print $2}'|xargs kill -9
4.3启动Supervisor
supervisord -c /etc/supervisord.conf
4.3查看运行状态
上一步执行成功之后,如果没有报错那么就是启动成功了,然后我们执行下面的命令查看uwsgi启动状态
supervisorctl status
如果显示:
uwsgi RUNNING pid 20986, uptime 0:31:33
那么代表的就是启动成功,可以看到这个结果有四列,uwsgi代表的是进程名称,RUNNING代表的进程启动状态,pid代表的是进程id,uptime代表的是进程运行时间。
###如果没有任何显示 尝试 chmod 777 /tmp
centos下Supervisor 加入开机自启动:
1.在目录/usr/lib/systemd/system/ 新建文件supervisord.service
2.添加配置内容:
[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=20s # 开机20后启动
[Install]
WantedBy=multi-user.target
3.启动服务 systemctl enable supervisord
4.验证一下是否为开机启动 systemctl is-enabled supervisord