安装基础开发包(CentOS):
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc
安装Python3
wget http://python.org/ftp/python/3.4.0/Python-3.4.0.tgz
tar -xzvf Python-3.4.0.tgz
cd Python-3.4.0
./configure
make && make install
安装pip
wget "https://bootstrap.pypa.io/get-pip.py" | python
建立软连接
mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/bin/python3.4 /usr/bin/python
修复不能正常工作的yum
vi /usr/bin/yum
将首行显示的 !#/usr/bin/python 修改为 !#/usr/bin/python2.6
安装uwsgi
pip install uwsgi
测试 uwsgi ,新建 test.py 文件,内容如下:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
终端运行:
uwsgi --http :8001 --wsgi-file test.py
访问127.0.0.1:8001 成功则可见Hello World
安装Django
pip install django
django-admin.py startproject mysite
cd mysite
python manage.py runserver 0.0.0.0:8002
访问127.0.0.1:8002 成功则可见it work
连接django和uwsgi
编写mysite_wsgi.py文件,放在manage.py相同目录下
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE","mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
终端运行,假设Django目录是/root/mysite
uwsgi --http :8000 --chdir /root/mysite --module django_wsgi
访问127.0.0.1:8000 成功则可见it work
创建uWSGI配置文件
新建mysite_socket.ini,放在manage.py相同目录下
[uwsgi]
socket=127.0.0.1:8010
chdir=/root/mysite
module=mysite_wsgi
processes=4
daemonize=uwsgi.log
安装Nginx
wget http://nginx.org/download/nginx-1.5.6.tar.gz
tar xf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure
make && make install
配置Nginx
vim /usr/local/nginx/conf/nginx.conf
添加以下配置
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8010;
}
运行
uwsgi --ini mysite_socket.ini &
/usr/local/nginx/sbin/nginx
访问 127.0.0.1 成功则可见it work
开机自启
在/etc/init.d/ 目录下创建nginx,内容为nginx官网给出的脚步
https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
修改nginx和NGINX_CONF_FILE为自己的路径
chomd a+x /etc/init.d/nginx
chkconfig --add /etc/init.d/nginx
chkconfig nginx on
编辑/etc/rc.local文件,添加
/usr/local/bin/uwsgi --ini home/mysite/mysite_socket.ini
常见问题:
1.invalid request block size…
解决:nginx用来把请求发送给uwsgi。所以uwsgi被配置成使用socket方式(为tcp协议)进行通信。如果打开浏览器访问uwsgi指定的端口,那么浏览器请求uwsgi的方式为http协议,而不是socket方式。修改配置文件中的socket为http即可
2.raise AppRegistryNotReady(“Apps aren’t loaded yet.”)…
解决:版本问题,修改mysite_wsgi.py文件,django.core.handlers.wsgi 改为django.core.wsgi,其余做相应改变,见本文配置
3.DisallowedHost at / Invalid HTTP_HOST header:…
解决:Django项目的setting.py文件, ALLOW设置为["*"]
4.unable to load app 0 (mountpoint=’’) (callable not found or import error) …
解决:版本问题,修改mysite_wsgi.py文件,去掉 ‘application’ 前的 '’
注:本文引用多篇文章和文档,用于学习用途,如有侵权,还望告知