This is how to get CGI Perl scripts executing correctly on CentOS 7.
I'm leaving this here as it seems a lot of the resources on the internet don't combine the steps and leave people like me very confused.
In short this, is what needs to be done.
- Install software.
- Create your test CGI file.
- Ensure the CGI module is loaded. Inside
httpd.conf
.
- Change the directory settings in
httpd.conf
.
- Change permissions to allow for CGI to execute.
Install and configure software
sudo yum update
sudo yum install httpd
sudo yum install perl perl-CGI
sudo systemctl start httpd.service
sudo systemctl enable httpd.service
Create your test CGI file
Even following these steps all the way through, I never once got a CGI script inside /var/www/cgi-bin
to load without modifying the web root inside httpd.conf
. Instead I decided to just activate CGI in another directory.
On my server, I want the web root html
to hold my CGI files. This is /var/www/html/hello.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n"; # This is mandatory.
print "<h2>Hello world!</h2>";
Ensure the CGI module is loaded. Inside httpd.conf
You can do an easy check for this via the command.
grep -n "LoadModule" /etc/httpd/conf/httpd.conf
I could see that no CGI modules were specified and I confirmed their existence inside the modules folder via:
find /etc/httpd/modules/ -iname "*cgi*"
This is the one I am after:
/etc/httpd/modules/mod_cgi.so
Let's add that to the /etc/httpd/conf/httpd.conf
file:
LoadModule cgi_module modules/mod_cgi.so
I believe that you need to load the module in a different way in Ubuntu, keep that in mind.
Change the directory settings in httpd.conf
Before restarting httpd
we have to change one more thing inside /etc/httpd/conf/httpd.conf
:
<Directory "var/www/html">
Options +ExecCGI
AddHandler cgi-script .cgi .pl
</Directory>
According to many Googlings, you may have to also modify this part of the httpd.conf
file so that it points to the same directory as above "var/www/html"
:
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
I didn't modify mine, it didn't seem to make a difference.
Change permissions to allow for CGI to execute
And this one got me stuck a lot! Don't overlook this!
You need to tell your server that these CGI scripts are allowed to be executed as programs.
chmod 705 *.cgi
Or you can target individual CGI scripts.
chmod 705 hello.cgi
(Some people on the internet have said that chmod 755
and 777
might also work.)
Now restart httpd
like so:
sudo systemctl restart httpd.service
At this point my CGI scripts are rendering into HTML correctly.
They are being served from the web root like this: http://<IP>/hello.cgi
Please feel free to add any more information that might help others.