CentOS7安装nginx以及添加模块

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


目录

一、准备工作

二、Nginx安装配置

三、其他


一、准备工作

1、相关下载

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

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

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

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

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

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

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

(该链接已经失效)
wget https://ftp.pcre.org/pub/pcre/pcre-8.33.tar.gz

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

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

2、解压

tar zxvf openssl-1.1.1.tar.gz
tar zxvf zlib-1.2.11.tar.gz
tar zxvf pcre-8.33.tar.gz
tar zxvf nginx-1.14.0.tar.gz

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

3、依赖安装

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

​yum install -y gcc gcc-c++ make

安装PCRE

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

安装SSL

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

安装zlib

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

二、Nginx安装配置

1、安装

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

mv nginx-1.14.0 nginx; cd nginx/

 进入nginx目录并执行

cd /usr/local/nginx

./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

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

make && make install

至此,nginx安装完成。

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

mkdir /usr/local/nginx/logs

2、配置开机启动

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

cd /lib/systemd/system

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

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

systemctl enable nginx.service
systemctl start nginx.service  #启动,也可以使用sbin/nginx启动
systemctl stop nginx.service  #结束nginx 
systemctl restart nginx.service  #重启,可使用sbin/nginx -s reload
systemctl status nginx.service  #查看状态

3、配置文件

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

三、其他

1、添加三方模块

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

创建目录存放模块包:

mkdir /usr/local/nginx/module

cd /usr/local/nginx/module

下载ngx_headers_more包并解压:

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

tar vzxf v0.33.tar.gz

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

查看编译安装的模块:

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

./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

完成后执行:

make

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

备份原有nginx,重新拷贝nginx

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx20180913

cp /usr/local/nginx/objs/nginx /usr/local/nginx/sbin/

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

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即可。


其他版本:

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

#zlib
wget http://www.zlib.net/zlib-1.2.13.tar.gz

#pcre
wget https://udomain.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz

#nginx
wget http://nginx.org/download/nginx-1.22.1.tar.gz

END

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值