环境介绍:
12345678910111213141516[[email protected] opsdev]# nginx -vTengine version: Tengine/2.0.3 (nginx/1.4.7)[[email protected] opsdev]# cat /etc/redhat-releaseCentOS release 6.4 (Final)[[email protected] opsdev]# uwsgi -vuwsgi: option requires an argument -- 'v'getopt_long() error[[email protected] opsdev]# uwsgi --version2.0.9[[email protected] opsdev]# django -v-bash: django: command not found[[email protected] opsdev]# python -c "import django;print (django.get_version())"1.7.2[[email protected] opsdev]#[[email protected] opsdev]# python -VPython 2.7.6
Come on~
1、安装nginx
12345[[email protected] tmp]# yum install pcre pcre-devel -y[[email protected] tmp]# tar xf tengine-2.0.3.tar.gz[[email protected] tmp]# cd tengine-2.0.3[[email protected] tmp]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_stub_status_module --with-pcre --with-file-aio --with-http_flv_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi[[email protected] tmp]# make && make install
-
相关选项
-
12345678910111213141516171819202122
--prefix=/usr/local/nginx安装目录--conf-path=/etc/nginx/nginx.conf配置文件存放位置--error-log-path=/var/log/nginx/error.log错误日志存放位置--http-log-path=/var/log/nginx/access.log访问日志存放的位置--with-http_ssl_module启用SSL认证模块--with-http_flv_module启用流媒体模块--with-http_gzip_static_module启用静态页面压缩--http-client-body-temp-path=/var/tmp/nginx/client/HTTP包体存放的临时目录--http-proxy-temp-path=/var/tmp/nginx/proxy/定义从后端服务器接收的临时文件的存放路径,可以为临时文件路径定义至多三层子目录的目录树--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/接收到FastCGI服务器数据,临时存放于本地某目录--with-pcre启动正则表达式rewrite模块 -
添加nginx二进制路径到PATH
123456[[email protected] tengine-2.0.3]# vim /etc/profile.d/nginx.sh[[email protected] tengine-2.0.3]# cat /etc/profile.d/nginx.shexport PATH=$PATH:/usr/local/nginx/sbin/[[email protected] tengine-2.0.3]# source /etc/profile.d/nginx.sh[[email protected] tengine-2.0.3]# nginx -vTengine version: Tengine/2.0.3 (nginx/1.4.7)
-
导出头文件
12[[email protected] tengine-2.0.3]# ln -sv /usr/local/nginx/include/ /usr/include/nginx`/usr/include/nginx' -> `/usr/local/nginx/include/'
-
为tengine提供Sysv服务脚本
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091#!/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: /usr/local/nginx/conf/nginx.conf# pidfile: /usr/local/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 0nginx="/usr/local/nginx/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"lockfile=/var/lock/subsys/nginxstart() {[ -x $nginx ] || exit 5[ -f $NGINX_CONF_FILE ] || exit 6echo -n $"Starting $prog: "daemon $nginx -c $NGINX_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $"Stopping $prog: "killproc $prog -QUITretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval}restart() {configtest || return $?stopstart}reload() {configtest || return $?echo -n $"Reloading $prog: "killproc $nginx -HUPRETVAL=$?echo}force_reload() {restart}configtest() {$nginx -t -c $NGINX_CONF_FILE}rh_status() {status $prog}rh_status_q() {rh_status >/dev/null 2>&1}case "$1" instart)rh_status_q && exit 0$1;;stop)rh_status_q || exit 0$1;;restart|configtest)$1;;reload)rh_status_q || exit 7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q || exit 0;;*)echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit 2esac
-
把该脚本放到/etc/init.d目录下并赋予该脚本权限
12[[email protected] tmp]# mv nginx /etc/init.d/nginx[[email protected] tmp]# chmod +x /etc/init.d/nginx
-
添加到开机启动项并启动服务
12345[[email protected] tmp]# chkconfig --add nginx[[email protected] tmp]# chkconfig --level 35 nginx on[[email protected] tmp]# chkconfig --list | grep nginxnginx 0:off 1:off 2:off 3:on 4:off 5:on 6:off[[email protected] tmp]#
-
启动服务
12345678[[email protected] init.d]# service nginx restartthe configuration file /etc/nginx/nginx.conf syntax is okconfiguration file /etc/nginx/nginx.conf test is successfulStopping nginx: [ OK ]Starting nginx: [ OK ][[email protected] init.d]# ss -tunlp | grep 80tcp LISTEN 0 128 *:80 *:* users:(("nginx",7602,6),("nginx",7604,6))[[email protected] init.d]#
2、升级Python
为毛要升级Python呢,因为django1.5之后的版本导入django core是使用推导式的,而推导式list是python2.7之后才支持的,so~
如果你建立django项目的时候出现如下报错,请升级你的Python或降级django版本
123commands = {name: 'django.core' for name in find_commands(__path__[0])}^SyntaxError: invalid syntax
-
我们这里安装python 2.7.5
123456789101112[[email protected] tmp]# wget --no-check-certificate[[email protected] tmp]# tar -xzvf Python-2.7.5.tgz[[email protected] tmp]# cd Python-2.7.5[[email protected] Python-2.7.5]# ./configure[[email protected] Python-2.7.5]# make && make install[[email protected] Python-2.7.5]# mv /usr/bin/python /usr/bin/python.bak[[email protected] Python-2.7.5]# ln -s /usr/local/bin/python2.7 /usr/bin/python
-
修复yum,修改后/usr/bin/yum第一行如下所示
123
-
安装pip(如果你更新python之前安装过pip,这个时候多半是不能用的,建议从官网从新下载安装)
常见报错如下:
1234Traceback (most recent call last):File "/usr/bin/pelican-quickstart", line 5, in <module>from pkg_resources import load_entry_pointImportError:No module named pkg_resources
解决办法:
1重新安装setuptools和pip即可解决
3、安装django并创建项目
-
安装django
1[[email protected] ~]# pip install django
-
创建项目(在nginx定义的目录下面)
1[[email protected] www]# django-admin startproject opsdev
4、安装uwsgi
-
安装uwsgi
1[[email protected] opsdev]# pip install uwsgi
-
测试
1234#test.pydef application(env,start_response):start_response('200 OK',[('Content-Type','text/html')])return ["hello world"]
-
运行并查看结果
1uwsgi:uwsgi --http :9090 --wsgi-file test.py
5、uwsgi配置django
-
创建django_wsgi.py文件(和mange.py同级目录)
123456789101112131415161718192021222324[[email protected] opsdev]# lsdb.sqlite3 django_wsgi.py manage.py opsdevdjango_wsgi.pyc media static[[email protected] opsdev]# cat django_wsgi.py#!/usr/bin/env python# coding: utf-8import osimport sys# 将系统的编码设置为UTF8#reload(sys)#sys.setdefaultencoding('utf8')#注意:"mysite.settings" 和项目文件夹对应。os.environ.setdefault("DJANGO_SETTINGS_MODULE", "opsdev.settings")#from django.core.handlers.wsgi import WSGIHandler#application = WSGIHandler()# 上面两行测试不对,然后从stackflow上面看到了下面两行,测试okfrom django.core.wsgi import get_wsgi_applicationapplication = get_wsgi_application()[[email protected] opsdev]#
-
创建django_socket.xml文件(和mange.py同级目录)
123456789101112[[email protected] opsdev]# cat django_socket.xml<uwsgi><socket>127.0.0.1:8077</socket><!-- 和nginx中定义的要一致 --><chdir>/www/opsdev</chdir><!-- 你django的项目目录 --><module>django_wsgi</module><!-- 名称为刚才上面定义的py文件名 --><processes>4</processes><!-- 进程数 --><daemonize>/var/log/uwsgi.log</daemonize></uwsgi>[[email protected] opsdev]# lsdb.sqlite3 django_wsgi.py manage.py opsdevdjango_socket.xml django_wsgi.pyc media static[[email protected] opsdev]#
-
验证是否能够正常访问
12345[[email protected] opsdev]# uwsgi --http :8000 --chdir /www/opsdev/ --wsgi-file django_wsgi.py*** Starting uWSGI 2.0.9 (64bit) on [Thu Jan 8 12:21:15 2015] ***compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-11) on 07 January 2015 23:29:52os: Linux-2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013nodename: localhost.localdomain
-
如果上面没有出错,就说明uwsgi和django结合完毕
6、配置nginx配置文件(其他未做更改)
123456789101112location / {include uwsgi_params;uwsgi_pass 127.0.0.1:8077;}location /static/ {alias /www/opsdev/static/;index index.html index.htm;}location /media/ {alias /www/opsdev/media/;}
7、启动测试
-
启动uwsgi
123[[email protected] opsdev]# uwsgi -x django_socket.xml[uWSGI] parsing config file django_socket.xml[[email protected] opsdev]#
-
启动nginx
123
-
查看进程
1234[[email protected] opsdev]# ss -tunlpNetid State Recv-Q Send-Q Local Address:Port Peer Address:Porttcp LISTEN 0 100 127.0.0.1:8077 *:* users:(("uwsgi",25703,3),("uwsgi",25704,3),("uwsgi",25705,3),("uwsgi",25706,3))tcp LISTEN 0 128 *:80 *:* users:(("nginx",26427,6),("nginx",26429,6))
-
查看结果
常见FAQ:
-
-
uwsgi: invalid option -- 'x' getopt_long() error
解决办法:用“ yum install libxml*”即可解决。安装uwsgi时看到 “xml = libxml2”就表示成功了
-
“The translation infrastructure cannot be initialized before the ”
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don’t make non-lazy gettext calls at import time.
解决办法(修改django_wsgi.py):
1234567原来:import django.core.handlers.wsgiapplication = django.core.handlers.wsgi.WSGIHandler()改成:from django.core.wsgi import get_wsgi_applicationapplication = get_wsgi_application()重启一下uwsgi。
-
no python application found, check your startup logs for errorsGET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0switches on core 0)
解决办法:检查django_wsgi.py配置文件,然后uwsgi单独访问下是否正常
还有很多东东要做,卧槽类。。。。
参考文档:在此表示谢谢!
1http://stackoverflow.com/questions/22148144/python-importerror-no-module-named-pkg-resources
1https://github.com/imelucifer/MyNote/blob/master/django/django%2Buwsgi%2Bnginx%2Bcentos%E9%83%A8%E7%BD%B2.md
1http://www.aaini.com/
本文出自 “追马” 博客,请务必保留此出处http://lovelace.blog.51cto.com/1028430/1600594