1. 程式人生 > 實用技巧 >nginx編譯安裝指令碼

nginx編譯安裝指令碼

寫了個nginx編譯安裝指令碼
版本:nginx-1.19.2

#!/bin/bash

# 安裝依賴關係包
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make &>/dev/null

# 下載安裝包
curl -O http://nginx.org/download/nginx-1.19.2.tar.gz

# 新建用來啟動nginx的使用者
useradd nginx -s /sbin/nologin

# 解壓原始碼包,進入解壓後的目錄
tar xf nginx-1.19.2.tar.gz
cd nginx-1.19.2

# 編譯安裝前的配置
./configure --prefix=/usr/local/nginx --user=nginx --with-http_realip_module  --with-http_ssl_module  --with-pcre

# 編譯安裝
make
make install

cd -

# 修改環境變數
echo 'PATH=$PATH:/usr/local/nginx/sbin/' >>/etc/bashrc
source /etc/bashrc

# 關閉防火牆
service firewalld stop
systemctl disable firewalld

# 設定service方式啟動nginx
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]

Description=The nginx HTTP and reverse proxy server

After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking

PIDFile=/usr/local/nginx/logs/nginx.pid

# Nginx will fail to start if /run/nginx.pid already exists but has the wrong

# SELinux context. This might happen when running nginx -t from the cmdline.

# https://bugzilla.redhat.com/show_bug.cgi?id=1268621

ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid

ExecStartPre=/usr/local/nginx/sbin/nginx -t

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/bin/kill -s HUP $MAINPID

KillSignal=SIGQUIT

TimeoutStopSec=5

KillMode=mixed

PrivateTmp=true

[Install]

WantedBy=multi-user.target
EOF

# 重新載入服務的配置檔案
systemctl daemon-reload

#啟動nginx
service nginx start

# 設定nginx開機啟動
systemctl enable nginx.service

echo '*************** nginx安裝完成 *****************'

補充:nginx監聽80埠,如果apache或者Tomcat已經把80端口占用,nginx服務將會無法啟動。可以先用 netstat -anplut 檢查一下80埠是否被佔用,再考慮是殺死佔用80埠的程序,還是更改nginx監聽的埠,這裡就不細說了。