本文介绍了手动编译安装 NGINX 的具体步骤。
建立用户及用户组
1 | $ groupadd -r nginx |
Alpine 较特殊
1 | $ addgroup -S nginx |
- http://git.alpinelinux.org/cgit/aports/tree/main/apache2/apache2.pre-install?h=v3.3.2
- http://git.alpinelinux.org/cgit/aports/tree/main/lighttpd/lighttpd.pre-install?h=v3.3.2
- http://git.alpinelinux.org/cgit/aports/tree/main/nginx-initscripts/nginx-initscripts.pre-install?h=v3.3.2
安装依赖包
RedHat 系
1 | $ yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel |
Debian 系
1 | $ sudo apt install gcc g++ libpcre3 libpcre3-dev libssl-dev zlib1g-dev zlib1g |
编译
1 | $ ./configure --prefix=/etc/nginx \ |
错误排查
错误1
checking for OS
Linux 3.10.0-327.36.1.el7.x86_64 x86_64
checking for C compiler … not found
./configure: error: C compiler cc is not found
1 | $ yum install -y gcc gcc-c++ |
错误2
./configure: error: the HTTP rewrite module requires the PCRE library.You can either disable the module by using –without-http_rewrite_moduleoption, or install the PCRE library into the system, or build the PCRE librarystatically from the source with nginx by using –with-pcre=option.
1 | $ yum install pcre-devel |
错误3
./configure: error: SSL modules require the OpenSSL library.You can either do not enable the modules, or install the OpenSSL libraryinto the system, or build the OpenSSL library statically from the sourcewith nginx by using –with-openssl=option.
1 | $ yum install openssl-devel |
错误4
./configure: error: the HTTP gzip module requires the zlib library.
1 | $ yum install zlib-devel |
安装
1 | $ make |
加入环境变量
编辑 ~/.bash_profile
1 | export PATH=/etc/nginx/sbin:$PATH |
编辑 /etc/sudoers
注意:必须编辑此文件,否则 sudo 会找不到命令。
1 | Defaults secure_path="/etc/nginx/sbin:..." |
systemd
在 /etc/systemd/system/
下增加 nginx.service
文件,以下路径根据实际自己修改。
1 | [Unit] |
启动
1 | $ sudo nginx -t |