以下环境:
django:1.8.2
python:2.7.3
项目名称:flow_check
app名称:flow_check
0x01 安装nginx
yum install nginx
默认www目录:/usr/share/nginx/html
0x02 安装uwsgi
pip install uwsgi
0x03 安装django
pip install django==1.8.2
0x04 配置uwsgi
新建目录/logs用于存放nginx和uwsgi日志
mkdir /logs
在项目工程下新建个django_wsgi.py,内容如下,其中【flow_check】是项目名称指的是找flow_check目录下的settings.py:
#!/usr/bin/env python
# coding: utf-8
import os
import sys
# 将系统的编码设置为UTF8
reload(sys)
sys.setdefaultencoding('utf8')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "flow_check.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
再同一目录下新建个uwsgi.xml,内容如下:
<uwsgi>
<socket>:8077</socket>
<chdir>/usr/share/nginx/html/flow_check</chdir>
<module>django_wsgi</module>
<processes>4</processes> <!-- 进程数 -->
<master />
<memory-report />
<pidfile>/tmp/uswgi.pid</pidfile>
<daemonize>/logs/uwsgi.log</daemonize>
<harakiri>60</harakiri>
<reaper />
<no-orphans />
</uwsgi>
0x05 配置nginx
vim /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes 1;
error_log /data/logs/nginx-error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data/logs/nginx-access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf;
}
vim /etc/nginx/conf.d/default.conf
#
# The default server
#
server {
listen 8080 default_server;
server_name _;
#charset koi8-r;
access_log /logs/access.log;
error_log /logs/error.log;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
# root /usr/share/nginx/html/flow_check;
include uwsgi_params;
uwsgi_pass 127.0.0.1:8077;
# uwsgi_param UWSGI_CHDIR /usr/share/nginx/html/flow_check;
# uwsgi_param UWSGI_SCRIPT django_wsgi;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
location ~ ^/static/ {
root /usr/share/nginx/html/flow_check/;
expires 24h;
access_log off;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
0x06 运行nginx和uwsgi
编写uwsgi启动脚本:
vim /etc/init.d/uwsgi
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add uwsgi'
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the uwsgi web server
# Description: starts uwsgi using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi
DAEMON=/usr/local/bin/uwsgi
CONFIGFILE=/usr/share/nginx/html/flow_check/uwsgi.xml
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON $CONFIGFILE || echo -n "uwsgi already running"
}
do_stop() {
$DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
rm -f $PIDFILE
echo "$DAEMON STOPED."
}
do_reload() {
$DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"
}
do_status() {
ps aux|grep $DAEMON
}
case "$1" in
status)
echo -en "Status $NAME: \n"
do_status
;;
start)
echo -en "Starting $NAME: \n"
do_start
;;
stop)
echo -en "Stopping $NAME: \n"
do_stop
;;
reload|graceful)
echo -en "Reloading $NAME: \n"
do_reload
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2
exit 3
;;
esac
exit 0
chmod 755 /etc/init.d/uwsgi
chkconfig uwsgi on
service uwsgi start
service nginx start
PS:如果出现以下错误
No handlers could be found for logger "django.request"
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 218, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 261, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 97, in technical_500_response
html = reporter.get_traceback_html()
File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 384, in get_traceback_html
return t.render(c)
是因为django版本的问题,问题出在django_wsgi.py这个文件上,1.6以下应该是这样写:
#!/usr/bin/env python
# coding: utf-8
import os
import sys
# 将系统的编码设置为UTF8
reload(sys)
sys.setdefaultencoding('utf8')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "flow_check.settings")
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
而1.6和1.6以上,需要这样写:
#!/usr/bin/env python
# coding: utf-8
import os
import sys
# 将系统的编码设置为UTF8
reload(sys)
sys.setdefaultencoding('utf8')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "flow_check.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()