通常来说编译不通过都是因为系统环境不满足条件,如缺少包等,本文以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 这些参数只是个默认值,是可以被配置文件的配置覆盖的

常见报错解决

  • ./configure: error: C compiler cc is not found

    1
    yum install -y gcc-c++
  • 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

开始编译

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