วันอาทิตย์ที่ 15 มิถุนายน พ.ศ. 2557

เริ่มต้นกับ Openresty

เรื่องของเรื่องคือ มีเพื่อนผมคนนึงเขียน Openresty มาเป็นปี เคยเขียน MVC ให้ Openresty ด้วย แต่กลับมาบ่นๆ ว่า "พี่ๆ Openresty นี่ create project แยกกันยังไง คือผมอยากทำหลาย project บนเครื่องเดียวกัน"

อ้าว...สะงั้น เอางี้ผมบันทึกให้ แต่แชร์เพื่อนๆ ที่สนใจด้วยเลยละกัน...

หมายเหตุ : ผมใช้ OSX Maverick

ผมเริ่มจากเอา pcre verison ล่าสุด (ที่ผมใช้มัน 8.33 ไม่ล่าสุดหรอก) มา build ง่ายๆ ด้วย
$ ./configure
$ make
$ sudo make install

จากนั้น download source openresty มาจาก openresty.org untar แล้ว build
$ ./configure --with-luajit --with-cc-opt="-I/usr/local/include" --with-ld-opt="-L/usr/local/lib"
$ make
$ sudo make install

พอเวลาจะสร้าง application ก็เริ่มจาก สร้าง project folder

$ mkdir myproject
$ cd myproject
$ mkdir logs
$ mkdir conf

สร้าง nginx.conf ใน folder conf โดยเริ่มจาก default config แบบนี้ครับ

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }
    }
}

จากนั้น set path 

$ export PATH=/usr/local/openresty/nginx/sbin:$PATH

แล้ว start project 

$ nginx -p `pwd`/ -c conf/nginx.conf

แล้วใช้ browser เรียก http://127.0.0.1:8080

จะได้


หากต้องการสร้าง Project อื่นๆ อีก ก็สามารถทำแบบเดียวกันได้เลย โดยเริ่มจากการสร้าง Project folder

หากแก้ไข script conf หรือเพิ่ม lua ไฟล์ ต้องทำการ reload nginx ด้วย

$ nginx -s reload -p `pwd`/ -c conf/nginx.conf 



ไม่มีความคิดเห็น:

แสดงความคิดเห็น