スポンサーリンク
先日、【ubuntu】nginx環境でシェルスクリプトをcgiとして動かすというどこに需要があるかわからない非常にニッチな記事を書いたのですが、今回はそれのCentOS7版を書きたいと思います。
まずnginxをインストールします。
ubuntuでは標準リポジトリでインストールできましたが、centos7は別リポジトリを設定する必要があります。
vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
これで準備OKですので、インストールを実行します。
yum install nginx
起動してみます。
systemctl start nginx.service
問題無さそうです。プロセスが立ち上がっているか確認します。
# ps -ef | grep nginx
root 9438 1 0 03:03 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 9439 9438 0 03:03 ? 00:00:00 nginx: worker process
root 9441 9225 0 03:03 pts/0 00:00:00 grep --color=auto nginx
上記のような感じで表示されればOKです。
ついでに自動起動の設定もしておきます。
systemctl enable nginx.service
次にfastcgi(nginxでcgiを動かすために必要なモジュール)をインストールします。
centos7の場合、ソースからビルドする必要があるため、開発ツールをインストールします。
yum groupinstall "Development tools"
次にepelリポジトリもインストールします。
yum install epel-release
epelからfastcgiのライブラリをインストールします。
yum install epel-release
yum install spawn-fcgi fcgi-devel
ここまででインストールの準備はOKです。
いよいよfcgiwrapのソースをダウンロードし、コンパイルします。
# download
cd /usr/local/src
wget http://github.com/gnosek/fcgiwrap/tarball/master
# 解凍
tar zxvf fcgiwrap.tar.gz
cd gnosek-fcgiwrap*/
# コンパイルとインストール
autoreconf -i
./configure
make
make install
これでfastcgiのインストールが完了しました。
次にfastcgiを使うための設定を入れます。conf.d/配下に別ファイルとして作成します。
cd /etc/nginx/conf.d/
vim bash.conf
とりあえず動かすだけなら以下のパラメータでOKです。
server {
listen 80;
server_name 127.0.0.1;
location ~ \.sh$ {
root /usr/share/nginx/html/;
fastcgi_pass 127.0.0.1:9001;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
ubuntuの場合はデフォルトでも動きましたが、centos7の場合は設定が必要です。
vim /etc/sysconfig/spawn-fcgi
ファイルの末尾に1行追記します。
# 末尾に追記
OPTIONS="-u nginx -g nginx -a 127.0.0.1 -p 9001 -P /var/run/spawn-fcgi.pid -- /usr/local/sbin/fcgiwrap"
ここまで来たらfastcgiを起動します。起動スクリプトの名前は spawn-fcgi です。
systemctl enable spawn-fcgi
systemctl start spawn-fcgi
プロセスが起動しているか確認します。
# netstat -atunp | grep 9001
tcp 0 0 127.0.0.1:9001 0.0.0.0:* LISTEN 22882/fcgiwrap
上記のようにプロセスが存在すれば起動しています。
ここまでで環境面の準備は完了しました。
次にcgiとして動かすシェルスクリプトを作成します。
今回は"hello world"と表示するだけのスクリプトを作成します。
vim /usr/share/nginx/html/hello.sh
中身はこんな感じ
#!/bin/sh
echo 'Content-type: text/html'
echo ''
echo '<html>'
echo '<head></head>'
echo '<body>'
echo '<h1>hello world!!</h1>'
echo '</body>'
echo '</html>'
作成したら実行権限を付与します。
chmod 755 /usr/share/nginx/html/hello.sh
一度nginxを再起動します。
systemctl restart nginx.service
ここまで来たら準備OKです。いよいよ確認してみます。
curl http://localhost/index.sh
以下のように表示されればOKです。
<html>
<head></head>
<body>
<h1>hello world!!</h1>
</body>
</html>
ということで、シェルスクリプトをcgiとして動かすのcentos7版でした。
スポンサーリンク
2020/12/11 05:36:50
Best Price Net For Tadalis Sx 100 50 Mg usacybes canadian pharmacy cialis 20mg glicky Cialis 5mg 30 Pack Cheap