Virtualenv加Django加UWSGI加Nginx多站点部署

tuxlcsdn 2019-06-27

安装 Virtualenv

pip install –U virtualenv

搭建并进入 Virtualenv 环境

mkdir /Django/cmdb/
cd /Django/cmdb/
virtualenv --no-site-packages --distribute env
cd env/
source bin/activate

查看虚拟环境

/Django/cmdb/env
[root@saltstack env]# ll
drwxr-xr-x 2 root root 4096 May 27 16:04 bin
drwxr-xr-x 2 root root 4096 May 27 12:01 include
drwxr-xr-x 3 root root 4096 May 27 12:01 lib
lrwxrwxrwx 1 root root    3 May 27 12:01 lib64 -> lib

安装 Django 等需要的类库

pip install django

下面就可以进行Django项目、应用的部署了

安装 uwsgi

pip install uwsgi

配置多站点

假设我们的两个站点分别叫cmdb和bbs

在env目录下(类似/Django/cmdb/env)新建文件夹 uwsgi_conf

mkdir uwsgi_conf/

进入uwsgi_conf 下建立 wa_uwsgi.ini 和 wb_uwsgi.ini 两个配置文件

cd uwsgi_conf/
touch cmdb.ini
touch bbs.ini

cmdb.ini配置文件的内容

[uwsgi]
#plugins = python
virtualenv = /Django/cmdb/env/
chdir = /Django/cmdb/env/CMDB/
touch-reload = /Django/cmdb/env/CMDB/
module = CMDB.wsgi
socket = 127.0.0.1:3500
vhost = false
chmod-socket = 664
master = true
enable-threads = true
workers = 4 
threads = 2
vacuum = true
max-requests = 6000
pidfile = /var/log/uwsgi/cmdb.pid
daemonize = /var/log/uwsgi/cmdb.log
#touch-logreopen = /var/log/module/.touchforlogrotate

bbs.ini和cmdb.ini内容差不多,参考修改即可。

可以用 uwsgi 的 emperor 模式来启动 uwsgi 服务

uwsgi --emperor /Django/cmdb/env/uwsgi_conf/

想要重启某站点的时候,就 touch 一下该站点的配置文件

touch /Django/cmdb/env/uwsgi_conf/cmdb.ini

配置Nginx

server {
        listen 80;
        server_name cmdb.test.com;

        #location /service/ {
        #    proxy_set_header Host service.test.com;
        #    proxy_pass http://service.test.com;
        #}

        charset utf-8;
        access_log logs/cmdb/access.log;
        error_log logs/cmdb/error.log;
        root /Django/cmdb/env/CMDB;
        client_max_body_size 75M;
        # location /admin {
        #    alias /Django/mysite/static/admin;
        #    }

        location /static {
            alias /Django/cmdb/env/CMDB/static; # your Django project's static files
            }

        location /media {
            alias /Django/cmdb/env/CMDB/static; # your Django project's media files
            }

        location / {
            #uwsgi_pass unix:/var/log/uwsgi/cmdb.sock;
            uwsgi_pass 127.0.0.1:3500;
            include uwsgi_params; # the uwsgi_params file you installed
            }
  }

重启Nginx
访问测试

相关推荐

inspuryhq / 0评论 2020-07-28