一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

CentOS6.5_64下 nginx+uwsgi+Python +多站点环境搭建 python web django 框架

时间:2022-06-30 21:04:04 编辑:袖梨 来源:一聚教程网

nginx+uwsgi+Python环境介绍:
系统:CentOS6.5_64_mini
nginx版本:nginx-1.6.0
python版本:Python2.7.8
 
第一部分系统设置
1:查看系统内核
#  uname -r
2.6.32-431.el6.x86_64

2:更新内核
#  yum -y install kernel

3:重启系统
#  reboot

4:重启后查看是否启用新内核
#  uname -r
2.6.32-431.20.5.el6.x86_64

5:可以删除老内核节约空间呵呵
#  rpm -q kernel
kernel-2.6.32-431.el6.x86_64
kernel-2.6.32-431.20.5.el6.x86_64
#  rpm -e kernel-2.6.32-431.el6.x86_64

6:删除mysql
#  rpm -qa|grep mysql
#  rpm -e --allmatches --nodeps mysql-libs-5.1.71-1.el6.x86_64

第二部分安装安装nginx
1:安装Nginx所需的库:
#  yum -y install gcc gcc-c++ openssl openssl-devel wget
 
2.创建/main/soft文件夹
#  mkdir /yunwei8/soft
#  cd /yunwei8/soft
 
3:下载所需的安装包至soft文件夹下
#  wget http://down.yunwei8.com/soft/linux/distribute-0.6.49.tar.gz
#  wget http://down.yunwei8.com/soft/linux/nginx-1.6.0.tar.gz
#  wget http://down.yunwei8.com/soft/linux/pcre-8.35.tar.gz
#  wget http://down.yunwei8.com/soft/linux/Python-2.7.8.tar.gz
 

4:安装Nginx所需的pcre库
解压并安装
#  tar zxvf pcre-8.35.tar.gz
#  cd pcre-8.35/
#  ./configure
#  make && make install
#  cd ../
 
5:创建www用户和组,创建www虚拟主机使用的目录,以及Nginx使用的日志目录,并且赋予他们适当的权限
#  /usr/sbin/groupadd www
#  /usr/sbin/useradd -s /sbin/nologin -M -g www www
#  mkdir -p /yunwei8/web/www
#  chmod 777 /yunwei8/web/www
#  chown -R www:www /yunwei8/web/www
#  chmod g+s /yunwei8/web/www
#  mkdir -p /yunwei8/web/logs
#  chmod +w /yunwei8/web/logs
#  chown -R www:www /yunwei8/web/logs
 
6:安装nginx
解压并安装
#  cd /yunwei8/soft
#  tar zxvf nginx-1.6.0.tar.gz
#  cd nginx-1.6.0
#  ./configure --user=www --group=www --prefix=/yunwei8/server/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-pcre=../pcre-8.35 --with-pcre-jit
#  make && make install

7:修改 nginx.conf配置文件
#  vi /yunwei8/server/nginx/conf/nginx.conf

修改前面几行为:
user www www;
worker_processes 8;
error_log  /yunwei8/web/logs/nginx_error.log  crit;
pid logs/nginx.pid;
events{
  use epoll;
  worker_connections 65535;
}

测试
#  /yunwei8/server/nginx/sbin/nginx -t
 
如果显示下面信息,即表示配置没问题
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
 
输入代码运行nginx服务
#  /yunwei8/server/nginx/sbin/nginx
 
查看nginx进程
#  ps aux|grep [n]ginx
root     10603  0.0  0.1  42996  1128 ?        Ss   04:06   0:00 nginx: master process /yunwei8/server/nginx/sbin/nginx
www      10604  0.2  2.8  70132 28512 ?        S    04:06   0:00 nginx: worker process          
www      10605  0.2  2.8  70132 28512 ?        S    04:06   0:00 nginx: worker process          
www      10606  0.1  2.8  70132 28512 ?        S    04:06   0:00 nginx: worker process          
www      10607  0.3  2.8  70132 28512 ?        S    04:06   0:00 nginx: worker process          
www      10608  0.2  2.8  70132 28512 ?        S    04:06   0:00 nginx: worker process          
www      10609  0.2  2.8  70132 28512 ?        S    04:06   0:00 nginx: worker process          
www      10610  0.2  2.8  70132 28512 ?        S    04:06   0:00 nginx: worker process          
www      10611  0.2  2.8  70132 28496 ?        S    04:06   0:00 nginx: worker process
显以类似上面的信息,即表示nginx已经启动
 
8:编写nginx 启动服务
#  vi /etc/init.d/nginx
 
输入以下代码并保存
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /yunwei8/server/nginx/conf/nginx.conf
# pidfile:     /yunwei8/server/nginx/logs/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/yunwei8/server/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/yunwei8/server/nginx/conf/nginx.conf"
 
lockfile=/var/lock/subsys/nginx
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"
        exit 2
esac
 

保存后,设置权限并添加到启动服务列表中
#  chmod 755 /etc/init.d/nginx
#  chkconfig --add nginx
#  chkconfig --level 345 nginx on
#  service nginx start
 
测试脚本
#  service nginx restart

9:防火墙设置
端口开放
#  /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
 
然后保存:
#  /etc/rc.d/init.d/iptables save

第三部分安装Python及相应环境
1:安装Python2.7.8所需要的库
#  yum -y groupinstall "Development tools"
#  yum -y install zlib-devel bzip2-devel pcre-devel ncurses-devel sqlite-devel readline-devel tk-devel

2:安装Python2.7.8
#  cd /yunwei8/soft
#  xz -d Python-2.7.8.tar.xz
#  tar xvf Python-2.7.8.tar
#  cd Python-2.7.8
#  ./configure --prefix=/usr/local
#  make && make altinstall

3:安装完毕后可是使用如下命令进入python2.7的环境
#  python2.7
 
退出指令有点特别,如下
#  exit()

4:安装Python包管理
4.1:安装distribute-0.6.49
#  cd /yunwei8/soft/
#  tar zxvf distribute-0.6.49.tar.gz
#  cd distribute-0.6.49
#  python2.7 setup.py install
#  easy_install --version

4.2:安装pip包,安装pip的好处是可以pip list、pip uninstall管理Python包,easy_install没有这个功能,只有uninstall
#  easy_install pip
#  pip --version

5:安装uwsgi
5.1:安装uwsgi
#  pip install uwsgi
 
5.2:增加软连接防止报错
#  ln -s /usr/local/lib/libpcre.so.1 /lib64/
 
5.3查看版本
#  uwsgi --version
 
5.4:测试uwsgi是否正常
#  cd /yunwei8/web/www/
#  vi yunwei8.py

复制如下代码保存并退出
def application(env, start_response):
  start_response('200 OK', [('Content-Type','text/html')])
  return "Hello World"
 
赋予相应权限
#  chmod 755 yunwei8.py
 
关闭防火墙
#  service iptables stop
 
启动uwsgi
#  uwsgi --http :8001 --wsgi-file yunwei8.py
 
在浏览器内输入:http://你的IP地址:8001,看是否有“Hello World”输出,若没有输出,请检查你的安装过程
 
6:安装django
6.1:安装django
#  pip install django
 
6.2:测试django是否正常运行
#  cd /yunwei8/web/www
#  django-admin.py startproject test1
#  cd test1
#  python2.7 manage.py runserver 0.0.0.0:8002
 
在浏览器内输入:http://你的IP地址:8002
It worked!
Congratulations on your first Django-powered page.

7:配置uwsgi
7.1uwsgi支持ini、xml等多种配置方式,但个人感觉ini更方便:
在/ect/目录下新建uwsgi9090.ini,添加如下配置:
#  vi /etc/uwsgi9090.ini
[uwsgi]
socket = 127.0.0.1:9090
master = true                                      #主进程
vhost = true                                        #多站模式
no-stie = true                                      #多站模式时不设置入口模块和文件
workers = 2                                        #子进程数
reload-mercy = 10    
vacuum = true                                    #退出、重启时清理文件
max-requests = 1000  
limit-as = 512
buffer-sizi = 30000
pidfile = /var/run/uwsgi9090.pid            #pid文件,用于下面的脚本启动、停止该进程
daemonize = /yunwei8/web/logs/uwsgi9090.log
 
7.2设置uwsgi开机启动,在/ect/init.d/目录下新建uwsgi9090文件,并复制如下内容:
#  vi /etc/init.d/uwsgi9090
#! /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
 
# Author:   licess
# website:  http://lnmp.org
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi9090
DAEMON=/usr/local/bin/uwsgi
CONFIGFILE=/etc/$NAME.ini
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
 

7.3添加服务并设置开机启动
#  chmod 755 /etc/init.d/uwsgi9090
#  chkconfig --add uwsgi9090
#  chkconfig uwsgi9090 on

8:配置nginx
#  rm -rf /yunwei8/server/nginx/conf/nginx.conf
#  vi /yunwei8/server/nginx/conf/nginx.conf
user  www www;
worker_processes  8;
error_log  /yunwei8/web/logs/nginx_error.log  crit;
pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  60;
   
    tcp_nodelay on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    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;
server {
        listen       80;
        server_name  localhost;
       
        location / {           
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9090;                                              #必须和uwsgi中的设置一致
            uwsgi_param UWSGI_SCRIPT test1.wsgi;                            #入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录
            uwsgi_param UWSGI_CHDIR /yunwei8/web/www/test1;       #项目根目录
            index  index.html index.htm;
            client_max_body_size 35m;
        }
     error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

9:测试启动uwsgi并重启nginx
#  service uwsgi9090 start
#  service nginx restart

10:开启防火墙
#  service iptables start

11:访问测试
在浏览器内输入:http://你的IP地址
It worked!
Congratulations on your first Django-powered page.

12:多站配置
我采用运行多个uwsgi服务的方法来实现多个站点
12.1配置uwsgi
在/ect/目录下新建uwsgi9091.ini
#  vi /etc/uwsgi9091.ini
[uwsgi]
socket = 127.0.0.1:9091
master = true
vhost = true
no-stie = true
workers = 2
reload-mercy = 10    
vacuum = true
max-requests = 1000  
limit-as = 512
buffer-sizi = 30000
pidfile = /var/run/uwsgi9091.pid
daemonize = /yunwei8/web/logs/uwsgi9091.log
 
12.2设置uwsgi开机启动,在/ect/init.d/目录下新建uwsgi9091文件,并复制如下内容:
#  vi /etc/init.d/uwsgi9091
#! /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
 
# Author:   licess
# website:  http://lnmp.org
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi9091
DAEMON=/usr/local/bin/uwsgi
CONFIGFILE=/etc/$NAME.ini
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
 

12.3添加服务并设置开机启动
#  chmod 755 /etc/init.d/uwsgi9091
#  chkconfig --add uwsgi9091
#  chkconfig uwsgi9091 on
 
12.4使用django增加一个test2
#  cd /yunwei8/web/www
#  django-admin.py startproject test2
 
12.5然后修改nginx的配置文件为:
#  rm -rf /yunwei8/server/nginx/conf/nginx.conf
#  vi /yunwei8/server/nginx/conf/nginx.conf
user  www www;
worker_processes  8;
error_log  /yunwei8/web/logs/nginx_error.log  crit;
pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  60;
   
    tcp_nodelay on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    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;
server {
        listen       80;
        server_name  test1.yunwei8.com;
       
        location / {           
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9090;
            uwsgi_param UWSGI_SCRIPT test1.wsgi;
            uwsgi_param UWSGI_CHDIR /yunwei8/web/www/test1;
            index  index.html index.htm;
            client_max_body_size 35m;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
     }
server {
        listen       80;
        server_name  test2.yunwei8.com;
        location / {           
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9091;
            uwsgi_param UWSGI_SCRIPT test2.wsgi;
            uwsgi_param UWSGI_CHDIR /yunwei8/web/www/test2;
            index  index.html index.htm;
   client_max_body_size 35m;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
     }
}
 
12.6重启nginx
#  service uwsgi9090 start
#  service nginx restart
 
13:访问测试
首先在本地计算机中C:WindowsSystem32driversetc目录下Host文件下添加网址:
192.168.100.11    test1.yunwei8.com
192.168.100.11    test2.yunwei8.com
在浏览器内输入:http://test1.yunwei8.com、http://test2.yunwei8.com
It worked!
Congratulations on your first Django-powered page.

14:安装Mysqldb
#  yum -y install mysql-devel
#  easy_install-2.7 MySQL-python

热门栏目