CentOS 下使用uwsgi+nginx部署django
当前环境:
- Centos7
- Django2.0
- Python3.7
安装uwsgi
pip install uwsgi
安装Nginx
cd /usr/local
wget http://nginx.org/download/nginx-1.7.4.tar.gz
tar -zxvf nginx-1.7.4.tar.gz
cd nginx-1.7.4
./configure $默认安装在/usr/local/nginx
make
make install
Nginx常用命令
启动nginx:
/usr/local/nginx/sbin/nginx
其他参数:
[user@host dir]$ /usr/local/nginx/sbin/nginx -h
nginx version: nginx/1.8.0
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help 查看帮助信息
-v : show version and exit 显示 nginx 的版本
-V : show version and configure options then exit 显示 nginx 的版本,编译器版本和配置参数
-t : test configuration and exit 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload 传递一个信号,stop快速关闭,quit从容关闭,reopen重新打开日志文件、用于切换日志文件,reload重载配置文件
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf) 设置配置文件
-g directives : set global directives out of configuration file
django项目结构
├── db.sqlite3
├── manage.py
├── okr
│ ├── admin.py
│ ├── apps.py
│ ├── exc.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── okr_middleware.py
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ ├── utils.py
│ └── views.py
├── okr_manage
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── README.md
├── requirements.txt
├── static
├── templates
│ ├── 404.html
│ ├── back.html
│ ├── Home.html
│ ├── index_back.html
│ ├── login.html
│ ├── personal_goals.html
│ ├── sector_strategy.html
│ ├── team_goals.html
│ └── test.html
└── uwsgi.ini
测试uwsgi
# 创建test.py文件
def application(env, start_response):
start_response("200 OK", [("Content-Type","text/html")])
return [b"Hello World"]
通过uwsgi运行该文件
uwsgi --http :8001 --wsgi-file test.py
# 访问127.0.0.1:8001 返回hello world代表uwsgi正常可用
配置Django与uwsgi连接
在我们通过Django创建okr项目时,在子目录okr_manage下已经帮我们生成的 wsgi.py文件。所以,我们只需要再创建uwsgi.ini配置文件即可,当然,uwsgi支持多种类型的配置文件,如xml,ini等。此处,使用ini类型的配置.
# uwsgi.ini配置
[uwsgi]
http = :9000
socket = /tmp/uwsgi.sock # uwsgi启动时会创建这个文件,需chmod 777权限
chdir = /opt/okr
wsgi-file = okr_manage/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191
vacuum= true
其他配置项:
- http : 协议类型和端口号
- processes : 开启的进程数量
- workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
- chdir : 指定运行目录(chdir to specified directory before apps loading)
- wsgi-file : 载入wsgi-file(load .wsgi file)
- stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
- threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
- master : 允许主进程存在(enable master process)
- daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
- pidfile : 指定pid文件的位置,记录主进程的pid号。
- vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
使用uwsgi启动django
uwsgi --ini myweb_uwsgi.ini
# 成功信息:
# 注意查看uwsgi的启动信息,如果有错,就要检查配置文件的参数是否设置有误
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.17.1 (64bit) on [Mon Aug 27 06:16:03 2018] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-28) on 27 August 2018 05:39:18
os: Linux-3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017
nodename: workstation-qa.yiguo.com
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /opt/okr
detected binary path: /usr/local/python3/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /opt/okr
your processes number limit is 63082
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :9000 fd 3
uwsgi socket 0 bound to TCP address 127.0.0.1:8000 fd 6
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.7.0 (default, Aug 9 2018, 22:10:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
Python main interpreter initialized at 0x1f7b360
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 416880 bytes (407 KB) for 8 cores
*** Operational MODE: preforking+threaded ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1f7b360 pid: 4511 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 4511)
spawned uWSGI worker 1 (pid: 4512, cores: 2)
spawned uWSGI worker 2 (pid: 4513, cores: 2)
spawned uWSGI worker 3 (pid: 4515, cores: 2)
spawned uWSGI worker 4 (pid: 4517, cores: 2)
*** Stats server enabled on 127.0.0.1:9191 fd: 22 ***
spawned uWSGI http 1 (pid: 4520)
nginx.conf配置
# vim /usr/local/nginx/conf/nginx.conf
# 如果出现403问题,请设置项目目录的权限,并且在nginx配置中去掉注释# user=nobady 改成user=root即可
server {
listen 8099; # 指定的是nginx代理uwsgi对外的端口
server_name 127.0.0.1 # 网上大多资料都是设置的一个网址(例,www.example.com),我这里如果设置成网址无法访问,所以,指定的到了本机默认ip
charset UTF-8;
access_log /var/log/nginx/myweb_access.log;
error_log /var/log/nginx/myweb_error.log;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
uwsgi_read_timeout 600;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /opt/okr/static/; # 这里的目录必须是当前项目的static目录
}
}
在进行配置的时候,我有个问题一直想不通。nginx到底是如何uwsgi产生关联。现在看来大概最主要的就是这两行配置 include uwsgi_params; uwsgi_pass 127.0.0.1:8000; include 必须指定为uwsgi_params;而uwsgi_pass指的本机IP的端口号与myweb_uwsgi.ini配置中的文件中的必须一致.
django设置
收集Django静态文件,把Django自带的静态文件收集到同一个static中,不然访问Django的admin页面会找不到静态文件。在django的setting文件中,添加下面一行内容.
settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "/static/)
DEBUG = False
ALLOWED_HOSTS = ["*"]
运行manage.py命令
python manage.py collectstatic
启动服务
uwsgi --ini uwsgi.ini
/usr/local/nginx/sbin/nginx
测试访问
http://127.0.0.1:8099