设为首页 收藏本站
查看: 581|回复: 0

[经验分享] nginx+uwsgi+django

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-2-14 09:05:43 | 显示全部楼层 |阅读模式
以下操作全在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 ... 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.iyunv.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

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


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-341835-1-1.html 上篇帖子: centos7编译安装nginx 下篇帖子: nginx 禁止ip访问只允许域名访问
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表