Linux下Nginx+Django+uWsgi+MySQL多站点虚拟环境搭建

互联网十八般武艺 互联网十八般武艺

本文章为各位介绍Linux下Nginx+Django+uWsgi+MySQL多站点虚拟环境搭建希望文章能对各位有所帮助哦.

环境说明:

  1. Linux:rhel5.3
  2. nginx:nginx-1.6.2
  3. python:2.7.9
  4. django:1.6.7
  5. uwsgi:2.0.10

注意,要预先装好编译环境必备的开发包及libxml2,python-devel,pcre-devel,mysql-devel

2.软件下载地址

  1. nginxhttp://nginx.org/download/nginx-1.6.2.tar.gz
  2. Pythonhttps://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
  3. Virtualenvhttps://pypi.python.org/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz#md5=e08796f79d112f3bfa6653cc10840114
  4. Djangohttps://www.djangoproject.com/download/1.6.7/tarball
  5. uwsgihttp://projects.unbit.it/downloads/uwsgi-2.0.10.tar.gz
  6. MySQL-pythonhttps://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip#md5=654f75b302db6ed8dc5a898c625e030c

3.软件安装,安装nginx:

  1. #yuminstallpcre-develgccopenssl-devel–y
  2. #cd/usr/local/src/django-env
  3. #tar-zxvfnginx-1.6.2.tar.gz
  4. #cdnginx-1.6.2
  5. #sed-i-e's/1.6.2//g'-e's/nginx\//WS/g'-e's/"NGINX"/"WS"/g'src/core/nginx.h
  6. #useraddwebadmin
  7. #./configure--user=webadmin--group=webadmin--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module
  8. #make&&makeinstall

安装python:

  1. #cd/usr/local/src/django-env
  2. #tar-zxvfPython-2.7.9.tgz
  3. #cdPython-2.7.9
  4. #./configure--prefix=/usr/local/python2.7
  5. #make&&makeinstall

安装virtualenv:

  1. #cd/usr/local/src/django-env
  2. #tar–zxvfvirtualenv-12.0.7.tar.gz
  3. #cdvirtualenv-12.0.7
  4. #/usr/local/python2.7/bin/pythonsetup.pyinstall

构建虚拟环境:

  1. #cd/usr/local/python2.7/bin
  2. #./virtualenv/home/webadmin/virtualenv/tools
  3. Newpythonexecutablein/home/webadmin/tools/bin/python
  4. Installingsetuptools,pip...done.

进入并查看虚拟环境:进入虚拟环境在虚拟环境(长前缀的都表示在虚拟环境中的操作),默认会安装好easy_install这类工具.

  1. #source/home/webadmin/virtualenv/tools/bin/activate(退出执行命令deactivate)
  2. (tools)[root@toolsbin]#
  3. (tools)[root@toolsbin]#python-V
  4. Python2.7.9

安装Django-1.5.4:

  1. (tools)[root@tools~]#cd/usr/local/src/django-env/
  2. (tools)[root@toolsdjango-env]#tar-zxvfDjango-1.5.4.tar.gz
  3. (tools)[root@toolsdjango-env]#cdDjango-1.6.7
  4. (tools)[root@toolsDjango-1.6.7]#pythonsetup.pyinstall

安装uwsgi:

  1. (tools)[root@tools~]#cd/usr/local/src/django-env/
  2. (tools)[root@toolsdjango-env]#tar-zxvfuwsgi-2.0.10.tar.gz
  3. (tools)[root@toolsdjango-env]#cduwsgi-2.0.10
  4. (tools)[root@toolsuwsgi-2.0.10]#pythonuwsgiconfig.py--build
  5. (tools)[root@toolsuwsgi-2.0.10]#pythonsetup.pyinstall

安装mysql-python:

  1. (tools)[root@tools~]#cd/usr/local/src/django-env/
  2. (tools)[root@toolsdjango-env]#unzipMySQL-python-1.2.5.zip
  3. (tools)[root@toolsdjango-env]#cdMySQL-python-1.2.5
  4. (tools)[root@toolsMySQL-python-1.2.5]#pythonsetup.pyinstall

注意事项:如果系统没有安装mysql-devel包,在安装MySQL-python的时候会报如下错误:

  1. Traceback(mostrecentcalllast):
  2. File"setup.py",line16,in<module>
  3. metadata,options=get_config()
  4. File"/root/lzh/MySQL-python-1.2.2/setup_posix.py",line43,inget_config
  5. libs=mysql_config("libs_r")
  6. File"/root/lzh/MySQL-python-1.2.2/setup_posix.py",line24,inmysql_config
  7. raiseEnvironmentError,"%snotfound"%mysql_config.path
  8. EnvironmentError:mysql_confignotfound

4.软件配置,配置django项目,部署代码:

  1. (tools)[root@tools~]#mkdir/home/webadmin/djangoproj/tools/-p
  2. (tools)[root@toolstools]#unziptools.zip
  3. (tools)[root@toolstools]#ls
  4. dmclogsmanage.pypluginssrcstatictemplatestoolstools.zip

在tools的项目配置文件目录,即setting.py所在目录,检查文件wsgi.py.

  1. (tools)[root@toolstools]#cdtools
  2. (tools)[root@toolstools]#catwsgi.py
  3. importos
  4. os.environ.setdefault("DJANGO_SETTINGS_MODULE","tools.settings")
  5. fromdjango.core.wsgiimportget_wsgi_application
  6. application=get_wsgi_application()

同级目录下,创建uwsgi的启动参数的xml文件(wsgi.xml):

  1. <uwsgi>
  2. <socket>127.0.0.1:9090</socket>
  3. <listen>20</listen>
  4. <master>true</master>
  5. <pidfile>/home/webadmin/logs/tools/uwsgi/uwsgi.pid</pidfile>
  6. <processes>4</processes>
  7. <module>wsgi</module>#就是上面那个wsgi.py文件
  8. <pythonpath>/home/webadmin/djangoproj/tools/tools</pythonpath>

#建立项目的路径(此项需要注意,在虚拟环境中需要配置到wsgi所在目录,在非虚拟环境中,只需要配置到项目目录即可).

  1. <profiler>true</profiler>
  2. <memory-report>true</memory-report>
  3. <enable-threads>true</enable-threads>
  4. <logdate>true</logdate>
  5. <limit-as>6048</limit-as>
  6. <daemonize>/home/webadmin/logs/tools/uwsgi/tools.log</daemonize>
  7. </uwsgi>

配置nginx:

  1. location/{
  2. includeuwsgi_params;
  3. uwsgi_pass127.0.0.1:9090;
  4. }
  5. #静态文件的处理(如果django中有处理静态文件的了,就可以不要这个了;一般建议将静态文件交给nginx处理好些)
  6. location/static/{
  7. alias/home/webadmin/djangoproj/tools/static/;
  8. expires30d;
  9. break;
  10. }
  11. #配置djangoadmin需要的文件
  12. location~*^/media/(css|img|js)/.*${
  13. root/usr/local/lib/python2.6/site-packages/django/contrib/admin;
  14. expires30d;//phpfensi.com
  15. break;
  16. }
  17. location~*^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)${
  18. access_logoff;
  19. break;
  20. }
  21. location~*^.+\.(jpg|jpeg|gif|png|ico|css|bmp|js)${
  22. expires30d;
  23. break;
  24. }

检查和修正目录权限

(tools)[root@tools webadmin]# chown webadmin:webadmin /home/webadmin/ -R

5.启动服务

  1. (tools)[webadmin@tools~]$uwsgi-x/home/webadmin/djangoproj/tools/tools/wsgi.xml
  2. #/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.备注

启动时,正常的日志:

  1. MonMar2310:59:202015–***StartinguWSGI2.0.10(64bit)on[MonMar2310:59:202015]***
  2. MonMar2310:59:202015–compiledwithversion:4.1.220080704(RedHat4.1.2-44)on22March201522:08:54
  3. MonMar2310:59:202015–os:Linux-2.6.18-194.el5#1SMPTueMar1621:52:39EDT2010
  4. MonMar2310:59:202015–nodename:tools.cyou.com
  5. MonMar2310:59:202015–machine:x86_64
  6. MonMar2310:59:202015–clocksource:unix
  7. MonMar2310:59:202015–pcrejitdisabled
  8. MonMar2310:59:202015–detectednumberofCPUcores:16
  9. MonMar2310:59:202015–currentworkingdirectory:/home/webadmin/djangoproj/tools
  10. MonMar2310:59:202015–writingpidfileto/home/webadmin/logs/tools/uwsgi/uwsgi.pid
  11. MonMar2310:59:202015–detectedbinarypath:/home/webadmin/virtualenv/tools/bin/uwsgi
  12. MonMar2310:59:202015–yourprocessesnumberlimitis268288
  13. MonMar2310:59:202015–limitingaddressspaceofprocesses…
  14. MonMar2310:59:202015–yourprocessaddressspacelimitis6341787648bytes(6048MB)
  15. MonMar2310:59:202015–yourmemorypagesizeis4096bytes
  16. MonMar2310:59:202015–detectedmaxfiledescriptornumber:1024
  17. MonMar2310:59:202015–lockengine:pthreadrobustmutexes
  18. MonMar2310:59:202015–thunderlock:disabled(youcanenableitwith–thunder-lock)
  19. MonMar2310:59:202015–uwsgisocket0boundtoTCPaddress127.0.0.1:9090fd3
  20. MonMar2310:59:202015–Pythonversion:2.7.9(default,Mar232015,10:40:10)[GCC4.1.220080704(RedHat4.1.2-44)]
  21. MonMar2310:59:202015–Pythonmaininterpreterinitializedat0x161c5580
  22. MonMar2310:59:202015–pythonthreadssupportenabled
  23. MonMar2310:59:202015–yourserversocketlistenbacklogislimitedto20connections
  24. MonMar2310:59:202015–yourmercyforgracefuloperationsonworkersis60seconds
  25. MonMar2310:59:202015–mapped363840bytes(355KB)for4cores
  26. MonMar2310:59:202015–***OperationalMODE:preforking***
  27. MonMar2310:59:202015–added/home/webadmin/djangoproj/tools/tools/topythonpath.
  28. MonMar2310:59:202015–WSGIapp0(mountpoint=”)readyin0secondsoninterpreter0x161c5580pid:24588(defaultapp)
  29. MonMar2310:59:202015–***uWSGIisrunninginmultipleinterpretermode***
  30. MonMar2310:59:202015–spawneduWSGImasterprocess(pid:24588)
  31. MonMar2310:59:202015–spawneduWSGIworker1(pid:24599,cores:1)
  32. MonMar2310:59:202015–spawneduWSGIworker2(pid:24600,cores:1)
  33. MonMar2310:59:202015–spawneduWSGIworker3(pid:24601,cores:1)
  34. MonMar2310:59:202015–spawneduWSGIworker4(pid:24602,cores:1)

相关广告
  • Linux下Nginx+Django+uWsgi+MySQL多站点虚拟环境搭建 Linux下Nginx+Django+uWsgi+MySQL多站点虚拟环境搭建 Linux下Nginx+Django+uWsgi+MySQL多站点虚拟环境搭建
相关阅读

Linux下Nginx+Django+uWsgi+MySQL多站点虚拟环境搭建

2019/10/10 17:45:20 | 谷歌SEO算法 | Linus Torvalds