[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 install ii 安裝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.conf port=10101 dbpath=/data/db/data logpath=/data/db/log/mongodb.log logappend=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 = 10101 MONGODB_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_createsuperuser [email protected] P4$$W0Rd ii 填充示例數據 (可選,如果你想要示例數據) $ python manage.py populate credentials for /admin will be email: [email protected] passwd: admin iii 運行測試 $ python manage.py runserver --host 0.0.0.0 --port 5000 Site on http://localhost:5000 Admin 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-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 install 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: 可以替換頁面中的文本
配置nginx
(quokka-env)[root@linux2 conf]# vim nginx.conf worker_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/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 $NAME as `whoami`" #激活python虛擬運行環境 cd $FLASKDIR source /root/quokka-env/bin/activate #啟動flask exec /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-reinstall 2、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 -v Collecting 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.5 Exception information: Traceback (most recent call last): File "/root/quokka-env/lib/python2.7/site-packages/pip/basecommand.py", line 211, in main status = self.run(options, args) File "/root/quokka-env/lib/python2.7/site-packages/pip/commands/install.py", line 305, in run wb.build(autobuilding=True) File "/root/quokka-env/lib/python2.7/site-packages/pip/wheel.py", line 705, in build self.requirement_set.prepare_files(self.finder) File "/root/quokka-env/lib/python2.7/site-packages/pip/req/req_set.py", line 334, in prepare_files functools.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_install more_reqs = handler(req_to_install) File "/root/quokka-env/lib/python2.7/site-packages/pip/req/req_set.py", line 461, 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", line 250, in populate_link self.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' % req DistributionNotFound: 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 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 ~]# 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.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: 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_simple ImportError: No module named werkzeug.serving 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_simple ImportError: 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 run WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run() File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py", line 189, in run super(Application, self).run() File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py", line 72, in run Arbiter(self).run() File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 174, in run self.manage_workers() File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 477, in manage_workers self.spawn_workers() File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 541, in spawn_workers time.sleep(0.1 * random.random()) File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 214, in handle_chld self.reap_workers() File "/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 459, in reap_workers raise 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]# python Python 2.7.10 (default, Sep 21 2015, 17:31:07) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "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組有執行權限即可