OpenResty官方安装文档强烈推荐MacOS使用homebrew包管理工具安装 。但是,对于使用老版本Mac系统(如10.13.6)的用户来说不是很友好,安装过程可能会找不到各种依赖、变量等,不如手动编译源码安装OpenResty。
参考Linux下安装OpenResty在Mac上编译安装
安装前准备
安装OpenResty前确保当前机器有以下依赖库:
- perl 5.6.1+
- libreadline
- libpcre
- libssl
参考Linux系统下非root用户安装下安装Nginx安装pcre和openssl即可。
下载源码包、编译、安装
wget https://openresty.org/download/openresty-1.19.3.2.tar.gz
tar xzvf openresty-1.19.3.2.tar.gz # 解压
cd openresty-1.19.3.2/
./configure
make
make install
默认情况下程序会被安装到 /usr/local/openresty 目录。该目录下的nginx目录就是独立的Nginx服务。
配置PATH环境变量
方便操作OpenResty,因为Nginx/OpenResty发布包中并没有提供好的启动、停止脚本,配置完后,就可以在终端直接使用nginx命令了。
vi ~/.zshrc
#添加如下配置
export PATH=/usr/local/openresty/nginx/sbin:$PATH
命令及参数详解
OpenResty 的原始启动命令为 nginx,其参数有-v、-t、-p、-c、-s 等,使用说明如下:
-v参数
表示查看Nginx版本
? ~ nginx -v
nginx version: openresty/1.13.6.2
-c参数
指定一个Nginx配置文件来替换默认的Nginx配置文件
#单独建一个目录,并在目录中创建logs和conf目录
mkdir ~/Software/openresty/work
cd ~/Software/openresty/work
mkdir logs/ conf/
在conf目录下,定义一个后缀为.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 '
ngx.say("<p>Hello, World!</p>")
';
}
}
}
启动Nginx:
#若在上一步新建的目录下:
cd ~/Software/openresty/work
#执行下面的命令启动
? work nginx -p ./ -c conf/nginx.conf
#若在其他目录,比如用户目录
? ~ nginx -c ~/Software/openresty/work/conf/nginx.conf
-p参数
表示设置前缀路径
“-p ./”表示将当前目录作为前缀路径,也就是说-c后面指定的配置文件conf/nginx.conf中,所用到的相对路径都加上这个前缀。
"-p ./" 等价于 "-p `pwd`/"
-t参数
表示测试Nginx配置文件语法是否正确
若不能确定编写的Nginx配置文件语法的正确性,就使用-t参数,指定某个配置文件进行检测。不加配置文件路径,默认检测Nginx服务目录下的配置文件。
-s参数
表示给Nginx进程发送信号,包含stop(停止)、reload(重新加载)
? ~ nginx -p ~/Software/openresty/work/ -c conf/nginx.conf -s reload
? ~
? ~ ps -ef|grep nginx
0 1564 1 0 11:09下午 ?? 0:00.00 nginx: master process nginx
-2 1565 1564 0 11:09下午 ?? 0:00.00 nginx: worker process
0 2345 1 0 11:40下午 ?? 0:00.01 nginx: master process nginx -p ./ -c conf/nginx.conf
-2 2833 2345 0 11:51下午 ?? 0:00.00 nginx: worker process
501 2845 495 0 11:51下午 ttys000 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 nginx
? ~
? ~
? ~ nginx -p ~/Software/openresty/work/ -c conf/nginx.conf -s stop
? ~
? ~ ps -ef|grep nginx
0 1564 1 0 11:09下午 ?? 0:00.00 nginx: master process nginx
-2 1565 1564 0 11:09下午 ?? 0:00.00 nginx: worker process
501 2882 495 0 11:52下午 ttys000 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 nginx
? ~
测试
终端下输入curl http://localhost:8080
? ~ curl http://localhost:8080
<p>Hello, World!</p>
? ~
本文暂时没有评论,来添加一个吧(●'◡'●)