Shell編譯安裝nginx

ElfCafe發表於2020-06-14

環境及規劃

[root@nginx-node01 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
ID 主機名 ip 系統配置 軟體版本 作業系統
01 nginx-node01 192.168.56.104 1C 1G 1.16.0穩定版 CentOS 7.6.1810

編譯依賴檢查

  1. gcc編譯環境

  2. pcre & pcre-dev

  3. zlib & zlib-dev

  4. openssl & openssl-dev

[root@nginx-node01 nginx]# cat ifinstall.sh 
#!/bin/bash
#auto install nginx v0.1

#check if package was installed
function checkInstall(){
  echo "Checking $1 installed or not"
  #rpm -q $1 >/dev/null 2>&1
  rpm -q $1 
  if [ $? -eq 0 ]; then
        echo "$1 installed "
  else
        echo "$1 not installed, will install $1 "
        yum -y install $1
        if [ $? -ne 0 ];then
          echo "Install $1" /bin/false
          exit 1
        fi
  fi
}

##環境及依賴檢查
#gcc
checkInstall gcc-c++

#pcre & pcre-devel
checkInstall pcre && checkInstall pcre-devel 

#zlib & zlib-devel
checkInstall zlib && checkInstall zlib-devel

#openssl & openssl-devel
checkInstall openssl && checkInstall openssl-devel

安裝步驟

  1. 檢查並新增nginx使用者和組

  2. 解壓軟體包、建立相關目錄

  3. 執行預編譯配置

  4. 執行編譯、執行安裝

[root@nginx-node01 ~]# useradd -g nginx nginx
[root@nginx-node01 ~]# id nginx
uid=1000(nginx) gid=1000(nginx) groups=1000(nginx)

[root@nginx-node01 nginx]# cat nginx_install.sh       
#!/bin/bash
#check if a user exists and add it

#set -n
set -v

#使用者及組 
NX_GROUP='nginx'
NX_USER='nginx'

#原始碼包位置
NX_TMP_DIR='/tmp/nginx/'

#原始碼版本
NX_VER=1.16.0
NX_FILE="nginx-$NX_VER.tar.gz"
NX_FILE_ABS=$NX_TMP_DIR$NX_FILE

#解壓後原始碼位置
NX_DIR=`echo $NX_FILE |sed 's/\.tar.*//g'`
NX_SRC_ABS=${NX_TMP_DIR}${NX_DIR}

#Nginx home 目錄
NX_HOME='/home/nginx'
NX_LOG=$NX_HOME/log
NX_TARGET=${NX_HOME}/${NX_VER}

#解壓原始碼
if [ -d $NX_SRC_ABS ];then
    echo "$NX_SRC_ABS already exists"
else
    tar -xvzf $NX_FILE 
fi

##配置相關目錄、預編譯
cd $NX_SRC_ABS

##設定安裝目錄
#新增log目錄
if [ ! -d $NX_LOG ]; then
   mkdir -p $NX_LOG
fi 
#Nginx 安裝目錄
if [ ! -d ${NX_TARGET} ]; then
   mkdir -p ${NX_TARGET}
fi 

#預編譯配置
./configure \
--user=$NX_USER \
--group=$NX_GROUP \
--prefix=${NX_TARGET} \
--sbin-path=${NX_TARGET}/nginx \
--conf-path=${NX_TARGET}/nginx.conf \
--pid-path=${NX_LOG}/nginx.pid \
--error-log-path=${NX_LOG}/error.log \
--http-log-path=${NX_LOG}/access.log \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module

#編譯 && 安裝
make && make install

安裝驗證

[root@nginx-node01 ~]# /home/nginx/1.16.0/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/home/nginx/1.16.0 --sbin-path=/home/nginx/1.16.0/nginx --conf-path=/home/nginx/1.16.0/nginx.conf --pid-path=/home/nginx/log/nginx.pid --error-log-path=/home/nginx/log/error.log --http-log-path=/home/nginx/log/access.log --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module

啟動驗證

[root@nginx-node01 ~]# netstat -ntlp |grep -aiw 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      21949/nginx: master 
[root@nginx-node01 ~]# ps -ef |grep nginx
root     21949     1  0 11:58 ?        00:00:00 nginx: master process ./nginx
nginx    21950 21949  0 11:58 ?        00:00:00 nginx: worker process

服務驗證

[root@nginx-node01 ~]# curl localhost:80          
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

對外服務驗證

#關閉防火牆
systemctl stop firewalld.service

相關文章