Centos下搭建 nginx+uwsgi+python
用python做web应用最麻烦的还是配置服务器了,此话不假,光中间件就有好几种选择,fastcgi、wsgi、uwsgi,难 免让人眼花缭乱。 而听说uwsgi的效率是fastcgi和wsgi的10倍,因此初学python的我就有点跃跃欲试了,打算在centos下搭建个 nginx+uwsgi+python玩玩。
下面是本人经过google和亲身实践所得:
准备工作:
yum install python-devel libxml2-devel python-setuptools zlib-devel wget pcre-devel gcc make
编译安装nginx:
cd /tmp wget http://nginx.org/download/nginx-1.7.4.tar.gz tar -zxvf nginx-1.7.4.tar.gz ./configure //此处可能会提示有些library未安装,安装完再运行此命令检查即可 make;make install //确认无误,编译安装
编译安装uwsgi:
wget http://projects.unbit.it/downloads/uwsgi-2.0.tar.gz tar -zxvf uwsgi-2.0.tar.gz
cd uwsgi-2.0.tar.gz python setup.py build make mv uwsgi /usr/bin //将编译好的文件移动到此处
配置nginx
vim /usr/local/nginx/conf/nginx.conf
在server下的location下增加以下2行
location / { uwsgi_pass 127.0.0.1:9001; include uwsgi_params; }
新建test.py文件,内容如下:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"然后在终端运行:
/usr/local/nginx/sbin/nginx uwsgi -s 127.0.0.1:9001 --wsgi-file test.py (居然没有采用9001端口端)改为:
uwsgi --http :9001 --wsgi-file test.py 采用了9001端口。
二,
1. 软件下载地址:
uwsgi
wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gzflup
wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gzdjango
wget http://media.djangoproject.com/releases/1.2/Django-1.2.5.tar.gz2. 安装
flup和django 都是用 python setup.py install
uwsgi安装
cd uwsgi-0.9.6.8python uwsgiconfig.py --buildcd nginxcp uwsgi_params /usr/local/nginx/conf/3. 建立项目目录
cd /rootdjango-admin.py startproject my_djangovi uwsgi.xml
vi django_wsgi
import osimport django.core.handlers.wsgios.environ['DJANGO_SETTINGS_MODULE'] = 'my_django.settings' #这里的my_django.settings 表示 "项目名.settings"application = django.core.handlers.wsgi.WSGIHandler()4. 添加nginx配置
server {listen 80;server_name 192.168.0.100;location / {root /root/my_django;uwsgi_pass 127.0.0.1:8000;include uwsgi_params;access_log off; }}5. 启动uwsgi和nginx
/usr/local/nginx/sbin/nginxuwsgi -x /root/my_django/uwsgi.xml &好了,打开浏览器测试下吧
二,安装Django
Download: https://www.djangoproject.com/download/
解压安装:
tar xzvf Django-1.6b1.tar.gz
cd Django-1.6b1 sudo python setup.py install 不到1分钟左右,就安装好了;如果安装过django需要卸载以后再安装;
执行django-admin.py startproject PROJECT_NAME 创建项目
其中PROJECT_NAME是项目的名称,这里假设项目的名称是Blog 那么可以得到这样的一个目录树:
Blog/ |____ manager.py |____ Blog/ |____ urls.py |____ wsgi.py |____ __init__.py |____ settings.py执行 django-admin.py startproject ztgame 创建名为ztgame的项目
执行 python ztgame/manage.py runserver 0.0.0.0:80 即可开启服务,输入IP,就可以打开Django的默认欢迎页面了;如下图:
在django项目主目录下创建 django_wsgi.py
3)在django项目主目录下创建 django.xml
- import os,sys
- if not os.path.dirname(__file__) in sys.path[:1]:
- sys.path.insert(0, os.path.dirname(__file__))
- os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
- from django.core.handlers.wsgi import WSGIHandler
- application = WSGIHandler()
接下来运行
- uwsgi -x /home/example/django.xml
OK,输入127.0.0.1就能看到 django worked!
不过每次重启服务器都需要输上面那行代码比较麻烦,可以将代码复制到 /etc/init.d/rc.local ,这样开机就自动启动
从网上找了段代码也可以试试,或者将下面代码copy到 /etc/init.d/uwsgi
- #!/bin/bash
- PORT=3031
- PROCESSES=4
- LOG=/var/log/uwsgi
- PID=`pidof -o %PPID /usr/bin/uwsgi`
- . /etc/rc.conf
- . /etc/rc.d/functions
- case "$1" in
- start)
- stat_busy "Starting uwsgi"
- if [ -n "$PID" ]; then
- stat_busy "uwsgi is already running"
- stat_die
- else
- uwsgi -s ":$PORT" -M -p $PROCESSES -d $LOG &> /dev/null
- add_daemon uwsgi
- stat_done
- fi
- ;;
- stop)
- stat_busy "Stopping uwsgi"
- killall -QUIT uwsgi &> /dev/null
- rm_daemon uwsgi
- stat_done
- ;;
- restart)
- $0 stop
- sleep 1
- $0 start
- ;;
- *)
- echo "usage: $0 {start|stop|restart}"
- esac
- exit 0