nginx+uWSGI+django+virtualenv+superviso发布web服务器

1.安装python3

https://www.cnblogs.com/zdqc/p/11788212.html

3.安装virtualenv

https://www.cnblogs.com/zdqc/p/11788367.html

创建虚拟环境

mkvirtualenv zbb

4.安装代码模块依赖

pip3 freeze >  requirements.txt 

pip3 install -i https://pypi.douban.com/simple  -r requirements.txt 

5.安装UWSGI

#进入虚拟环境venv,安装uwsgi
workon zbb
pip3 install -i https://pypi.douban.com/simple uwsgi 

#检查uwsgi版本
uwsgi --version
2.0.17.1
#检查uwsgi python版本
uwsgi --python-versio

测试

#test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3

#启动一个python
uwsgi --http :8000 --wsgi-file test.py

http :8000: 使用http协议,端口8000
wsgi-file test.py: 加载指定的文件,test.py

浏览器访问ip端口号

6.运行django程序

#uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 在/etc/uwsgi_conf/目录下新建uwsgi_zbb.ini,添加如下配置:
mkdir /etc/uwsgi_conf/ -p 
vim /etc/uwsgi_conf/uwsgi_zbb.ini

[uwsgi]
# 填写项目的完整绝对路径,第一层
chdir           = /zbb/project
#指定django的wsgi文件路径,填写相对路径
module          = project.wsgi  
#填写虚拟环境的绝对路径
home            = /root/Envs/zbb
master          = true
#定义程序的多进程数量的,以cpu核数的2倍+1数量填写   2n+1 数量 
processes       = 5
#把uwsgi启动在socket协议上,的8000端口
socket          = 0.0.0.0:8000
#指定http协议的话,用户是可以直接访问到的,不安全的,因此使用socket协议
#http =0.0.0.0:8000
vacuum          = true

启动

#python manage.py runserver 8080
uwsgi --ini  /etc/uwsgi_conf/uwsgi_zbb.ini

6.安装nginx

echo "第一步下载" |pv -qL 25
mkdir /server/tools -p
cd /server/tools
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
yum install -y pcre-devel openssl-devel

echo "第二步创建虚拟用户"|pv -qL 25
useradd -M -s /sbin/nologin www

echo "第三步编译安装"|pv -qL 25
./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
make && make install

echo "第四步软连接" |pv -qL 25
ln -s /application/nginx-1.12.2 /application/nginx
echo "export PATH=$PATH:/application/nginx/sbin/" >>/etc/profile
sleep 3
source /etc/profile

echo "第五步启动并检查"|pv -qL 25
/application/nginx/sbin/nginx
sleep 3
ps -ef|grep nginx

配置文件:

项目未分离的配置文件

settings.py

STATIC_ROOT='/opt/static'
STATIC_URL = '/static/'
STATICFILES_DIRS=[
    os.path.join(BASE_DIR,"static"),
]

通过python3 manage.py collectstatic 收集所有你使用的静态文件保存到STATIC_ROOT!

STATIC_ROOT 文件夹 是用来将所有STATICFILES_DIRS中所有文件夹中的文件,以及各app中static中的文件都复制过来
# 把这些文件放到一起是为了用nginx等部署的时候更方便
server {
		listen       80;
        server_name  www.zhuimengnan,c.com;
        location / {
        uwsgi_pass 127.0.0.1:8000;
        include  uwsgi_params;
        }
        location /static {
        alias /opt/static;
}
}
}

分离之后的前端配置

server {
		listen 80;
		server_name w.zhuimengnan.com;
		location / {
		root  /dist;
		index  index.html;
	}

7.安装supervisor

退出虚拟环境

yum install  supervisor -y

然后再/etc/supervisord.conf末尾添加上如下代码

[program:xx] 
command=/root/Envs/zbb/bin/uwsgi  --ini   /etc/uwsgi_conf/uwsgi_zbb.ini 
autostart=true      
stopasgroup=true  
killasgroup=true   

[program:auth]
directory = /application/jar/
command = /application/jdk/bin/java -jar -Xms1024M -Xmx1536M -XX:PermSize=512M -XX:MaxPermSize=1536M  blade-auth.jar --spring.profiles
.active=prod
autostart = true
startsecs = 10
autorestart = true
startretries = 3
user = root
redirect_stderr = true
stdout_logfile_maxbytes = 1000MB
stdout_logfile_backups = 5
stdout_logfile =  /application/jar/log/blade-auth.out
stopasgroup=true
killasgroup=true


文件详解

supervisord.conf配置文件参数解释

[program:xx] ;是被管理的进程配置参数,xx是进程的名称
;environment=JAVA_HOME="/application/jdk/",JAVA_BIN="/application/jdk/bin/"
;directory=/data/product/app  ;项目地址
command=/root/Envs/zbb/bin/uwsgi  --ini   /etc/uwsgi_conf/uwsgi_zbb.ini ; 程序启动命令
autostart=true       ; 在supervisord启动的时候也自动启动
startsecs=10         ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true     ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3       ; 启动失败自动重试次数,默认是3
user=root          ; 用哪个用户启动进程,默认是root
priority=999         ; 进程启动优先级,默认999,值小的优先启动
redirect_stderr=true ; 把stderr重定向到stdout,默认false
stdout_logfile_maxbytes=20MB  ; stdout 日志文件大小,默认50MB
stdout_logfile_backups=20   ; stdout 日志文件备份数,默认是10
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile=/logs/zbb.out
stopasgroup=true    ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=true     ;默认为false,向进程组发送kill信号,包括子进程

最后启动supervisor,完成uWSGI启动django,nginx反向代理

#启动
supervisord -c /etc/supervisord.conf 
#systemctl start  supervisord.service
#关闭
kill - 9进程号
#systemctl stop  supervisord.service
第一种方式启动,如果supervisord挂了其他项目不会挂掉,
第二种方式启动,如果supervisord挂了其他项目会挂掉,  看项目选择启动方式

重新加载

一、重启配置文件修改过的程序    
supervisorctl update   

二、启动某个进程(program_name=你配置中写的程序名称)
supervisorctl start program_name

三、停止某一进程 (program_name=你配置中写的程序名称)
supervisorctl stop program_name

四、重启某一进程 (program_name=你配置中写的程序名称)
supervisorctl restart program_name

五、停止全部进程
supervisorctl stop all

六、查看 该程序的日志
tail -f program_name 

七、重启所有配置的所有程序
supervisorctl   reload

八、关闭 supervisor
supervisorctl shutdown
注意:用stop停止掉的进程,用reload或者update都不会自动重启。
原文地址:https://www.cnblogs.com/zdqc/p/11899175.html