顯示具有 AWS 標籤的文章。 顯示所有文章
顯示具有 AWS 標籤的文章。 顯示所有文章

2012年8月13日 星期一

django + haystack + elasticsearch


前言
網路評價,同樣也是全文檢索的 sphinx 在效能上是非常優異的,而 java base 的 lucene 在效能上表現較差。而先前也曾經整合過 sphinxdjango 中。
為何會再研究整合 elasticsearch 主要原因有二:

  • sphinx 採用的授權方式用在商業上有可能會有爭議
  • 由於 haystack 的支援,對 django 的整合性非常高
  • 至於效能的議題,或許等資料量真的大到某個程度後再來煩惱也不遲。(尤其 sphinx 的 reindex 速度如此之快,到時再派它出馬也行…)

    事前準備
    請先閱讀ec2(ubuntu12.04) + django + apache + mysql + scrapy + sfc(sphinx) 安裝
    重複的步驟便不在贅述,主要是把 sfc 換成 haystack + elasticsearch

    安裝步驟
    安裝 elasticsearch(參考 https://gist.github.com/2026107)
    apt-get install openjdk-7-jre -y

    wget https://github.com/downloads/elasticsearch/elasticsearch/elasticsearch-0.19.8.tar.gz -O elasticsearch.tar.gz
    tar -xf elasticsearch.tar.gz
    rm elasticsearch.tar.gz
    mv elasticsearch-* elasticsearch
    mv elasticsearch /usr/local/share

    curl -L http://github.com/elasticsearch/elasticsearch-servicewrapper/tarball/master | tar -xz
    mv *servicewrapper*/service /usr/local/share/elasticsearch/bin/
    rm -Rf *servicewrapper*
    /usr/local/share/elasticsearch/bin/service/elasticsearch install
    ln -s `readlink -f /usr/local/share/elasticsearch/bin/service/elasticsearch` /usr/local/bin/rcelasticsearch


    EC2 micro 規格較差,需將 ES_HEAP_SIZE 改為 256 (or 512) 才有辦法跑起來,所以需要調整 configure 檔案,更動檔案最上方兩行

    vi /usr/local/share/elasticsearch/bin/service/elasticsearch.conf
    set.default.ES_HOME=/usr/local/share/elasticsearch/ set.default.ES_HEAP_SIZE=256

    啟動 elasticsearch
    service elasticsearch start

    透過 curl 測試
    curl http://localhost:9200
    {   "ok" : true,   "status" : 200,   "name" : "Mammomax",   "version" : {     "number" : "0.19.8",     "snapshot_build" : false   },   "tagline" : "You Know, for Search" }
    安裝 haystack
    (假設已經安裝好 django/mysql 等相關環境)
    apt-get install git
    pip install -e git+https://github.com/toastdriven/django-haystack.git@master#egg=django-haystack
    cd src/django-haystack/
    python setup.py install
    cd ~
    rm -r src

    安裝其他相關套件
    pip install requests

    pip install -e git+git://github.com/toastdriven/pyelasticsearch.git@master#egg=pyelasticsearch
    cd src/pyelasticsearch/
    python setup.py install
    cd ~
    rm -r src

    設定資料庫
    mysql -u root –p
    CREATE DATABASE HaystackSearch default character set utf8mb4;
    exit

    建立 Django 專案
    自行建立一個專案可參考
    http://django-haystack.readthedocs.org/en/latest/tutorial.html

    採用我已經建立好的專案(主要以上面教學為主進行操作)
    上傳 HaystackSearch 資料夾至 /root/
    (需設定 mysql 帳號密碼)

    cd ~/HaystackSearch
    python manage.py runserver 0.0.0.0:80

    http://[EC2_IP]/admin/HaystackSearch/note/add/
    新增幾筆後,下達
    python manage.py update_index

    接著就可以在底下頁面測試搜尋的結果了
    http://[EC2_IP]/search

    2012年8月7日 星期二

    ec2(ubuntu12.04) + django + apache + mysql + scrapy + sfc(sphinx) 安裝


    前言
    對一般人來說,比較陌生的是:

    • scrapypython 的爬蟲套件
    • sfc(sphinx) – Sphinx For Chinese 基sphinx 全文檢索套件,並加上中文分詞的支援。(本來打算用 coreseek,但似乎用在繁體中文會有問題,需另外花錢請 coreseek 研發團隊制定) 這是一篇基於 django 安裝搜尋引擎環境的文章。
    事前準備
    我假設你已經熟悉以下相關知識:
    • AWS EC2(Ubuntu)
    • django

    安裝步驟
    登入EC2後,首先進行 apt-get 的更新
    apt-get update
    apt-get upgrade

    EC2 本身就會 default 安裝好 python 2.7.3,因此只要直接安裝 pip 以及 virtualenv 即可
    安裝 pip
    apt-get install python-pip

    安裝 virtualenv
    apt-get install python-virtualenv

    建立虛擬環境
    之後便利用 virtualenv 建立一個虛擬環境給 django,使後續安裝的 python 元件獨立於系統 python 的函式庫外。(此步驟非必須,只是這樣可以建立乾淨的環境)
    virtualenv ./djangoenv

    將環境切換至 djangoenv 虛擬環境
    source ./djangoenv/bin/activate

    安裝 django(目前最新的版本是 1.4.1)
    pip install django

    安裝 scrapy
    要安裝 scrapy 之前,必須先安裝 gcc 以及編譯相關套件
    apt-get install python-dev
    apt-get install build-essential
    apt-get install libxml2-dev
    apt-get install libxslt-dev

    接著安裝 scrapy
    pip install scrapy

    安裝 mysql
    apt-get install mysql-server
    此步驟會進行mysql root 的帳號設定

    apt-get install libmysqlclient-dev
    apt-get install mysql-client
    pip install mysql-python



    安裝 sfc
    wget http://sphinx-for-chinese.googlecode.com/files/sphinx-for-chinese-2.0.2-beta-r3019.tar.gz
    tar zxvf sphinx-for-chinese-2.0.2-beta-r3019.tar.gz
    cd sphinx-for-chinese-2.0.2-beta-r3019/
    ./configure --prefix=/usr/local/sphinx --with-mysql
    make & make install
    cd ~

    設置環境變數
    vi ~/.bashrc

    在最底下加上(~/.bashrc 是每個使用者登入都會執行的一個 sh 檔,通常用來宣告環境參數)
    PATH="/bin:/sbin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sphinx/bin/"
    PATH="$PATH":/usr/X11R6/bin:/home/dmtsai/bin
    LANG=zh_TW.big5

    reboot

    重新登入後,一樣切換到 djangoenv 底下
    source ./djangoenv/bin/activate

    確認是否有成功
    echo $LANG
    => zh_TW.big5

    下載 & 安裝中文字庫(未來新增字詞就是反覆操作此步驟)
    wget http://sphinx-for-chinese.googlecode.com/files/xdict_1.1.tar.gz
    tar zxvf xdict_1.1.tar.gz
    mkdict xdict_1.1.txt xdict
    mv xdict /usr/local/sphinx/etc/

    產生出來的 xdict 檔案便是未來定義 sphinx.conf 檔案時,chinese_dictionary 參數所需指向的路徑(如 chinese_dictionary = /usr/local/sphinx/etc/xdict )
    若要新增字詞,則可編輯 xdict_1.1.txt 檔案後,重新執行底下的指令即可
    mkdict xdict_1.1.txt xdict

    安裝 django-sphinx
    pip install django-sphinx

    設定 sfc
    下載檔案
    壓縮檔內的檔案分別是:
    • dstest – django 的 sfc test application (需開啟 settings.py 設定資料庫帳密)
    • scrapytest – scrapy 的 test application
    • build_main_index.sh - 更新 index 的腳本 / 清除 sphinx log
    • sphinx.conf – sphinx 的設定檔 (需開啟設定資料庫帳密)

    解壓縮上傳之後
    mv sphinx.conf /usr/local/sphinx/etc
    mv build_main_index.sh /usr/local/sphinx/bin/
    chmod +x /usr/local/sphinx/bin/build_main_index.sh

    設置 crontab 每30分鐘重新建立一次索引
    crontab -e
    0-57/30 * * * * /bin/sh /usr/local/sphinx/bin/build_main_index.sh

    增加開機程序,主要使 searchd 不用每次都人工開啟
    vi /etc/rc.local
    在檔案最下方增加 (exit 0 之上)
    ulimit -SHn 500000
    /usr/bin/nohup /bin/sh /usr/local/sphinx/bin/build_main_index.sh 2>&1 > /dev/null &
    /usr/local/sphinx/bin/searchd

    如此一來便大功告成

    測試 sphinx
    將上面壓縮檔裡的 dstest 與 scrapytest 都放置在 /root/ 底下
    mysql -u root -p
    CREATE DATABASE dstest default character set utf8;
    exit

    cd ~/dstest/
    python manage.py syncdb
    python manage.py runserver 0.0.0.0:80
    然後進入到 admin 介面增加幾筆資料

    接著下達底下指令建立索引
    indexer --all

    打開 searchd
    searchd

    打開瀏覽器,輸入
    http://[EC2_IP]/search/

    就可以看到搜尋畫面,並可以搜尋剛剛建立的資料(default 是全部的資料)

    測試 scrapy
    cd ~/scrapytest/

    下達底下指令就會開始爬文,並存入 mysql db dstest 中 model 的資料表裡
    scrapy crawl scrapytest

    都沒看到錯誤訊息就算是成功了,接著可以到後台看是否有資料匯入(但資料我是亂抓的,沒經過處理,所以看起來會很像亂碼)

    若要把爬過的資料也納入搜尋中,可執行
    indexer --rotate --all

    安裝 apache
    apt-get install apache2
     
    安裝 mod_wsgi
    mod_wsgi 可使 Apache 支援 python 的應用程式,據網路評價,在表現上比 mod_python 還優異許多,底下是官方網站
    http://code.google.com/p/modwsgi/
    apt-get install libapache2-mod-wsgi
     
    設定 django + apache
    (記得先停用 python manage.py runserver)
    NOTE:
    /etc/apache2/sites-available
    這裡放的是可用但不一定啟用的設定檔,需透過 a2ensite, a2dissite 指令啟停
     
    /etc/apache2/sites-enabled
    這裡放的是會自動啟用的設定檔,若設定上有衝突,從檔名排序小的優先
    (如都佔用了 VirtualHost *:80)
    

    先開啟 /root/ 資料夾權限,讓 apache 可存取
    chmod 777 /root
     
    cd /etc/apache2/sites-enabled
    mv 000-default 999-default
    vi 000-dstest

    貼上底下的內容
    <VirtualHost *:80>
            WSGIScriptAlias / /root/dstest/dstest/wsgi.py
            Alias /static /root/djangoenv/lib/python2.7/site-packages/django/contrib/admin/static
    </VirtualHost>


    因使用 virtualenv,所以 dstest/dstest/wsgi.py 也做了些修改,在最上方加入
    import sys
    import site

    site.addsitedir('/root/djangoenv/lib/python2.7/site-packages')
    sys.path.append('/root/dstest')


    重啟apache
    /etc/init.d/apache2 restart

    打開瀏覽器,輸入
    http://[EC2_IP]/admin/

    就可以看到 admin 頁面,完成了所有設定。
     
     
    注意事項:
    Alias /static /root/djangoenv/lib/python2.7/site-packages/django/contrib/admin/static
    這行只是暫時的解法,未來應該要在適合的地方建立 django 的 static 資料夾以及 media 資料夾,並用上述的方法分別設置。
    設置後也需記得將 /root/djangoenv/lib/python2.7/site-packages/django/contrib/admin/static/admin 資料夾複製到你設置的 static 底下)
     
    Reference:

    Amazon EC2 使用密碼登入(不用 pem 檔案)

    Quick Note
    1. 正常方式登入 EC2(use ssh –i xxxx.pem)
    2. sudo vi /etc/ssh/sshd_config
    3. 將 PasswordAuthentication 改成 yes 後存檔
    4. sudo passwd root - 設置 root 密碼
    5. sudo reboot
    補充