CentOS 7 下编译安装lnmp之nginx篇详解

时间:2023-03-09 18:24:18
CentOS 7 下编译安装lnmp之nginx篇详解

一、安装环境

宿主机=> win7,虚拟机 centos => 系统版本:CentOS Linux release 7.5.1804 (Core),ip地址 192.168.1.168

CentOS 7 下编译安装lnmp之nginx篇详解

 二、nginx下载

打开官网: http://nginx.org/ ,选择 nginx-1.14.1 stable 版本,

CentOS 7 下编译安装lnmp之nginx篇详解

打开链接找到 nginx-1.14.2,右键复制链接地址,

CentOS 7 下编译安装lnmp之nginx篇详解

将其下载到目录  /usr/local/src 中,

CentOS 7 下编译安装lnmp之nginx篇详解

三、nginx安装

1、解压nginx-1.14.2.tar.gz

CentOS 7 下编译安装lnmp之nginx篇详解

2、进入nginx-1.14.2目录查看原始文件

CentOS 7 下编译安装lnmp之nginx篇详解

3、执行 ./configure 命令,指定安装路径为 /usr/local/nginx

CentOS 7 下编译安装lnmp之nginx篇详解

执行完毕报如下错误:

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

大意是 HTTP的重写模块需要PCRE库的支持,要么通过--without-http_rewrite_module忽略此模块,要么将PCRE安装进系统,

要么构筑PCRE库的源码并通过 --with-pcre=<path>选项指定其所在路径,这里选择最后一项,下载PCRE源码到 /usr/local/src 。

3.1、打开 http://pcre.org/ ,找到  ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

CentOS 7 下编译安装lnmp之nginx篇详解

点击进入找到  pcre2-10.21.tar.bz2 最新版,复制链接地址下载,这个是pcre2新版本,可能会不兼容,此处测试一下

CentOS 7 下编译安装lnmp之nginx篇详解

3.2、下载

CentOS 7 下编译安装lnmp之nginx篇详解

3.3、解压

CentOS 7 下编译安装lnmp之nginx篇详解

3.4、 重新执行 ./configure

CentOS 7 下编译安装lnmp之nginx篇详解

结果又报如下错误:

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

意思是缺少 zlib 库,如同pcre一样,我们下载解压zlib

3.5、打开 http://www.zlib.net/  ,找到

CentOS 7 下编译安装lnmp之nginx篇详解

复制链接地址并下载到  /usr/local/src 目录中

CentOS 7 下编译安装lnmp之nginx篇详解

解压

CentOS 7 下编译安装lnmp之nginx篇详解

3.6、再次执行 ../configure 命令

./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre2-10.21 --with-zlib=/usr/local/src/zlib-1.2.11

CentOS 7 下编译安装lnmp之nginx篇详解

3.7 、继续执行 make && make install

CentOS 7 下编译安装lnmp之nginx篇详解

结果报错:

src/core/ngx_regex.h:15:18: fatal error: pcre.h: No such file or directory
#include <pcre.h>
^
compilation terminated.
make[1]: *** [objs/src/core/nginx.o] Error 1
make[1]: Leaving directory `/usr/local/src/nginx-1.14.2'
make: *** [build] Error 2

查询资料,应该还是 pcre 版本问题,可能 pcre 2版本高的问题吧,尝试下载 pcre-8.42.tar.bz2 版本

CentOS 7 下编译安装lnmp之nginx篇详解

下载

CentOS 7 下编译安装lnmp之nginx篇详解

解压

CentOS 7 下编译安装lnmp之nginx篇详解

3.8、再次尝试执行 ./configure 命令

./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11

CentOS 7 下编译安装lnmp之nginx篇详解

3.9、未报错,再次执行 make && make install

CentOS 7 下编译安装lnmp之nginx篇详解

执行结果成功安装 nginx

CentOS 7 下编译安装lnmp之nginx篇详解

四、nginx 启动

运行 sbin 下的 nginx 如下

CentOS 7 下编译安装lnmp之nginx篇详解

说明运行成功,如果有诸如  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)   之类的提示,说明是80端口被占用了,使用fuser -n tcp 80查看80端口被谁占用,

使用 kill -9 pid号 关闭后重新启动 nginx 即可

五、nginx 测试

在客户端浏览器输入centos主机ip地址,测试 nginx 服务器是否可访问

CentOS 7 下编译安装lnmp之nginx篇详解

nginx无法访问,看一下网络是否连通

CentOS 7 下编译安装lnmp之nginx篇详解

网络没问题,很大可能就是防火墙问题了,systemctl status firewalld 查看防火墙状态

CentOS 7 下编译安装lnmp之nginx篇详解

可知防火墙处于开启状态,此时可以关闭防火墙,也可将80端口配置为允许,此处配置80端口为允许,

firewall-cmd --zone=public --add-port=80/tcp --permanent

CentOS 7 下编译安装lnmp之nginx篇详解

重新载入   firewall-cmd --reload ,并查看80端口开启状态 firewall-cmd --list-ports

CentOS 7 下编译安装lnmp之nginx篇详解

到此,成功配置防火墙例外80端口,再次测试 nginx ,如下

CentOS 7 下编译安装lnmp之nginx篇详解

说明 nginx 服务器已经搭建成功。

六、设置开机启动

6.1、在系统服务目录里创建nginx.service文件

[root@bogon php7]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target [Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true [Install]
WantedBy=multi-user.target

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

6.2、设置开机启动

systemctl enable nginx.service

七、nginx 安装总结

1、nginx编译安装需要 pcre 和 zlib 库的支持,且  pcre2-10 最新版容易出错,最好使用 pcre-8版本。

2、注意nginx 启动时80端口是否被占用。

3、如果nginx启动成功,但不能访问时,首先查看网络是否处于连通状态,当网络连通时查看防火墙状态及80端口是否被拒绝