From the initial state of CentOS8 to running php python perl ruby with nginx

Introduction

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.

Target device and environment

Verification environment (2020/8/15)

Work content

Pre-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

Package installation

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.

Content file

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

For PHP

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>

For Perl

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>

For Python

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>

For Ruby

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>

Other configuration files

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

From the initial state of CentOS8 to running php python perl ruby with nginx
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
The story of migrating from home server (MariaDB + Java) to AWS (DynamoDB + Python + PHP) with reduced monthly cost
How to correctly upgrade the software when building Linux (CentOS) with Vagrant ~ Using the example of upgrading from Python 2.7 to Python 3.6 ~
The wall of changing the Django service from Python 2.7 to Python 3
Learn Nim with Python (from the beginning of the year).
From buying a computer to running a program with python
Extract images and tables from pdf with python to reduce the burden of reporting
How to return to the command from the state where you can not enter interactive mode with python of git bash
Cheating from PHP to Python
About Perl, Python, PHP, Ruby
Switch from python2.7 to python3.6 (centos7)
I tried to find the entropy of the image with python
I tried using the Python library from Ruby with PyCall
Try to automate the operation of network devices with Python
Get the source of the page to load infinitely with python.
How to scrape stock prices of individual stocks from the Nikkei newspaper website with Python
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
[python, ruby] fetch the contents of a web page with selenium-webdriver
Create folders from '01' to '12' with python
[Python] Try to graph from the image of Ring Fit [OCR]
Dynamic proxy with python, ruby, PHP
I want to extract an arbitrary URL from the character string of the html source with python
Existence from the viewpoint of Python
I want to output the beginning of the next month with Python
Output the contents of ~ .xlsx in the folder to HTML with Python
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
The latest NGINX is an application server! ?? I measured the benchmark of NGINX Unit with PHP, Python, Go! !!
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
Try to calculate the position of the transmitter from the radio wave propagation model with python [Wi-Fi, Beacon]
I tried to improve the efficiency of daily work with Python
PhytoMine-I tried to get the genetic information of plants with Python
Summary of how to write if statements (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby)
Summary of how to write increment / decrement (Scala, Java, Rust, C, C ++, Go, PHP, Perl, Python, Ruby, JavaScript)
[Completed version] Try to find out the number of residents in the town from the address list with Python
How to crop the lower right part of the image with Python OpenCV
How to write the correct shebang in Perl, Python and Ruby scripts
Try to image the elevation data of the Geographical Survey Institute with Python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
I tried to get the authentication code of Qiita API with Python.
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
[Introduction to Python] What is the method of repeating with the continue statement?
Realize PHP / Python generator with Golang / Ruby
Learning notes from the beginning of Python 1
Check the existence of the file with python
Connecting from python to MySQL on CentOS 6.4
The basics of running NoxPlayer in Python
Try calling Python from Ruby with thrift
Learning notes from the beginning of Python 2
The road to compiling to Python 3 with Thrift
How to install python3 with docker centos
Try to measure the position of the object on the desk (real coordinate system) from the camera image with Python + OpenCV
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
Let's simulate the transition of infection rate with respect to population density with python
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
Python version (PHP to Python) that deletes the subsequent character string from the specified character string (extension)
When using PyQtGraph with Python Pyside, pay attention to the order of import
I tried to easily visualize the tweets of JAWS DAYS 2017 with Python + ELK
SSH login to the target server from Windows with a click of a shortcut