Centos 7 安装Nginx

1,163 阅读8分钟

在Linux中,编译程序曾经非常流行,但是现在,版本发行商维护着很大的预编译二进制库,以便用户下载使用。但是,在很多情况下,我们仍然需要通过源代码编译生成可执行程序。编译源代码有以下好处。

  • 可用性:尽管有些发行版中已经包含了一些预编译程序,但并不会包含用户所有可能需要的所有应用程序。这时候,我们只能通过编译源代码生成可执行程序。
  • 及时性:有些时候我们需要获取最新的程序,这时候自己编译源代码就有必要了。

编译软件源代码是一件非常复杂并有技术性的任务,然而,仍然有一些简单的编译的任务,只需要几个步骤即可完成。编译的复杂程度往往取决于所要安装的软件包。这时,我们需要使用一个维护程序的工具——make命令。

为了演示这一编译程序过程,我们尝试安装nginx服务器。在安装nginx之前,我们先安装nginx所需的编译环境。

安装nginx所需的编译环境

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,我们先使用which命令查看是否存在gcc,若gcc不存在,则需要安装gcc。

  1. 先查看并安装gcc环境
which gcc

若存在gcc环境,则输出/usr/bin/gcc。若不存在,则安装gcc。

yum install gcc gcc-c++
  1. 安装pcre pcre-devel

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在 linux 上安装pcre库,pcre-devel是使用 pcre 开发的一个二次开发库,nginx也需要此库,所以,我们一并安装了。

yum install -y pcre pcre-devel
  1. 安装zlib

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel
  1. 安装OpenSSL

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

官网下载nginx源代码

现在,我们就可以从nginx官网下载nginx源代码了,执行命令。

wget http://nginx.org/download/nginx-1.16.0.tar.gz

wget是一个下载文件的工具,它用在命令行下。对于Linux用户是必不可少的工具。若没有安装wget,则需要先安装它。

yum -y install wget

解压nginx源代码

当我们下载完nginx源代码之后,需要对其进行解压,解压命令如下。

tar -zxvf nginx-1.16.0.tar.gz

如果解压正常,我们将在当前目录下发现一个名为nginx-1.16.0的目录。

anaconda-ks.cfg  lady_dog.txt  ls-output.txt  nginx-1.16.0  nginx-1.16.0.tar.gz  original-ks.cfg

分析生成的环境

此时我们进入到nginx-1.16.0目录下

 cd nginx-1.16.0

然后,我们就可以执行./configure命令了。

./configure

configure程序其实是源代码树下的一个shell脚本。它的任务就是分析生成环境。多数源代码都设计成可移植的。也就是说,源代码可以在多种UNIX系统上生成,只是源代码在生成时可能需要经过细微的调整以适应各系统之间的不同。configure同样会检查系统是否已经安装了必要的外部工具和组件。

由于configure并不是存放在shell通常期望程序所在的目录下,所以必须明确告知shell有关configure的位置,我们可以在命令前添加“./”目录符来实现这一目的。该符号表示configure程序在当前工作目录下。

执行configure之后,我们看到configure输出了很多信息

checking for OS
 + Linux 4.4.0-17763-Microsoft x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-36) (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 ... not 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 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 zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + 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"

需要注意的是,若此时发生错误,则该configure操作将以失败告终,并且不会生成可执行程序。

可以看到,configure程序在源目录中创建了新文件,其中最重要的就是Makefile。

::: tip Makefile的作用

Makefile是指导make命令如何生成可执行程序的配置文件,如果没有该文件,make便无法运行。Makefile是一个文本文件,可以使用less命令查看。

:::

总的来说,configure脚本程序的作用就是检查linux的运行环境,

编译生成nginx可执行程序

通过上面的步骤,我们可以使用make命令生成可执行程序了,执行make命令。

make

make程序运行时,会使用Makefile文件中的内容指导其操作,命令执行过程中将会产生许多信息。

若我们再次运行程序,make将不会重新生成所有文件,将只生成改动过的文件,即make能够智能地仅生成需要building操作地目标文件,这一点尤其重要,可以减少不必要的时间花费。

安装nginx的可执行程序

打包好的源代码一般包含一个特殊的make目标程序,它便是install。该目标程序将会在系统目录下安装最后生成的可执行程序。通常会安装在目录/usr/local/bin下,该目录是本地主机上生成软件的常用安装目录,执行命令。

make install

当然,nginx设置了默认的安装位置/usr/local/nginx。在安装完之后,就可以使用该安装程序了。

安装成功与否校验

为了验证nginx是否安装成功,我们切换目录,启动nginx。我们先切换到nginx安装目录

cd /usr/local/nginx

然后,进入执行命令./sbin/nginx

./sbin/nginx

为了查看nginx是否正常运行,执行命令

ps -ef | grep nginx

若正常运行,可以看到类似如下的输出结果

root      2616     1  0 16:51 ?        00:00:00 nginx: master
nobody    2617  2616  0 16:51 ?        00:00:00 nginx: worker
root      2620     1  0 16:52 ?        00:00:00 nginx: master
nobody    2621  2620  0 16:52 ?        00:00:00 nginx: worker
root      2623     4  0 16:52 tty1     00:00:00 grep --color=auto nginx