Configure Perl-FastCGI on CentOS 7 Nginx Server - CentLinux

Latest

Friday, 30 August 2019

Configure Perl-FastCGI on CentOS 7 Nginx Server

Configure Perl-FastCGI on CentOS 7 Nginx Server

Perl is a famous scripting language. It is mostly used to write scripts for Linux operating systems. Usually, Perl is not used for web development, however, there are situations in which, we have to host Perl scripts on a web server. Although Apache HTTP server has good native support for Perl language with straight forward configurations. But, we are using Nginx web server in this tutorial with Perl-FastCGI.

Nginx web server does not has native support for Perl language. Therefore, we have to use a Perl-FastCGI process manager to add the Perl language support in our Nginx web server.

In this article, we will configure a Nginx web server on CentOS 7 and the add Perl-FastCGI support using spawn-fcgi package.

Reading Advise:

 

This Article Provides:

     

    Environment Specification:

    We have provisioned a CentOS 7 virtual machine with following specifications.

    • CPU - 3.4 Ghz (2 Cores)
    • Memory - 2 GB
    • Storage - 20 GB
    • Operating System - CentOS 7.6
    • Hostname - nginx-01.example.com
    • IP Address - 192.168.116.197 /24

     

    Installing Nginx on CentOS 7:

    Connect with nginx-01.example.com using ssh as root user.

    Nginx is not available in standard yum repositories, therefore, we are installing EPEL (Extra Packages for Enterprise Linux) yum repository.

    [root@nginx-01 ~]# yum install -y epel-release Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.ges.net.pk * extras: mirrors.ges.net.pk * updates: mirrors.ges.net.pk Resolving Dependencies --> Running transaction check ---> Package epel-release.noarch 0:7-11 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: epel-release noarch 7-11 extras 15 k Transaction Summary ================================================================================ Install 1 Package Total download size: 15 k Installed size: 24 k Downloading packages: epel-release-7-11.noarch.rpm | 15 kB 00:02 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : epel-release-7-11.noarch 1/1 Verifying : epel-release-7-11.noarch 1/1 Installed: epel-release.noarch 0:7-11 Complete!

    Build cache for EPEL yum repository.

    [root@nginx-01 ~]# yum makecache fast Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile epel/x86_64/metalink | 6.9 kB 00:00 * base: mirrors.ges.net.pk * epel: my.fedora.ipserverone.com * extras: mirrors.ges.net.pk * updates: mirrors.ges.net.pk base | 3.6 kB 00:00 epel | 5.4 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 (1/3): epel/x86_64/group_gz | 88 kB 00:03 (2/3): epel/x86_64/updateinfo | 998 kB 00:07 (3/3): epel/x86_64/primary_db | 6.8 MB 01:07 Metadata Cache Created

    Install Nginx web server using yum command.

    [root@nginx-01 ~]# yum install -y nginx ... Installed: nginx.x86_64 1:1.12.2-3.el7 Dependency Installed: dejavu-fonts-common.noarch 0:2.33-6.el7 dejavu-sans-fonts.noarch 0:2.33-6.el7 fontconfig.x86_64 0:2.13.0-4.3.el7 fontpackages-filesystem.noarch 0:1.44-8.el7 gd.x86_64 0:2.0.35-26.el7 gperftools-libs.x86_64 0:2.6.1-1.el7 libX11.x86_64 0:1.6.5-2.el7 libX11-common.noarch 0:1.6.5-2.el7 libXau.x86_64 0:1.0.8-2.1.el7 libXpm.x86_64 0:3.5.12-1.el7 libjpeg-turbo.x86_64 0:1.2.90-6.el7 libxcb.x86_64 0:1.13-1.el7 nginx-all-modules.noarch 1:1.12.2-3.el7 nginx-filesystem.noarch 1:1.12.2-3.el7 nginx-mod-http-geoip.x86_64 1:1.12.2-3.el7 nginx-mod-http-image-filter.x86_64 1:1.12.2-3.el7 nginx-mod-http-perl.x86_64 1:1.12.2-3.el7 nginx-mod-http-xslt-filter.x86_64 1:1.12.2-3.el7 nginx-mod-mail.x86_64 1:1.12.2-3.el7 nginx-mod-stream.x86_64 1:1.12.2-3.el7 perl.x86_64 4:5.16.3-294.el7_6 perl-Carp.noarch 0:1.26-244.el7 perl-Encode.x86_64 0:2.51-7.el7 perl-Exporter.noarch 0:5.68-3.el7 perl-File-Path.noarch 0:2.09-2.el7 perl-File-Temp.noarch 0:0.23.01-3.el7 perl-Filter.x86_64 0:1.49-3.el7 perl-Getopt-Long.noarch 0:2.40-3.el7 perl-HTTP-Tiny.noarch 0:0.033-3.el7 perl-PathTools.x86_64 0:3.40-5.el7 perl-Pod-Escapes.noarch 1:1.04-294.el7_6 perl-Pod-Perldoc.noarch 0:3.20-4.el7 perl-Pod-Simple.noarch 1:3.28-4.el7 perl-Pod-Usage.noarch 0:1.63-3.el7 perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 perl-Socket.x86_64 0:2.010-4.el7 perl-Storable.x86_64 0:2.45-3.el7 perl-Text-ParseWords.noarch 0:3.29-4.el7 perl-Time-HiRes.x86_64 4:1.9725-3.el7 perl-Time-Local.noarch 0:1.2300-2.el7 perl-constant.noarch 0:1.27-2.el7 perl-libs.x86_64 4:5.16.3-294.el7_6 perl-macros.x86_64 4:5.16.3-294.el7_6 perl-parent.noarch 1:0.225-244.el7 perl-podlators.noarch 0:2.5.1-3.el7 perl-threads.x86_64 0:1.87-4.el7 perl-threads-shared.x86_64 0:1.43-6.el7 Complete!

    Enable and start nginx service.

    [root@nginx-01 ~]# systemctl enable --now nginx.service Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

    Allow HTTP service in Linux firewall.

    [root@nginx-01 ~]# firewall-cmd --permanent --add-service=http success [root@nginx-01 ~]# firewall-cmd --reload success

    Browse URL http://nginx-01.example.com using a client's browser.

    01-nginx-perl-fastcgi-default-homepage

    Nginx web server has been installed on CentOS 7.

     

    Installing spawn-fcgi on CentOS 7:

    Unlike Apache HTTP Server, Nginx do not spawn FastCGI processes. Therefore, we required to install spawn-fcgi to process Perl scripts.

    [root@nginx-01 ~]# yum install -y spawn-fcgi Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.ges.net.pk * epel: my.fedora.ipserverone.com * extras: mirrors.ges.net.pk * updates: mirrors.ges.net.pk Resolving Dependencies --> Running transaction check ---> Package spawn-fcgi.x86_64 0:1.6.3-5.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: spawn-fcgi x86_64 1.6.3-5.el7 epel 18 k Transaction Summary ================================================================================ Install 1 Package Total download size: 18 k Installed size: 27 k Downloading packages: spawn-fcgi-1.6.3-5.el7.x86_64.rpm | 18 kB 00:02 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : spawn-fcgi-1.6.3-5.el7.x86_64 1/1 Verifying : spawn-fcgi-1.6.3-5.el7.x86_64 1/1 Installed: spawn-fcgi.x86_64 0:1.6.3-5.el7 Complete!

    Now install fcgiwrap package using yum command.

    [root@nginx-01 ~]# yum install -y fcgiwrap Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.ges.net.pk * epel: sg.fedora.ipserverone.com * extras: mirrors.ges.net.pk * updates: mirrors.ges.net.pk Resolving Dependencies --> Running transaction check ---> Package fcgiwrap.x86_64 0:1.1.0-9.20181108git99c942c.el7 will be installed --> Processing Dependency: libfcgi.so.0()(64bit) for package: fcgiwrap-1.1.0-9.20181108git99c942c.el7.x86_64 --> Running transaction check ---> Package fcgi.x86_64 0:2.4.0-25.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: fcgiwrap x86_64 1.1.0-9.20181108git99c942c.el7 epel 21 k Installing for dependencies: fcgi x86_64 2.4.0-25.el7 epel 47 k Transaction Summary ================================================================================ Install 1 Package (+1 Dependent package) Total download size: 68 k Installed size: 128 k Downloading packages: (1/2): fcgi-2.4.0-25.el7.x86_64.rpm | 47 kB 00:02 (2/2): fcgiwrap-1.1.0-9.20181108git99c942c.el7.x86_64.rpm | 21 kB 00:03 -------------------------------------------------------------------------------- Total 21 kB/s | 68 kB 00:03 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : fcgi-2.4.0-25.el7.x86_64 1/2 Installing : fcgiwrap-1.1.0-9.20181108git99c942c.el7.x86_64 2/2 Verifying : fcgiwrap-1.1.0-9.20181108git99c942c.el7.x86_64 1/2 Verifying : fcgi-2.4.0-25.el7.x86_64 2/2 Installed: fcgiwrap.x86_64 0:1.1.0-9.20181108git99c942c.el7 Dependency Installed: fcgi.x86_64 0:2.4.0-25.el7 Complete!

    Edit spawn-fcgi configuration file.

    [root@nginx-01 ~]# vi /etc/sysconfig/spawn-fcgi

    and add following line therein.

    OPTIONS="-u nginx -g nginx -a 127.0.0.1 -p 9001 -P /var/run/spawn-fcgi.pid -- /usr/sbin/fcgiwrap"

    Enable and start spawn-fcgi service.

    [root@nginx-01 ~]# systemctl enable --now spawn-fcgi spawn-fcgi.service is not a native service, redirecting to /sbin/chkconfig. Executing /sbin/chkconfig spawn-fcgi on

    Create a directory as Nginx document root.

    [root@nginx-01 ~]# mkdir /var/www

    Create a sample Perl script in Nginx document root directory.

    [root@nginx-01 ~]# vi /usr/share/nginx/html/index.cgi

    and add following lines of code.

    #!/usr/bin/perl print "Content-type: text/html\n\n"; print "<html><body><h1>Hello World!"; print "</h1></body></html>\n";

    Adjust file permissions for index.cgi.

    [root@nginx-01 ~]# chmod +x /usr/share/nginx/html/index.cgi

    Add our Nginx server configuration file as follows.

    [root@nginx-01 ~]# vi /etc/nginx/default.d/default.conf

    and add following directive therein.

    index index.cgi; location ~* \.(pl|cgi)$ { gzip off; include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9001; fastcgi_index index.cgi; }

    Restart nginx service.

    [root@nginx-01 ~]# systemctl restart nginx

    Set SELinux boolean, so the Nginx can communicate to spawn-fcgi.

    [root@nginx-01 ~]# setsebool -P httpd_can_network_connect on

    Browse URL http://nginx-01.example.com/ in a client's browser.

    02-nginx-perl-fastcgi-helloworld-script

    Our Perl script has been executed successfully. We have successfully configured Perl-FastCGI using Nginx on CentOS 7 server.

    No comments:

    Post a comment