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

注:安装配置比较简单,没做注释,有不理解的可以给我留言。

时间: 2024-12-18 23:49:21

nginx+uwsgi+django的相关文章

Ubuntu Nginx uwsgi django 初试

/************************************************************************************** * Ubuntu Nginx uwsgi django 初试 * 说明: * 最近打算通过Python搭建一个数据收集的网站,先做一个搭建测试. * * 2016-8-5 深圳 南山平山村 曾剑锋 ***************************************************************

五步教你实现使用Nginx+uWSGI+Django方法部署Django程序

新建一个XML文件: djangochina_socket.xml,将它放在/data/www/org_management目录下: Nginx采用8077端口与uWSGI通讯,请确保此端口没有被其它程序采用 <uwsgi> <socket>:8077</socket> <chdir>/data/www/org_management</chdir> <module>org_management.wsgi</module>

转载nginx+uwsgi+django

Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.nginx把所有静态请求自己来处理(这是NGINX的强项).然后,NGINX将所有非静态请求通过uwsgi传递给Django,由Django来进行处理,从而完成一次WEB请求. 可见,uwsgi的作用就类似一个桥接器.起到桥梁的作用. Linux的强项是用来做服务器,所以,下面的整个部署过程我们选择在U

nginx+Uwsgi+Django总结与分析

配置与调试nginx与uwsgi 参考: 1.uWSGI其三:uWSGI搭配Nginx使用 2.学习VirtualEnv和Nginx+uwsgi用于django项目部署 3.部署备忘 4.nginx+uwsgi 5.编程小结 6.nginx + uwsgi + django + python 部署 最近三天一直在调试Django+nginx+uwsgi+sqlite3部署的阿里云的服务器,系统是ubuntu的所以下载什么的都很方便sudo spt-get就可以了 所以想着把这几天遇到的问题跟过程

Nginx+uWSGI+Django原理

Python的Web开发中,如果使用Django框架,那么较为成熟稳定的服务器架构一般是Nginx+uWSGI+Django.而为什么一定要三个结合在一起呢?直接使用Django的runserver来启动服务器进程,或者uWSGI+Django可不可以呢?为什么? 概念说明: APP(应用程序),就是开发者写的应用程序,例如django,bottle这些.记录怎么处理客户端发来的请求的逻辑部分. WSGI,是一个协议,Python用于Web开发的协议 uWSGI,是一个程序,充当Web服务器或中

Nginx+uWSGI+Django原理(转发)

Python的Web开发中,如果使用Django框架,那么较为成熟稳定的服务器架构一般是Nginx+uWSGI+Django.而为什么一定要三个结合在一起呢?直接使用Django的runserver来启动服务器进程,或者uWSGI+Django可不可以呢?为什么? 概念说明: APP(应用程序),就是开发者写的应用程序,例如django,bottle这些.记录怎么处理客户端发来的请求的逻辑部分. WSGI,是一个协议,Python用于Web开发的协议 uWSGI,是一个程序,充当Web服务器或中

VPS上配置NGINX+UWSGI+DJANGO+MYSQL

1.安装Django 参见:http://djangobook.py3k.cn/2.0/chapter02/ 2.安装pip $ sudo apt-get install python-pip python-dev build-essential $ sudo pip install --upgrade pip $ sudo pip install --upgrade virtualenv $ sudo apt-get install libmysqld-dev 3.安装MySQL $ apt-

debian完整部署 Nginx + uWSGI + Django

debian完整部署 Nginx + uWSGI + Django 手工部署一个Django服务器真心不容易,需要安装很多东西.从头开始搭建服务器,主要是为了梳理一下后续开发中一般为碰到的平台部署.对后续问题的解决有一定帮助. 通常部署有2中方式: 一种是使用现成提供的服务器包用apt-get这种方式安装的.这种方式比较简单,但没有新版本. 另外就是使用源代码自己编译安装,这种比较繁琐,但能选择适合的版本安装. 这里介绍的是第二种,使用源代码编译的版本进行安装部署. 部署测试环境: - wind

使用Nginx+uWSGI+Django方法部署Django程序(下)

在上一篇文章<五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(上)>中,阐述了如何只使用uWSGI来部署Django程序. 当然,单单只有uWSGI是不够的,在实际的部署环境中,Nginx是必不可少的工具. 在本篇文章中,我将一直延用“N步法”的风格来阐述如何将uWSGI与Nginx做连接来部署Django程序.并在最后,会较为完整的阐述本社区的部署方法. 本文大纲: 环境介绍 配置uWSGI 配置Nginx Nginx+uWSGI+Django的实现方式 一些建

【转】五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(下)

五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(下) By Django中国社区 at 2013-05-18 04:38 在上一篇文章<五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(上)>中,阐述了如何只使用uWSGI来部署Django程序. 当然,单单只有uWSGI是不够的,在实际的部署环境中,Nginx是必不可少的工具. 在本篇文章中,我将一直延用“N步法”的风格来阐述如何将uWSGI与Nginx做连接来部署Django程序.并