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。

# 說明:
# 檢查環境執行命令:./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++ pcre-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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章