Nginx 安装小课题
(一)下载解压
nginx 官网下载,选择 Stable version 稳定版本进行下载,我这边是linux系统,下载的是
nginx-1.22.1
版本
[root@localhost install-package]#
[root@localhost install-package]#
[root@localhost install-package]# pwd
/home/lvzb/install-package
[root@localhost install-package]# wget http://nginx.org/download/nginx-1.22.1.tar.gz
--2023-03-31 06:20:27-- http://nginx.org/download/nginx-1.22.1.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073948 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.22.1.tar.gz’
100%[=================================================================================================================================================================
2023-03-31 06:22:57 (6.99 KB/s) - ‘nginx-1.22.1.tar.gz’ saved [1073948/1073948]
[root@localhost install-package]# tar -zxvf nginx-1.22.1.tar.gz -C /opt/software/middleware
(二)环境检测前置依赖安装
- gcc是可以在多种硬件平台上编译出可执行程序的超级编译器,安装
nginx
需要先从官网下载源码进行编译,编译的过程依赖gcc环境pcre
是一个用C语言编写的正则表达式函数,Nginx的http模块需要用到pcre来解析正则表达式,pcre-devel
是使用pcre开发的二次开发库zlib
库提供了很多种压缩和解压缩的方式,zlib库里面有很多压缩和解压的方式,Nginx使用zlib对包的内容进行gzipopenssl
提供了这个强大的安全套接字密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用,Nginx除了支持http还支持https
[root@localhost install-package]# rpm -qa openssl
openssl-1.0.2k-12.el7.x86_64
[root@localhost install-package]# rpm -qa zlib
zlib-1.2.7-17.el7.x86_64
[root@localhost install-package]# rpm -qa pcre
pcre-8.32-17.el7.x86_64
[root@localhost install-package]#
[root@localhost install-package]# ls -l
total 371084
-rw-r--r-- 1 root root 159019376 Nov 14 05:05 jdk-8u11-linux-x64.tar.gz
-rw-r--r-- 1 root root 149697219 Nov 14 05:54 jdk-8u351-linux-x64.tar.gz
-rw-r--r-- 1 root root 68583422 Nov 14 19:55 kafka_2.13-2.7.0.tgz
-rw-r--r-- 1 root root 1073948 Oct 19 17:23 nginx-1.22.1.tar.gz
-rw-r--r-- 1 root root 1601978 Nov 17 05:47 ZooInspector.zip
[root@localhost nginx-1.22.1]#
[root@localhost nginx-1.22.1]# pwd
/opt/software/middleware/nginx-1.22.1
[root@localhost nginx-1.22.1]#
[root@localhost nginx-1.22.1]#
[root@localhost nginx-1.22.1]#
[root@localhost nginx-1.22.1]# ls -l
total 800
drwxr-xr-x 6 1001 1001 326 Mar 31 06:39 auto
-rw-r--r-- 1 1001 1001 317399 Oct 19 16:02 CHANGES
-rw-r--r-- 1 1001 1001 485035 Oct 19 16:02 CHANGES.ru
drwxr-xr-x 2 1001 1001 168 Mar 31 06:39 conf
-rwxr-xr-x 1 1001 1001 2590 Oct 19 16:02 configure
drwxr-xr-x 4 1001 1001 72 Mar 31 06:39 contrib
drwxr-xr-x 2 1001 1001 40 Mar 31 06:39 html
-rw-r--r-- 1 1001 1001 1397 Oct 19 16:02 LICENSE
drwxr-xr-x 2 1001 1001 21 Mar 31 06:39 man
-rw-r--r-- 1 1001 1001 49 Oct 19 16:02 README
drwxr-xr-x 9 1001 1001 91 Mar 31 06:39 src
前置依赖安装:
yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
运行configure
脚本程序可以指定nginx的安装目录:./configure --prefix=/opt/software/middleware/nginx-install-1.22.1
(nginx-install-1.22.1
需要提前创建好)执行
make
: 运行完成后,该文件夹下多出一个文件—Makefile,此时执行make指令进行源代码编译,编译过程中屏幕会有编译全过程执行
make install
: 编译完成后,执行make的install命令安装Nginx服务器
[root@localhost nginx-1.22.1]# pwd
/opt/software/middleware/nginx-1.22.1
[root@localhost nginx-1.22.1]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package gcc.x86_64 0:4.8.5-28.el7 will be updated
--> Processing Dependency: gcc = 4.8.5-28.el7 for package: libquadmath-devel-4.8.5-28.el7.x86_64
--> Processing Dependency: gcc = 4.8.5-28.el7 for package: gcc-gfortran-4.8.5-28.el7.x86_64
..............
[root@localhost nginx-1.22.1]#
[root@localhost nginx-1.22.1]# ./configure --prefix=/opt/software/middleware/nginx-install-1.22.1
checking for OS
+ Linux 3.10.0-862.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
.......
checking for zlib library ... found
creating objs/Makefile
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/opt/software/middleware/nginx-install-1.22.1"
nginx binary file: "/opt/software/middleware/nginx-install-1.22.1/sbin/nginx"
nginx modules path: "/opt/software/middleware/nginx-install-1.22.1/modules"
nginx configuration prefix: "/opt/software/middleware/nginx-install-1.22.1/conf"
nginx configuration file: "/opt/software/middleware/nginx-install-1.22.1/conf/nginx.conf"
nginx pid file: "/opt/software/middleware/nginx-install-1.22.1/logs/nginx.pid"
nginx error log file: "/opt/software/middleware/nginx-install-1.22.1/logs/error.log"
nginx http access log file: "/opt/software/middleware/nginx-install-1.22.1/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@localhost nginx-1.22.1]#
[root@localhost nginx-1.22.1]# ls -l
total 804
drwxr-xr-x 6 1001 1001 326 Mar 31 06:39 auto
-rw-r--r-- 1 1001 1001 317399 Oct 19 16:02 CHANGES
-rw-r--r-- 1 1001 1001 485035 Oct 19 16:02 CHANGES.ru
drwxr-xr-x 2 1001 1001 168 Mar 31 06:39 conf
-rwxr-xr-x 1 1001 1001 2590 Oct 19 16:02 configure
drwxr-xr-x 4 1001 1001 72 Mar 31 06:39 contrib
drwxr-xr-x 2 1001 1001 40 Mar 31 06:39 html
-rw-r--r-- 1 1001 1001 1397 Oct 19 16:02 LICENSE
-rw-r--r-- 1 root root 554 Mar 31 06:54 Makefile
drwxr-xr-x 2 1001 1001 21 Mar 31 06:39 man
drwxr-xr-x 3 root root 174 Mar 31 07:01 objs
-rw-r--r-- 1 1001 1001 49 Oct 19 16:02 README
drwxr-xr-x 9 1001 1001 91 Mar 31 06:39 src
[root@localhost nginx-1.22.1]#
[root@localhost nginx-1.22.1]# make
[root@localhost nginx-1.22.1]#
[root@localhost nginx-1.22.1]# make install
(三)测试 安装是否存在问题
./sbin/nginx -v
./sbin/nginx -t
[root@localhost nginx-install-1.22.1]# pwd
/opt/software/middleware/nginx-install-1.22.1
[root@localhost nginx-install-1.22.1]# ls -l
total 0
drwxr-xr-x 2 root root 333 Mar 31 07:03 conf
drwxr-xr-x 2 root root 40 Mar 31 07:01 html
drwxr-xr-x 2 root root 6 Mar 31 07:01 logs
drwxr-xr-x 2 root root 19 Mar 31 07:01 sbin
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# ./sbin/nginx -v
nginx version: nginx/1.22.1
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# ./sbin/nginx -t
nginx: the configuration file /opt/software/middleware/nginx-install-1.22.1/conf/nginx.conf syntax is ok
nginx: configuration file /opt/software/middleware/nginx-install-1.22.1/conf/nginx.conf test is successful
查看 nginx 启动状况:
启动
nginx
: ./sbin/nginx停止
nginx
: ./sbin/nginx -s stop重新加载nginx: ./sbin/nginx -s reload
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# ./sbin/nginx -h
nginx version: nginx/1.22.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /opt/software/middleware/nginx-install-1.22.1/)
-e filename : set error log file (default: logs/error.log)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
[root@localhost nginx-install-1.22.1]# ./sbin/nginx
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# ps -ef | grep nginx
root 9754 1 0 07:06 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 9755 9754 0 07:06 ? 00:00:00 nginx: worker process
root 9780 3015 0 07:06 pts/2 00:00:00 grep --color=auto nginx
(四)日志文件
logs 里的 access.log是访问日志文件,我们可以通过监听该日志文件,然后在浏览器里多次访问nginx,它就会被记录到
[root@localhost logs]# pwd
/opt/software/middleware/nginx-install-1.22.1/logs
[root@localhost logs]#
[root@localhost logs]# tail -f access.log
192.168.125.1 - - [31/Mar/2023:07:09:02 +0800] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
192.168.125.1 - - [31/Mar/2023:07:09:02 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.125.100/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
192.168.125.1 - - [31/Mar/2023:07:15:29 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
(五)遇到的问题
前置依赖缺失导致
./configure: error: the HTTP rewrite module requires the PCRE library.
error, 安装好前置依赖即可
[root@localhost nginx-1.22.1]# pwd
/opt/software/middleware/nginx-1.22.1
[root@localhost nginx-1.22.1]# ./configure --prefix=/opt/software/middleware/nginx-install-1.22.1
checking for OS
+ Linux 3.10.0-862.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
.......................
.......................
checking for PCRE2 library ... not found
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
./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.