centos7源码包安装nginx-1.18.0最新稳定版

目录

 

1.获取源码包

2.检查编译环境

3.编译安装nginx

4.启动nginx服务

5.浏览器访问nginx

6.配置前端页面

7.常用命令


1.获取源码包

个人习惯将安装包放在/opt目录下,解压后,进入安装目录

# 进入/opt目录
cd /opt

# 获取最新稳定版
wget http://nginx.org/download/nginx-1.18.0.tar.gz

# 解压
tar -zxvf nginx-1.18.0.tar.gz

2.检查编译环境

安装流程为:./configure(检查编译安装环境)     ==>    make(编译) ==>     make install (编译安装)。

过程其实并不复杂,我将安装依赖的过程也放上来,只为了提供一些学习的思路。

以下是检查编译安装环境的全部过程,熟悉安装的可以跳过此步骤,需要注意的是:./configure命令并不会安装nginx,只是生成makefile文件,重复执行./configure命令,会覆盖上一次的makefile文件。

# 说明:
# 检查环境执行命令:./configure
# 这里我们添加一些参数,所以检查环境执行的命令是:./configure --prefix=/usr/local/nginx --with-http_gzip_static_module --with-http_ssl_module
# 添加的参数说明:
# --prefix=/usr/local/nginx         指定我们将nginx安装在/usr/local/nginx目录
# --with-http_gzip_static_module    启用nginx压缩模块
# --with-http_ssl_module            启用nginx加密模块
# 也可以不启用任何模块,直接直接:./configure --prefix=/usr/local/nginx,只指定位置,看个人需求

1.进入并查看nginx源码包目录
[root@localhost opt]# cd /opt/nginx-1.18.0
[root@localhost nginx-1.18.0]# ll
总用量 768
drwxr-xr-x. 6 1001 1001   4096 12月 29 10:59 auto
-rw-r--r--. 1 1001 1001 302863 4月  21 2020 CHANGES
-rw-r--r--. 1 1001 1001 462213 4月  21 2020 CHANGES.ru
drwxr-xr-x. 2 1001 1001    168 12月 29 11:03 conf
-rwxr-xr-x. 1 1001 1001   2502 4月  21 2020 configure
drwxr-xr-x. 4 1001 1001     72 12月 29 10:59 contrib
drwxr-xr-x. 2 1001 1001     40 12月 29 10:59 html
-rw-r--r--. 1 1001 1001   1397 4月  21 2020 LICENSE
-rw-r--r--. 1 root root     46 12月 29 11:04 Makefile
drwxr-xr-x. 2 1001 1001     21 12月 29 10:59 man
drwxr-xr-x. 2 root root     77 12月 29 11:04 objs
-rw-r--r--. 1 1001 1001     49 4月  21 2020 README
drwxr-xr-x. 9 1001 1001     91 12月 29 10:59 src

# 2.检查编译环境,提示未安装gcc-c++,检查报错中断
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_gzip_static_module --with-http_ssl_module 
checking for OS
 + Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

# 3.安装gcc-c++
[root@localhost nginx-1.18.0]# yum -y install gcc-c++
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.163.com
...
...
...
...
已安装:
  gcc-c++.x86_64 0:4.8.5-44.el7                                                                                                 

作为依赖被安装:
  cpp.x86_64 0:4.8.5-44.el7                gcc.x86_64 0:4.8.5-44.el7                       glibc-devel.x86_64 0:2.17-317.el7   
  glibc-headers.x86_64 0:2.17-317.el7      kernel-headers.x86_64 0:3.10.0-1160.11.1.el7    libmpc.x86_64 0:1.0.1-3.el7         
  libstdc++-devel.x86_64 0:4.8.5-44.el7    mpfr.x86_64 0:3.1.1-4.el7                      

完毕!

# 4.继续检查编译环境,发现缺少pcre库,检查报错中断
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_gzip_static_module --with-http_ssl_module 
checking for OS
 + Linux 3.10.0-1160.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 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.

# 5.安装pcre-devel
[root@localhost nginx-1.18.0]# yum -y install pcre-devel
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.163.com
...
...
...
...

已安装:
  pcre-devel.x86_64 0:8.32-17.el7                                                                                               

完毕!

# 6.继续检查编译环境,发现缺少zlib库,检查报错中断
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_gzip_static_module --with-http_ssl_module 
checking for OS
 + Linux 3.10.0-1160.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 zlib library ... not found

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

# 6.安装zlib-devel
[root@localhost nginx-1.18.0]# yum -y install zlib-devel
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.163.com
...
...
...
...
  正在安装    : zlib-devel-1.2.7-18.el7.x86_64                                                                              1/1 
  验证中      : zlib-devel-1.2.7-18.el7.x86_64                                                                              1/1 

已安装:
  zlib-devel.x86_64 0:1.2.7-18.el7                                                                                              

完毕!

# 7.继续检查编译环境,检查通过,但发现缺少openssl-devel,这会导致我们无法启用http_ssl_module模块。
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_gzip_static_module --with-http_ssl_module 
checking for OS
 + Linux 3.10.0-1160.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) 
...
...
...
...

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used  # 提示没有找到openssl
  + using system zlib library

# 8.安装openssl-devel
[root@localhost nginx-1.18.0]# yum -y install openssl-devel
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.163.com
...                                                                                                                                            
...
...
...
作为依赖被安装:
  keyutils-libs-devel.x86_64 0:1.5.8-3.el7     krb5-devel.x86_64 0:1.15.1-50.el7       libcom_err-devel.x86_64 0:1.42.9-19.el7     libkadm5.x86_64 0:1.15.1-50.el7     libselinux-devel.x86_64 0:2.5-15.el7    
  libsepol-devel.x86_64 0:2.5-10.el7           libverto-devel.x86_64 0:0.2.5-4.el7    

作为依赖被升级:
  openssl.x86_64 1:1.0.2k-21.el7_9                                                                     openssl-libs.x86_64 1:1.0.2k-21.el7_9                                                                    

完毕!

# 9.继续检查编译环境,环境检测通过,环境检测最终如下:
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_gzip_static_module --with-http_ssl_module
checking for OS
 + Linux 3.10.0-1160.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 gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... not found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for prctl(PR_SET_KEEPCAPS) ... found
checking for capabilities ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for sched_setaffinity() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for eventfd() ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for clock_gettime(CLOCK_MONOTONIC) ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for ioctl(FIONREAD) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/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"

安装依赖和检测环境过程内容较多,因此省略了一部分,注意核对展示出来的关键信息即可。

综上,安装nginx需要的依赖正常来说就是4个,gcc-c++,pcre-devel,zlib-devel,openssl-devel。

既然知道缺少这几个依赖,我们安装nginx前先安装依赖即可。由于之前已经逐个安装过依赖,此步骤跳过:)

# 安装依赖
yum -y install gcc-c++ prce-devel zlib-devel openssl-devel

3.编译安装nginx

此步骤是真正将nginx安装到我们在./configure时指定的路径:/usr/local/nginx

# 1.编译&&编译安装
[root@localhost nginx-1.18.0]# make && make install
make -f objs/Makefile
make[1]: 进入目录“/opt/nginx-1.18.0”
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
	src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_log.o \
	src/core/ngx_log.c
...
...
...
...

test -d '/usr/local/nginx/logs' \
	|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
	|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
	|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
	|| mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/opt/nginx-1.18.0”

# 2.查看nginx目录下是否正常
# 目录分别是:
# conf:配置文件目录
# html:前端页面目录
# logs:日志目录
# sbin:启动程序目录
[root@localhost nginx-1.18.0]# ll /usr/local/nginx/
总用量 4
drwxr-xr-x. 2 root root 4096 12月 29 16:51 conf
drwxr-xr-x. 2 root root   40 12月 29 16:51 html
drwxr-xr-x. 2 root root    6 12月 29 16:51 logs
drwxr-xr-x. 2 root root   19 12月 29 16:51 sbin

4.启动nginx服务

# 进入启动文件所在目录启动nginx
[root@localhost nginx-1.18.0]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx

5.浏览器访问nginx

本机IP是:192.168.44.66

端口默认:80

浏览器输入:http://192.168.44.66:80,效果如下:

6.配置前端页面

配置一个自己的简单的前端页面,首先你需要知道以下文件的位置及作用:

配置文件位置:/usr/local/nginx/conf/nginx.conf

启动文件位置:/usr/local/nginx/sbin/nginx

配置示例:

# 1.写一个index.html放在任意目录
# 比如:/home
[root@localhost sbin]# echo 'welcome to nginx!' > /home/index.html


# 2.修改配置文件将前端路径指向该index.html文件
[root@localhost sbin]# vi /usr/local/nginx/conf/nginx.conf
# 将此处
location / {
            root   html;
            index  index.html index.htm;
        }

# 改为这样,保存退出
location / {
            root   /home;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

# 3.重载配置
[root@localhost sbin]# ./nginx -s reload

示例效果:

注意:这种配置简单粗暴,想使用更优雅的方法,参考其他配置

7.nginx常用命令

# 启动
[root@localhost sbin]# ./nginx

# 重载配置文件,相当于重启
[root@localhost sbin]# ./nginx -s reload

# 重启
[root@localhost sbin]# ./nginx -s reopen

# 停止
[root@localhost sbin]# ./nginx -s stop

# 检查配置文件
[root@localhost sbin]# ./nginx -t

猜你喜欢

转载自blog.csdn.net/ct_666/article/details/111883887