# OpenResty

# 安装(MacOS)

# Brew安装

brew tap openresty/brew
brew install openresty
1
2

如果要开机启动openresty,可使用命令/usr/local/opt/openresty

openresty安装目录/usr/local/opt/openresty/

# 手动安装

如果通过brew install openresty/brew/openresty安装openresty失败。也可以通过源码安装

1.通过官方 (opens new window)下载最新的源码包

2.修改压缩包中的configure文件,添加一行$extra_opts .= " CFLAGS='-fno-stack-check'"; 参考Can't install openresty on macOS 10.15 (opens new window)

if ($platform eq 'macosx') {
    my $v = $ENV{MACOSX_DEPLOYMENT_TARGET};
    if (!defined $v || $v !~ /^\d+\.\d+$/) {
        $v = `sw_vers -productVersion`;
        if (defined $v && $v =~ /^\s*(\d+\.\d+)/) {
            $ENV{MACOSX_DEPLOYMENT_TARGET} = $1;
            #warn "MACOSX_DEPLOYMENT_TARGET = $1";
        }
    }
# 在这里添加
    $extra_opts .= " CFLAGS='-fno-stack-check'";
}
1
2
3
4
5
6
7
8
9
10
11
12

3.编译

cd openresty-1.15.8.2
./configure \
   --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/" \
   -j8
gmake
sudo gmake install
1
2
3
4
5
6
7

4.增加环境变量

export PATH=/usr/local/openresty/bin:$PATH
export PATH=/usr/local/openresty/nginx/sbin:$PATH
1
2

# 使用

# HelloWorld

我们来测试下 OpenResty 是否安装成功,正常情况以下语句应该输出hello, world

resty -e 'print("hello, world")'
1

# 准备工作路径

mkdir ~/work
cd ~/work
mkdir logs/ conf/
1
2
3

# 创建 nginx.conf配置文件

创建conf/nginx.conf配置文件,内容如下:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua_block {
                ngx.say("<p>hello, world</p>")
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 启动服务

# brew 安装使用openresty
openresty -p `pwd`/ -c conf/nginx.conf

# 源码安装使用nginx
nginx -p `pwd`/ -c conf/nginx.conf

# 测试

curl http://localhost:8080/
1
2
3
4
5
6
7
8
9

# 停止服务

nginx -s stop -p `pwd`
1

# 参考文档

OpenResty最佳实践 (opens new window)

更新时间: 12/23/2021, 2:41:47 PM