Skip to main content

Linux 编译与安装 Nginx

Nginx的环境依赖

安装 gcc

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

#CentOS:

yum install gcc-c++

#Ubuntu:
(以下选择一种)

1. sudo apt-get -y install build-essential #安装包含gcc c++编译器的包
2. sudo apt-get -y install gcc #安装gcc编译器

安装 PCRE pcre-devel

安装:CRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。

未安装pcre库的报错

./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.

./configure: 错误:HTTP 重写模块需要 PCRE 库。 您可以使用 --without-http_rewrite_module 禁用该模块 选项,或者将 PCRE 库安装到系统中,或者构建 PCRE 库 使用 --with-pcre=<path> 选项从 nginx 的源静态地获取。

#CentOS:

yum install -y pcre pcre-devel

#Ubuntu:

sudo apt-get update
sudo apt-get -y install libpcre3 libpcre3-dev

安装 zlib

zlib 安装:zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

未安装zlib 库的报错

./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option.

./configure: 错误:HTTP gzip 模块需要 zlib 库。 您可以使用 --without-http_gzip_module 禁用该模块 选项,或者将zlib库安装到系统中,或者构建zlib库 使用 --with-zlib=<path> 选项从 nginx 源静态地获取。

#CentOS:

yum install -y zlib zlib-devel

#Ubuntu:

sudo apt-get -y install zlib1g-dev

安装 OpenSSL

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

未安装OpenSSL 库的报错

./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl=<path> option.

./configure: 错误:SSL 模块需要 OpenSSL 库。 您可以不启用模块,也可以安装 OpenSSL 库 进入系统,或者从源代码静态构建 OpenSSL 库 通过使用 --with-openssl=<path> 选项与 nginx 一起使用。

#CentOS:

yum install -y openssl openssl-devel

#Ubuntu:
sudo apt-get -y install openssl libssl-dev
如果你安装的是tengine-2.3.3 需要安装 openssl-1.1.1
    wget https://ftp.openssl.org/source/openssl-1.1.1k.tar.gz
tar -zxvf openssl-1.1.1k.tar.gz
# 在./configure 中添加
./configure ... --with-openssl=/{绝对路径}/openssl-1.1.1k ...

安装 libraries

未安装libraries库的报错

./configure: error: the HTTP XSLT module requires the libxml2/libxslt libraries. You can either do not enable the module or install the libraries.

./configure: 错误:HTTP XSLT 模块需要 libxml2/libxslt 图书馆。 您可以不启用该模块或安装库。

#Ubuntu:

sudo apt-get -y install libxml2 libxml2-dev libxslt-dev
sudo apt-get -y install libgd2-xpm libgd2-xpm-dev

安装 GD库

未安装GD库的报错

./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.

./configure: 错误:HTTP 图像过滤器模块需要 GD 库。 您可以不启用该模块或安装库。

#CentOS:

yum install gd gd-devel

#Ubuntu:

sudo apt-get install -y libgd-dev

配置Nginx模块


./configure --with-http_ssl_module
--with-http_stub_status_module
--with-http_ssl_module
--with-http_v2_module
--with-http_gzip_static_module
--with-http_addition_module
--with-stream
--with-stream=dynamic
--with-stream_ssl_module
--with-stream_realip_module
--with-http_dav_module
--http-proxy-temp-path=/usr/local/nginx/tmp/proxy
--http-client-body-temp-path=/usr/local/nginx/tmp/client
--http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi
--http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi
--http-scgi-temp-path=/usr/local/nginx/tmp/scgi
--add-module=./modules/ngx_http_upstream_check_module
--add-module=./modules/ngx_http_proxy_connect_module
支持stream(--with-stream 支持流Tcp和udp)

vim /usr/local/nginx/conf/nginx.conf ...load_module /usr/local/nginx/modules/ngx_stream_module.so;

  • 支持静态文件压缩:--with-http_gzip_static_module

配置代理

SSL 配置
server {
listen 80;
server_name www.javabase.cn;

#http请求强制跳转https
rewrite ^(.*)$ https://$server_name$1 permanent;
}

server {
listen 443;
server_name www.javabase.cn;
ssl on;
ssl_certificate /etc/nginx/ssl/7842125_www.javabase.cn.pem;
ssl_certificate_key /etc/nginx/ssl/7842125_www.javabase.cn.key;
ssl_session_timeout 5m;
#使用此加密套件
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#使用该协议进行配置
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# 设置首页
index index.html;

# 文章
location ^~ /p/ {

alias /www/javabase/client-web/notes/;
# 开启压缩
gzip_static on;
expires max;
add_header Cache-Control public;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
if (!-e $request_filename){
rewrite ^/(.*)$ /$1.html last;
}
}
}
支持websocket
server {
listen 80;
server_name tb_demo.javabase.cn;
location / {
# 重要的是这两行,它表明是websocket连接进入的时候,进行一个连接升级将http连接变成websocket的连接。
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://10.8.0.7:8080;
}
}