部署django+uwsgi+Virtualenv+nginx+supervisor详细步骤

1.安装依赖包

yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++ 

# yum方法安装的,可以用 yum list installed 查找,如果是查找指定包,用 yum list installed | grep "软件名或者包名" 

2.安装python3

1、下载python安装包

wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz

2、 使用root权限打开终端,进行解压缩操作

tar -zxvf Python-3.6.5.tgz

3、将目录重命名

mv Python-3.6.5 python3.6

4、修改setup文件,启动ssl模块

vi /root/python3.6/Modules/Setup.dist
#使用/Socket 找到所在行

#修改结果如下:
# Socket module helper for socket(2)
_socket socketmodule.c timemodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

5、进入python3.6目录编译安装

./configure --with-ssl --prefix=/opt/python3.6  # 目录/opt/python3.6可以根据自己的喜好随意选择,此目录为python的真正安装目录

6、make&make install

make & make install

7、创建软连接

ln -s /opt/python3.6/bin/python3.6 /usr/bin/python3
ln -s /opt/python3.6/bin/pip3 /usr/bin/pip3

3.安装 virtualenv

#更改国内源
pip3 install pip -U
pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/

1、安装 virtualenv 模块

pip3 install virtualenv==16.7.9 #使用此版本,最新版有bug

2、查看版本

virtualenv --version

3、查看路径

find / -name virtualenv

4、创建软连接

ln -s /opt/python3.6/bin/virtualenv /usr/bin/virtualenv #/opt/python3.6/bin/virtualenv替换成你找到路径

5、创建虚拟环境,并命名

virtualenv --no-site-packages venv #venv是虚拟环境的名称

6、激活进入虚拟环境

source venv/bin/activate

7、退出

deactivate

8、其他命令

worken #查看虚拟环境目录

workon venv2 #切换虚拟环境

rmvirtualenv venv #删除虚拟环境

4.安装uwsgi

1、安装

pip3 install uwsgi

2、创建软路由

ln -s /opt/python3.6/bin/uwsgi /usr/bin/uwsgi

3、配置

路径:/opt/rpa_server/server_uwsgi.ini
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /opt/rpa_server
# Django's wsgi file
module          = rpa_server.wsgi
enable-threads = true
lazy-apps=true #部署tensorflow时需要添加此参数,不然请求会卡死
# process-related settings # master master = true # maximum number of worker processes processes = 1 threads = 10 # the socket (use the full path to be safe socket = 0.0.0.0:8080 #http = 0.0.0.0:9000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true home = /root/venv daemonize = /opt/rpa_server/maple_uwsgi.log
启动
uwsgi --ini  server_uwsgi.ini
#杀死进程
ps -A|grep uwsgi

kill -9 uwsgi的进程id

5.安装nginx

1、安装

yum install epel-release -y

yum install nginx -y

2、配置

路径:/etc/nginx/conf.d/rpa_server.conf
upstream rpa {
    server 0.0.0.0:8080; # for a web port socket (we'll use this first)
}

server {
        listen       8000;
        server_name  ;
        #client_max_body_size 75M;
        access_log logs/rpa_server_access.log main;


        location / {
        uwsgi_pass rpa;
        include /etc/nginx/uwsgi_params;
        }

        location /static {
            alias /opt/rpa_server/static;
        }

        error_page 400 403 404 405 505 500 501 502 503 504 /404.html;
        location = /404.html {
                root    /opt/rpa_server/templates/;
}
}
# nginx扩展
django多路径static文件,nginx的配置方法

1.设置static_root

```
#在setting中设置
STATIC_ROOT=os.path.join(BASE_DIR,"static")
```

2.创建新的static目录

```
#该命令会把当前项目的所有静态文件统一放在STATIC_ROOT
python manage.py collectstatic
```

3.然后nginx直接配置STATIC_ROOT

```
location /static {
    alias <STATIC_ROOT的实际目录>
}
```

3、启动

启动
systemctl start nginx
停止
systemctl stop nginx
重启
systemctl restart nginx
查看状态
systemctl status nginx

6.安装supervisor

1.安装

yum install -y supervisor

2.启动

#启动supervisord
systemctl start supervisord

#查看状态
systemctl status supervisord

3.修改

#修改配置文件
vim /etc/supervisord.conf

#把[inet_http_server]模块的注释去掉

#并修改IP、用户名与密码

[inet_http_server]
port=0.0.0.0:9001
username=root 
password=123456

4.重新加载

#重新加载配置文件
supervisorctl reload

5.配置需要管理的进程(重点)

cat /etc/supervisord.conf
#在/etc/supervisord.d 创建配置文件rpa_uwsgi.ini

[program:uwsgi]
directory = /root                           ;命令执行目录(这里是存放rpa_server_uwsgi.ini)
command =  /root/venv/bin/uwsgi /root/rpa_server_uwsgi.ini       ;启动命令
autostart = true                                               ;在supervisord启动的时候也启动
startsecs = 5                                                   ;启动5秒后没有异常退出,就当作已经正常启动了
autorestart = true                                            ;程序异常退出后自动重启
startretries = 3                                                ;启动失败自动重试次数,默认是3
user = root                                                      ;哪个用户启动
redirect_stderr = true                                      ;把stderr重定向到stdout,默认false
stdout_logfile_maxbytes = 20MB                    ;stdout日志文件大小,默认50MB
stdout_logfile_backups = 20                           ;stdout日志文件备份数
stdout_logfile = /opt/supervisor/logs/rpa_uwsgi_stdout.log

6.启动

#重新加载
supervisorctl reload

#启动
supervisorctl start uwsgi  对应[program:uwsgi]可以自己随便定义

7.开启防火墙端口

防火墙添加规则
firewall-cmd --permanent --zone=public --add-port=8080/tcp
加载
firewall-cmd --reload
重启
systemctl restart firewalld.service

8.更换yum源

1. 查看yum源信息:
   yum repolist
2. 安装base reop源
   cd /etc/yum.repos.d
3. 接着备份旧的配置文件
   sudo mv CentOS-Base.repo CentOS-Base.repo.bak
4. 下载阿里源的文件
   sudo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/Centos-7.repo
5. 清理缓存
   yum clean all
6. 重新生成缓存
   yum makecache
7. 再次查看yum源信息
   yum repolist

 

posted @ 2020-05-19 22:12  Maple_feng  阅读(469)  评论(0编辑  收藏  举报