How to run CGI-C in Mac?

What must I do to run CGI-C scripts in Mac?


We were required in class to use CGI for the server-side processing of HTML. And we use the C programming language.


Anyway, so what I did was that I made an HTML file in Documents with form action='/Library/WebServer/CGI-Executables/sample3.cgi', made a C file and compiled it in CGI (sample3.cgi). I saved this in /Library/WebServer/CGI-Executables but when I ran the HTML file and clicked some buttons, it opened the folder instead and did not execute the CGI file. My professor said something about setting up a web server first, but he didn't exactly tell me how, why and how it was going to help with anything. Can anyone tell me the best thing that I should do in step-by-step? Or if there was something wrong with the directories I used? Thaank you.


For additional info, I use 2013 MacBook Air, the gcc compiler for C and Xcode as my editor.

MacBook Air, OS X Mavericks (10.9.4)

Posted on Aug 29, 2014 4:33 AM

Reply
11 replies

Aug 29, 2014 6:25 AM in response to izzaclairej

Last time I did anything with CGI the executables went into the cgi-bin directory but I suppose that might have changed.


However as your instructor said this will only work if you have a web sever running on your system and you open the files through it. Just pointing the browser at the local html file and opening it will not work.


As for getting a web server running on Mavericks, it is not to difficult al the files are include in OS X however if you have not done it before it can be tricky. Have a look at Setting up a local web server on OS X


regards

Aug 30, 2014 1:51 PM in response to izzaclairej

The /Library/WebServer/ paths you're specifying are typical of OS X Server circa 10.6, and the analogous paths for OS X Server 10.9 Mavericks Server are under /Library/Server/Web. Usually /library/server/web/data/sites and /library/server/web/data/webapps, depending on what you're up to.


If you are running OS X Server here — if Server.app is installed and configured — then you generally don't want to install a second web server, as it'll conflict with the existing web server.


In the Advanced setting for the web site within Server.app, also enable CGI execution.

Aug 31, 2014 6:39 AM in response to izzaclairej

Is unlikely you have the server software running on the system if you have to ask, especially as you wrote you are doing this on a Mac Book Air. The server software is something that you would have to install and configure to use.


So if this is this your system did you install the server software? If it is the schools you will need to ask the System Admin if it is installed. But As I wrote it is highly unlikely that it is.


Once you follow the instructions in the link I provided above and have the basic HTTP server running post back and we can discuss getting CGI to work/ There will be some other changes you will need to make to the config files.


regards


Note there is a difference between the server MrHoffman wrote about and the basic HTTP server all OS X systems come with. In the first case that is separate software that must be purchased and installed. It turns the system into a true server. The HTTP server you need to test your CGI programs is the basic web sever that all OS X systems come with. It allows the system to respond to web requests.

Aug 31, 2014 4:17 PM in response to Frank Caggiano

Hello again, Frank!


I followed what the instructions in the link said. But when I reached that part where I have to uncomment the ff line: #LoadModule php5_module libexec/apache2/libphp5.so, I can't find it in my terminal. Should it appear after I enable my php? I would've opted to ask etresoft directly but I couldn't find any comment/reply button on the link.

Sep 1, 2014 5:45 AM in response to Frank Caggiano

Actually, the localhost already worked. I installed this web sharing app that somehow manually turns the web sharing preference on and off: http://clickontyler.com/web-sharing/ I don't know though if the result of this would just be the same as when I'll follow the link you gave (I actually followed the steps but it didn't work. I don't know why. TT^TT). Anyway, after this, I tried again with the CGI but it didn't work. CGI is still saved in /Library/WebServer/CGI-Executables/ and the HTML file in Documents. Is there a separate 'installation process' for CGI to work? Or is the directory (the folder/s where I save CGI or HTML files) I used at fault?


I'm sorry for taking your time with all my questions, but I'd be very grateful for all the help you can give.

Thank youu. 😀

Sep 2, 2014 7:08 AM in response to izzaclairej

Sorry been away for a few days.


If the local web server is running you should be able to access the cgi files by referencing them in your HTML document as


/cgi-bin/program_name


I know the folder is CGI-Executables but the httpd.conf file aliases that to cgi-bin so that is the name you use in the HTML code. Try that and see if it works.


Here is a simple perl cgi script


#! /usr/bin/perl

print "Content-type: text/html\n\n";

print "<html><body><h1>Hello World!";

print "</h1></body></html>\n";


Save that to CGI-Executables as test.pl chmod it to 755.

With the above installed you should be ale to open a web browser and enter

http://localhost/cgi-bin/test.pl

and the script should run.


Here is a simple html document to test it also


<html><body><h1>It WORKS! in sites</h1>

<FORM ACTION="/cgi-bin/test.pl"

METHOD="GET">

<DIV>Your input:<BR>

<INPUT NAME="data" SIZE="60" MAXLENGTH="80"><BR>

<INPUT TYPE="SUBMIT" VALUE="Send"></DIV>

</FORM>

</body></html>


When you press the submit button the cgi program will run.


As I wrote its been a while since I used CGI (most of this has been replaced by php) and I can;t remember if there needs to be any changes to the https.conf if that doesn't work post back and we'll see if the conf file needs to be modified.


regards

Sep 6, 2014 7:33 PM in response to Frank Caggiano

Hello Frank!


Sorry if I replied late. I was a bit busy with other school requirements. Just a question though, how do you copy it to CGI-Exec again? I tried sudo cp <filename> <address> where address is ~cgi-bin but it did not save in CGI-Exec. I also tried address = Library/WebServer/CGI-Excec but it said that there was no such file/directory. I did not use perl script since our professor said we'll be using C-cgi (where we write a C file and compile it as .cgi). Original C file is saved in My Docs. D:

Sep 18, 2014 4:59 AM in response to Frank Caggiano

Hello Frank!


I'm very sorry for the late reply (again). The program worked, though I used an external program MAMP to make it work. Basically, the concept/procedure is the same as what you said. I copied it on the default cgi-bin and htdocs of MAMP. This time around however, I did not have to chmod it or use other admin access codes. MAMP has its own localhost and stuffs so... everything's fine now (or at least for now). XD


Anyway, thank you very much for your patience and your help. I am really grateful for it. 😀

Best regards!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to run CGI-C in Mac?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.