C++/C as CGI

From HelioHost Wiki
Jump to: navigation, search

Heliohost users are able to use compiled C/C++ executables as CGI by following the instructions listed below. However, in order to use C or C++ code with your hosting account you will need to compile the code before it can be executed.

Create the Source Code

There are many valid ways that you can get your source code uploaded onto your account, such as, FTP, Web Disk, or the built in cPanel File Manager. Whichever way you choose to access your files, the first step is to create a new source code file located in your cgi-bin directory:

/home/username/public_html/cgi-bin/test.c

  #include <stdio.h>
  int main(void) {
    printf("Content-Type: text/html\n\n");
    printf("Hello world.\n\n");
    return 0;
  }

Compile the Source Code

This portion of the guide can be rather dangerous because there is a high chance of your account being suspended for cron abuse if you don't set everything up correctly. You are limited to two (2) cron jobs per day. If you don't understand what you're doing it might be best to ask for help on our forum or our chat.

If you feel comfortable enough to continue, then log into your cPanel, and navigate to the "Cron jobs" section. The goal here is to set up a cron job to run once, and then you can delete the job once it has completed.

The simplest way to get the cron job scheduled correctly is to use a simple php script to check what the time is currently on the server. If you already know what time it is where the server is located then you can safely skip this step. Create a new php file in your public_html:

/home/username/public_html/time.php

   <?php
   echo date( "H:i", time());

Once you have the php file created you can check what time it is on the server by visiting your script in a browser, such as, username.heliohost.org/time.php

  1. On the cron job screen of cPanel type a number a few minutes in the future into the box labeled "Minute:". The reason you want to set it in the future is to give yourself enough time to fill out the rest of the form, and get it submitted before the execution time.
  2. In the box labeled "Hour:" type the hour returned by your php time script. If you aren't using the php time script this is just the hour in US Pacific timezone with 24 hour format.
  3. In the boxes labeled "Day:", "Month:", and "Weekday:" just put a *
  4. In the box labeled "Command:" type
   gcc /home/username/public_html/cgi-bin/test.c -o /home/username/public_html/cgi-bin/test.cgi

Make sure you get the path correct based on your own account, and where you created the source code file. It might also be a good idea to change the box labeled "Email:" to something different than the default, because you will be emailed any error messages that the command may output. These emails can be very useful in your troubleshooting, but keep in mind that even if your cron job isn't successful it still counts towards your limit of two runs per day. If everything looks good, and the minute field is still at least a couple minutes in the future go ahead and click "Add New Cron Job."

The next step is to wait for the executable to be created. The easiest way is to open the directory where the output from your cron job is supposed to be written and just refresh occasionally until you see the file appear. Keep in mind that cron jobs don't always run exactly when you schedule them, but they can vary slightly depending on various server factors. Once the executable is created it is recommended that you delete the cron job to prevent it from running over and over.

Setting Permissions

Your newly created output file will most likely not have executable permissions so you will need to change that before it will work. Depending on which method you choose to upload and edit files on your account the steps to change the permissions will vary, but once the file is set to 755 it will be executable and can be viewed at username.heliohost.org/cgi-bin/test.cgi

This should give you a good idea of how to get started using C++/C as CGI. Obviously, this is a very simple example, but this can be a very powerful tool for creating your website hosted on Heliohost.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox
Google AdSense