CGI Script served as Text

Hi, I haven’t been able to find a solution for this in the forum that has resolved my issue. I have a simple perl cgi script that I would like to run, but it gets rendered as text. Here’s what I know:

  • I’d like this to run in public_html
  • my script is chmod 0755
  • Apache has CGI mod enabled
  • Under services, configure website, I added content handler: cgi-script with extension cgi

My directives:
Options -Indexes +IncludesNOEXEC +SymLinksIfOwnerMatch +ExecCGI
allow from all
AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
Require all granted
AddType application/x-httpd-php .php
AddHandler fcgid-script .php
AddHandler fcgid-script .php7.2
AddHandler cgi-script cgi
FCGIWrapper /home/websitename/fcgi-bin/php7.2.fcgi .php
FCGIWrapper /home/websitename/fcgi-bin/php7.2.fcgi .php7.2

Don’t feel afraid to point the obvious out to me, I appreciate the help.

I think you have done all correctly, including added +ExecCGI option but just missing a . before declaring cgi extension in the handler. Note, you can do it for multiple files as well, like:

<FilesMatch "\.(cgi|pl)$">
AddHandler cgi-script .cgi .pl

… my script is chmod 0755

Aside from it, your script must be owned with the same uid/gui.

1 Like

Hi thank you so much for response, I was working on this light night and I got this work, but I will also my the correction by adding the full-stop (.) in the directive! I fixed by adding this apache conf: serve-cgi-bin.conf:

<Directory “/home/website/public_html”>
Options +ExecCGI
Addhandler cgi-script .cgi .pl

I am not sure if this is the ‘correct’ way, but my script works. Is there a better method for getting CGI perl scripts working on Apache?


Put them in cgi-bin, where it’s already done for you. :wink:

Also, beware that in Virtualmin 7 (and Virtualmin 6 for CentOS 8) we’ll be deprecating CGI execution modes, in the interest of getting rid of having to ship a custom httpd package and simplifying usage and reducing the possibility of configuration mistakes/mixups (one of the most common complaints about Virtualmin comes down to confusion over how to run applications and people mixing up various tutorials they’ve found on the web and getting into a state where Virtualmin is trying to setup different and mutually incompatible execution models or configurations…that’ll still happen, because folks like to experiment, but at least we’ll reduce the number of ways it can go wrong).

CGI may stick around in Debian and Ubuntu for another year or two, since they provide a suexec-custom package, but CentOS 8 does not. So…you’ll need to run CGI apps some other way (preferably under an application server, but I guess you could also accept the security risk of running CGI as the Apache user or the complexity of putting the code into /var/www so it can run under suexec).

Edit: Here’s some options for modernizing (incrementally) a Perl CGI app:

Hi Dan, sorry for a late reply, but I spent a lot of time looking at your response to get everything set up properly. Thanks so much for this, everything you listed was very helpful!