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:
- Nginx HTTP Server - Fourth Edition (PAID LINK) by Packt Publishing
- Learning Perl: Making Easy Things Easy and Hard Things Possible (PAID LINK) by O'Reilly Media
Table of Contents:
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.
# yum install -y epel-release
Build cache for EPEL yum repository.
# 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.
# yum install -y nginx
Enable and start nginx service.
# 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.
# firewall-cmd --permanent --add-service=http success # firewall-cmd --reload success
Browse URL http://nginx-01.example.com using a client's browser.
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.
# yum install -y spawn-fcgi
Now install fcgiwrap package using yum command.
# yum install -y fcgiwrap
Edit spawn-fcgi configuration file.
# 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.
# 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.
# mkdir /var/www
Create a sample Perl script in Nginx document root directory.
# 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.
# chmod +x /usr/share/nginx/html/index.cgi
Add our Nginx server configuration file as follows.
# 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.
# systemctl restart nginx
Set SELinux boolean, so the Nginx can communicate to spawn-fcgi.
# setsebool -P httpd_can_network_connect on
Browse URL http://nginx-01.example.com/ in a client's browser.
Our Perl script has been executed successfully. We have successfully configured Perl-FastCGI using Nginx on CentOS 7 server.