Using Perl with Apache under OS X El Capitan

If you've got Apache running on your Mac OS X system and want to be able to display the output of Perl programs, you need to remove the hash sign (#) from the following line in /etc/apache2/httpd.conf.

#LoadModule cgi_module libexec/apache2/mod_cgi.so

You will neeed to edit the file with a text editor, such as the TextEdit app found in the Applications directory, or GNU nano or vi. And you will need to run the editor with root, i.e., administrator privileges by using the sudo command in order to be able to save modifications to the file. E.g.:

sudo nano /etc/apache2/httpd.conf

After modifying the file, you will then need to restart Apache, which you can do with the command sudo apachectl restart. The default configuration for Apache allows Common Gateway Interface (CGI) scripts, such as Perl scripts, to be run from the /Library/WebServer/CGI-Executables directory. So you can place a Perl script with a .cgi or .pl extension in that directory and have it executed when you access it from a web browser. E.g., a simple test script containing the following lines named perltest.cgi or perltest.pl could be placed in that directory.

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

You should then be able to see "Hello, World!" displayed by the script if you access it from a web browser, e.g., http://localhost/cgi-bin/perltest.cgi. Note: if you don't remove the hash/pound sign from the beginning of the LoadModule cgi_module libexec/apache2/mod_cgi.so line, whenever you access a Perl script on the site, you will just see the code for the script itself displayed, i.e., it won't be executed, but will be displayed like it was just a text file.

If you want to run Perl scripts you will store in a cgi-bin directory that you will create for a virtual host as noted in Running an Apache web server under OS X El Capitan, you can create a cgi-bin directory where you will store your Perl scripts. E.g., if the document root for the website on the Mac OS X/macOS system is beneath a Documents/www directory within the current user account, I could create the following directory from a Terminal window.

$ mkdir ~/Documents/www/cgi-bin
$

I could then put a test script like the one above within it. Note, any Perl script you place in either the default directory or one you create for a virtual host will need to have its file system permissions set so that Apache has "execute" permission. Linux File Permissions explains the file permissions; they are the same for Mac OS X/macOS systems. You can change a files permissions so that all accounts on the system have read and execute permission and the owner has read, write, and execute permissions by setting the permissions on the file to 755. E.g., chmod 755 ~/Documents/www/cgi-bin/perltest.cgi. E.g., suppose the directory ~/Documents/www/cgi-bin/ has the following .cgi files in it where perltest.cgi has execute permissions, but perltest2.cgi does not.

$ ls -l -o ~/Documents/www/cgi-bin/*.cgi
-rwxr-xr-x  1 jasmith1  91 Feb  4 15:45 /Users/jasmith1/Documents/www/cgi-bin/perltest.cgi
-rw-r--r--  1 jasmith1  91 Feb  5 21:35 /Users/jasmith1/Documents/www/cgi-bin/perltest2.cgi
$

If I used http://myserver.example.com/cgi-bin/perltest.cgi, I would see the "Hello, World!" output of the script displayed. However, for http://myserver.example.com/cgi-bin/perltest2.cgi, I would get a "404 Not Found" error with the following displayed in the browser:

Advanced Mac OS X - Technical and Security Skills
Advanced Mac OS X
Technical and Security Skills
1x1 px

Not Found

The requested URL /cgi-bin/perltest2.cgi was not found on this server.

And I would see lines like the following ones in the access log for the website:

::1 - - [05/Feb/2017:21:34:57 -0500] "GET /cgi-bin/perltest.cgi HTTP/1.1" 200 23
::1 - - [05/Feb/2017:21:37:21 -0500] "GET /cgi-bin/perltest2.cgi HTTP/1.1" 404 219

I.e., I would see a 200 HTTP status code returned indicating the perltest.cgi page was retrieved successfully, whereas with the perltest2.cgi file, a 4xx client error code is returned, specifically the "404 Not Found" error code.

If I placed the perltest.cgi file in another directory e.g., ~/Documents/www/, which is the document root for the virtual host as specified in /private/etc/apache2/extra/httpd-vhosts.conf, it would not be executed; I would just see the lines in the file displayed as if it was a plain text file. Note: using the virtual hosts file requires that the hash sign (#) be removed from the Include /private/etc/apache2/extra/httpd-vhosts.conf line in the hosts.conf file. That is because the httpd.conf file allows scripts to be executed in a directory named cgi-bin with the ScriptAlias line in the following section of httpd.conf:

    #
    # ScriptAlias: This controls which directories contain server scripts.
    # ScriptAliases are essentially the same as Aliases, except that
    # documents in the target directory are treated as applications and
    # run by the server when requested rather than as documents sent to the
    # client.  The same rules about trailing "/" apply to ScriptAlias
    # directives as to Alias.
    #
    ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"

Related articles:

  1. Running an Apache web server under OS X El Capitan
  2. PHP for Apache on OS X El Capitan
  3. Using Python scripts with Apache on OS X El Capitan