您的位置:首页 > 运维架构 > Nginx

nginx+uwsgi+django1.11 环境部署

2017-10-22 21:38 501 查看
    前段时间配置 python3.6.2+nginx+uwsgi 用于 django1.11 做请求,整理记录一下(自己配置测试没有问题)。

一、安装配置环境

python 3.6.2(virtualenv)
djano 1.11
nginx 1.12.1
uwsgi

1、安装virtualenv python 3.6.2,请查看之前写的环境初始化文档

2、django安装也查看环境初始化文档

3、安装nginx
在/etc/yum.repos.d/目录下创建一个源配置文件nginx.repo:
cd /etc/yum.repos.d/
vim nginx.repo
#填写如下内容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
保存,则会产生一个/etc/yum.repos.d/nginx.repo文件。
下面直接执行如下指令即可自动安装好Nginx:
yum install nginx -y
安装完成,下面直接就可以启动Nginx了:
service nginx start
现在Nginx已经启动了,直接访问服务器就能看到Nginx欢迎页面了的。

4、安装uwsgi
pip install uwsgi

django

一个基于python的开源web框架,请确保自己熟悉它的框架目录结构。

uWSGI

一个基于自有的uwsgi协议、wsgi协议和http服务协议的web网关

nginx

常用高性能代理服务器

wsgi.py
django项目携带的一个wsgi接口文件

例子项目名叫opsweb的话,此文件就位于 opsweb/opsweb/wsgi.py

二、配置

首先,确保你已经安装好了nginx并可以正常使用。 

接着,别忘了确认自己项目所需的django已经完成安装并正常工作。 

##配置前,项目目录结构
cd /root/Documents/project
tree opsweb
opsweb/
|-- accounts
|-- dashboard
|-- manage.py
|-- opsweb
|   |-- __init__.py
|   |-- settings_dev.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
|-- resources
|-- static

配置nginx
默认配置文件的路径是:/etc/nginx/nginx.conf
然后,确保nginx.conf的同目录下有uwsgi_params文件(/etc/nginx/uwsgi_params),没有的话根据链接获取, 后面要用到

vim /etc/nginx/nginx.conf
##修改第一行user配置root,如果你的项目用户权限是nginx用户也可配置nginx
user  root;
##最后http段落里include配置成下面的内容
include /root/Documents/project/opsweb/opsweb.conf;

##这里的文件是上面include指定的文件,也可以放置在nginx的默认/etc/nginx/conf.d/目录下面
##这里是为了配置测试查看方便
vim /root/Documents/project/opsweb/opsweb.conf ##添加下面内容
server {
listen 80;
server_name localhost;
charset     utf-8;
access_log      /root/Documents/project/opsweb/access_log; ##nginx访问日志文件,最后放置项目文件下
error_log       /root/Documents/project/opsweb/error_log;
client_max_body_size 75M;

location /static {
alias /root/Documents/project/opsweb/static; ##指定项目的静态文件目录,注意权限
}

location / {
include     /etc/nginx/uwsgi_params; ##queren这个文件是存在的
uwsgi_pass  127.0.0.1:9090;
}
}

#配置nginx后,重启nginx
service nginx restart

配置uwsgi
vim /root/Documents/project/opsweb/uwsgi_opsweb.ini
##这里的uwsgi_opsweb.ini要命名成*.ini后缀文件
##uwsgi配置文件放置在项目文件里,也可把这些文件整理放置在统一的目录下
[uwsgi]
socket = 127.0.0.1:9090 #uwsgi监听的地址及端口(与nginx配置是一致的)
chmod-socket=666
chdir = /root/Documents/project/opsweb ##项目的主目录
module = opsweb.wsgi:application  ##是项目名称.wsgi:application
wsgi-file = /root/Documents/project/opsweb/opsweb/wsgi.py  ##项目里的wsgi.py文件路径
master = true
pythonpath = /root/Desktop/reboot/env/lib/python3.6/site-packages  ##指定virtualenv环境里python3.6.2的库目录,一定要指定,是使用virtualenv环境启动python的主要配置行
processes=2
threads=2
max-requests=2000
enable-threads = true
vacuum=true
daemonize=/root/Documents/project/opsweb/opsweb.log  ##指定uwsig的日志文件,否则会输出到屏幕上,这个在调试时可以暂时不配置
pidfile = /root/Documents/project/opsweb/lin.pid
py-autoreload = 1

##确保进入virtualenv环境
source ~/Desktop/reboot/env/bin/activate
cd /root/Documents/project/opsweb ##项目目录下
执行:uwsgi --ini /root/Documents/project/opsweb/uwsgi_opsweb.ini
##输出下面内容,表示成功
'''
*** Starting uWSGI 2.0.15 (64bit) on [Tue Oct  3 09:06:36 2017] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-11) on 03 October 2017 01:29:18
os: Linux-2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014
nodename: web.com
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /root/Documents/project/opsweb
writing pidfile to /root/Documents/project/opsweb/lin.pid
detected binary path: /root/Desktop/reboot/env/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /root/Documents/project/opsweb
your processes number limit is 14717
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 socket 0 bound to TCP address 127.0.0.1:9090 #uwsgi监听的地址及端口(在nginx配置中会用到) fd 3
Python version: 3.6.2 (default, Aug  5 2017, 01:32:30)  [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]
Python main interpreter initialized at 0xeb43b0
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 249216 bytes (243 KB) for 4 cores
*** Operational MODE: preforking+threaded ***
added /root/Desktop/reboot/env/lib/python3.6/site-packages/ to pythonpath.
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0xeb43b0 pid: 6870 (default app)
mountpoint  already configured. skip.
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 6870)
spawned uWSGI worker 1 (pid: 6872, cores: 2)
spawned uWSGI worker 2 (pid: 6873, cores: 2)
Python auto-reloader enabled
'''

##配置完成后,项目目录结构
cd /root/Documents/project
tree opsweb
opsweb/
|-- access_log
|-- accounts
|-- dashboard
|-- error_log
|-- lin.pid
|-- manage.py
|-- opsweb
|   |-- __init__.py
|   |-- settings_dev.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
|-- opsweb.conf
|-- opsweb.log
|-- resources
|-- static
`-- uwsgi_opsweb.ini

使用浏览器访问(是自己服务器的ip地址)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息