I used to touch CGI scripts on rental servers without knowing it well, but I tried to run it on my own. It is a work from the clean installation state of CentOS8. Since the code is reused, I am trying to copy it. Since it is the purpose of work history, there are few explanations. I'm sorry.
If there is a package installed configuration file, I use it as much as possible. In an environment that is already running, the default configuration file may have been changed, so this procedure does not always work.
Timezone fix, package update, selinux disabled
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
Install each script and a package to run as CGI.
dnf install -y nginx php php-fpm python38 ruby fcgi spawn-fcgi fcgiwrap
Make sure to call fcgi when the python perl ruby script is called.
Since it is a here document, copy it as it is
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
Prepare a spawn-fcgi configuration file to run the fcgi wrapper as a service.
Since it is a here document, copy it as it is
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
Register and start spawn-fcgi and php-fpm services.
systemctl enable nginx php-fpm spawn-fcgi
systemctl start nginx php-fpm spawn-fcgi
Make a hole in the firewall.
firewall-cmd --add-service=http --zone=public --permanent
firewall-cmd --reload
In addition, php-fpm is OK with the default setting file when the package is installed.
Create an index for each script.
Since it is a here document, copy it as it is
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
Script file
Since it is a here document, copy it as it is
cat <<'EOF' > /usr/share/nginx/html/php.php
<?php
print "<html><body>\n";
print "Hello PHP Script!<br>\n";
print "</body></html>\n";
?>
EOF
Operation check
# curl http://localhost/php.php
<html><body>
Hello PHP Script!<br>
</body></html>
Script file
Since it is a here document, copy it as it is
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
Operation check
# curl http://localhost/perl.pl
<html><body>
Hello Perl Script!<br>
</body></html>
Script file
Since it is a here document, copy it as it is
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
Operation check
# curl http://localhost/python.py
<html><body>
Hello Python Script!<br>
</body></html>
Script file
Since it is a here document, copy it as it is
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
Operation check
# curl http://localhost/ruby.rb
<html><body>
Hello Ruby Script!<br>
</body></html>
php-fpm is running with the settings as it is in the package installation.
:/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;
}
Recommended Posts