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

热门教程

Nginx+uWSGI+Django+Python环境安装配置详解

时间:2022-06-30 18:36:45 编辑:袖梨 来源:一聚教程网

一、安装PYTHON2.7.7:

[root@hz tools]# pwd
/byrd/tools
[root@hz tools]# wget https://www.python.org/ftp/python/2.7.7/Python-2.7.7.tgz
[root@hz tools]# tar zxf Python-2.7.7.tgz
[root@hz tools]# cd Python-2.7.7
[root@hz Python-2.7.7]# ./configure --prefix=/byrd/server/Python-2.7.7
[root@hz Python-2.7.7]# make && make install
[root@hz Python-2.7.7]# mv /usr/bin/python /usr/bin/python_2.6.6
[root@hz Python-2.7.7]# ln -s /byrd/server/Python-2.7.7/bin/python2.7 /usr/bin/python
########################################################################
注意:vim /usr/bin/yum    #将!/usr/bin/python改成!/usr/bin/python2.6。
########################################################################

二、安装uWSGI:

方式1:pip方式

[root@hz tools]# wget https://bootstrap.pypa.io/get-pip.py
[root@hz tools]# python get-pip.py
[root@hz tools]# sed -i "s#PATH=\$PATH:\$HOME\/bin#PATH=\$PATH:\$HOME\/bin:\/byrd\/server\/Python-2.7.7\/bin#g" ~/.bash_profile
[root@hz tools]# source ~/.bash_profile
[root@hz tools]# pip install uwsgi

方式2:编译安装

[root@hz tools]# wget https://pypi.python.org/packages/source/s/setuptools/setuptools-5.2.tar.gz
[root@hz setuptools-5.2]# python setup.py build
[root@hz setuptools-5.2]# python setup.py install
[root@hz tools]# wget http://projects.unbit.it/downloads/uwsgi-2.0.5.tar.gz    #安装uwsgi必须安装setuptools
[root@hz tools]# tar zxf uwsgi-2.0.5.tar.gz
[root@hz tools]# cd uwsgi-2.0.5    
[root@hz uwsgi-2.0.5]# python setup.py build
[root@hz uwsgi-2.0.5]# python setup.py install    #make也可以
Copying uWSGI.egg-info to /byrd/server/Python-2.7.7/lib/python2.7/site-packages/uWSGI-2.0.5-py2.7.egg-info
running install_scripts
[root@hz uwsgi-2.0.5]# make
[root@hz uwsgi-2.0.5]# cp uwsgi /usr/bin
[root@hz uwsgi-2.0.5]# chmod +x /usr/bin/uwsgi

三、安装Django:

其他:

[root@hz tools]# wget https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip#md5=654f75b302db6ed8dc5a898c625e030c
[root@hz tools]# unzip MySQL-python-1.2.5.zip
[root@hz tools]# cd MySQL-python-1.2.5
[root@hz MySQL-python-1.2.5]# sed -i 's#\#mysql_config = /usr/local/bin/mysql_config#mysql_config = /usr/local/mysql/bin/mysql_config#g' site.cfg      #改成/usr/local/mysql/bin/mysql_config
[root@hz MySQL-python-1.2.5]# python setup.py build
[root@hz MySQL-python-1.2.5]# python setup.py install
方式1:

[root@hz tools]# pip install Django==1.6.5

方式2:

[root@hz tools]# wget https://www.djangoproject.com/m/releases/1.6/Django-1.6.5.tar.gz
[root@hz tools]# tar zxf Django-1.6.5.tar.gz
[root@hz tools]# cd Django-1.6.5
[root@hz Django-1.6.5]# python setup.py build
[root@hz Django-1.6.5]# python setup.py install

四、建立项目

[root@hz tools]# cd /web/site/
[root@hz site]# sed -i 's#\$HOME/bin#\$HOME/bin:/byrd/tools/Django-1.6.8/django/bin#g' ~/.bash_profile
[root@hz site]# source ~/.bash_profile
[root@hz site]# django-admin.py startproject Byrd
[root@hz site]# cd Byrd/
[root@hz Byrd]# /usr/local/mysql/bin/mysql -uroot -p
mysql> create database django default character set utf8 collate utf8_general_ci;
mysql> create user 'django'@'localhost' identified by 'admin';
mysql> grant all on django.* to 'django'@'localhost' identified by 'admin';
mysql> flush privileges;
[root@hz Byrd]# vim Byrd/settings.py
####58-63行修改为####
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME':'django',
        'USER': 'django',
        'PASSWORD': 'admin',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}
####58-63行修改为####
[root@hz Byrd]# python manage.py runserver 0.0.0.0:8080    #It worked!证明django配置成功

五、Django+uWSGI:

[root@hz ~]# cat /usr/local/nginx/conf/uwsgi.ini
[uwsgi]
socket = 127.0.0.1:9001
chdir = /web/site/Byrd
pythonpath = ..
env = DJANGO_SETTINGS_MODULE=Byrd.settings
module = django.core.handlers.wsgi:WSGIHandler()
processes = 4
threads = 2
stats = 127.0.0.1:9191

六、Nginx:

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /web/site/Byrd;
            include     uwsgi_params;
            uwsgi_pass   127.0.0.1:9001;
            #uwsgi_param UWSGI_CHDIR  /;
            #uwsgi_param UWSGI_SCRIPT  /;
            index  index.py index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

七、开机启动

[root@hz conf]# echo "uwsgi /usr/local/nginx/conf/uwsgi.ini >/dev/null 2>&1 &" >> /etc/rc.local

八、uWSGI启动脚本:

#!/bin/bash
# uwsgi script
# it is v.0.0.1 version.
# chkconfig: - 89 19
# description: uwsgi script
# processname: uwsgi
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
     
uwsgi_config=/usr/local/nginx/conf/uwsgi.ini
uwsgi_pn=`ps aux|grep -v "grep"|grep -c "uwsgi"`
uwsgi_pid=`ps -eo pid,comm|grep uwsgi|sed -n 1p|awk '{print $1}'`
uwsgi_PID=/usr/local/nginx/logs/uwsgi.pid
uwsgi=/usr/bin/uwsgi
RETVAL=0
prog="uwsgi"
# Source function library.
.  /etc/rc.d/init.d/functions
     
     
if [ $(id -u) != "0" ]; then
    printf "Error: You must be root to run this script!\n"
    exit 1
fi
     
     
# Start nginx daemons functions.
start() {
if [ $uwsgi_pn -gt 5 ];then
        action "uwsgi is running!" /bin/true
    exit 0
fi
    daemon $uwsgi -x ${uwsgi_config}
        action "uwsgi start ..." /bin/true
}
# Stop nginx daemons functions.
stop() {
if [ $uwsgi_pn -gt 5 ]
then
        kill -9 `ps -eo pid,comm|grep uwsgi|sed -n 1p|awk '{print $1}'`
    RETVAL=$?
        action "uwsgi stopping ..." /bin/true
else
        action "uwsgi not running!" /bin/false
fi
}
     
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
*)
        echo $"Usage: $prog {start|stop|restart}"
        exit 1
esac
exit $RETVAL

热门栏目