Linux编译安装程序

在Linux中,编译程序曾经很是流行,可是如今,版本发行商维护着很大的预编译二进制库,以便用户下载使用。可是,在不少状况下,咱们仍然须要经过源代码编译生成可执行程序。编译源代码有如下好处。linux

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

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

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

安装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也须要此库,因此,咱们一并安装了。shell

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 库。bash

yum install -y openssl openssl-devel
复制代码

官网下载nginx源代码

如今,咱们就能够从nginx官网下载nginx源代码了,执行命令。服务器

wget http://nginx.org/download/nginx-1.16.0.tar.gz
复制代码

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

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
复制代码
相关文章
相关标签/搜索