Linux Nginx 1.15.5源码编译安装

说明:

  • 安装nginx版本为1.15.5,其他版本类似
  • nginx解压后工作空间目录:/data/workspace/nginx
  • nginx安装目录:/data/soft/nginx
  • nginx依赖opensslpcrezlib,这些包可以下载解压,但不用安装

1、下载并解压nginx、openssl、pcre、zlib源码

# nginx (https://nginx.org/download/)
wget https://nginx.org/download/nginx-1.15.5.tar.gz
# openssl
wget https://www.openssl.org/source/openssl-1.1.0i.tar.gz
# pcre
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
# zilb
wget https://www.openssl.org/source/openssl-1.1.0i.tar.gz
# 解压
tar zxf nginx-1.15.5.tar.gz -C /data/workspace
tar zxf openssl-1.1.0i.tar.gz -C /data/workspace
tar zxf pcre-8.42.tar.gz -C /data/workspace
tar zxf zlib-1.2.11.tar.gz -C /data/workspace
# 将nginx目录改名
mv nginx-1.15.5 nginx
cd nginx

2、安装所需依赖和工具

apt-get install make gcc g++ perl libperl-dev

3、配置

./configure --prefix=/data/soft/nginx \
--user=www-data \
--group=www-data \
--with-openssl=../openssl-1.1.0i \
--with-pcre=../pcre-8.42 \
--with-pcre-jit \
--with-zlib=../zlib-1.2.11 \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module

4、编译安装

make
make install

5、nginx配置

  • 隐藏nginx版本号:
    编辑nginx.conf,添加配置项

    server_tokens off;
  • 隐藏相应头中的Server信息
    无法通过设置配置项隐藏Server信息,只能通过更改源码进行编译再安装。

    vim src/http/ngx_http_header_filter_module.c

    更改内容:

    # 源内容
    static char ngx_http_server_string[] = "Server: nginx" CRLF;
    static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
    
    # 更改为
    static char ngx_http_server_string[] = "Server: X-Web" CRLF;
    static char ngx_http_server_full_string[] = "Server:X-Web " CRLF;
  • nginx + php中nginx配置:

    location ~ .*\.php(\/.*)*$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }

参考文章:

标签: lnmp, nginx

添加新评论