LoginSignup
1
1

More than 1 year has passed since last update.

CentOS7の初期状態から nginx で php python perl ruby を動かすまで

Posted at

はじめに

と同じことをCentOS7でやっただけ。
作業履歴の目的なので、解説は少なめ。

作業内容

事前作業

SELinuxは無効化にしておく。

upstreamとかproxyとかでアクセスさせようとしてPermissionDeniedが出て、ソケットのパーミッションやらなんやら調べてたけど、面倒になってSELinuxを無効化したら動いたので、そうした。
いろいろ検証したり調査したりするときにはやっぱりSELinuxは外さないと時間がかかる。。。

パッケージインストール

各スクリプトと、CGIとして動作させるためのパッケージをインストールします。

いろいろインストール
yum install -y epel-release
yum update -y
yum install -y nginx php php-fpm python3 ruby fcgi spawn-fcgi fcgiwrap perl perl-CGI

python perl ruby のスクリプトが呼び出された場合にはfcgiを呼び出すようにします。
php のスクリプトが呼び出された場合にはPHP-FPMを呼び出すようにします。

nginx設定
cat <<'EOF' > /etc/nginx/default.d/cgi.conf
location ~ \.(py|pl|rb)$ {
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_pass   unix:/var/run/spawn-fcgi.socket;
}

location ~ \.(php|phar)(/.*)?$ {
    fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

    fastcgi_intercept_errors on;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    fastcgi_pass   127.0.0.1:9000;
}
EOF

fcgiのラッパーをサービスとして動作させるためのspawn-fcgiの設定ファイルを準備します。

spawn-fcgi設定
cat <<'EOF' >> /etc/sysconfig/spawn-fcgi
SOCKET=/var/run/spawn-fcgi.socket
OPTIONS="-u nginx -g nginx -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /sbin/fcgiwrap"
EOF

spawn-fcgi と php-fpm のサービス登録と起動をします。

サービス登録と起動
systemctl enable nginx php-fpm spawn-fcgi
systemctl start nginx php-fpm spawn-fcgi

ファイアウォールの穴あけをしておきます。

あなあけ
firewall-cmd --add-service=http --zone=public --permanent
firewall-cmd --reload

なお、php-fpmはパッケージインストールされた時のデフォルト設定ファイルそのままでOKです。

コンテンツファイル

各スクリプトへのインデックスを作成しておきます。

index.html
cat <<'EOF' > /usr/share/nginx/html/index.html
<html><body>
<h1>Running CGI scripts on NGINX</h1>
<a href=/php.php>php script</a><br>
<br>
<a href=/python.py>python script</a><br>
<br>
<a href=/perl.pl>perl script</a><br>
<br>
<a href=/ruby.rb>ruby script</a><br>
</body></html>
EOF

PHPの場合

スクリプトファイル

/usr/share/nginx/html/php.php
cat <<'EOF' > /usr/share/nginx/html/php.php
<?php
print "<html><body>\n";
print "Hello PHP Script!<br>\n";
print "</body></html>\n";
?>
EOF

動作確認

# curl http://localhost/php.php
<html><body>
Hello PHP Script!<br>
</body></html>

Perlの場合

スクリプトファイル

/usr/share/nginx/html/perl.pl
cat <<'EOF' > /usr/share/nginx/html/perl.pl
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html><body>\n";
print "Hello Perl Script!<br>\n";
print "</body></html>\n";
exit;
EOF
chmod 755 /usr/share/nginx/html/perl.pl

動作確認

# curl http://localhost/perl.pl
<html><body>
Hello Perl Script!<br>
</body></html>

Pythonの場合

スクリプトファイル

/usr/share/nginx/html/python.py
cat <<'EOF' > /usr/share/nginx/html/python.py
#!/usr/bin/python3
print("HTTP/1.0 200 OK")
print("Content-type: text/html\n")
print("<html><body>")
print("Hello Python Script!<br>")
print("</body></html>")
EOF
chmod 755 /usr/share/nginx/html/python.py

動作確認

# curl http://localhost/python.py
<html><body>
Hello Python Script!<br>
</body></html>

Rubyの場合

スクリプトファイル

/usr/share/nginx/html/ruby.rb
cat <<'EOF' > /usr/share/nginx/html/ruby.rb
#!/usr/bin/ruby
puts "Content-type: text/html\n\n"
puts "<html><body>"
puts "Hello Ruby Script!<br>"
puts "</body></html>"
EOF
chmod 755 /usr/share/nginx/html/ruby.rb

動作確認

# curl http://localhost/ruby.rb
<html><body>
Hello Ruby Script!<br>
</body></html>

ブラウザから確認

index.html
image.png
php.php
image.png
python.py
image.png
perl.pl
image.png
ruby.rb
image.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1