Like Share Discussion Bookmark Smile

J.J. Huang   2020-12-16   OpenRestry   瀏覽次數:

OpenResty - 第一章 | OpenResty 介绍/編譯安裝

前言

公司在對一些項目進行重構,使用了很多的平台、技術、架構、語言,而在使用開發上就會遇到很多環境安裝上的問題;當然這一切還是歸咎於不熟悉。

原先透過官方介紹的Mac OS X (macOS)用户安裝OpenResty,可以簡單的啟用,但是遇到的問題是不知道如何指定其讀取專案中的nginx.conf檔和其他目錄位置(預設會讀取/usr/local/etc/openresty/)。故產生了此篇文章,針對Mac OS X (macOS)環境透過編譯的方式安裝,並且透過指令動專案。

註:對OpenRestry不熟悉固使用編譯的方式解決,相信一定有很多大神也更多簡單的方法,敬請不吝賜教。

介紹

OpenResty是基於nginxWeb平台,可以使用其LuaJIT引擎運行Lua腳本。該軟件由張一春創建。它最初是在2011年前由Taobao.com贊助的,在2012年至2016年期間主要由Cloudflare支持。自2017年以來,它主要受到OpenResty軟件基金會和OpenResty Inc.的支持。

OpenResty是一個強大的Web應用服務器,Web開發人員可以使用Lua腳本語言調動nginx支持的各種C以及Lua模塊,更主要的是在性能方面,OpenResty可以快速構造出足以勝任10K以上並發連接響應的超高性能Web應用系統。

360UPYUN、阿里雲、新浪、騰訊網、去哪兒網、酷狗音樂等都是OpenResty的深度用戶。

問題描述

透過Homebrew安裝的OpenRestry,預設會去參考原始目錄和設定檔(類似:/usr/local/etc/openresty/nginx.conf),這樣的情況下,自己的專案程式、設定檔須要複製一份至該目錄下,啟動後才可正確抓取設定和程式,雖然還是可以使用,但是在開發上並沒有這麼直覺方便。

解決方案

官方有提到可以透過編譯的方式構建一份OpenRestry
但是這篇文章並沒有真的手把手教學(Mac OS X (macOS)),例如--with-ld-opt='-L/lib',下方將會有完整的安裝過程教學。

構建 OpenResty

以下是針對Mac OS環境的教學:

    1. 安裝Homebrew
    • 註:如已經安裝可跳過此步驟。
    1. 使用Homebrew將這些庫perlpcreopenssl安裝在你的電腦之中。
    • 檢查perl是否有安裝
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      $ perl -v

      This is perl 5, version 32, subversion 0 (v5.32.0) built for darwin-thread-multi-2level

      Copyright 1987-2020, Larry Wall

      Perl may be copied only under the terms of either the Artistic License or the
      GNU General Public License, which may be found in the Perl 5 source kit.

      Complete documentation for Perl, including FAQ lists, should be found on
      this system using "man perl" or "perldoc perl". If you have access to the
      Internet, point your browser at http://www.perl.org/, the Perl Home Page.
    • 檢查pcre是否有安裝
      1
      2
      $ ls -l /usr/local/opt/pcre
      lrwxr-xr-x 1 morose admin 19 10 21 11:00 /usr/local/opt/pcre -> ../Cellar/pcre/8.44
    • 檢查openssl是否有安裝
      1
      2
      $ openssl version
      LibreSSL 2.8.3
    • 如未安裝可透過此指令安裝(已安裝請忽略)
      1
      2
      3
      4
      $ brew update
      $ brew install perl
      $ brew install pcre
      $ brew install openssl
    1. 下載OpenResty原始碼
      1
      2
      Download page:https://openresty.org/cn/download.html
      Downlaod link:https://openresty.org/download/openresty-1.19.3.1.tar.gz

      註:文章撰寫時最新版為:openresty-1.19.3.1

    1. 解壓縮[openresty-1.19.3.1.tar.gz]
      1
      $ tar -xzvf openresty-1.19.3.1.tar.gz

      註:openresty-1.19.3.1.tar.gz解壓縮路徑自己選擇即可,不影響過程,這邊解壓縮至/Users/morose/Documents/Temp/tutorial/openresty-1.19.3.1

    1. 準備構建OpenResty的目錄
      1
      $ mkdir /Users/morose/Documents/Temp/tutorial/openresty

      註:該目錄會是構建和執行OpenResty的主要目錄,建議放在自己規劃好的路徑。

    1. 進入到步驟4,解壓縮後的目錄下。
      1
      $ cd /Users/morose/Documents/Temp/tutorial/openresty-1.19.3.1
    1. ./configure (配置)

      註:/Users/morose/Documents/Temp/tutorial/openresty 為定義編譯位置,對應第5步驟建立的目錄。
      因為是使用Homebrew安裝pcre、openssl,在沒有設定環境變數的情況下,需要指定–with-cc-opt和–with-ld-opt。
      說明:–with-cc-opt=parameters — 設置將會添加額外參數到CFLAGS變量中;–with-ld-opt=parameters — 設置將會在鏈接(linking)過程中使用的額外參數。

      1
      2
      3
      4
      5
      6
      7
      $ ./configure --prefix=/Users/morose/Documents/Temp/tutorial/openresty \
      --with-cc-opt="-I/usr/local/opt/openssl/include/ -I/usr/local/opt/pcre/include/" \
      --with-ld-opt="-L/usr/local/opt/openssl/lib/ -L/usr/local/opt/pcre/lib/" \
      --with-pcre-jit \
      --with-ipv6 \
      --without-http_redis2_module \
      --with-http_iconv_module
      output:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      # 以上省略
      Configuration summary
      + using system PCRE library
      + using system OpenSSL library
      + using system zlib library

      nginx path prefix: "/Users/morose/Documents/Temp/tutorial/openresty/nginx"
      nginx binary file: "/Users/morose/Documents/Temp/tutorial/openresty/nginx/sbin/nginx"
      nginx modules path: "/Users/morose/Documents/Temp/tutorial/openresty/nginx/modules"
      nginx configuration prefix: "/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf"
      nginx configuration file: "/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/nginx.conf"
      nginx pid file: "/Users/morose/Documents/Temp/tutorial/openresty/nginx/logs/nginx.pid"
      nginx error log file: "/Users/morose/Documents/Temp/tutorial/openresty/nginx/logs/error.log"
      nginx http access log file: "/Users/morose/Documents/Temp/tutorial/openresty/nginx/logs/access.log"
      nginx http client request body temporary files: "client_body_temp"
      nginx http proxy temporary files: "proxy_temp"
      nginx http fastcgi temporary files: "fastcgi_temp"
      nginx http uwsgi temporary files: "uwsgi_temp"
      nginx http scgi temporary files: "scgi_temp"

      ./configure: warning: the "--with-ipv6" option is deprecated
      cd ../..
      Type the following commands to build and install:
      make
      make install

      註:輸出過長僅放最後部分訊息。完整輸出configure-output.txt.zip

    1. make (製作)
      1
      $ make
      output:
      1
      2
      3
      4
      5
      6
      7
      # 以上省略
      -L/Users/morose/Documents/Temp/tutorial/openresty-1.19.3.1/build/luajit-root/Users/morose/Documents/Temp/tutorial/openresty/luajit/lib -L/Users/morose/Documents/Temp/tutorial/openresty-1.19.3.1/build/luajit-root/Users/morose/Documents/Temp/tutorial/openresty/luajit/lib -Wl,-rpath,/Users/morose/Documents/Temp/tutorial/openresty/luajit/lib -L/usr/local/opt/openssl/lib/ -L/usr/local/opt/pcre/lib/ -Wl,-u,_pcre_version -liconv -L/Users/morose/Documents/Temp/tutorial/openresty-1.19.3.1/build/luajit-root/Users/morose/Documents/Temp/tutorial/openresty/luajit/lib -lluajit-5.1 -lm -ldl -pagezero_size 10000 -image_base 100000000 -L/Users/morose/Documents/Temp/tutorial/openresty-1.19.3.1/build/luajit-root/Users/morose/Documents/Temp/tutorial/openresty/luajit/lib -lluajit-5.1 -lm -ldl -pagezero_size 10000 -image_base 100000000 -lpcre -lssl -lcrypto -lz
      sed -e "s|%%PREFIX%%|/Users/morose/Documents/Temp/tutorial/openresty/nginx|" \
      -e "s|%%PID_PATH%%|/Users/morose/Documents/Temp/tutorial/openresty/nginx/logs/nginx.pid|" \
      -e "s|%%CONF_PATH%%|/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/nginx.conf|" \
      -e "s|%%ERROR_LOG_PATH%%|/Users/morose/Documents/Temp/tutorial/openresty/nginx/logs/error.log|" \
      < docs/man/nginx.8 > objs/nginx.8

      註:輸出過長僅放最後部分訊息。完整輸出make-output.txt.zip

    1. make install (安裝)
      1
      $ make install
      output:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      # 以上省略
      test -d '/Users/morose/Documents/Temp/tutorial/openresty/nginx/sbin' \
      || mkdir -p '/Users/morose/Documents/Temp/tutorial/openresty/nginx/sbin'
      test ! -f '/Users/morose/Documents/Temp/tutorial/openresty/nginx/sbin/nginx' \
      || mv '/Users/morose/Documents/Temp/tutorial/openresty/nginx/sbin/nginx' \
      '/Users/morose/Documents/Temp/tutorial/openresty/nginx/sbin/nginx.old'
      cp objs/nginx '/Users/morose/Documents/Temp/tutorial/openresty/nginx/sbin/nginx'
      test -d '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf' \
      || mkdir -p '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf'
      cp conf/koi-win '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf'
      cp conf/koi-utf '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf'
      cp conf/win-utf '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf'
      test -f '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/mime.types' \
      || cp conf/mime.types '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf'
      cp conf/mime.types '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/mime.types.default'
      test -f '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/fastcgi_params' \
      || cp conf/fastcgi_params '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf'
      cp conf/fastcgi_params \
      '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/fastcgi_params.default'
      test -f '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/fastcgi.conf' \
      || cp conf/fastcgi.conf '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf'
      cp conf/fastcgi.conf '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/fastcgi.conf.default'
      test -f '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/uwsgi_params' \
      || cp conf/uwsgi_params '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf'
      cp conf/uwsgi_params \
      '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/uwsgi_params.default'
      test -f '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/scgi_params' \
      || cp conf/scgi_params '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf'
      cp conf/scgi_params \
      '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/scgi_params.default'
      test -f '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/nginx.conf' \
      || cp conf/nginx.conf '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/nginx.conf'
      cp conf/nginx.conf '/Users/morose/Documents/Temp/tutorial/openresty/nginx/conf/nginx.conf.default'
      test -d '/Users/morose/Documents/Temp/tutorial/openresty/nginx/logs' \
      || mkdir -p '/Users/morose/Documents/Temp/tutorial/openresty/nginx/logs'
      test -d '/Users/morose/Documents/Temp/tutorial/openresty/nginx/logs' \
      || mkdir -p '/Users/morose/Documents/Temp/tutorial/openresty/nginx/logs'
      test -d '/Users/morose/Documents/Temp/tutorial/openresty/nginx/html' \
      || cp -R docs/html '/Users/morose/Documents/Temp/tutorial/openresty/nginx'
      test -d '/Users/morose/Documents/Temp/tutorial/openresty/nginx/logs' \
      || mkdir -p '/Users/morose/Documents/Temp/tutorial/openresty/nginx/logs'
      mkdir -p /Users/morose/Documents/Temp/tutorial/openresty/site/lualib /Users/morose/Documents/Temp/tutorial/openresty/site/pod /Users/morose/Documents/Temp/tutorial/openresty/site/manifest
      ln -sf /Users/morose/Documents/Temp/tutorial/openresty/nginx/sbin/nginx /Users/morose/Documents/Temp/tutorial/openresty/bin/openresty

      註:輸出過長僅放最後部分訊息。完整輸出makeInstall-output.txt.zip

    1. 完成後OpenResty目錄結構

      註:主要會使用的是/Users/morose/Documents/Temp/tutorial/openresty/bin/openresty檔。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      /Users/morose/Documents/Temp/tutorial/openresty
      ├── COPYRIGHT
      ├── bin
      │   ├── md2pod.pl
      │   ├── nginx-xml2pod
      │   ├── openresty -> /Users/morose/Documents/Temp/tutorial/openresty/nginx/sbin/nginx
      │   ├── opm
      │   ├── resty
      │   ├── restydoc
      │   └── restydoc-index
      # 以下省略

      註:輸出過長僅放最後部分訊息。完整輸出openresty-tree.txt.zip

啟動/關閉

這邊簡單的示範使用OpenResty指定專案做啟動和關閉。
首先會先建立一個簡單的HelloWorld專案。

    1. 建立openrest-test目錄
      1
      $ mkdir /Users/morose/Documents/Temp/tutorial/demo
    1. 建立conflogs目錄
      1
      2
      $ mkdir /Users/morose/Documents/Temp/tutorial/demo/conf/
      $ mkdir /Users/morose/Documents/Temp/tutorial/demo/logs/
    1. 建立nginx.conf設定檔於conf目錄下
      1
      $ touch /Users/morose/Documents/Temp/tutorial/demo/conf/nginx.conf
    1. 編輯nginx.conf,將下方設定貼入並儲存
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      worker_processes 1;       #nginx worker 數量
      error_log logs/error.log; #指定錯誤日誌文件路徑
      events {
      worker_connections 1024;
      }

      http {
      server {
      #監聽端口,若你的6699端口已經被佔用,則需要修改
      listen 6699;
      location / {
      default_type text/html;

      content_by_lua_block {
      ngx.say("HelloWorld")
      }
      }
      }
      }

      註:至此簡單的project已經準備完畢。

    1. 至編譯後的OpenRestybin目錄
      1
      $ cd /Users/morose/Documents/Temp/tutorial/openresty/bin
    1. 啟動OpenResty並指定啟動專案路徑
      1
      $ ./openresty -p /Users/morose/Documents/Temp/tutorial/demo

      註:/Users/morose/Documents/Temp/tutorial/demo為專案目錄。
      提示:在啟動過OpenResty後,可以再去看看demo目錄下的變化。

    1. 確認是否已啟動
      1
      $ ps -ef | grep openresty
      output:
      1
      2
      501 29498     1   0 12:20下午 ??         0:00.00 nginx: master process ./openresty -p /Users/morose/Documents/Temp/tutorial/demo
      501 29518 29204 0 12:20下午 ttys008 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox openresty

      註:可以看到nginx: master process ./openresty -p /Users/morose/Documents/Temp/tutorial/demo啟動,且-p 到指定目錄。

    1. 瀏覽器瀏覽http://127.0.0.1:6699
      output:
      1
      HelloWorld

      註:表示成功連接到location /,且輸出”HelloWorld”

    1. 關閉OpenResty指定的專案路徑
      1
      $ ./openresty -s stop -p /Users/morose/Documents/Temp/tutorial/demo

結語

在這盡量依照手把手教學的想法寫完這篇文章,希望在使用OpenResty上遇到有這方面需求的人可以得到幫助。原創文章撰寫不易,還請多多支持,謝謝。


註:以上參考了
OpenResty