简易编译

openssl 和 pcre 等组件,使用源里的包,源里面的包版本比较低,可能还有漏洞,但是比较方便。

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
27
28
./configure \
--prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
--pid-path=/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--user=nginx \
--group=nginx \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_mp4_module \
--with-http_stub_status_module \
--with-pcre \
--with-stream \
--with-stream_ssl_preread_module \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-http_v2_module \
--with-http_realip_module

静态编译 openssl 和 pcre

通常来说编译不通过都是因为系统环境不满足条件,如缺少包等,本文以 CentOS 7 系统为例,其中有些包如 xxx-devel 在 ubuntu 下一般都是叫做 xxx-dev 的,实在找不到可以使用 apt-file 查找文件属于哪个包。

需要准备好这些包(不用监控可以不用准备 nginx-module-vts):

1
2
3
4
wget https://nginx.org/download/nginx-1.22.0.tar.gz
wget https://github.com/vozlt/nginx-module-vts/archive/refs/tags/v0.2.2.tar.gz
wget https://ftp.openssl.org/source/openssl-1.1.1p.tar.gz
wget https://ftp.exim.org/pub/pcre/pcre-8.45.tar.gz

全部解压,然后进入 nginx-1.22.0 目录操作

首先运行一遍 configure 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
./configure --prefix=/usr/local/nginx \
--user=www --group=www \
--with-stream \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-openssl=../openssl-1.1.1p \
--with-pcre=../pcre-8.45 \
--with-pcre-jit \
--with-ld-opt=-ljemalloc \
--add-module=../nginx-module-vts-0.1.18

nginx-module-vts 是监控模块,如果不用监控,可以将这一行删除
user group prefix 这些参数只是个默认值,是可以被配置文件的配置覆盖的

常见报错解决

C compiler cc is not found

1
sudo yum -y install gcc gcc-c++ autoconf automake make

checking for --with-ld-opt="-ljemalloc" ... not found

1
2
3
4
5
6
# centos
yum install -y epel-release
yum install -y jemalloc-devel

# ubuntu
sudo apt install -y libjemalloc-dev

checking for zlib library ... not found

1
yum install zlib-devel

You need Perl 5.

1
yum install perl-devel

the HTTP rewrite module requires the PCRE library

1
sudo yum install pcre pcre-devel -y

SSL modules require the OpenSSL library

1
sudo yum install openssl openssl-devel -y

开始编译

1
make
1
make -j 4

4 表示用 4 个核心来编译

安装

创建用户组和用户

1
2
groupadd www
useradd -g www -M -s /sbin/nologin www

安装可执行文件

可以用 make install 来快速安装,也可以自己进入 objs 目录把 nginx 复制到其他地方,配置文件放在/usr/loca/nginx 下

收尾工作

生成 systemd 文件

vim /usr/lib/systemd/system/nginx.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
TimeoutStartSec=120
LimitNOFILE=1000000
LimitNPROC=1000000
LimitCORE=1000000

[Install]
WantedBy=multi-user.target

然后就可以愉快的使用 Nginx 了。

创建软链接

添加后就可以直接使用 nginx 命令了

1
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

配置文件软链接

1
sudo ln -s /usr/local/nginx/conf/ /etc/nginx

其他

监控

监控的使用方法可以查看 Nginx使用module_vts模块来做监控

查看编译参数

1
nginx -V