django -- uwsgi+nginx部署

一、 安装nginx

How To Install Nginx on CentOS 7
  • 添加epel扩展仓
sudo yum install epel-release
  • 安装Nginx
yum install nginx
  • 开启Nginx
sudo systemctl start nginx

如果防火墙拦截,可用下面的命令

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
  • 浏览器打开http://your_ip/, 出现Welcome to nginx!说明安装成功

  • 开机启动项

    systemclt enable nginx

服务器默认的根目录/usr/share/nginx/html, 服务器配置目录/etc/nginx/conf.d/,文件要以.conf结尾. 全局配置文件/etc/nginx/nginx.conf

二、安装uwsgi

pip install uwsgi

# 测试uwsgi  如:./Projects/Project/wsgi.py
cd Projects
uwsgi --http :8000 --module Project.wsgi

# or
uwsgi --http :8000 --file Project/wsgi.py

uwsgi 常用命令

uwsgi --chdir=/path/to/your/project \
    --module=mysite.wsgi:application \
    --env DJANGO_SETTINGS_MODULE=mysite.settings \
    --master --pidfile=/tmp/project-master.pid \
    --socket=127.0.0.1:49152 \      # can also be a file
    --processes=5 \                 # number of worker processes
    --uid=1000 --gid=2000 \         # if root, uwsgi can drop privileges
    --harakiri=20 \                 # respawn processes taking more than 20 seconds
    --max-requests=5000 \           # respawn processes after serving 5000 requests
    --vacuum \                      # clear environment on exit
    --home=/path/to/virtual/env \   # optional path to a virtualenv
    --daemonize=/var/log/uwsgi/yourproject.log      # background the process 

三、配置nginx

# mysite_nginx.conf
 
# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
 
# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;
 
    # max upload size
    client_max_body_size 75M;   # adjust to taste
 
    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }
 
    location /static {
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
    }
 
    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}

加入nginx项目配置

sudo ln -s 你的目录/Mxonline/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/

重启nginx

systemctl restart nginx

四、通过配置文件启动uwsgi

新建uwsgi.ini 配置文件, 内容如下:

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /path/to/Projects
# Django's wsgi file
module          = Project.wsgi
# the virtualenv (full path)

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
virtualenv = .virtualenvs_dirs/venv_py

logto = /tmp/mylog.log

注:
    chdir: 表示需要操作的目录,也就是项目的目录
    module: wsgi文件的路径
    processes: 进程数
    virtualenv:虚拟环境的目录

参考: centos7 下通过nginx+uwsgi部署django应用

参考: Django + Uwsgi + Nginx 的生产环境部署

相关推荐