[toc]

源码安装

安装包类型

安装包 安装方式
rpm包 rpm;yum
源码包 源码安装
二进制包 解压即用

获取源码包

1
2
# 官网获取
https://nginx.org/

image-20221117151701743

image-20221117152526897

1
2
3
4
5
6
7
# 下载源码包
复制网页中的链接地址:https://nginx.org/download/nginx-1.22.1.tar.gz
使用wget下载至当前目录:[root@localhost ~]# wget https://nginx.org/download/nginx-1.22.1.tar.gz
使用ll命令查看当前目录下是否存在此文件:
[root@localhost ~]# ll
total 1052
-rw-r--r--. 1 root root 1073948 Oct 19 17:23 nginx-1.22.1.tar.gz

生成安装文件并安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 解压并进入安装文件所在目录下
使用tar命令将gzip格式的文件解压至当前目录:[root@localhost ~]# tar xf nginx-1.22.1.tar.gz
使用cd命令进入解压出来的安装文件所在目录下[root@localhost ~]# cd nginx-1.22.1/

# 使用以下命令生成安装文件
./configure --prefix=/opt/nginx-1.22.1 --with-http_ssl_module --with-http_stub_status_module

## 报错如下1(提示缺少依赖文件:c语言环境)
[root@localhost ~/nginx-1.22.1]# ./configure --prefix=/opt/nginx-1.22.1 --with-http_ssl_module --with-http_stub_status_module
checking for OS
+ Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found
## 解决方法:安装c语言环境
[root@localhost ~/nginx-1.22.1]# yum install -y gcc gcc-c++ glibc

## 报错如下2(提示缺少依赖文件: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.
## 解决方法:安装PCRE库文件
yum install -y pcre-devel

## 报错如下3(提示缺少依赖文件: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.
## 解决方法:安装OpenSSL库文件
yum install -y openssl-devel

# 编译(让系统识别你的代码)
make

# 安装(安装并生成安装目录)
make install

启动nginx服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 创建软连接
使用ln -s创建软连接:
[root@localhost /opt]# ln -s /opt/nginx-1.22.1/ /opt/nginx
使用ll查看软连接创建情况:
[root@localhost /opt]# ll
total 0
lrwxrwxrwx. 1 root root 18 Nov 17 16:35 nginx -> /opt/nginx-1.22.1/
drwxr-xr-x. 6 root root 54 Nov 17 16:34 nginx-1.22.1

# 添加环境变量
使用vim打开配置文件并进行编辑及保存:
[root@localhost /opt]# vim /etc/profile.d/nginx.sh
export PATH="$PATH:/opt/nginx/sbin"

# 应用环境变量文件
[root@localhost /opt]# source /etc/profile

# 启动nginx服务
nginx
查看命令是否运行成功:echo $?

## PS:
如果运行失败需查看后台进程是否在运行:[root@localhost /opt]# ps -ef|grep 'nginx'
终结对应的进程的进程号:[root@localhost /opt]# kill -9 ***** *****

# 关闭防火墙
[root@localhost /opt]# systemctl stop firewalld

# 关闭selinux(0是关闭 1是开启)
[root@localhost /opt]# setenforce 0

image-20221117164925270