CentOS7安装nginx以及添加模块

左手的ㄟ右手 2022-05-10 01:58 412阅读 0赞

根据搜集的资料安装测试并整理的文档,如有不足希望不吝赐教、


目录

一、准备工作

二、Nginx安装配置

三、其他


一、准备工作

1、相关下载

【注意】*以下默认主机可以访问外网,如果不能访问外网,则相关下载需要先下载,然后使用ftp工具将安装包上传到主机相应目录。本次默认文件下载目录在/usr/local/。*

SSL功能需要openssl库,下载地址:http://www.openssl.org/

70

选择版本下载,本次选择1.1.1,使用wget命令下载安装包。

  1. wget https://www.openssl.org/source/openssl-1.1.1.tar.gz

gzip模块需要zlib库,下载地址:http://www.zlib.net/

70 1

  1. wget http://www.zlib.net/zlib-1.2.11.tar.gz

rewrite模块需要pcre库,下载地址:http://www.pcre.org/

70 2

70 3

  1. wget https://ftp.pcre.org/pub/pcre/pcre-8.33.tar.gz

Nginx的安装包:下载地址为:http://nginx.org/en/download.html

70 4

  1. wget http://nginx.org/download/nginx-1.14.0.tar.gz

2、解压

  1. tar zxvf openssl-1.1.1.tar.gz
  2. tar zxvf zlib-1.2.11.tar.gz
  3. tar zxvf pcre-8.33.tar.gz
  4. tar zxvf nginx-1.14.0.tar.gz

解压完成后会得到四个相应目录,如需要可自行对目录重命名。

3、依赖安装

如果是新环境需要安装gcc gcc-c++ make:

  1. yum install -y gcc gcc-c++ make

安装**PCRE**库

  1. cd /usr/local/pcre-8.33
  2. ./configure
  3. make && make install

安装**SSL**库

  1. cd /usr/local/openssl-1.1.1
  2. ./config
  3. make && make install

安装**zlib**库

  1. cd /usr/local/zlib-1.2.11
  2. ./configure
  3. make && make install

二、Nginx安装配置

1、安装

为方便操作,此处将nginx目录重命名:

  1. mv nginx-1.14.0 nginx; cd nginx/

进入nginx目录并执行

  1. cd /usr/local/nginx
  2. ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.33 --with-zlib=/usr/local/zlib-1.2.11 --with-openssl=/usr/local/openssl-1.1.1

其中

--with-pcre=/usr/local/pcre-8.33

--with-zlib=/usr/local/zlib-1.2.11

--with-openssl=/usr/local/openssl-1.1.1

分别指其相应源码目录,即刚刚解压得到的目录,需要根据自己实际情况修改。完成后如下:

70 5

  1. make && make install

至此,nginx安装完成。

默认日志路径为/usr/local/nginx/logs/,但是默认没有创建logs目录,因此需要手动创建logs目录,否则启动时会报错。

  1. mkdir /usr/local/nginx/logs

2、配置开机启动

配置nginx开机启动,切换到/lib/systemd/system目录,创建nginx.service文件:

  1. cd /lib/systemd/system
  2. vim nginx.service

文件文件内容如下:

[Unit]

Description=nginx

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx reload

ExecStop=/usr/local/nginx/sbin/nginx quit

PrivateTmp=true

[Install]

WantedBy=multi-user.target

保存并退出,使用下面命令设置开机启动:

  1. systemctl enable nginx.service
  2. systemctl start nginx.service #启动,也可以使用sbin/nginx启动
  3. systemctl stop nginx.service #结束nginx
  4. systemctl restart nginx.service #重启,可使用sbin/nginx -s reload

3、配置文件

/usr/local/nginx/conf/nginx.conf文件是nginx默认的配置文件,对其修改即可。

三、其他

1、添加三方模块

以headers-more-nginx-module-0.33为例

创建目录存放模块包:

  1. mkdir /usr/local/nginx/module
  2. cd /usr/local/nginx/module

下载ngx_headers_more包并解压:

  1. wget https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz
  2. tar vzxf v0.33.tar.gz

退回nginx主目录:cd .. (即/usr/local/nginx/)

查看编译安装的模块:

  1. sbin/nginx -V

nginx version: nginx/1.14.0

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)

built with OpenSSL 1.1.1 11 Sep 2018

TLS SNI support enabled

configure arguments: —user=nobody —group=nobody —prefix=/usr/local/nginx —with-http_stub_status_module —with-http_gzip_static_module —with-http_realip_module —with-http_sub_module —with-http_ssl_module —with-pcre=/usr/local/pcre-8.33 —with-zlib=/usr/local/zlib-1.2.11 —with-openssl=/usr/local/openssl-1.1.1

添加新的模块headers-more-nginx-module-0.33

  1. ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.33 --with-zlib=/usr/local/zlib-1.2.11 --with-openssl=/usr/local/openssl-1.1.1 --add-module=module/headers-more-nginx-module-0.33

完成后执行:

  1. make

注意不是make install,这个命令是覆盖安装

备份原有nginx,重新拷贝nginx

  1. mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx20180913
  2. cp /usr/local/nginx/objs/nginx /usr/local/nginx/sbin/

查看查看重新编译安装的模块:

  1. sbin/nginx -V

nginx version: nginx/1.14.0

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)

built with OpenSSL 1.1.1 11 Sep 2018

TLS SNI support enabled

configure arguments: —user=nobody —group=nobody —prefix=/usr/local/nginx —with-http_stub_status_module —with-http_gzip_static_module —with-http_realip_module —with-http_sub_module —with-http_ssl_module —with-pcre=/usr/local/pcre-8.33 —with-zlib=/usr/local/zlib-1.2.11 —with-openssl=/usr/local/openssl-1.1.1 —add-module=module/headers-more-nginx-module-0.33

重启nginx即可。


END

发表评论

表情:
评论列表 (有 0 条评论,412人围观)

还没有评论,来说两句吧...

相关阅读

    相关 centos7安装Nginx

    安装所需环境 Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 [CentOS][] 7 作为安装环境。 一

    相关 centos7 安装 Nginx

    > 现在使用springBoot 或者SpringCloud 的项目越来越多,因为在部署的时候,会打包项目为jar包,所以一些静态文件,例如:图片等资源在需要预览操作的时候,