MacOS 安装Nginx,记录安装过程及nginx编译过程
MacOS安装nginx的方式有几种,可以通过brew install nginx的方式,也可以向本次记录的一样,采用下载nginx的tar.gz包,在本地进行编译。
1 下载nginx
wget https://nginx.org/download/nginx-1.18.0.tar.gz
tar zxf nginx-1.18.0.tar.gz
cd nginx-1.18.0
下载后进入到nginx的源码目录,
$ ls
CHANGES LICENSE auto configure html src
CHANGES.ru README conf contrib man
2 编译nginx
编译配置选项在这个名为configure的文件里面,里面设置了各种参数
ll
total 1520
-rw-r--r-- 1 1001 1001 296K 4 21 2020 CHANGES
-rw-r--r-- 1 1001 1001 451K 4 21 2020 CHANGES.ru
-rw-r--r-- 1 1001 1001 1.4K 4 21 2020 LICENSE
-rw-r--r-- 1 1001 1001 49B 4 21 2020 README
drwxr-xr-x 25 1001 1001 800B 4 21 2020 auto
drwxr-xr-x 11 1001 1001 352B 4 21 2020 conf
-rwxr-xr-x 1 1001 1001 2.4K 4 21 2020 configure
drwxr-xr-x 6 1001 1001 192B 4 21 2020 contrib
drwxr-xr-x 4 1001 1001 128B 4 21 2020 html
drwxr-xr-x 3 1001 1001 96B 4 21 2020 man
drwxr-xr-x 9 1001 1001 288B 4 21 2020 src
在目录中直接运行,如果先在一台机器上安装多个nginx,这时就需要修改configure文件。
case ".$NGX_PREFIX" in
.)
NGX_PREFIX=${NGX_PREFIX:-/usr/local/nginx1.21.6}
have=NGX_PREFIX value="\"$NGX_PREFIX/\"" . auto/define
;;
.!)
sudo ./configure
checking for OS
+ Darwin 20.6.0 x86_64
checking for C compiler ... found
+ using Clang C compiler
+ clang version: 13.0.0 (clang-1300.0.29.30)
看生成的的后续打印情况,里面有没有提示缺少依赖
./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=<path> option.
提示缺少依赖,先根据提示情况进行安装
$ brew install pcre
==> Downloading https://ghcr.io/v2/homebrew/core/pcre/manifests/8.45
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/pcre/blobs/sha256:fb2fefbe12327
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Pouring pcre--8.45.big_sur.bottle.tar.gz
🍺 /usr/local/Cellar/pcre/8.45: 204 files, 5.8MB
==> `brew cleanup` has not been run in the last 30 days, running now...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Removing: /Users/bird/Library/Logs/Homebrew/ncurses... (64B)
Removing: /Users/bird/Library/Logs/Homebrew/apr-util... (64B)
Removing: /Users/bird/Library/Logs/Homebrew/utf8proc... (64B)
Removing: /Users/bird/Library/Logs/Homebrew/lz4... (64B)
Removing: /Users/bird/Library/Logs/Homebrew/ca-certificates... (64B)
Removing: /Users/bird/Library/Logs/Homebrew/gettext... (64B)
Removing: /Users/bird/Library/Logs/Homebrew/apr... (64B)
Removing: /Users/bird/Library/Logs/Homebrew/pcre2... (64B)
Removing: /Users/bird/Library/Logs/Homebrew/openssl@1.1... (64B)
Removing: /Users/bird/Library/Logs/Homebrew/fish... (64B)
Removing: /Users/bird/Library/Logs/Homebrew/subversion... (64B)
Pruned 0 symbolic links and 2 directories from /usr/local
再次执行configure后
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/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"
没有报错了,提示了nginx的安装位置等一些信息。
3 make 编译与安装
$ sudo make && make install
按一下输出信息,确保正确编译安装
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
< man/nginx.8 > objs/nginx.8
/Library/Developer/CommandLineTools/usr/bin/make -f objs/Makefile install
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
|| mkdir -p '/usr/local/nginx/sbin'
mkdir: /usr/local/nginx/sbin: Permission denied
make[1]: *** [install] Error 1
make: *** [install] Error 2
这里一直输出没想到最后还是因为权限问题install失败了,加上sudo安装成功,这里要说明一下,我是make成功了,但是执行make install因为权限问题失败,后面单独运行的sudo make install
$ ls
conf html logs sbin
运行nginx
$ ./nginx
在/usr/local/nginx/sbin/下直接运行有提示没有权限,我这里采用的是setUID的方式,就是给二进制文件运行权限,
$ chmod u+s /usr/loacl/nginx/sbin/nginx
执行后可以直接
$ ./nginx
可以使用下面的命令查询是否运行成功
ps -ef |grep nginx