首页 > 代码库 > python版本随意切换之python2.7+django1.8.7+uwsgi+nginx源码包部署。

python版本随意切换之python2.7+django1.8.7+uwsgi+nginx源码包部署。

资源准备:

wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz

wget https://www.djangoproject.com/m/releases/1.8/Django-1.8.7.tar.gz

wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz

wget https://pypi.python.org/packages/source/s/setuptools/setuptools-19.1.1.tar.gz#md5=792297b8918afa9faf826cb5ec4a447a

 

以下不是必须安装

wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz

wget https://git.oschina.net/cp_m/hk_root/raw/master/uwsgi-2.0.14.tar.gz

wget https://pypi.python.org/packages/11/ef/3d64655693e12f163f659bc3a45837893d7ebbb242e608feb9adc788d2ca/uwsgi-2.0.14.tar.gz#md5

 
 
开始安装:
 
安装python2.7
tar xf Python-2.7.13.tgz
cd Python-2.7.13
./configure --prefix=/usr/local/python2.7
make
make install
 
安装setuptools-19.1.1
tar xf setuptools-19.1.1.tar.gz
cd setuptools-19.1.1
/usr/local/python2.7/bin/python setup.py install
 
 
安装django-1.8.7
tar xf Django-1.8.7.tar.gz
cd Django-1.8.7
/usr/local/python2.7/bin/python setup.py install
 
安装MySQL-python
tar xf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
/usr/local/python2.7/bin/python setup.py build
/usr/local/python2.7/bin/python setup.py install
 
安装pip
tar xf pip-9.0.1.tar.gz
cd pip-9.0.1
/usr/local/python2.7/bin/python setup.py install
 
安装uwsgi
/usr/local/python2.7/bin/pip install uwsgi
 
 
测试
cd /data/apps/uptest_svn && uwsgi --http :8000 --module web.wsgi
如果没有/data/apps/uptest_svn/web/wsgi.py 这个文件可以手动添加也可以;
django-admin startproject app1  #创建一个新项目查看wsgi.py文件复制过来.
 
/data/apps/uptest_svn/web/wsgi.py
"""
WSGI config for hello project.
 
It exposes the WSGI callable as a module-level variable named ``application``.
 
For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""
 
import os
 
from django.core.wsgi import get_wsgi_application
 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.settings")
 
application = get_wsgi_application()
 
项目部署
uwsgi配置
vim /etc/uwsgi.ini
[uwsgi]
socket = 127.0.0.1:8088
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/uwsgi.pid
daemonize = /var/log/uwsgi.log
 
 
添加进系统服务器
vim /etc/init.d/uwsgi  && chmod +x /etc/init.d/uwsgi
DESC="uwsgi daemon"
NAME=uwsgi
DAEMON=/usr/local/python2.7/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 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
 
 
nginx 配置
vim /usr/local/nginx/conf/vhosts/django.conf
server {
        listen 80;
        server_name localsvn.test.com;
        location / {
                root /data/apps/uptest_svn;
                include uwsgi_params;
                uwsgi_pass 127.0.0.1:8088;
                uwsgi_param UWSGI_SCRIPT web.wsgi;
                uwsgi_param UWSGI_CHDIR /data/apps/uptest_svn;
                client_max_body_size 35m;
        }
        location /static/ {    
                root /data/apps/uptest_svn/web; 
#我的静态模板和CSS文件放在这个目录下所以设置成这个/data/apps/uptest_svn/web/static/
#如果有开启admin后台的话,建议可以把admin目录的css和js拷贝过来
          }
        access_log /data/log/nginx/django_access.log access;
}

 

service uwsgi reload

service nginx restart

###############END################

python版本随意切换之python2.7+django1.8.7+uwsgi+nginx源码包部署。