在Linux中,編譯程序曾經很是流行,可是如今,版本發行商維護着很大的預編譯二進制庫,以便用戶下載使用。可是,在不少狀況下,咱們仍然須要經過源代碼編譯生成可執行程序。編譯源代碼有如下好處。linux
編譯軟件源代碼是一件很是複雜並有技術性的任務,然而,仍然有一些簡單的編譯的任務,只須要幾個步驟便可完成。編譯的複雜程度每每取決於所要安裝的軟件包。這時,咱們須要使用一個維護程序的工具——make
命令。nginx
爲了演示這一編譯程序過程,咱們嘗試安裝nginx服務器。在安裝nginx以前,咱們先安裝nginx所需的編譯環境。c++
安裝 nginx 須要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,若是沒有 gcc 環境,咱們先使用which
命令查看是否存在gcc,若gcc不存在,則須要安裝gcc。正則表達式
which gcc
複製代碼
若存在gcc環境,則輸出/usr/bin/gcc
。若不存在,則安裝gcc。算法
yum install gcc gcc-c++
複製代碼
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,因此須要在 linux 上安裝pcre庫,pcre-devel是使用 pcre 開發的一個二次開發庫,nginx也須要此庫,因此,咱們一併安裝了。shell
yum install -y pcre pcre-devel
複製代碼
zlib 庫提供了不少種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,因此須要在 Centos 上安裝 zlib 庫。安全
yum install -y zlib zlib-devel
複製代碼
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。nginx 不只支持 http 協議,還支持 https(即在ssl協議上傳輸http),因此須要在 Centos 安裝 OpenSSL 庫。bash
yum install -y openssl openssl-devel
複製代碼
如今,咱們就能夠從nginx官網下載nginx源代碼了,執行命令。服務器
wget http://nginx.org/download/nginx-1.16.0.tar.gz
複製代碼
wget是一個下載文件的工具,它用在命令行下。對於Linux用戶是必不可少的工具。若沒有安裝wget,則須要先安裝它。less
yum -y install wget
複製代碼
當咱們下載完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的運行環境,
經過上面的步驟,咱們可使用make
命令生成可執行程序了,執行make
命令。
make
複製代碼
make
程序運行時,會使用Makefile文件中的內容指導其操做,命令執行過程當中將會產生許多信息。
若咱們再次運行程序,make
將不會從新生成全部文件,將只生成改動過的文件,即make
可以智能地僅生成須要building操做地目標文件,這一點尤爲重要,能夠減小沒必要要的時間花費。
打包好的源代碼通常包含一個特殊的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
複製代碼