1. ホーム
  2. スクリプト・コラム
  3. リナックスシェル

シェルスクリプトによる複数インスタンスでのnginxのデプロイメントの詳細チュートリアル

2022-01-05 01:53:14

1. スクリプトとインストールパッケージを格納するディレクトリを作成します。

[root@localhost nginx]# tree
.
├── install.sh
└── packages
    └── nginx-1.20.1.tar.gz

1 directory, 2 files
[root@localhost nginx]# 



2. 対応するインストールパッケージをダウンロードする

[root@localhost packages]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
[root@localhost packages]# ls
nginx-1.20.1.tar.gz
[root@localhost packages]# 


3. スクリプトを書く

[root@localhost nginx]# cat install.sh 
#! /bin
log_dir=/varinstall_dir=/usr
id nginx &>/devif [ $? -ne 0 ];then
 useradd -r -M -s /sbin/nologin nginx
fi

yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel

if [ ! -d $log_dir/nginx ];then
    mkdir -p $log_dir    chown -R nginx.nginx $log_dirfi


if [ ! -d $install_dir/nginx-1.20.1 ];then
    tar xf packages/nginx-1.20.1.tar.gz -C $install_dir
fi

cd $install_dir/nginx-1.20.1
if [ ! -d $install_dir/nginx ];then
    . /configure --prefix=$install_dir/nginx \
        --user=nginx \
        --group=nginx \
        --with-debug \
        --with-http_ssl_module \
        --with-http_realip_module \
        --with-http_image_filter_module \
        --with-http_gunzip_module \
        --with-http_gzip_static_module \
        --with-http_stub_status_module \
        --http-log-path=/var/log/nginx/access.log \
        --error-log-path=/var/log/nginx/error.log
    make && make install
fi

echo "export PATH=$install_dir/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh

cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=Nginx server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx 
ExecStop=/usr/local/nginx/sbin/nginx -s quit
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now nginx.service


[root@localhost nginx]# 



4. 効果を確認する

[root@localhost nginx]# bash -x install.sh 
+ log_dir=/var+ install_dir=/usr+ id nginx
+ '[' 0 -ne 0 ']'
+ yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 1:03:20 ago, performed on Sunday, October 24, 2021 at 20:57:26.
Package pcre-devel-8.42-4.el8.x86_64 has been installed.
Package pcre-8.42-4.el8.x86_64 has been installed.
Package gcc-8.4.1-1.el8.x86_64 has been installed.
Package gcc-c++-8.4.1-1.el8.x86_64 has been installed.
The package openssl-devel-1:1.1.1g-15.el8_3.x86_64 has been installed.
Package zlib-1.2.11-17.el8.x86_64 has been installed.
Package zlib-devel-1.2.11-17.el8.x86_64 has been installed.
The package make-1:4.2.1-10.el8.x86_64 has been installed.
Package vim-enhanced-2:8.0.1763-15.el8.x86_64 has been installed.
Package wget-1.19.5-10.el8.x86_64 has been installed.
The package openssl-1:1.1.1g-15.el8_3.x86_64 has been installed.
Package gd-devel-2.2.5-7.el8.x86_64 has been installed.
Dependency resolution.
No processing required.
Done!
+ '[' '!' -d /var/log/nginx ']'
+ '[' '!' -d /usr/local/nginx-1.20.1 ']'
+ cd /usr/local/nginx-1.20.1
+ '[' '!' -d /usr/local/nginx ']'
+ echo 'export PATH=/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin'
+ cat
+ systemctl daemon-reload
+ systemctl enable --now nginx.service
[root@localhost nginx]# 
[root@localhost nginx]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port           
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*              
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*              
LISTEN 0 128 [::]:22 [::]:*              
[root@localhost nginx]# 



シェルスクリプトで nginx をマルチインスタンス展開する方法についての詳細なチュートリアルは以上です。シェルスクリプトで nginx を展開する方法の詳細については、過去の記事を検索するか、次の記事を引き続き参照してください。