Run a C Program on a Linux Server

14,479

You need to compile your C program. Linux is distributed with the GNU C Compiler (gcc). In a pinch, you can compile your program with the command line:

gcc -o myprog myprog.c

This means you need shell access. In the comments, people have suggested running shell commands via PHP with the system function. It is possible that your PHP installation has disabled this command for security reasons.

You should really ask your host whether they can provide shell access via SSH.

If your host can do that, you can run a terminal session (using PuTTY if you have Windows locally, or command-line ssh on most other systems).

So you upload your file (via FTP, SCP, or whatever), then compile it. All good so far.

Now you need your web server to allow it to run. Generally you can't execute binary files from a your web server's document root. You need to put the binary into the configured cgi-bin directory. This usually resides one directory level up from your document root.

On my system, my documents are in:

/srv/httpd/htdocs

And my cgi-bin are in:

/srv/httpd/cgi-bin

Generally the cgi-bin directory will be aliased so that it appears to be in your document root. So you could run your script via http://mysite.com/cgi-bin/myprog. You do need to have CGI enabled on your webserver. On Linux, your web server will probably be Apache.

Also, the server needs permission to run the file. That means execute permissions for the appropriate user. In a pinch:

chmod 0770 /srv/httpd/cgi-bin/myprog
chown apache:apache /srv/httpd/cgi-bin/myprog

Example is for my particular system.

This all assumes that you want to run your program and get output via HTTP. If that's not the case, simply having shell access should be enough. If you didn't understand any of this answer, then I recommend you do some serious reading about Linux, networking, and HTTP.

Here is an Apache guide for getting CGI working: http://httpd.apache.org/docs/2.2/howto/cgi.html

Share:
14,479
ManOx
Author by

ManOx

Updated on July 18, 2022

Comments

  • ManOx
    ManOx almost 2 years

    This question I'm sure has been answered, I honestly don't know how to ask it via search though. So please excuse my lack of knowledge as this one of the only place I really have a lack of knowledge in the world of Computer Science.

    How can I/ Is it possible, to run a C program on a Hosted Server. To where I could go to http://mysite.com/myspecialcprogram.c and it would run? Or better yet, to what extent can I use a high level language like C to program for my server?

    It should also be noted that I have a Dedicated Linux box running apache. So I have full access.

  • ManOx
    ManOx over 11 years
    I did understand it, however I got this error when I tried the chown command: "chown: changing ownership of `blah/blah/blah' Operation not permitted". Also, I got the 500 internal server error when going to mysite.com/cgi-bin/myprog. I'm guessing that's because of the problem above.
  • paddy
    paddy over 11 years
    Don't worry about ownership for now. I am assuming you have located your cgi-bin directory, compiled your program, copied the compiled binary into there, and that the user apache and group apache exist on your system. This varies between distributions. I use Slackware, so if your webserver keeps stuff elsewhere, you'll need to go looking.
  • ManOx
    ManOx over 11 years
    I use plesk. And actually, I'm not sure what you mean when you say that I have a apache user... I'm currently logged in as the admin for my plesk setup. And yes I've compiled it, and put it in my cgi-bin folder
  • paddy
    paddy over 11 years
    One step at a time. Forget the apache user. You can get around the permissions for now by allowing everyone to execute your file (chmod a+rx myprog).
  • ManOx
    ManOx over 11 years
    hmm... still getting the 500 error when I test the webpage. when I run that command however, it doesn't give me any error
  • paddy
    paddy over 11 years
    It could be that CGI is disabled in Apache. I just did a quick google, and the internet is full of information for you. Try working through this: httpd.apache.org/docs/2.2/howto/cgi.html -- don't forget you'll need to resart Apache after reconfiguring it. On slackware I can do /etc/rc.d/rc.httpd restart - maybe plesk has something similar.
  • ManOx
    ManOx over 11 years