1、安装pcre库
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是很有用的。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。 nginx也需要此库。
命令:yum install -y pcre pcre-devel
:它的目的是安装名为 pcre
和 pcre-devel
的软件包。这些软件包是用于支持正则表达式功能的库。
[root@localhost ~]# yum install -y pcre pcre-devel
2、安装zlib库
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
命令:yum install -y zlib zlib-devel
:运行这个命令将会安装 zlib 库及其开发文件,使你能够在系统中使用 zlib 库。
[root@localhost ~]# yum install -y zlib zlib-devel
3、安装GCC C++ 编译器
GCC 是一套GNU编程工具,其中包括 C、C++、Objective-C、Fortran、Java 和 Ada 等多种编程语言的编译器。
具体来说,安装 gcc-c++
包将会在你的系统中安装 g++
,这是 GCC 的 C++ 编译器。通过安装这个包,你就可以使用 g++
命令来编译和构建 C++ 程序
命令:yum install -y gcc-c++
:系统将会安装 GCC C++ 编译器,使你能够在系统中使用它来编译 C++ 程序。
[root@localhost ~]# yum install -y gcc-c++
4、安装OpenSSL库
在计算机网络上,OpenSSL是一个开放源代码的软件库包,应用程序可以使用这个包来进行安全通信,避免窃听,同时确认另一端连接者的身份。这个包广泛被应用在互联网的网页服务器上。
命令:yum install -y openssl openssl-devel
:安装 OpenSSL 库以及开发文件,使你能够在系统中使用 OpenSSL 库进行加密和安全通信相关的操作。
[root@localhost ~]# yum install -y openssl openssl-devel
5、下载nginx-1.14.0(如已有安装包,可省略此步骤)
可以使用下载命令:wget -c https://nginx.org/download/nginx-1.14.0.tar.gz
[root@localhost downfile]wget -c https://nginx.org/download/nginx-1.14.0.tar.gz
6、解压安装包
### 解压
[root@localhost downfile]tar -xvf nginx-1.14.0.tar.gz
### 进入nginx目录
[root@localhost downfile]cd nginx-1.14.0
7、使用nginx的默认配置
命令:./configure
:是在源代码安装软件时常见的一步。这个命令通常用于运行软件包中的配置脚本,以便根据系统环境和需求对软件进行配置。在运行 ./configure
后,它会检查系统环境,并生成适合当前系统的 Makefile 文件,以便后续使用 make
命令进行编译。
[root@localhost nginx-1.14.0]./configure
8、make命令执行 Makefile 文件
make
是一个用于自动化软件编译过程的工具。当你从源代码安装软件时,通常需要使用 make
命令来执行 Makefile 文件,以便将源代码编译成可执行的程序或库。
[root@localhost nginx-1.14.0]make
注:执行以上如果报错 -Werror= ******错误信息
则 进入到nginx-1.6.2目录中
vim objs/Makefile
把里面的 CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused -Werror -g 其中的 -Werror
给移除了就行了
[root@localhost nginx-1.14.0]vim objs/Makefile
如果出现 struct crypt_data’ has no member named ‘current_salt’
则需要进入nginx目录执行 vim src/os/unix/ngx_user.c
在里面找到 cd.current_salt[0] = ~salt[0];
给注释掉即可
[root@localhost nginx-1.14.0]vim src/os/unix/ngx_user.c
修改完后,再次执行make
命令。不再报错后,执行make install
命令
[root@localhost nginx-1.14.0]make install
9、启动nginx
启动代码格式:nginx安装目录地址 -c nginx配置文件地址
所以需要先查找自己的nginx安装目录
[root@localhost nginx-1.14.0]whereis nginx
nginx: /usr/local/nginx
之后启动
[root@localhost nginx-1.14.0]/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
10、停止
nginx的停止有三种方式
从容停止
ps -ef|grep nginx
杀死进程
kill -QUIT 3905
快速停止
kill -TERM 3905
或者
kill -INT 3905
强制停止
pkill -9 nginx
11、 重启
验证nginx配置文件是否正确
方法一:
进入nginx安装目录sbin下,输入命令./nginx -t
看到如下显示nginx.conf syntax is ok
nginx.conf test is successful
说明配置文件正确!
方法二:
在启动命令-c前加-t
重启Nginx服务
方法一:
进入nginx可执行目录sbin下,输入命令**./nginx -s reload** 即可
方法二:
查找当前nginx进程号,然后输入命令:kill -HUP 进程号 实现重启nginx服务
12、配置ssl
[root@localhost ~]cd /root/downfile/nginx/nginx-1.14.0
[root@localhost nginx-1.14.0]./configure --prefix=/usr/local/nginx --with-http_ssl_module
注意:这里需要先停掉nginx 然后在执行make 进行重新编译 如果未启动则直接执行 出现错误问题参考上面的make后的异常信息
执行完成后,我们备份一下原来的nginx (这个以防万一,如果你的nginx中没有其他部署那倒是无所谓)
[root@localhost nginx-1.14.0]cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak
再把刚才编译的nginx 拷贝覆盖原来的nginx
[root@localhost nginx-1.14.0] cp ./objs/nginx /usr/local/nginx/sbin/
查看nginx 安装情况
[root@localhost nginx-1.14.0] /usr/local/nginx/sbin/nginx -v
在sbin中可以执行下面这个语句查询自己使用的nginx.conf在哪个位置,同时这个语句也可以验证你的nginx.conf文件是否是正确的。正确的格式会提示test is successful
[root@localhost nginx-1.14.0] cd /usr/local/nginx/sbin/nginx -t