Run CGI scripts within the user-level document root of an Apache webserver on Mac OS X 10.11 El Capitan

1. Download and install Sublime Text if you haven’t already

2. Launch Terminal, (Applications/Utilities), and follow the commands below

ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

You can now run Sublime Text from the command line using the sublime command

4. Start Apache

sudo apachectl start

5. Create a Sites folder if you don’t already have one

mkdir ~/Sites

6. Create a configuration file for your username

sudo touch /etc/apache2/users/username.conf

Make sure to replace username with your account’s username, if you don’t know your account’s username type whoami the Terminal prompt

7. Open this file with Sublime Text

sudo sublime /etc/apache2/users/username.conf

Make sure to replace username with your username

8. Add the following lines to the newly-created file:

<Directory "/Users/username/Sites/">
	Options Indexes MultiViews SymLinksIfOwnerMatch Includes ExecCGI
	DirectoryIndex index.html index.cgi
	Require all granted
	AddHandler cgi-script .cgi
</Directory>

Make sure to replace username with your username

9. Save all your changes

10. Change the permissions of this file

chmod 644 username.conf

Make sure to replace username with your username

11. Open httpd.conf with Sublime Text

sudo nano /etc/apache2/httpd.conf

12. Make sure the following lines are uncommented in this file:

LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule authz_core_module libexec/apache2/mod_authz_core.so
LoadModule cgi_module libexec/apache2/mod_cgi.so
LoadModule userdir_module libexec/apache2/mod_userdir.s
Include /private/etc/apache2/extra/httpd-userdir.conf

13. Save all your changes

14. Open httpd-userdir.conf with Sublime Text

sudo sublime /etc/apache2/extra/httpd-userdir.conf

15. Uncomment the following line:

Include /private/etc/apache2/users/*.conf

16. Restart Apache for the new files to be read

sudo apachectl restart
http://localhost/~username/

Make sure to replace username with your username

You should only see a directory tree like structure if the folder is empty.

18. Create a helloworld folder in ~/Sites/

mkdir ~/Sites/helloworld

19. Write and compile the following C program:

#include <stdio.h>

int main(int argc, const char * argv[]) {
    printf("Content-Type: text/plain;charset=us-ascii\n\n");
    printf("Hello, World!\n\n");
    return 0;
}

20. Copy the compiled binary to ~/Sites/helloworld/ and add the .cgi extension

cp helloworld ~/Sites/helloworld/helloworld.cgi

Make sure this program has the .cgi extension

21. Change the permissions of this program to allow execution

chmod +x ~/Sites/helloworld/*.cgi
http://localhost/~username/helloworld/helloworld.cgi

Make sure to replace username with your username

23. That’s all!

You should see the output as “Hello, World!”