ติดตั้ง CKAN ไม่ได้ยากอย่างที่คิด !

ติดตั้ง CKAN ไม่ได้ยากอย่างที่คิด !

09 February 2021
demo.ckan.org

จากที่เราได้กล่าวถึง Data Catalog ว่าคืออะไร และมีประโยชน์อย่างไรในบทความ “ดาต้าแคตตาล็อก (Data Catalog) จะช่วยเตรียมความพร้อมรับมือ COVID-19 ได้อย่างไร” ในบทความนี้ เราจะมาลองติดตั้ง Data Catalog เพื่อทดลองใช้งานกัน

https://ckan.org

CKAN เป็น Open Source Data Portal Software ที่สามารถใช้ทำ Data Catalog ของหน่วยงานได้ โดยหน่วยงานต่าง ๆ สามารถนำมาใช้งานได้ฟรีไม่เสียค่าใช้จ่าย การเป็น Open Source หมายความว่า มีการเปิดให้เห็น source code ของตัวซอฟต์แวร์ ซึ่งเราสามารถนำมาศึกษาได้

CKAN ถูกใช้โดยรัฐบาลและหน่วยงานมากมายทั่วโลก อาทิ สหรัฐอเมริกา แคนาดา สวิสเซอร์แลนด์ และออสเตรเลีย เป็นต้น และยังมีตัวอย่างอื่น ๆ อีกมากมาย สำหรับผู้ที่สนใจสามารถเข้าไปเยี่ยมได้ที่ https://ckan.org/about/instances อีกทั้ง Data Portal Software ที่สำนักงานพัฒนาวิทยาศาสตร์และเทคโนโลยีแห่งชาติ (สวทช.) กำลังพัฒนา ก็มีการต่อยอดมาจาก CKAN เช่นกัน

จึงเป็นการดีที่หน่วยงานที่กำลังคิดจะทำ Data Catalog จะมาลองติดตั้งและเล่นกับ CKAN ดู เพื่อเป็นหนึ่งในตัวเลือกของ software ที่จะมาใช้ทำ Data Catalog


Requirements

สำหรับการทดลองติดตั้ง CKAN ขอให้จัดเตรียม Virtual Machine ที่มีคุณสมบัติ ดังนี้

  • OS: Ubuntu 18
  • CPU: 2 core
  • RAM: 4 GB
  • Disk: 10 GB

**หมายเหตุ คุณสมบัตินี้ ใช้สำหรับการทำ prototype เท่านั้น


Installation

สำหรับ prototype นี้ เราจะทำการติดตั้ง CKAN 2.9 from source (Python 3)

ขั้นตอนที่ 1 Install the required packages

การติดตั้ง CKAN  เริ่มต้นที่การติดตั้ง package ที่เกี่ยวข้อง

1. เริ่มต้นที่การ update รายการ package ที่ VM รู้จักด้วยคำสั่ง

sudo apt update

2. จากนั้นทำการติดตั้ง package ที่จำเป็นสำหรับ CKAN 2.9 โดยรายการ package ที่จำเป็นมีดังนี้

  • Python3-dev ใช้สำหรับรันโปรแกรม CKAN เนื่องจาก CKAN 2.9 เขียนด้วยภาษา python 3
  • Postgresql เป็นระบบฐานข้อมูลที่ CKAN เลือกใช้
  • Libpq-dev
  • Python3-pip ใช้สำหรับติดตั้ง library ภายใน python 3
  • Python3-venv ใช้สำหรับสร้าง virtual environment ของ python 3
  • Git-core ใช้สำหรับโหลด source code ของ CKAN และ extension อื่น ๆ
  • Openjdk-8-jdk
  • Redis-server
sudo apt-get install python3-dev postgresql libpq-dev python3-pip python3-venv git-core openjdk-11-jdk redis-server solr-tomcat

ขั้นตอนที่ 2 Install CKAN into a Python virtual environment

ทำการติดตั้ง CKAN ไว้ใน python virtual environment

1. สร้าง directory และทำการอ้างอิง (symlink) ไปยังโฟลเดอร์ด้วยคำสั่ง

mkdir -p ~/ckan/lib
sudo ln -s ~/ckan/lib /usr/lib/ckan
mkdir -p ~/ckan/etc
sudo ln -s ~/ckan/etc /etc/ckan

2. สร้าง python virtual environment สำหรับ CKAN เพื่อให้สามารถควบคุมเวอร์ชั่นของ library ที่เจาะจงสำหรับ CKAN

sudo mkdir -p /usr/lib/ckan/default
sudo chown `whoami` /usr/lib/ckan/default
python3 -m venv /usr/lib/ckan/default

3. ทำการเข้าสู่ virtual environment ที่สร้างสำหรับ CKAN

. /usr/lib/ckan/default/bin/activate

สังเกตว่า เราได้เข้าสู่ virtual environment ได้จากการมี (default) เพิ่มขึ้นมาข้างหน้า

เข้าสู่ virtual environment ที่ชื่อว่า default

4. ติดตั้ง setuptools เวอร์ชั่น 44.1.0

pip install setuptools==44.1.0

5. อัพเดท pip (package สำหรับจัดการ library python)

pip install --upgrade pip

6. ติดตั้ง CKAN 2.9.1 ใน virtual environment ที่สร้างโดยใช้ source code จาก github

pip install -e 'git+https://github.com/ckan/ckan.git@ckan-2.9.1#egg=ckan[requirements]'

7. หลังจากติดตั้ง CKAN-2.9.1 เรียบร้อยแล้ว ทำการ restart virtual environment

deactivate
. /usr/lib/ckan/default/bin/activate

ขั้นตอนที่ 3 Setup postgres database for CKAN

1. ขั้นตอนถัดมาเป็นการ setup database สำหรับ CKAN ทำการ list ดู postgres database และตรวจสอบว่า Encoding เป็น UTF8 และ เป็น en_US.UTF-8 (หากไม่ใช่ สามารถทำตาม website https://makandracards.com/makandra/18643-how-to-change-the-locale-of-a-postgresql-cluster นี้ เพื่อทำการเปลี่ยน cluster’s locale)

sudo -u postgres psql -l
ตรวจสอบ Encoding

2. สร้าง database user สำหรับ CKAN โดยตั้งชื่อว่า ckan_default

sudo -u postgres createuser -S -D -R -P ckan_default

3. สร้าง database ชื่อ ckan_default โดยมี owner เป็น ckan_default user ที่เพิ่งสร้างเมื่อครู่

sudo -u postgres createdb -O ckan_default ckan_default -E utf-8

4. List ดู databases อีกที และตรวจสอบ database ที่เพิ่งสร้าง

sudo -u postgres psql -l
ตรวจสอบ Database ที่เพิ่งสร้าง

ขั้นตอนที่ 4 Create and Edit a CKAN config file

สร้างและตั้งค่า config file ของ CKAN

1. สร้าง directory สำหรับ CKAN config file พร้อมทั้งตั้งค่าให้เราเป็นเจ้าของ directory นั้น

sudo mkdir -p /etc/ckan/default
sudo chown -R `whoami` /etc/ckan/

2. สร้าง CKAN config file

ckan generate config /etc/ckan/default/ckan.ini

3. ตั้งค่าตัวแปร ภายใน CKAN config file
เปิด config file โดยใช้โปรแกรม vim

sudo vim /etc/ckan/default/ckan.ini

แก้ไขบรรทัดต่อไปนี้

sqlalchemy.urlsqlalchemy.url = postgresql://ckan_default:pass@bigdata.go.th/ckan_default
**ให้แทน pass ด้วย password ที่เพิ่งตั้งให้ database user เมื่อสักครู่
ckan.site_urlhttp://ipaddress
**แทน ipaddress ด้วย ip ของ virtual machine
แก้ไข config file ของ CKAN

ขั้นตอนที่ 5 Setup Solr

ทำการ config solr ซึ่งเป็น software ที่ช่วยในเรื่องการค้นหาชุดข้อมูลของ CKAN

1. เปลี่ยน config file ของ tomcat ให้เชื่อมต่อกับ port 8983
เปิด config file โดยใช้โปรแกรม vim

sudo vim /etc/tomcat9/server.xml

แก้ไขบรรทัดต่อไปนี้ (ประมาณบรรทัดที่ 69)

จาก
<Connector port=”8080” protocol=”HTTP/1.1″
เป็น
<Connector port=”8983” protocol=”HTTP/1.1″
แก้ไข config file ของ tomcat

2. ใช้ schema.xml จาก CKAN source แทนตัวที่ solr ใช้อยู่

ทำการ backup schema.xml ที่มี

sudo mv /etc/solr/conf/schema.xml /etc/solr/conf/schema.xml.bak

จากนั้น Symlink schema.xml มาจาก CKAN source

sudo ln -s /usr/lib/ckan/default/src/ckan/ckan/config/solr/schema.xml /etc/solr/conf/schema.xml

3. Restart solr service

sudo service tomcat9 restart

4. ตรวจสอบว่า solr ทำงาน ผ่าน web browser โดยการพิมพ์ URL

http://<ip address>:8983/solr

ปรากฎหน้าจอดังตัวอย่าง

หน้าจอ solr ที่ทำงานถูกต้อง

5. **สำหรับ ubuntu 18.04 หรือเก่ากว่า อาจจะเจอ error message
java.io.IOException: Cannot create directory: /var/lib/solr/data/index
ให้ทำการแก้ไข โดยใช้คำสั่งต่อไปนี้ แล้วลองเปิดหน้า solr ผ่าน web browser ดูใหม่อีกครั้ง

sudo mv /etc/systemd/system/tomcat9.d /etc/systemd/system/tomcat9.service.d

sudo systemctl daemon-reload

sudo service tomcat9 restart

6. แก้ไข solr_url setting ใน CKAN config file
เปิด config file โดยใช้โปรแกรม vim

sudo vim /etc/ckan/default/ckan.ini

แก้ไขบรรทัดต่อไปนี้

solr_url**uncomment บรรทัดนี้
solr_url=http://127.0.0.1:8983/solr

ขั้นตอนที่ 6 Link to who.ini

1. สร้าง symlink

ln -s /usr/lib/ckan/default/src/ckan/who.ini /etc/ckan/default/who.ini

ขั้นตอนที่ 7 Create CKAN database tables

สร้าง ckan database tables

ckan -c /etc/ckan/default/ckan.ini db init

ขั้นตอนที่ 8 Install and Config nginx

CKAN2.9 จำเป็นต้องลง nginx web server เพื่อให้สามารถเปิด CKAN website จากภายนอกได้ (nginx จะทำหน้าที่รับ requests จากภายนอกแล้ว forwards เข้า localhost ซึ่ง CKAN2.9 ถูกติดตั้งเอาไว้)

1. ติดตั้ง nginx

sudo apt install nginx

2. ตั้งค่า nginx สำหรับ CKAN ที่ sites-available

เข้าไปยัง directory sites-available และเขียนการตั้งค่าโดยใช้โปรแกรม vim

cd /etc/nginx/sites-available
sudo vim ckan

กรอกการตั้งค่าดังตารางข้างล่าง

server{
  location / {
    proxy_pass http://127.0.0.1:5000/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection ‘upgrade’;
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

3. ทำการ enable CKAN site โดยจะทำการ symlink การตั้งค่าไปยัง sites-enabled

เข้าไปยัง directory sites-enabled และตรวจสอบดูไฟล์ภายใน

cd ../sites-enabled
ls

จะพบว่ามี default อยู่ ให้ลบออก

พบว่ามี site: default อยู่ภายใน folder sites-enabled
sudo rm default

จากนั้น symlink การตั้งค่าสำหรับ ckan เมื่อสักครู่มายัง sites-enabled

sudo ln -s /etc/nginx/sites-available/ckan /etc/nginx/sites-enabled/ckan

หากตรวจสอบดูอีกที จะพบว่ามี ckan ใน directory นี้แล้ว

ls
ตรวจสอบว่ามี site: ckan ภายใน sites-enabled

4. หลังจากตั้งค่าเสร็จแล้ว ทำการ restart nginx

sudo systemctl restart nginx

5. หลังจากนั้นตรวจสอบสถานะ nginx ว่า active (running) อยู่

sudo systemctl status nginx
ตรวจสอบสถานะของ nginx ว่าเป็น active (running)

ขั้นตอนที่ 9 Create system admin account

ก่อนที่จะใช้งาน CKAN ได้นั้น จำเป็นต้องสร้าง system admin account

1. หากยังไม่ได้อยู่ใน virtual environment ให้เข้าไปเสียก่อน

. /usr/lib/ckan/default/bin/activate
cd /usr/lib/ckan/default/src/ckan

2. จากนั้นสร้าง system admin account โดยใช้คำสั่งต่อไปนี้
(ให้ตั้งชื่อ admin และ email ตามที่ต้องการ)

ckan -c /etc/ckan/default/ckan.ini sysadmin add admin email=admin@bigdata.go.th name=admin

ระบบจะทำการถามว่า ต้องการสร้าง account ใหม่ใช่หรือไม่ (Y) และให้กรอก password

ขั้นตอนที่ 10 Test accessing CKAN website

ทดสอบการเข้าถึง CKAN website

1. run CKAN server

ckan -c /etc/ckan/default/ckan.ini run
หน้าจอหลังจากสั่งคำสั่ง run CKAN server

2. เปิดที่ web browser และเข้าไปยัง IP address ของเครื่อง Virtual Machine ที่ลง CKAN

http://<ipaddress>

หน้าเริ่มต้นของ CKAN

CKAN Extensions

ในการติดตั้งที่เราได้ทำไปนั้น เป็นการติดตั้งในส่วนพื้นฐานของ CKAN แต่ CKAN ยังมีส่วนเพิ่มเติม (Extensions) ที่เราสามารถไปทดลองติดตั้งเพิ่มเติมได้ เพื่อเพิ่มฟังก์ชั่นการทำงานให้กับ Data Catalog ของเรา อาทิ

FileStoreที่ทำให้เราสามารถอัพโหลดไฟล์ขึ้นไปยัง Data Catalog
DataStoreที่หลังจากข้อมูลถูกส่งเข้าไปเก็บที่ Datastore จะทำให้เราสามารถเรียกใช้งาน DataStore API ในการค้นหา กรอง เปลี่ยนแปลงข้อมูลได้ และยังสามารถใช้งาน Data Explorer ในการ preview ข้อมูลในหน้า resource อย่างอัตโนมัติได้อีกด้วย
DataPusherที่ช่วยดึงข้อมูลที่เราใส่เข้าไปใน Data Catalog (ไม่ว่าจะด้วยการวาง link หรือการอัพโหลดไฟล์) และส่งเข้าไปยัง DataStore
Extensions อื่นๆ**ทั้งนี้เราต้องตรวจสอบดูให้ดีก่อนลง extensions ว่าสนับสนุน Python version ที่เราลงหรือไม่

ศึกษาเกี่ยวกับ Data Catalog และการใช้งาน CKAN เพิ่มเติม

สำหรับผู้ที่สนใจว่า Data Catalog คืออะไร และวิธีการใช้งาน CKAN เบื้องต้น ทาง GBDi ได้ทำคลิปวิดีโออธิบายเรื่องราวดังกล่าว โดยสามารถเข้าไปติดตามชมได้ที่

Data Catalog ตอนที่ 1 – Data Catalog

Data Catalog ตอนที่ 2 – สำรวจโปรแกรม CKAN

Data Catalog ตอนที่ 3 – การใช้งาน CKAN เบื้องต้น

แบ่งปันบทความ

กลุ่มเนื้อหา

แท็กยอดนิยม

แจ้งเรื่องที่อยากอ่าน

คุณสามารถแจ้งเรื่องที่อยากอ่านให้เราทราบได้ !
และเราจะนำไปพัฒนาบทความให้มีเนื้อหาที่น่าสนใจมากขึ้น

PDPA Icon

We use cookies to optimize your browsing experience and improve our website’s performance. Learn more at our Privacy Policy and adjust your cookie settings at Settings

Privacy Preferences

You can choose your cookie settings by turning on/off each type of cookie as needed, except for necessary cookies.

Accept all
Manage Consent Preferences
  • Strictly Necessary Cookies
    Always Active

    This type of cookie is essential for providing services on the website of the Personal Data Protection Committee Office, allowing you to access various parts of the site. It also helps remember information you have previously provided through the website. Disabling this type of cookie will result in your inability to use key services of the Personal Data Protection Committee Office that require cookies to function.
    Cookies Details

  • Performance Cookies

    This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

  • Functional Cookies

    This type of cookie enables the Big Data Institute (Public Organization)’s website to remember the choices you have made and deliver enhanced features and content tailored to your usage. For example, it can remember your username or changes you have made to font sizes or other customizable settings on the page. Disabling these cookies may result in the website not functioning properly.

  • Targeting Cookies

    "This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

Save settings
This site is registered on wpml.org as a development site. Switch to a production site key to remove this banner.