LoginSignup
4

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-08-14

はじめに

いままでレンタルサーバなどでよくわからずにさわっていたCGIスクリプトですが、自前で動作させてみました。
CentOS8のクリーンインストール状態からの作業です。
コードは使いまわしのため、コピペできるようにしています。
作業履歴の目的なので、解説は少なめです。すみません。

設定ファイルはパッケージインストールされたものがあれば、できるだけそれを利用しています。
すでに動作している環境では、デフォルトの設定ファイルから変更されている場合もあり、必ずしも今回の手順で動作するとは限りません。

対象機器および環境

検証環境 ( 2020/8/15 )

  • CentOS8 ( 8.2.2004 )
  • PHP / PHP-FPM ( 7.2.24 )
  • Python ( 3.8.0 )
  • Perl ( 5.26.3 )
  • Ruby ( 2.5.5 )

作業内容

事前作業

タイムゾーンの修正、パッケージのアップデート、selinuxの無効化
timedatectl set-timezone Asia/Tokyo
dnf install -y epel-release
dnf update -y
sed -i -e 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
shutdown -r now

パッケージインストール

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

dnf install -y nginx php php-fpm python38 ruby fcgi spawn-fcgi fcgiwrap

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

ヒアドキュメントなので、そのままコピペする
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;
}
EOF

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です。

コンテンツファイル

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

ヒアドキュメントなので、そのままコピペする
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の場合

スクリプトファイル

ヒアドキュメントなので、そのままコピペする
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の場合

スクリプトファイル

ヒアドキュメントなので、そのままコピペする
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の場合

スクリプトファイル

ヒアドキュメントなので、そのままコピペする
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の場合

スクリプトファイル

ヒアドキュメントなので、そのままコピペする
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>

ほかの設定ファイル

php-fpm は パッケージインストールそのままの設定で動作させています。

/etc/nginx/conf.d/php-fpm.conf
upstream php-fpm {
        server unix:/run/php-fpm/www.sock;
}
/etc/nginx/default.d/php.conf
index index.php index.html index.htm;

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   php-fpm;
}

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
4