Advertisement
metalx1000

C program cgi for Apache2 server

May 3rd, 2017
1,222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.26 KB | None | 0 0
  1. #setting Apache2 up for C cgi programs
  2. #install needed programs
  3. sudo apt-get install build-essential vim apache2
  4.  
  5. ##########create C code 'vim test.c'###########
  6. #include <stdio.h>
  7.  
  8. int main(){
  9.   printf("Content-type:text/plain\n\n");
  10.   printf("Hello, you are still learning C!!\n");
  11.  
  12.   return 0;
  13. }
  14. ################################################
  15. #compile code and put it in cgi folder for apache
  16. gcc test.c test
  17. sudo cp test /usr/lib/cgi-bin/
  18.  
  19. #enable cgi for apache and restart server
  20. sudo a2enmod cgi
  21. sudo service apache2 restart
  22.  
  23. ###################################another cgi example with user variables##################
  24. /*****************************************************************
  25. * C Programming in Linux (c) David Haskins 2008
  26. * chapter1_3.c *
  27. *****************************************************************/
  28. #include <stdio.h>
  29. int main(int argc, char *argv[]){
  30.   int i=0;
  31.   printf("Content-type:text/plain\n\n");
  32.   printf("Hello, you are still learning C!!\n");
  33.   printf("Number of arguments to the main function:%d\n", argc);
  34.   for(i=0;i<argc;i++){
  35.     printf("argument number %d is %s\n", i, argv[i]);
  36.   }
  37.   return 0;
  38. }
  39. ################################################################
  40. #usage
  41. http://localhost/cgi-bin/test?hello world
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement