macos安装Nginx

278 阅读2分钟

首先说一下我的机器是黑苹果(macOS Big Sur 11.6)。

刚开始尝试使用Brew安装,安装了N次一直失败,只好放弃选择离线安装。安装Nginx需要提前安装OpenSSL和PCRE。

一、  安装OpenSSL

到官网www.openssl.org/source/下砸安装包

此处我选择了最老的包,老的稳定。

解压后使用终端切换到OpenSSL的目录,执行一下命令:

./config --prefix=/Volumes/Soft/Nginx/openssl

/Volumes/Soft/Nginx/openssl是自己创建的配置路径

看到以下信息表示配置成功。

然后执行以下命令:

make && make install

执行完进行漫长的等待。结束安装完成

二、  安装PCRE

仍然采用离线的方式安装,到下面的地址下载安装包:

sourceforge.net/projects/pc…

解压后,切换到解压后的目录输入以下命令:

./configure --prefix=/Volumes/Soft/Nginx/pcre --enable-utf8 --enable-unicode-properties

三、  安装Nginx

到官网nginx.org/en/download…下载离线包

我选择的是这个包

解压后切换到安装目录,执行以下命令:

./configure --prefix=/Volumes/Soft/Nginx/nginx

屏幕呼啦啦的一堆代码,最后几行如下:

./configure: error: the HTTP rewrite module requires the PCRE library.

You can either disable the module by using --without-http_rewrite_module

option, or install the PCRE library into the system, or build the PCRE library

statically from the source with nginx by using --with-pcre= option.

哟!出错了,提示缺少pcre库,在上面的命令中指定一下pcre的目录和openssl的目录,但是这个目录不是安装后的目录,而是刚开始解压的后的位置。执行以下命令:

./configure --prefix=/Volumes/Soft/Nginx/nginx --with-pcre=/Volumes/Soft/Nginx/pcre-8.45 --with-openssl=/Volumes/Soft/Nginx/openssl-3.0.8

出现以下结果说明成功:

执行以下命令进行安装

make && make install

安装成功以后会在/Volumes/Soft/Nginx/下出现nginx目录

四、启动nginx

切换到/Volumes/Soft/Nginx/nginx目录,执行以下命令,需要输入密码

sudo sbin/nginx

浏览器中输入localhost出现下图所示,说明安装成功