CentOS 部署flask cms

來源:互聯網
上載者:User

CentOS 部署flask cms
最近,學習了python,但是苦於沒任何編程經驗,沒有辦法實踐,所以準備自己打一個python架構的網站來玩一玩,鞏固鞏固知識~ 參考了網上各位給的初學者的意見最後選擇了flask比較適合初學者的web架構所以就找到了這個項目:https://github.com/quokkaproject/quokka好了,開始搭建 1、運行環境 我的系統內容 虛擬機器system: CentOS release 6.4 (Final)kernel: 2.6.32-504.23.4.el6.x86_64Memory: 997MB i 下載安裝新版的Python

[root@linux2 tmp]# wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz...[root@linux2 tmp]# tar zxf Python-2.7.10.tgz[root@linux2 tmp]# cd Python-2.7.10[root@linux2 Python-2.7.10]# ./configure[root@linux2 Python-2.7.10]# make && make installii 安裝python-pip與virtualenv[root@linux2 tmp]# wget https://pypi.python.org/packages/source/p/pip/pip-7.1.2.tar.gz #下載pip源碼包[root@linux2 pip-7.1.2]# wget https://bootstrap.pypa.io/get-pip.py #下載安裝指令碼[root@linux2 pip-7.1.2]# python get-pip.py  #會更新安裝setuptools、wheel等包[root@linux2 pip-7.1.2]# pip install virtualenv

 

iii 更新新版git linux內建的git版本太低,後續用到的命令參數不支援,所以這裡更新一下git命令
[root@linux2 pip-7.1.2]# yum clean all[root@linux2 pip-7.1.2]# rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt[root@linux2 pip-7.1.2]# rpm -i http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm[root@linux2 pip-7.1.2]# yum --enablerepo=rpmforge-extras update git

 

 2、安裝mongodb
[root@linux2 tmp]# curl -O  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.0.6.tgz[root@linux2 tmp]# tar zxvf mongodb-linux-x86_64-rhel62-3.0.6.tgz [root@linux2 tmp]# mv mongodb-linux-x86_64-rhel62-3.0.6 /usr/local/mongodb[root@linux2 mongodb]# vim /etc/mongodb.confport=10101dbpath=/data/db/datalogpath=/data/db/log/mongodb.loglogappend=true  #日誌追加非覆蓋fork=true  #後台運行[root@linux2 mongodb]# mkdir -p /data/db/data /data/db/log #建立資料檔案夾與記錄檔夾[root@linux2 mongodb]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf #啟動mongodb,注意記憶體要富裕

 

 標註:mongo資料庫操作寫入資料既建立資料庫,所以這裡不需要單獨建立資料庫,只要有程式去寫入資料庫,它就會去建立一個資料庫 3、安裝配置quokka
[root@linux2 ~]# virtualenv -p /usr/local/bin/python2.7 quokka-env #指定python版本的絕對路徑[root@linux2 ~]# source quokka-env/bin/activate   #需要退出vir環境可用deactivate命令(不加參數)(quokka-env)[root@linux2 www]# git clone https://github.com/quokkaproject/quokka --branch development --single-branch #只擷取這個分支的內容,(quokka-env)[root@linux2 www]# cd quokka/quokka/(quokka-env)[root@linux2 quokka]# cp example.local_settings.py local_settings.py (quokka-env)[root@linux2 quokka]# vim local_settings.py #配置mongodb資料庫...MONGODB_DB = "quokka_db"MONGODB_HOST = 'localhost'MONGODB_PORT = 10101MONGODB_USERNAME = None   #這裡我的資料庫就在本地,所以我可以不設定密碼MONGODB_PASSWORD = None...(quokka-env)[root@linux2 quokka]# cd ..(quokka-env)[root@linux2 quokka]# pip install -r requirements/requirements.txt 讀取txt中的安裝包列表

 

建立管理員和運行quokka i 建立一個超級管理員 (需要登入管理介面) $ python manage.py accounts_createsuperuseryou@email.comP4$$W0Rdii 填充樣本資料 (可選,如果你想要樣本資料) $ python manage.py populatecredentials for /admin will be email: admin@example.com passwd: adminiii 運行測試 $ python manage.py runserver --host 0.0.0.0 --port 5000Site on http://localhost:5000Admin on http://localhost:5000/admin 至此,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效能模組,需要先安裝google-perftools 下載連結:http://download.savannah.gnu.org/releases/libunwind #libunwind庫為基於64位CPU和作業系統的程式提供了基本函數調用鏈和函數調用寄存器功能http://https://github.com/gperftools/gperftools/releases #google效能庫安裝libunwind
(quokka-env)[root@linux2 tmp]# tar zxf libunwind-1.1.tar.gz (quokka-env)[root@linux2 tmp]# cd libunwind-1.1(quokka-env)[root@linux2 libunwind-1.1]# CFLAGS=-fPIC ./configure (quokka-env)[root@linux2 libunwind-1.1]# make CFLAGS=-fPIC(quokka-env)[root@linux2 libunwind-1.1]# make CFLAGS=-fPIC install

 

安裝google-perftools
(quokka-env)[root@linux2 tmp]# wget  https://github.com/gperftools/gperftools/releases/download/gperftools-2.4/gperftools-2.4.tar.gz(quokka-env)[root@linux2 gperftools-2.4]# ./configure(quokka-env)[root@linux2 gperftools-2.4]# make && make install為google-perftools添加線程目錄[root@localhost home]#mkdir /tmp/tcmalloc  [root@localhost home]#chmod 0777 /tmp/tcmalloc

 

安裝nginx 
(quokka-env)[root@linux2 nginx]# yum install -y gcc-c++ gcc automake autoconf libtool make(quokka-env)[root@linux2 tmp]# curl -O http://nginx.org/download/nginx-1.8.0.tar.gz  #下載最新穩定版nginx(quokka-env)[root@linux2 tmp]# tar zxvf nginx-1.8.0.tar.gz (quokka-env)[root@linux2 tmp]# cd nginx-1.8.0(quokka-env)[root@linux2 nginx-1.8.0]# groupadd www(quokka-env)[root@linux2 nginx-1.8.0]# useradd -s /sbin/nologin -g www www(quokka-env)[root@linux2 nginx-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@linux2 nginx-1.8.0]# make && make installnginx編譯模組說明:--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: 可以替換頁面中的文本

 

 配置nginx 
(quokka-env)[root@linux2 conf]# vim nginx.confworker_processes  1;google_perftools_profiles /tmp/tcmalloc; #增加線程目錄events {worker_connections  1024;}http {include   mime.types;default_type  application/octet-stream;sendfileon;keepalive_timeout  65;upstream quokka {server unix:/tmp/quokka.sock fail_timeout=0;}server {listen 80 default_server;return 444;  #當請求不匹配server_name 返回http code 444}server { #配置沒有主機頭    listen       80;    server_name  *.eason.wang;    return       301 https:/eason.wang$request_uri;}server {listen   80;client_max_body_size 4G;server_name  eason.wang; #server name 也就是你的網域名稱keepalive_timeout 5;root /data/www/quokka; #暫時沒搞明白為什麼要配置這個路徑location / {try_files $uri @proxy_to_app;}location @proxy_to_app {proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Host $http_host;proxy_redirect off;proxy_pass   http://quokka;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}}

 

 安裝配置gunicorn一個python WSGI的HTTP Server,具體我也不知道該怎麼說.可以參考官網
(quokka-env)[root@linux2 logs]# pip install gunicorn(quokka-env)[root@linux2 quokka]# cd /data/www/quokka/(quokka-env)[root@linux2 quokka]# vim start_gunicorn.sh  #啟動指令碼#!/bin/bashNAME='quokka'   #應用的名稱FLASKDIR=/data/www/quokka  #django項目的目錄SOCKFILE=/tmp/quokka.sock   #使用這個sock來通訊USER=www #運行此應用的使用者NUM_WORKERS=3  #gunicorn使用的背景工作處理序數DJANGO_WSGI_MODULE=wsgi  #wsgi模組echo "starting $NAME as `whoami`"#啟用python虛擬運行環境cd $FLASKDIRsource /root/quokka-env/bin/activate#啟動flaskexec /root/quokka-env/bin/gunicorn \--workers $NUM_WORKERS \-u www \--bind=unix:$SOCKFILE \wsgi運行(quokka-env)[root@linux2 quokka]# ./start_gunicorn.sh &

 

 遇到的問題 1、安裝pip時,執行python get-pip.py後成功,但是沒有pip命令。反覆確認,執行這條安裝命令並沒有出錯,我想可能是Python版本的問題,因為linux預設是python 2.6.6,而我要使用virtualenv所以我也懶得做環境變數了,所以我用絕對路徑方法指定新版本python重新安裝解決。 [root@linux2 pip-7.1.2]# /usr/local/bin/python2.7 get-pip.py --force-reinstall2、mongodb遇到要刪除資料庫操作時,刪除命令為下 [root@linux2 shell]# /usr/local/mongodb/bin/mongo 127.0.0.1:10101> use xxx #選擇要刪除的資料庫> db.dropDatabase() #刪除資料庫 3、當執行pip安裝時報錯“Could not find a version that satisfies the requirement ...”時,使用-v參數查看詳細原因並解決。例:
[root@linux2 www]# pip install pyshorteners==0.5.5 -vCollecting pyshorteners==0.5.5  Getting page https://pypi.python.org/simple/pyshorteners/  Starting new HTTPS connection (1): pypi.python.org  "GET /simple/pyshorteners/ HTTP/1.1" 404 28063  Could not fetch URL https://pypi.python.org/simple/pyshorteners/: 404 Client Error: Not Found - skipping  1 location(s) to search for versions of pyshorteners:  * https://pypi.python.org/simple/pyshorteners/  Getting page https://pypi.python.org/simple/pyshorteners/  "GET /simple/pyshorteners/ HTTP/1.1" 404 28063  Could not fetch URL https://pypi.python.org/simple/pyshorteners/: 404 Client Error: Not Found - skipping  Could not find a version that satisfies the requirement pyshorteners==0.5.5 (from versions: )Cleaning up...No matching distribution found for pyshorteners==0.5.5Exception information:Traceback (most recent call last):  File "/root/quokka-env/lib/python2.7/site-packages/pip/basecommand.py", line 211, in mainstatus = self.run(options, args)  File "/root/quokka-env/lib/python2.7/site-packages/pip/commands/install.py", line 305, in runwb.build(autobuilding=True)  File "/root/quokka-env/lib/python2.7/site-packages/pip/wheel.py", line 705, in buildself.requirement_set.prepare_files(self.finder)  File "/root/quokka-env/lib/python2.7/site-packages/pip/req/req_set.py", line 334, in prepare_filesfunctools.partial(self._prepare_file, finder))  File "/root/quokka-env/lib/python2.7/site-packages/pip/req/req_set.py", line 321, in _walk_req_to_installmore_reqs = handler(req_to_install)  File "/root/quokka-env/lib/python2.7/site-packages/pip/req/req_set.py", line 461, in _prepare_filereq_to_install.populate_link(finder, self.upgrade)  File "/root/quokka-env/lib/python2.7/site-packages/pip/req/req_install.py", line 250, in populate_linkself.link = finder.find_requirement(self, upgrade)  File "/root/quokka-env/lib/python2.7/site-packages/pip/index.py", line 571, in find_requirement'No matching distribution found for %s' % reqDistributionNotFound: No matching distribution found for pyshorteners==0.5.5

 

 通過上邊可以看到,擷取串連404了,所以我們來驗證下是真的404還是pip有問題,可以直接存取web或者使用curl來驗證 (quokka-env)[root@linux2 www]# curl -I https://pypi.python.org/simple/pyshorteners/HTTP/1.1 404 Not Found 頁面404就屬於項目負責人在調整頁面或者撤銷的問題了。 如果想儘快解決,在github搜尋該項目,一般README文檔會有本地怎麼安裝。 4、解決nginx啟動時報錯
nginx: error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directorynginx: 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 ~]# whereis libprofiler.so.0 libunwind.so.8 #找到兩個lib檔案位置libprofiler.so: /usr/lib64/libprofiler.so.0 /usr/local/lib/libprofiler.so.0 /usr/local/lib/libprofiler.solibunwind.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: error while loading shared libraries: libunwind.so.8: cannot open shared object file: No such file or directory(quokka-env)[root@linux2 ~]# ln -s /usr/local/lib/libunwind.so.8 /usr/lib64/

 

 5、解決指定gunicorn -u 沒有許可權問題
(quokka-env)[root@linux2 quokka]# gunicorn -u www --workers 3 --bind unix:/tmp/quokka.sock -m 007 wsgi[2015-09-24 10:53:57 +0000] [28230] [INFO] Starting gunicorn 19.3.0[2015-09-24 10:53:57 +0000] [28230] [INFO] Listening at: unix:/tmp/quokka.sock (28230)[2015-09-24 10:53:57 +0000] [28230] [INFO] Using worker: sync[2015-09-24 10:53:57 +0000] [28235] [INFO] Booting worker with pid: 28235[2015-09-24 10:53:57 +0000] [28235] [ERROR] Exception in worker process:Traceback (most recent call last):  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 507, in spawn_worker  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/workers/base.py", line 118, in init_process  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/util.py", line 355, in import_app  File "/data/www/quokka/wsgi.py", line 4, in <module>from werkzeug.serving import run_simpleImportError: No module named werkzeug.servingTraceback (most recent call last):  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 507, in spawn_worker  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/workers/base.py", line 118, in init_process  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/util.py", line 355, in import_app  File "/data/www/quokka/wsgi.py", line 4, in <module>from werkzeug.serving import run_simpleImportError: No module named werkzeug.serving[2015-09-24 10:53:57 +0000] [28235] [INFO] Worker exiting (pid: 28235)Traceback (most recent call last):  File "/root/quokka-env/bin/gunicorn", line 11, in <module>sys.exit(run())  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in runWSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py", line 189, in runsuper(Application, self).run()  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py", line 72, in runArbiter(self).run()  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 174, in runself.manage_workers()  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 477, in manage_workersself.spawn_workers()  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 541, in spawn_workerstime.sleep(0.1 * random.random())  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 214, in handle_chldself.reap_workers()  File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 459, in reap_workersraise HaltServer(reason, self.WORKER_BOOT_ERROR)gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>

 

 根據執行的命令和堆疊追蹤來看,基本上確定兩個錯誤資訊: ImportError: No module named werkzeug.serving匯入模組錯誤,檢查是不是模組問題。 大面積的File "/root ...",env檔案夾在/root帳號下要檢查www帳號是不是有許可權問題。 下面檢查這兩個錯誤: 
(quokka-env)[root@linux2 tmp]# pythonPython 2.7.10 (default, Sep 21 2015, 17:31:07) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> from werkzeug.serving import run_simple

 

 沒有報錯,看來模組是沒有問題的
(quokka-env)[root@linux2 tmp]# gunicorn --workers 3 --bind unix:/tmp/quokka.sock -m 007 wsgi       [2015-09-24 11:36:19 +0000] [28350] [INFO] Starting gunicorn 19.3.0[2015-09-24 11:36:19 +0000] [28350] [INFO] Listening at: unix:/tmp/quokka.sock (28350)[2015-09-24 11:36:19 +0000] [28350] [INFO] Using worker: sync[2015-09-24 11:36:19 +0000] [28355] [INFO] Booting worker with pid: 28355[2015-09-24 11:36:19 +0000] [28356] [INFO] Booting worker with pid: 28356[2015-09-24 11:36:19 +0000] [28357] [INFO] Booting worker with pid: 28357

 

 去掉-u www可以正常啟動,那麼看來就是許可權惹的問題~ 解決方案: [root@linux2 /]# ll...drwxr-----. 14 root root  4096 9月  23 19:50 root...[root@linux2 /]# chmod g+x root 這裡為什麼要加屬組的執行許可權,是因為我們執行gunicorn命令的許可權是目前使用者和目前使用者組(可以通過-g改變),而我們改變了啟動的使用者為www,所以它會嘗試用www使用者權限,www沒有這個許可權,它又會去嘗試目前使用者組許可權(既root組),所以要給root組有執行許可權即可

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.