nginx+uwsgi+django
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+uwsgi+django相关的知识,希望对你有一定的参考价值。
以下操作全在root下操作,不是必须。nginx使用淘宝开发tengin。
更新依赖:
yum update -y
yum groupinstall "Development Tools" -y
yum groupinstall "Perl Support" -y
yum install ntpdate zlib libjpeg libpng curl zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel apr apr-util autoconf freetype gd libxslt libxslt-devel libxml2 openssl* ncurses ncurses-devel tree curl lrzsz dos2unix gcc gcc-c++ dstat e2fsprogs-devel flex flex-devel libtiff-devel pam-devel uuid libffi-devel dstat -y
下载源码包:
wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz
wget http://zlib.net/zlib-1.2.8.tar.gz
wget https://sourceforge.net/projects/pcre/files/pcre/8.39/pcre-8.39.tar.gz
wget http://luajit.org/download/LuaJIT-2.0.3.tar.gz
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/master.zip
安装Tengin及Lua语言依赖(Lua依赖可以不装,个人需求):
tar xf pcre-8.39.tar.gz
cd pcre-8.39
./configure --prefix=/usr/local/pcre
make && make install
cd ..
tar xf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=/usr/local/zlib
make && make install
cd ..
tar xf LuaJIT-2.0.3.tar.gz
cd LuaJIT-2.0.3
make && make install
echo "export LUAJIT_LIB=/usr/local/lib" >>/etc/profile
echo "export LUAJIT_INC=/usr/local/include/luajit-2.0" >>/etc/profile
source /etc/profile
cd ..
tar xf v0.3.0.tar.gz
unzip master.zip
useradd nginx
tar xf tengine-2.1.2.tar.gz
cd tengine-2.1.2
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre=/root/pcre-8.39 \
--with-http_gzip_static_module \
--with-http_concat_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-zlib=/root/zlib-1.2.8 \
--add-module=/root/lua-nginx-module-master \
--add-module=/root/ngx_devel_kit-0.3.0
make
make install
cd ..
echo "/usr/local/lib/" >>/etc/ld.so.conf
ldconfig
安装python2.7以上版本:
wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz
mv /usr/bin/python /usr/bin/python26
tar xf Python-2.7.12.tar.xz
cd Python-2.7.12
./configure
make -j8 && make install
ln -s /usr/local/bin/python2.7 /usr/bin/python
# python -V // check version
sed -i "s#\#\!/usr/bin/python#\#\!/usr/bin/python26#g" /usr/bin/yum
安装pip&uwsgi&django:
wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
python get-pip.py
pip install --upgrade pip
pip install uwsgi
pip install django
创建django项目:
Reference:http://784687488.blog.51cto.com/8774365/1897368
配置uwsgid.ini:
cat >>/etc/uwsgid.ini<<EOF
[uwsgi]
socket = 0.0.0.0:18000
master = true
vhost = true
no-stie = true
workers = 8
reload-mercy = 10
vacuum = true
max-requests = 1000
limit-as = 512
buffer-sizi = 30000
chdir = /data/DjangoProject
module = DjangoProject.wsgi
pidfile = /data/DjangoProject/tmp/uwsgid.pid
daemonize = /data/DjangoProject/logs/uwsgid.log
EOF
配置uwsgi系统启动:
cat >>/etc/init.d/uwsgid<<EOF
#! /bin/sh
# chkconfig: 235 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‘
. /etc/init.d/functions
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgid
DAEMON=/usr/bin/uwsgi
CONFIGFILE=/etc/$NAME.ini
PIDFILE=/data/DjangoProject/tmp/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON $CONFIGFILE || action "uwsgi already running" /bin/true
}
do_stop() {
$DAEMON --stop $PIDFILE || action "uwsgi not running" /bin/true
rm -f $PIDFILE
echo "$DAEMON STOPED."
}
do_reload() {
$DAEMON --reload $PIDFILE || action "uwsgi can‘t reload" /bin/false
}
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
EOF
配置nginx:
mkdir /usr/local/nginx/conf.d
cat >>/usr/local/nginx/conf/nginx.conf<<EOF
user root root;
worker_processes auto;
#worker_processes 2;
worker_cpu_affinity auto;
worker_rlimit_nofile 4096;
#error_log logs/error.log;
error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 4096;
}
# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}
http {
include mime.types;
default_type application/octet-stream;
fastcgi_intercept_errors on;
charset utf-8;
#log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
# ‘$status $body_bytes_sent "$http_referer" ‘
# ‘"$http_user_agent" "$http_x_forwarded_for"‘;
log_format main ‘{"@timestamp":"$time_iso8601"|‘
‘"client_ip":"$remote_addr"|‘
‘"request":"$request"|‘
‘"url":"$scheme://$host$request_uri"|‘
‘"status":"$status"|‘
‘"bytes":"$body_bytes_sent"|‘
‘"response_time":"$request_time"|‘
‘"upstream_host":"$upstream_addr"|‘
‘"upstream_response":"$upstream_status"|‘
‘"upstream_response_time":"$upstream_response_time"|‘
‘"upstream_content_type":"$upstream_http_content_type"|‘
‘"x_forword":"$http_x_forwarded_for"|‘
‘"referer":"$http_referer"|‘
‘"agent":"$http_user_agent"}|‘
‘"host":"$host"|‘
‘"server_name":"$server_name"|‘
‘"http_host":"$http_host"‘;
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
#or
#tcp_nodelay on;
# The hash table of server name
#server_name_hash_max_size 128;
#server_name_hash_bucket_size 128;
# The request header buffer of client. The parameter value according to the result of system command ‘getconf PAGESIZE‘ setup.
#client_header_buffers_size 4k;
#large_client_header_buffers 2 32k;
# Setup upload file size of client. According to the actual situation.
#client_max_body_size 300m;
# Setup the buffer size of reading client request body. By default, buffer size equal to two memory pages by 16k.
#client_body_buffer_size 16k;
# setup the store path of temporary files.
#client_body_temp_path client_body_temp;
# Proxy server use configuration. by default use upstream.
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path proxy_temp;
# Enable compression, by default use gzip. Can also be zlib cpmpression.
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
# The following configuration is the simple instance of upstream
#upstream test{
# ip_hash;
# server 192.168.1.1:8080 weight=1 max_fails=1 fail_timeout=60s;
#}
keepalive_timeout 200;
include /usr/local/nginx/conf.d/*.conf;
}
EOF
cat >>/usr/local/nginx/conf.d/DjangoProject.conf<<EOF
server {
listen 80;
server_name www.domain.com;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:18000;
uwsgi_param UWSGI_SCRIPT DjangoProject.wsgi;
uwsgi_param UWSGI_CHDIR /data/DjangoProject;
index index.html index.htm;
client_max_body_size 35m;
}
location /static/ {
alias /data/DjangoProject/static/;
}
}
EOF
配置nginx系统启动:
cat >>/etc/init.d/nginx<<EOF
#!/bin/bash
# chkconfig: 235 85 15
# description: nginx is a World Wide Web server. It is used to serve
. /etc/rc.d/init.d/functions
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
start() {
if [ ! -f /usr/local/nginx/logs/nginx.pid ];then
[ -x /usr/local/nginx/sbin/nginx ] || exit 5
/usr/local/nginx/sbin/nginx
retval=$?
if [ $retval -eq 0 ];then
action "Nginx Starting Successful" /bin/true
else
action "Nginx Starting Failed. Please check error log of nginx." /bin/false
exit 126
fi
else
echo "Nginx service have been started."
fi
return $retval
}
stop() {
if [ -f /usr/local/nginx/logs/nginx.pid ];then
[ -x /usr/local/nginx/sbin/nginx ] || exit 5
/usr/local/nginx/sbin/nginx -s stop
retval=$?
if [ $retval -eq 0 ];then
action "Nginx Stoping Successful" /bin/true
else
action "Nginx Stoping Failed. Please check error log of nginx." /bin/false
exit 127
fi
else
echo "Nginx service have been stop."
fi
return $retval
}
restart() {
if [ -f /usr/local/nginx/logs/nginx.pid ];then
stop
sleep 1
start
else
echo "Nginx service does not started."
fi
}
reload() {
if [ -f /usr/local/nginx/logs/nginx.pid ];then
/usr/local/nginx/sbin/nginx -s reload
action "Nginx reloading Successful" /bin/true
else
echo "Nginx service does not started."
fi
}
case "$1" in
start)
$1
;;
stop)
$1
;;
restart)
$1
;;
reload)
$1
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 2
esac
EOF
启动:
/etc/init.d/uwsgi start
/etc/init.d/nginx start
netstat -lntp|egrep "18000|80"
tcp 0 0 0.0.0.0:18000 0.0.0.0:* LISTEN 25776/uwsgi
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4272/nginx
注:安装配置比较简单,没做注释,有不理解的可以给我留言。
本文出自 “zhenj8nanzhuce” 博客,请务必保留此出处http://784687488.blog.51cto.com/8774365/1897407
以上是关于nginx+uwsgi+django的主要内容,如果未能解决你的问题,请参考以下文章