Ubuntu nginx+uwsgi+django多站点实现方法

1970-01-01   Programming, Linux,

<!--markdown-->安装pip

sudo apt install python-pip -y

安装django

sudo pip install django

创建一个名为helloworld的项目

cd /data
sudo django-admin startproject 
helloworld

修改settings.py
将 ALLOWED_HOSTS = [] 修改为 ALLOWED_HOSTS = ["*"]
安装uwsgi

sudo pip install uwsgi

创建uwsgi.ini

[uwsgi]
chdir = /home/ubuntu/helloworld
module = helloworld.wsgi
socket = 127.0.0.1:8080
master = true
vhost = true
no-site = true
workers = 2
reload-mercy = 10     
vacuum = true
max-requests = 1000   
limit-as = 512
buffer-size = 30000
pidfile = /tmp/uwsgi.pid
daemonize = /tmp/uwsgi.log

注意⚠️:socket,pidfile和daemonize在创建第二个站点的时候要修改,否则会失败
安装nginx

sudo apt-get install nginx -y

创建 /etc/nginx/sites-enabled/helloworld.conf

server {
        listen       80;
        server_name  localhost;
        
        location / {            
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9090;              //必须和uwsgi中的设置一致
            uwsgi_param UWSGI_SCRIPT demosite.wsgi;  //入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录
            uwsgi_param UWSGI_CHDIR /demosite;       //项目根目录
            index  index.html index.htm;
            client_max_body_size 35m;
        }
    }

运行

uwsgi --ini /home/ubuntu/mysite3/uwsgi.ini &
/usr/sbin/nginx
提交