Samir Parikh · Blog · Git


Originally published on 14 May 2022

One of of the things I like about the Perl programming language is how easy it is to get simple web apps up and running using the Apache web server and CGI (which, admittedly, is an old and outdated technology). Most of my limited experience with running a web site involves using Nginx so in this post, I thought I’d document how I installed and configured Apache on FreeBSD to run my CGI scripts.

Install Apache

Install the package:

$ sudo pkg install apache24

Enable Apache as a service:

$ sudo sysrc apache24_enable="YES"
apache24_enable:  -> YES

Start the Apache service:

$ sudo service apache24 start
Performing sanity check on apache24 configuration:
Syntax OK
Starting apache24.

Verify that the service is running:

$ sudo service apache24 status
apache24 is running as pid 2360.

If you have a domain name and have properly configured your nameservers and DNS settings to point to your server, go to that URL and you should see a simple page with the “It works!” message.

Configure Apache

We now need to edit the main configuration file, /usr/local/etc/apache24/httpd.conf.

The first thing we need to do is to enable the mod_ssl module by uncommenting the line #LoadModule ssl_module libexec/apache24/mod_ssl.so.

Next, we need to enable and configure our virtual host which Certbot will require for our site. To do this, we need to uncomment the line #Include etc/apache24/extra/httpd-vhosts.conf.

In order to run CGI scripts, we need to enable the mod_cgi module by uncommenting the line #LoadModule cgi_module libexec/apache24/mod_cgi.so.

Finally, we can enable the mod_rewrite module to allow us to redirect HTTP to HTTPS. We do this by uncommenting the line #LoadModule rewrite_module libexec/apache24/mod_rewrite.so.

We can save and close the httpd.conf file now.

Now we need to modify the default virtual host configuration file to replace the example domain with our own domain. We do this by replacing the existing two existing VirtualHost blocks with our own virtual host block in the /usr/local/etc/apache24/extra/httpd-vhosts.conf file:

<VirtualHost *:80>
    ServerAdmin your_email@your_domain.com
    DocumentRoot "/usr/local/www/apache24/data/your_domain.com"
    ServerName your_domain.com
    ServerAlias www.your_domain.com
    ErrorLog "/var/log/your_domain.com-error_log"
    CustomLog "/var/log/your_domain.com-access_log" common
</VirtualHost>

Finally, we can create the root directory to store our HTML files we want to serve. The path has to match the one specified in the DocumentRoot directive in the /usr/local/etc/apache24/extra/httpd-vhosts.conf file we configured above:

$ sudo mkdir /usr/local/www/apache24/data/your_domain.com

Change the permission of that folder so that the Apache service, running as the www user, can access it:

$ sudo chown -R www:www /usr/local/www/apache24/data/your_domain.com

Copy over the index.html file into our new document root directory:

$ sudo cp /usr/local/www/apache24/data/index.html /usr/local/www/apache24/data/your_domain.com/

Obtain Let’s Encrypt Certificate

Install the Certbot package:

$ sudo pkg install py38-certbot py38-certbot-apache

Obtain the certificate:

$ sudo certbot --apache -d your-domain.com -d www.your-domain.com

If successful, Certbot will have created a new file, /usr/local/etc/apache24/extra/httpd-vhosts-le-ssl.conf, which contains the names and locations of your Let’s Encrypt certificates.

Test the certificate renewal process:

$ sudo certbot renew --dry-run

In order to automatically renew the certificates, add this line to /etc/periodic.conf (you may have to create this file):

weekly_certbot_enable="YES"

Create and Run CGI Script

Now we can create our CGI script. Within the /usr/local/www/apache24/cgi-bin directory, create a file called hello.cgi:

#!/usr/local/bin/perl -wT

print <<END_OF_HTML;
Content-type: text/html

<HTML>
<HEAD>
    <TITLE>Welcome to this Site!</TITLE>
</HEAD>
<BODY>
<H1>About this Site</H1>
<HR>
<p>This is a site where I am trying to learn about CGI programming using Perl.</p>
</BODY>
</HTML>
END_OF_HTML

Save it and change the permissions to allow it to be executed by the Apache server:

$ sudo chmod 755 /usr/local/www/apache24/cgi-bin/hello.cgi

If all went well, you should now be able to go to https://your-domain.com/cgi-bin/hello.cgi and see the results of your script over HTTPS!

If you get any errors, you can try to validate the syntax of your Apache configuration by running sudo apachectl -t. In addition, you may have to restart the Apache service by running sudo service apache24 restart. If you continue to get errors, check the contents of the your-domain.com-access_log and your-domain.com-error_log log files in /var/log.

For more information on installing and securing Apache on FreeBSD, please check out these excellent articles over at Digital Ocean.