1.环境依赖
yum groupinstall "Development tools" -y
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel -y
2.安装python3
https://www.cnblogs.com/zdqc/p/11788212.html
3.安装virtualenv
https://www.cnblogs.com/zdqc/p/11788367.html
4.安装django1.1
pip3 install django==1.11
5.安装UWSGI
进入虚拟环境venv,安装uwsgi
(venv) [root@slave 192.168.11.64 /opt]$pip3 install uwsgi
检查uwsgi版本
(venv) [root@slave 192.168.11.64 /opt]$uwsgi --version
2.0.17.1
#检查uwsgi python版本
uwsgi --python-versio
测试
#启动一个python
uwsgi --http :8000 --wsgi-file test.py
http :8000: 使用http协议,端口8000
wsgi-file test.py: 加载指定的文件,test.py
#test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
浏览器访问ip端口号
热加载python程序
在启动命令后面加上参数
uwsgi --http :8088 --module mysite.wsgi --py-autoreload=1
#发布命令
#command= /home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi
#此时修改django代码,uWSGI会自动加载django程序,页面生效
运行django程序
uwsgi --ini /etc/uwsgi_nginx.ini
uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 在/etc/目录下新建uwsgi_nginx.ini,添加如下配置:
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /opt/mysite
# Django's wsgi file
module = mysite.wsgi
# the virtualenv (full path)
home = /opt/venv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 1
# the socket (use the full path to be safe
socket = 0.0.0.0:8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
uwsgi.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
配置文件:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.119.12;
location / {
root /opt/07-luffy_project_01/dist;
index index.html;
#这一条参数确保vue页面刷新时候,不会出现404页面
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8000;
server_name 192.168.119.12;
location / {
uwsgi_pass 0.0.0.0:9000;
include /opt/nginx/conf/uwsgi_params;
}
location /static {
alias /opt/static;
}
}
}
7.安装supervisor
退出虚拟环境
yum install python-setuptools easy_install supervisor -y
通过命令生成supervisor的配支文件
echo_supervisord_conf > /etc/supervisord.conf
然后再/etc/supervisord.conf末尾添加上如下代码
[program:my]
#command=/opt/venv/bin/uwsgi --ini /etc/uwsgi_nginx.ini #这里是结合virtualenv的命令 和supervisor的精髓!!!!
command= /home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi
#--home指的是虚拟环境目录 --module找到 mysite/wsgi.py
文件详解
supervisord.conf配置文件参数解释
[program:xx]是被管理的进程配置参数,xx是进程的名称
[program:xx]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run ; 程序启动命令
autostart=true ; 在supervisord启动的时候也自动启动
startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3 ; 启动失败自动重试次数,默认是3
user=tomcat ; 用哪个用户启动进程,默认是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=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup=false ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false ;默认为false,向进程组发送kill信号,包括子进程
最后启动supervisor,完成uWSGI启动django,nginx反向代理
supervisord -c /etc/supervisord.conf #启动
supervisorsupervisorctl -c /etxc/supervisord.conf restart my #重启my项目
supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
重新加载
一、添加好配置文件后
二、更新新的配置到supervisord
supervisorctl update
三、重新启动配置中的所有程序
supervisorctl reload
四、启动某个进程(program_name=你配置中写的程序名称)
supervisorctl start program_name
五、查看正在守候的进程
supervisorctl
六、停止某一进程 (program_name=你配置中写的程序名称)
pervisorctl stop program_name
七、重启某一进程 (program_name=你配置中写的程序名称)
supervisorctl restart program_name
八、停止全部进程
supervisorctl stop all
注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。
8.静态文件
settings.py
STATIC_ROOT='/opt/nginx1-12/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等部署的时候更方便