本文章为各位介绍Linux下Nginx+Django+uWsgi+MySQL多站点虚拟环境搭建希望文章能对各位有所帮助哦.
环境说明:
- Linux:rhel5.3
- nginx:nginx-1.6.2
- python:2.7.9
- django:1.6.7
- uwsgi:2.0.10
注意,要预先装好编译环境必备的开发包及libxml2,python-devel,pcre-devel,mysql-devel
2.软件下载地址
- nginxhttp://nginx.org/download/nginx-1.6.2.tar.gz
- Pythonhttps://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
- Virtualenvhttps://pypi.python.org/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz#md5=e08796f79d112f3bfa6653cc10840114
- Djangohttps://www.djangoproject.com/download/1.6.7/tarball
- uwsgihttp://projects.unbit.it/downloads/uwsgi-2.0.10.tar.gz
- MySQL-pythonhttps://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip#md5=654f75b302db6ed8dc5a898c625e030c
3.软件安装,安装nginx:
- #yuminstallpcre-develgccopenssl-devel–y
- #cd/usr/local/src/django-env
- #tar-zxvfnginx-1.6.2.tar.gz
- #cdnginx-1.6.2
- #sed-i-e's/1.6.2//g'-e's/nginx\//WS/g'-e's/"NGINX"/"WS"/g'src/core/nginx.h
- #useraddwebadmin
- #./configure--user=webadmin--group=webadmin--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module
- #make&&makeinstall
安装python:
- #cd/usr/local/src/django-env
- #tar-zxvfPython-2.7.9.tgz
- #cdPython-2.7.9
- #./configure--prefix=/usr/local/python2.7
- #make&&makeinstall
安装virtualenv:
- #cd/usr/local/src/django-env
- #tar–zxvfvirtualenv-12.0.7.tar.gz
- #cdvirtualenv-12.0.7
- #/usr/local/python2.7/bin/pythonsetup.pyinstall
构建虚拟环境:
- #cd/usr/local/python2.7/bin
- #./virtualenv/home/webadmin/virtualenv/tools
- Newpythonexecutablein/home/webadmin/tools/bin/python
- Installingsetuptools,pip...done.
进入并查看虚拟环境:进入虚拟环境在虚拟环境(长前缀的都表示在虚拟环境中的操作),默认会安装好easy_install这类工具.
- #source/home/webadmin/virtualenv/tools/bin/activate(退出执行命令deactivate)
- (tools)[root@toolsbin]#
- (tools)[root@toolsbin]#python-V
- Python2.7.9
安装Django-1.5.4:
- (tools)[root@tools~]#cd/usr/local/src/django-env/
- (tools)[root@toolsdjango-env]#tar-zxvfDjango-1.5.4.tar.gz
- (tools)[root@toolsdjango-env]#cdDjango-1.6.7
- (tools)[root@toolsDjango-1.6.7]#pythonsetup.pyinstall
安装uwsgi:
- (tools)[root@tools~]#cd/usr/local/src/django-env/
- (tools)[root@toolsdjango-env]#tar-zxvfuwsgi-2.0.10.tar.gz
- (tools)[root@toolsdjango-env]#cduwsgi-2.0.10
- (tools)[root@toolsuwsgi-2.0.10]#pythonuwsgiconfig.py--build
- (tools)[root@toolsuwsgi-2.0.10]#pythonsetup.pyinstall
安装mysql-python:
- (tools)[root@tools~]#cd/usr/local/src/django-env/
- (tools)[root@toolsdjango-env]#unzipMySQL-python-1.2.5.zip
- (tools)[root@toolsdjango-env]#cdMySQL-python-1.2.5
- (tools)[root@toolsMySQL-python-1.2.5]#pythonsetup.pyinstall
注意事项:如果系统没有安装mysql-devel包,在安装MySQL-python的时候会报如下错误:
- Traceback(mostrecentcalllast):
- File"setup.py",line16,in<module>
- metadata,options=get_config()
- File"/root/lzh/MySQL-python-1.2.2/setup_posix.py",line43,inget_config
- libs=mysql_config("libs_r")
- File"/root/lzh/MySQL-python-1.2.2/setup_posix.py",line24,inmysql_config
- raiseEnvironmentError,"%snotfound"%mysql_config.path
- EnvironmentError:mysql_confignotfound
4.软件配置,配置django项目,部署代码:
- (tools)[root@tools~]#mkdir/home/webadmin/djangoproj/tools/-p
- (tools)[root@toolstools]#unziptools.zip
- (tools)[root@toolstools]#ls
- dmclogsmanage.pypluginssrcstatictemplatestoolstools.zip
在tools的项目配置文件目录,即setting.py所在目录,检查文件wsgi.py.
- (tools)[root@toolstools]#cdtools
- (tools)[root@toolstools]#catwsgi.py
- importos
- os.environ.setdefault("DJANGO_SETTINGS_MODULE","tools.settings")
- fromdjango.core.wsgiimportget_wsgi_application
- application=get_wsgi_application()
同级目录下,创建uwsgi的启动参数的xml文件(wsgi.xml):
- <uwsgi>
- <socket>127.0.0.1:9090</socket>
- <listen>20</listen>
- <master>true</master>
- <pidfile>/home/webadmin/logs/tools/uwsgi/uwsgi.pid</pidfile>
- <processes>4</processes>
- <module>wsgi</module>#就是上面那个wsgi.py文件
- <pythonpath>/home/webadmin/djangoproj/tools/tools</pythonpath>
#建立项目的路径(此项需要注意,在虚拟环境中需要配置到wsgi所在目录,在非虚拟环境中,只需要配置到项目目录即可).
- <profiler>true</profiler>
- <memory-report>true</memory-report>
- <enable-threads>true</enable-threads>
- <logdate>true</logdate>
- <limit-as>6048</limit-as>
- <daemonize>/home/webadmin/logs/tools/uwsgi/tools.log</daemonize>
- </uwsgi>
配置nginx:
- location/{
- includeuwsgi_params;
- uwsgi_pass127.0.0.1:9090;
- }
- #静态文件的处理(如果django中有处理静态文件的了,就可以不要这个了;一般建议将静态文件交给nginx处理好些)
- location/static/{
- alias/home/webadmin/djangoproj/tools/static/;
- expires30d;
- break;
- }
- #配置djangoadmin需要的文件
- location~*^/media/(css|img|js)/.*${
- root/usr/local/lib/python2.6/site-packages/django/contrib/admin;
- expires30d;//phpfensi.com
- break;
- }
- location~*^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)${
- access_logoff;
- break;
- }
- location~*^.+\.(jpg|jpeg|gif|png|ico|css|bmp|js)${
- expires30d;
- break;
- }
检查和修正目录权限
(tools)[root@tools webadmin]# chown webadmin:webadmin /home/webadmin/ -R
5.启动服务
- (tools)[webadmin@tools~]$uwsgi-x/home/webadmin/djangoproj/tools/tools/wsgi.xml
- #/usr/local/nginx/sbin/ngin
6.多站点部署
重复从构建虚拟环境开始的步骤~然后在虚拟环境中安装好要运行的实际环境,并启动服务即可,配置nginx的虚拟主机即可.
7.故障检查思路
查看的日志包括(nginx日志[Nginx配置文件中定义],uwsgi日志[xml文件中定义]),查看uwsgi日志的主要原因是看uwsgi是否正常接管了nginx下发的请求任务,uwsgi日志可查看django执行错误信息,也可在开发中通过print 打印调试.
nginx错误日志/访问日志查看,两种日志区别在于,前者针对代码程序级的错误,后者针对网络访问级的错误.
8.一些建议
有时候使用killall uwsgi时不能将uwsgi干掉,你可以uwsgi -s reload的方式重启uwsgi或kill -HUP cat /pathto/uwsgi.pid
uWSG配置文件的进程数,可以根据实际情况分配。不要开得太大,否则机器可能会内存耗用太高。一般来说,对于一个小社区来说,4个进程已经足够了。
一般情况下,可以编写一下 stop.sh 脚本 来关闭uWSGI。
有了uWSGI为什么还需要nginx?nginx具备优秀的静态内容处理能力,然后将动态内容转发给uWSGI服务器,这样可以达到很好的客户端响应。
9.注意事项
日志里面报类似于“Mon Mar 23 10:26:49 2015 – — no python application found, check your startup logs for errors —”这类错误时,需要好好检查一下xml文件.
这个文件里面行尾不能有空格,参数左右也不能有空格,这个问题坑了我一晚上,日志里面报类似于”ImportError: No module named wsgi”这类错误,需要检查一下你的xml文件的pythonpath选项.
在虚拟环境下,需要配置到uwsgi所在目录,非虚拟环境下,只需要配置到项目所在路径即可.
10.备注
启动时,正常的日志:
- MonMar2310:59:202015–***StartinguWSGI2.0.10(64bit)on[MonMar2310:59:202015]***
- MonMar2310:59:202015–compiledwithversion:4.1.220080704(RedHat4.1.2-44)on22March201522:08:54
- MonMar2310:59:202015–os:Linux-2.6.18-194.el5#1SMPTueMar1621:52:39EDT2010
- MonMar2310:59:202015–nodename:tools.cyou.com
- MonMar2310:59:202015–machine:x86_64
- MonMar2310:59:202015–clocksource:unix
- MonMar2310:59:202015–pcrejitdisabled
- MonMar2310:59:202015–detectednumberofCPUcores:16
- MonMar2310:59:202015–currentworkingdirectory:/home/webadmin/djangoproj/tools
- MonMar2310:59:202015–writingpidfileto/home/webadmin/logs/tools/uwsgi/uwsgi.pid
- MonMar2310:59:202015–detectedbinarypath:/home/webadmin/virtualenv/tools/bin/uwsgi
- MonMar2310:59:202015–yourprocessesnumberlimitis268288
- MonMar2310:59:202015–limitingaddressspaceofprocesses…
- MonMar2310:59:202015–yourprocessaddressspacelimitis6341787648bytes(6048MB)
- MonMar2310:59:202015–yourmemorypagesizeis4096bytes
- MonMar2310:59:202015–detectedmaxfiledescriptornumber:1024
- MonMar2310:59:202015–lockengine:pthreadrobustmutexes
- MonMar2310:59:202015–thunderlock:disabled(youcanenableitwith–thunder-lock)
- MonMar2310:59:202015–uwsgisocket0boundtoTCPaddress127.0.0.1:9090fd3
- MonMar2310:59:202015–Pythonversion:2.7.9(default,Mar232015,10:40:10)[GCC4.1.220080704(RedHat4.1.2-44)]
- MonMar2310:59:202015–Pythonmaininterpreterinitializedat0x161c5580
- MonMar2310:59:202015–pythonthreadssupportenabled
- MonMar2310:59:202015–yourserversocketlistenbacklogislimitedto20connections
- MonMar2310:59:202015–yourmercyforgracefuloperationsonworkersis60seconds
- MonMar2310:59:202015–mapped363840bytes(355KB)for4cores
- MonMar2310:59:202015–***OperationalMODE:preforking***
- MonMar2310:59:202015–added/home/webadmin/djangoproj/tools/tools/topythonpath.
- MonMar2310:59:202015–WSGIapp0(mountpoint=”)readyin0secondsoninterpreter0x161c5580pid:24588(defaultapp)
- MonMar2310:59:202015–***uWSGIisrunninginmultipleinterpretermode***
- MonMar2310:59:202015–spawneduWSGImasterprocess(pid:24588)
- MonMar2310:59:202015–spawneduWSGIworker1(pid:24599,cores:1)
- MonMar2310:59:202015–spawneduWSGIworker2(pid:24600,cores:1)
- MonMar2310:59:202015–spawneduWSGIworker3(pid:24601,cores:1)
- MonMar2310:59:202015–spawneduWSGIworker4(pid:24602,cores:1)