CentOS部署flask cms

分类:CentOS教程 阅读:37186 次

最近,学习了python,但是苦于没任何编程经验,没有办法实践,所以准备自己打一个python框架的网站来玩一玩,巩固巩固知识~
参考了网上各位给的初学者的意见最后选择了flask比较适合初学者的web框架
所以就找到了这个项目:https://github.com/quokkaproject/quokka
好了,开始搭建

1、运行环境

我的系统环境

虚拟机 system:CentOSrelease6.4(Final) kernel:2.6.32-504.23.4.el6.x86_64 Memory:997MB

i 下载安装新版的Python

[root@linux2tmp]#wgethttps://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz ... [root@linux2tmp]#tarzxfPython-2.7.10.tgz [root@linux2tmp]#cdPython-2.7.10 [root@linux2Python-2.7.10]#./configure [root@linux2Python-2.7.10]#make&&makeinstall

ii 安装python-pip与virtualenv

参考:https://pip.pypa.io/en/stable/installing/

[root@linux2tmp]#wgethttps://pypi.python.org/packages/source/p/pip/pip-7.1.2.tar.gz#下载pip源码包 [root@linux2pip-7.1.2]#wgethttps://bootstrap.pypa.io/get-pip.py#下载安装脚本 [root@linux2pip-7.1.2]#pythonget-pip.py#会更新安装setuptools、wheel等包 [root@linux2pip-7.1.2]#pipinstallvirtualenv

iii 更新新版git

linux自带的git版本太低,后续用到的命令参数不支持,所以这里更新一下git命令

[root@linux2pip-7.1.2]#yumcleanall [root@linux2pip-7.1.2]#rpm--importhttp://apt.sw.be/RPM-GPG-KEY.dag.txt [root@linux2pip-7.1.2]#rpm-ihttp://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm [root@linux2pip-7.1.2]#yum--enablerepo=rpmforge-extrasupdategit

2、安装mongodb

[root@linux2tmp]#curl-Ohttps://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.0.6.tgz [root@linux2tmp]#tarzxvfmongodb-linux-x86_64-rhel62-3.0.6.tgz [root@linux2tmp]#mvmongodb-linux-x86_64-rhel62-3.0.6/usr/local/mongodb [root@linux2mongodb]#vim/etc/mongodb.conf port=10101 dbpath=/data/db/data logpath=/data/db/log/mongodb.log logappend=true#日志追加非覆盖 fork=true#后台运行 [root@linux2mongodb]#mkdir-p/data/db/data/data/db/log#创建数据文件夹与日志文件夹 [root@linux2mongodb]#/usr/local/mongodb/bin/mongod-f/etc/mongodb.conf#启动mongodb,注意内存要富裕

标注:mongo数据库操作写入数据既创建数据库,所以这里不需要单独创建数据库,只要有程序去写入数据库,它就会去新建一个数据库

3、安装配置quokka

[root@linux2~]#virtualenv-p/usr/local/bin/python2.7quokka-env#指定python版本的绝对路径 [root@linux2~]#sourcequokka-env/bin/activate#需要退出vir环境可用deactivate命令(不加参数) (quokka-env)[root@linux2www]#gitclonehttps://github.com/quokkaproject/quokka--branchdevelopment--single-branch#只获取这个分支的内容, (quokka-env)[root@linux2www]#cdquokka/quokka/ (quokka-env)[root@linux2quokka]#cpexample.local_settings.pylocal_settings.py (quokka-env)[root@linux2quokka]#vimlocal_settings.py#配置mongodb数据库 ... MONGODB_DB="quokka_db" MONGODB_HOST='localhost' MONGODB_PORT=10101 MONGODB_USERNAME=None#这里我的数据库就在本地,所以我可以不设置密码 MONGODB_PASSWORD=None ... (quokka-env)[root@linux2quokka]#cd.. (quokka-env)[root@linux2quokka]#pipinstall-rrequirements/requirements.txt读取txt中的安装包列表

创建管理员和运行quokka

i创建一个超级管理员 (需要登录管理界面)

$pythonmanage.pyaccounts_createsuperuser you@email.com P4$$W0Rd

ii填充示例数据 (可选,如果你想要示例数据)

$pythonmanage.pypopulate

credentials for /admin will be email: admin@example.com passwd: admin

iii运行测试

$pythonmanage.pyrunserver--host0.0.0.0--port5000

  • Site on http://localhost:5000

  • Admin on http://localhost:5000/admin

更多安装方法参考这里:https://github.com/quokkaproject/quokka
至此,quokka部分安装成功~如遇其他页面不正常显示等问题,可以询问项目负责人:https://github.com/quokkaproject/quokka/issues
接下来,安装http server

4、配置nginx+Gunicorn

裸着用flask内置的web(上条命令)来跑web也可以,但是毕竟不是专业的web server,建议还是用主流web server,我这里选用的为nginx。
关于python的其他部署方法,可参考这里:http://lutaf.com/141.htm

i 下载并安装配置nginx

如果要nginx支持谷歌性能模块,需要先安装google-perftools

下载链接:
http://download.savannah.gnu.org/releases/libunwind#libunwind库为基于64位CPU和操作系统的程序提供了基本函数调用链和函数调用寄存器功能
http://https://github.com/gperftools/gperftools/releases#google性能库

安装libunwind (quokka-env)[root@linux2tmp]#tarzxflibunwind-1.1.tar.gz (quokka-env)[root@linux2tmp]#cdlibunwind-1.1 (quokka-env)[root@linux2libunwind-1.1]#CFLAGS=-fPIC./configure (quokka-env)[root@linux2libunwind-1.1]#makeCFLAGS=-fPIC (quokka-env)[root@linux2libunwind-1.1]#makeCFLAGS=-fPICinstall 安装google-perftools (quokka-env)[root@linux2tmp]#wgethttps://github.com/gperftools/gperftools/releases/download/gperftools-2.4/gperftools-2.4.tar.gz (quokka-env)[root@linux2gperftools-2.4]#./configure (quokka-env)[root@linux2gperftools-2.4]#make&&makeinstall

为google-perftools添加线程目录

[root@localhosthome]#mkdir/tmp/tcmalloc [root@localhosthome]#chmod0777/tmp/tcmalloc

安装nginx

(quokka-env)[root@linux2nginx]#yuminstall-ygcc-c++gccautomakeautoconflibtoolmake (quokka-env)[root@linux2tmp]#curl-Ohttp://nginx.org/download/nginx-1.8.0.tar.gz#下载最新稳定版nginx (quokka-env)[root@linux2tmp]#tarzxvfnginx-1.8.0.tar.gz (quokka-env)[root@linux2tmp]#cdnginx-1.8.0 (quokka-env)[root@linux2nginx-1.8.0]#groupaddwww (quokka-env)[root@linux2nginx-1.8.0]#useradd-s/sbin/nologin-gwwwwww (quokka-env)[root@linux2nginx-1.8.0]#./configure--user=www--group=www--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module--with-google_perftools_module--with-http_gzip_static_module--with-http_sub_module (quokka-env)[root@linux2nginx-1.8.0]#make&&makeinstall

nginx编译模块说明:
--with-http_stub_status_module: 提供查看服务器统计信息的能力
--with-http_ssl_module: 支持 HTTPS/SSL
--with-http_gzip_static_module: 提供预压缩的静态文件
--with-google_perftools_module: 支持 Google 性能工具
--with-http_sub_module: 可以替换页面中的文本
更多请参考这篇博客:http://www.zicheng.net/article/42.htm

配置nginx

(quokka-env)[root@linux2conf]#vimnginx.conf worker_processes1; google_perftools_profiles/tmp/tcmalloc;#增加线程目录 events{ worker_connections1024; } http{ includemime.types; default_typeapplication/octet-stream; sendfileon; keepalive_timeout65; upstreamquokka{ serverunix:/tmp/quokka.sockfail_timeout=0; } server{ listen80default_server; return444;#当请求不匹配server_name返回httpcode444 } server{#配置没有主机头 listen80; server_name*.eason.wang; return301https:/eason.wang$request_uri; } server{ listen80; client_max_body_size4G; server_nameeason.wang;#servername也就是你的域名 keepalive_timeout5; root/data/www/quokka;#暂时没搞明白为什么要配置这个路径 location/{ try_files$uri@proxy_to_app; } location@proxy_to_app{ proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_set_headerHost$http_host; proxy_redirectoff; proxy_passhttp://quokka; } error_page500502503504/50x.html; location=/50x.html{ roothtml; } } }

安装配置gunicorn
一个python WSGI的HTTP Server,具体我也不知道该怎么说.可以参考官网

(quokka-env)[root@linux2logs]#pipinstallgunicorn (quokka-env)[root@linux2quokka]#cd/data/www/quokka/ (quokka-env)[root@linux2quokka]#vimstart_gunicorn.sh#启动脚本 #!/bin/bash NAME='quokka'#应用的名称 FLASKDIR=/data/www/quokka#django项目的目录 SOCKFILE=/tmp/quokka.sock#使用这个sock来通信 USER=www#运行此应用的用户 NUM_WORKERS=3#gunicorn使用的工作进程数 DJANGO_WSGI_MODULE=wsgi#wsgi模块 echo"starting$NAMEas`whoami`" #激活python虚拟运行环境 cd$FLASKDIR source/root/quokka-env/bin/activate #启动flask exec/root/quokka-env/bin/gunicorn\ --workers$NUM_WORKERS\ -uwww\ --bind=unix:$SOCKFILE\ wsgi

运行

(quokka-env)[root@linux2quokka]#./start_gunicorn.sh&


遇到的问题
1、安装pip时,执行python get-pip.py后成功,但是没有pip命令。
反复确认,执行这条安装命令并没有出错,我想可能是Python版本的问题,因为linux默认是python 2.6.6,而我要使用virtualenv所以我也懒得做环境变量了,所以我用绝对路径方法指定新版本python重新安装解决。

[root@linux2pip-7.1.2]#/usr/local/bin/python2.7get-pip.py--force-reinstall

2、mongodb遇到要删除数据库操作时,删除命令为下

[root@linux2shell]#/usr/local/mongodb/bin/mongo127.0.0.1:10101 >usexxx#选择要删除的数据库 >db.dropDatabase()#删除数据库

3、当执行pip安装时报错“Could not find a version that satisfies the requirement ...”时,使用-v参数查看详细原因并解决。例:

[root@linux2www]#pipinstallpyshorteners==0.5.5-v Collectingpyshorteners==0.5.5 Gettingpagehttps://pypi.python.org/simple/pyshorteners/ StartingnewHTTPSconnection(1):pypi.python.org "GET/simple/pyshorteners/HTTP/1.1"40428063 CouldnotfetchURLhttps://pypi.python.org/simple/pyshorteners/:404ClientError:NotFound-skipping 1location(s)tosearchforversionsofpyshorteners: *https://pypi.python.org/simple/pyshorteners/ Gettingpagehttps://pypi.python.org/simple/pyshorteners/ "GET/simple/pyshorteners/HTTP/1.1"40428063 CouldnotfetchURLhttps://pypi.python.org/simple/pyshorteners/:404ClientError:NotFound-skipping Couldnotfindaversionthatsatisfiestherequirementpyshorteners==0.5.5(fromversions:) Cleaningup... Nomatchingdistributionfoundforpyshorteners==0.5.5 Exceptioninformation: Traceback(mostrecentcalllast): File"/root/quokka-env/lib/python2.7/site-packages/pip/basecommand.py",line211,inmain status=self.run(options,args) File"/root/quokka-env/lib/python2.7/site-packages/pip/commands/install.py",line305,inrun wb.build(autobuilding=True) File"/root/quokka-env/lib/python2.7/site-packages/pip/wheel.py",line705,inbuild self.requirement_set.prepare_files(self.finder) File"/root/quokka-env/lib/python2.7/site-packages/pip/req/req_set.py",line334,inprepare_files functools.partial(self._prepare_file,finder)) File"/root/quokka-env/lib/python2.7/site-packages/pip/req/req_set.py",line321,in_walk_req_to_install more_reqs=handler(req_to_install) File"/root/quokka-env/lib/python2.7/site-packages/pip/req/req_set.py",line461,in_prepare_file req_to_install.populate_link(finder,self.upgrade) File"/root/quokka-env/lib/python2.7/site-packages/pip/req/req_install.py",line250,inpopulate_link self.link=finder.find_requirement(self,upgrade) File"/root/quokka-env/lib/python2.7/site-packages/pip/index.py",line571,infind_requirement 'Nomatchingdistributionfoundfor%s'%req DistributionNotFound:Nomatchingdistributionfoundforpyshorteners==0.5.5

通过上边可以看到,获取连接404了,所以我们来验证下是真的404还是pip有问题,可以直接访问web或者使用curl来验证

(quokka-env)[root@linux2www]#curl-Ihttps://pypi.python.org/simple/pyshorteners/ HTTP/1.1404NotFound

页面404就属于项目负责人在调整页面或者撤销的问题了。
如果想尽快解决,在github搜索该项目,一般README文档会有本地怎么安装。

4、解决nginx启动时报错

nginx: error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directory
nginx: error while loading shared libraries: libunwind.so.8: cannot open shared object file: No such file or directory

以上两个错误均属于因为目前是64位操作系统导致的,解决方法即为找到两个lib文件,将它们软连接到/usr/lib64

(quokka-env)[root@linux2~]#whereislibprofiler.so.0libunwind.so.8#找到两个lib文件位置 libprofiler.so:/usr/lib64/libprofiler.so.0/usr/local/lib/libprofiler.so.0/usr/local/lib/libprofiler.so libunwind.so:/usr/lib64/libunwind.so.8/usr/local/lib/libunwind.so.8/usr/local/lib/libunwind.so (quokka-env)[root@linux2~]#ln-s/usr/local/lib/libprofiler.so.0/usr/lib64/ (quokka-env)[root@linux2~]#/usr/local/nginx/sbin/nginx-V /usr/local/nginx/sbin/nginx:errorwhileloadingsharedlibraries:libunwind.so.8:cannotopensharedobjectfile:Nosuchfileordirectory (quokka-env)[root@linux2~]#ln-s/usr/local/lib/libunwind.so.8/usr/lib64/

5、解决指定gunicorn -u 没有权限问题

(quokka-env)[root@linux2quokka]#gunicorn-uwww--workers3--bindunix:/tmp/quokka.sock-m007wsgi [2015-09-2410:53:57+0000][28230][INFO]Startinggunicorn19.3.0 [2015-09-2410:53:57+0000][28230][INFO]Listeningat:unix:/tmp/quokka.sock(28230) [2015-09-2410:53:57+0000][28230][INFO]Usingworker:sync [2015-09-2410:53:57+0000][28235][INFO]Bootingworkerwithpid:28235 [2015-09-2410:53:57+0000][28235][ERROR]Exceptioninworkerprocess: Traceback(mostrecentcalllast): File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line507,inspawn_worker File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/workers/base.py",line118,ininit_process File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py",line67,inwsgi File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line65,inload File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line52,inload_wsgiapp File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/util.py",line355,inimport_app File"/data/www/quokka/wsgi.py",line4,in<module> fromwerkzeug.servingimportrun_simple ImportError:Nomodulenamedwerkzeug.serving Traceback(mostrecentcalllast): File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line507,inspawn_worker File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/workers/base.py",line118,ininit_process File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py",line67,inwsgi File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line65,inload File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line52,inload_wsgiapp File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/util.py",line355,inimport_app File"/data/www/quokka/wsgi.py",line4,in<module> fromwerkzeug.servingimportrun_simple ImportError:Nomodulenamedwerkzeug.serving [2015-09-2410:53:57+0000][28235][INFO]Workerexiting(pid:28235) Traceback(mostrecentcalllast): File"/root/quokka-env/bin/gunicorn",line11,in<module> sys.exit(run()) File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line74,inrun WSGIApplication("%(prog)s[OPTIONS][APP_MODULE]").run() File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py",line189,inrun super(Application,self).run() File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py",line72,inrun Arbiter(self).run() File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line174,inrun self.manage_workers() File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line477,inmanage_workers self.spawn_workers() File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line541,inspawn_workers time.sleep(0.1*random.random()) File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line214,inhandle_chld self.reap_workers() File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line459,inreap_workers raiseHaltServer(reason,self.WORKER_BOOT_ERROR) gunicorn.errors.HaltServer:<HaltServer'Workerfailedtoboot.'3>

根据执行的命令和堆栈跟踪来看,基本上确定两个错误信息:

  • ImportError: No module named werkzeug.serving导入模块错误,检查是不是模块问题。

  • 大面积的File "/root ...",env文件夹在/root账号下要检查www账号是不是有权限问题。

下面检查这两个错误:

(quokka-env)[root@linux2tmp]#python Python2.7.10(default,Sep212015,17:31:07) [GCC4.4.720120313(RedHat4.4.7-4)]onlinux2 Type"help","copyright","credits"or"license"formoreinformation. >>>fromwerkzeug.servingimportrun_simple

没有报错,看来模块是没有问题的

(quokka-env)[root@linux2tmp]#gunicorn--workers3--bindunix:/tmp/quokka.sock-m007wsgi [2015-09-2411:36:19+0000][28350][INFO]Startinggunicorn19.3.0 [2015-09-2411:36:19+0000][28350][INFO]Listeningat:unix:/tmp/quokka.sock(28350) [2015-09-2411:36:19+0000][28350][INFO]Usingworker:sync [2015-09-2411:36:19+0000][28355][INFO]Bootingworkerwithpid:28355 [2015-09-2411:36:19+0000][28356][INFO]Bootingworkerwithpid:28356 [2015-09-2411:36:19+0000][28357][INFO]Bootingworkerwithpid:28357

去掉-u www可以正常启动,那么看来就是权限惹的问题~
解决方法:

[root@linux2/]#ll ... drwxr-----.14rootroot40969月2319:50root ... [root@linux2/]#chmodg+xroot

这里为什么要加属组的执行权限,是因为我们执行gunicorn命令的权限是当前用户和当前用户组(可以通过-g改变),而我们改变了启动的用户为www,所以它会尝试用www用户权限,www没有这个权限,它又会去尝试当前用户组权限(既root组),所以要给root组有执行权限即可