Run a C Program on a Linux Server

前端 未结 2 1509
庸人自扰
庸人自扰 2021-02-03 12:21

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 h

2条回答
  •  花落未央
    2021-02-03 12:24

    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

提交回复
热议问题