下面,我们来了解一下 OpenResty 的安装方法。

使用包管理系统直接安装

安装 OpenResty 最简单的方式就是通过各个操作系统中自带的包管理工作来安装了。

例如,对于 MacOS,可以使用 brew 来安装,对于 CentOS,可以使用 yum 来安装,对于 Ubuntu 而言,可以使用 apt 来安装等等。

需要注意的是,在使用包管理工具来安装 OpenResty 时,我们通过用的不是操作系统内置的源,而是会使用 OpenResty 提供的仓库。

以 MacOS 系统为例,安装方式如下:

  1. brew tap openresty/brew
  2. brew install openresty

以 CentOS 系统为例,安装方式如下:

  1. # add the yum repo:
  2. wget https://openresty.org/package/centos/openresty.repo
  3. sudo mv openresty.repo /etc/yum.repos.d/
  4. # update the yum index:
  5. sudo yum check-update
  6. # install
  7. sudo yum install openresty
  8. sudo yum install openresty-resty
  9. # query related package
  10. sudo yum --disablerepo="*" --enablerepo="openresty" list available

源码编译安装

我们都知道,包管理器直接安装的 OpenResty 是没有办法自主控制编译选项的。
这对我们的使用场景是非常不友好的。

下面,我们就来看一下如何通过源码的方式来编译 OpenResty 吧。

Step1: 安装 pcre

  1. wget http://www.missshi.cn:8089/pcre-8.42.tar.bz2
  2. tar xjf pcre-8.42.tar.bz2
  3. cd pcre-8.42
  4. ./configure --prefix=/home/work/openresty-dependency/pcre --disable-cpp --enable-jit --enable-utf --enable-unicode-properties
  5. make -j24 V=1
  6. make install
  7. rm -rf /home/work/openresty-dependency/pcre/bin
  8. rm -rf /home/work/openresty-dependency/pcre/share
  9. rm -f /home/work/openresty-dependency/pcre/lib/*.la
  10. rm -f /home/work/openresty-dependency/pcre/lib/*pcrecpp*
  11. rm -f /home/work/openresty-dependency/pcre/lib/*pcreposix*
  12. rm -rf /home/work/openresty-dependency/pcre/lib/pkgconfig

Step2: 安装 zlib

  1. wget http://www.zlib.net/zlib-1.2.11.tar.xz
  2. tar xf zlib-1.2.11.tar.xz
  3. cd zlib-1.2.11
  4. ./configure --prefix=/home/work/openresty-dependency/zlib
  5. make -j24 \
  6. CFLAGS='-O3 -D_LARGEFILE64_SOURCE=1 -DHAVE_HIDDEN -g' \
  7. SFLAGS='-O3 -fPIC -D_LARGEFILE64_SOURCE=1 -DHAVE_HIDDEN -g'
  8. make install
  9. rm -rf /home/work/openresty-dependency/zlib/share/
  10. rm -f /home/work/openresty-dependency/zlib/lib/*.la
  11. rm -rf /home/work/openresty-dependency/zlib/lib/pkgconfig/

Step3: 安装 openssl

  1. wget https://www.openssl.org/source/openssl-1.1.0j.tar.gz
  2. wget http://www.missshi.cn:8089/openssl-1.1.0d-sess_set_get_cb_yield.patch --no-check-certificate
  3. wget http://www.missshi.cn:8089/openssl-1.1.0j-parallel_build_fix.patch --no-check-certificate
  4. tar zxf openssl-1.1.0j.tar.gz
  5. cd openssl-1.1.0j
  6. patch -p1 < ../openssl-1.1.0d-sess_set_get_cb_yield.patch
  7. patch -p1 < ../openssl-1.1.0j-parallel_build_fix.patch
  8. ./config \
  9. no-threads shared zlib -g \
  10. enable-ssl3 enable-ssl3-method \
  11. --prefix=/home/work/openresty-dependency/openssl \
  12. --libdir=lib \
  13. -I%/home/work/openresty-dependency/zlib/include \
  14. -L%/home/work/openresty-dependency/zlib/lib \
  15. -Wl,-rpath,/home/work/openresty-dependency/zlib/lib:/home/work/openresty-dependency/openssl/lib
  16. make -j24
  17. make install_sw
  18. rm -f /home/work/openresty-dependency/openssl/bin/c_rehash
  19. rm -rf /home/work/openresty-dependency/openssl/lib/pkgconfig

Step4: 编译安装 OpenResty

  1. wget https://openresty.org/download/openresty-1.15.8.1.tar.gz
  2. tar zxf openresty-1.15.8.1.tar.gz
  3. cd openresty-1.15.8.1
  4. ./configure \
  5. --prefix=/home/work/openresty \
  6. --with-cc-opt="-DNGX_LUA_ABORT_AT_PANIC \
  7. -I/home/work/openresty-dependency/zlib/include \
  8. -I/home/work/openresty-dependency/pcre/include \
  9. -I/home/work/openresty-dependency/openssl/include" \
  10. --with-ld-opt="-L/home/work/openresty-dependency/zlib/lib \
  11. -L/home/work/openresty-dependency/pcre/lib \
  12. -L/home/work/openresty-dependency/openssl/lib \
  13. -Wl,-rpath,/home/work/openresty-dependency/zlib/lib:/home/work/openresty-dependency/pcre/lib:/home/work/openresty-dependency/openssl/lib" \
  14. --with-pcre-jit \
  15. --without-http_rds_json_module \
  16. --without-http_rds_csv_module \
  17. --without-lua_rds_parser \
  18. --with-stream \
  19. --with-stream_ssl_module \
  20. --with-stream_ssl_preread_module \
  21. --with-http_v2_module \
  22. --without-mail_pop3_module \
  23. --without-mail_imap_module \
  24. --without-mail_smtp_module \
  25. --with-http_stub_status_module \
  26. --with-http_realip_module \
  27. --with-http_addition_module \
  28. --with-http_auth_request_module \
  29. --with-http_secure_link_module \
  30. --with-http_random_index_module \
  31. --with-http_gzip_static_module \
  32. --with-http_sub_module \
  33. --with-http_dav_module \
  34. --with-http_flv_module \
  35. --with-http_mp4_module \
  36. --with-http_gunzip_module \
  37. --with-threads \
  38. --with-luajit-xcflags='-DLUAJIT_NUMMODE=2 -DLUAJIT_ENABLE_LUA52COMPAT' \
  39. -j24
  40. make -j24
  41. make install

至此,我们的 OpenResty 就已经成功安装完成了,快来试试吧!

Ps: 上述用到源码包如果下载不下来,可以访问 package 下载。