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://zlib.net/zlib-1.2.11.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、更改源码(可选)

隐藏相应头中的Server信息
由于无法通过设置nginx.conf配置项隐藏Server信息,只能通过更改源码进行编译再安装。
有三个位置:

1. src/core/nginx.hNginx内部名称

vim src/core/nginx.h

更改内容:

# 源内容
define NGINX_VERSION      "1.15.5"
define NGINX_VER          "nginx/" NGINX_VERSION

# 更改为
define NGINX_VERSION      "6.66.666"
define NGINX_VER          "jyoryo-web/" NGINX_VERSION

2. src/http/ngx_http_header_filter_module.c:HTTP ResponseHeader

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: jyoryo-web" CRLF;
static char ngx_http_server_full_string[] = "Server: jyoryo-web" CRLF;

3. src/http/ngx_http_special_response.c:错误页的底部Footer

vim src/http/ngx_http_special_response.c

更改内容:

# 源内容
static u_char ngx_http_error_tail[] =
"<hr><center>nginx</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;

# 更改内容
static u_char ngx_http_error_tail[] =
"<hr><center>jyoryo-web</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;

4、配置

./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 \
--http-client-body-temp-path=/data/soft/nginx/temp/client_body_temp \
--http-proxy-temp-path=/data/soft/nginx/temp/proxy_temp \
--http-fastcgi-temp-path=/data/soft/nginx/temp/fastcgi_temp \
--http-uwsgi-temp-path=/data/soft/nginx/temp/uwsgi_temp \
--http-scgi-temp-path=/data/soft/nginx/temp/scgi_temp

5、编译安装

make
make install

# 创建temp目录
mkdir -p /data/soft/nginx/temp

6、nginx配置

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

    server_tokens off;
  • 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

添加新评论