django -- uwsgi+nginx deployment

1. Install nginx

How To Install Nginx on CentOS 7
  • add epel extension
    sudo yum install epel-release
  • Install Nginx
    yum install nginx
  • Open Nginx
    sudo systemctl start nginx

If the firewall blocks, use the following command

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
  • The browser is opened http://your_ip/, and Welcome to nginx appears! The installation is successful

  • Startup items

    systemclt enable nginx

The default root directory /usr/share/nginx/htmlof the server, the server configuration directory /etc/nginx/conf.d/, the file should .confend with. Global configuration file/etc/nginx/nginx.conf

2. Installationuwsgi

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 common commands

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 

3. Configurationnginx

# 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
    }
}

Add nginx project configuration

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

restart nginx

systemctl restart nginx

Fourth, start uwsgi through the configuration file

新建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:虚拟环境的目录

Reference: Deploy django applications through nginx+uwsgi under centos7

Reference: Production deployment of Django + Uwsgi + Nginx

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325285518&siteId=291194637