NGINX 编译安装

本文介绍了手动编译安装 NGINX 的具体步骤。

建立用户及用户组

1
2
3
$ groupadd -r nginx

$ useradd -r -g nginx -s /bin/false -M nginx

Alpine 较特殊

1
2
3
$ addgroup -S nginx

$ adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx

安装依赖包

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ ./configure --prefix=/etc/nginx  \
--sbin-path=/etc/nginx/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module

错误排查

错误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
2
3
$ yum install -y gcc gcc-c++

$ sudo apt install gcc g++

错误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
2
3
$ yum install pcre-devel

$ sudo apt install libpcre3 libpcre3-dev

错误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
2
3
$ yum install openssl-devel

$ sudo apt install libssl-dev

错误4

./configure: error: the HTTP gzip module requires the zlib library.

1
2
3
$ yum install zlib-devel

$ sudo apt install zlib1g-dev zlib1g

安装

1
2
3
$ make

$ sudo make install

加入环境变量

编辑 ~/.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/etc/nginx/run/nginx.pid
ExecStartPre=/etc/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/etc/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启动

1
2
3
$ sudo nginx -t

$ sudo nginx

相关链接

0%