11 Replies Latest reply on Feb 25, 2016 11:22 AM by FGT

    How to support cgi on lighttpd

    dtung

      I spend some time on enabling the cgi support on lighttpd, lighttpd is the default web server in Galileo BSP. Here is how I do it,  it might save you some trial and error time.

       

      1. By default, lighttpd doesn't enable cgi module, need to enable it in recipe.

       

      2. lighttpd recipe is in poky/meta/recipes-extended/lighttpd, copy a backup of the original recipe, open the recipe, add lighttpd-module-cgi to the section under RDEPENDS_${PN} as follows,

       

      RDEPENDS_${PN} += " \

                     lighttpd-module-access \

                     lighttpd-module-accesslog \

                     lighttpd-module-cgi \

                     lighttpd-module-indexfile \

                     lighttpd-module-dirlisting \

                     lighttpd-module-staticfile \

      "

       

      3. rebuild the package bitbake lighttpd or rebuild the whole image bitbake image-full (whichever you prefer, the only new file is mod_cgi.so to be put under /usr/lib on the Galileo board. I attached mod_cgi.so for your convenience).

       

      4. After the mod_cgi.so being put under your /usr/lib, you need to modify lightttp.conf (in Galileo board). Open /etc/lighttpd.conf enable mod-cgi by deleting the #, then adding the cgi.assign section as follows, then save and close the file.

       

      server.modules              = (

      #                               "mod_rewrite",

      #                               "mod_redirect",

      #                               "mod_alias",

                                      "mod_access",

      #                               "mod_cml",

      #                               "mod_trigger_b4_dl",

      #                               "mod_auth",

      #                               "mod_status",

      #                               "mod_setenv",

      #                               "mod_fastcgi",

      #                               "mod_proxy",

      #                               "mod_simple_vhost",

      #                               "mod_evhost",

      #                               "mod_userdir",

                                      "mod_cgi",

      #                               "mod_compress",

      #                               "mod_ssi",

      #                               "mod_usertrack",

      #                               "mod_expire",

      #                               "mod_secdownload",

      #                               "mod_rrdtool",

      #                               "mod_webdav",

                                      "mod_accesslog" )

       

      cgi.assign                 = ( ".pl"  => "/usr/bin/perl",

                                     ".cgi" => "/usr/bin/perl",

                                     ".rb"  => "/usr/bin/ruby",

                                     ".php" => "/usr/bin/php",

                                     ".py"  => "/usr/bin/python" )

       

      5. stop and start lighttpd by /etc/init.d/lighttpd restart

       

      6. use s script file to test the cgi setup. I put the following python script, hello.py, under /www/pages

       

      #!/usr/bin/python

       

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

      print '<html>'

      print '<head>'

      print '<title>Hello Word - First CGI Program</title>'

      print '</head>'

      print '<body>'

      print '<h2>Hello Word! This is my first CGI program</h2>'

      print '</body>'

      print '</html>'

       

      7. check it out with a browser open http://YOUR-GALILEO-IP/hello.py, if you see a hello world message, that's it.


        • 1. Re: How to support cgi on lighttpd
          BMEJ

          Hi dtung, i know this is old, but hopefully you're still around (or someone that can answer)...

           

          So I actually have to rebuild a custom yocto image to enable cgi on lighttpd?

           

          I have a lot into my current image and I'd really hate to start over again.  Correct me if I'm wrong here.

           

          I can't find a folder called cgi-bin.  I have made one in the www/pages/ directory but I'm not sure lighttpd is communicating with it.  I did change the lighttpd.conf file to enable cgi.

           

           

          tips appreciated

          • 2. Re: How to support cgi on lighttpd
            BMEJ

            I am not opposed to using any other method of executing a script on the server from the webpage.  php, python or perl?  If anybody can recommend the simplest approach I'd appreciate it.

            • 3. Re: How to support cgi on lighttpd
              FGT

              BMEJ wrote:

              
              

               

              I am not opposed to using any other method of executing a script on the server from the webpage.  php, python or perl?  If anybody can recommend the simplest approach I'd appreciate it.

              Actually, the simplest way I found is, in a full yocto for persistent sketches (SD Card Setup and Sketch Persistent for Boards and Kits):

              a) In the directory (create one)

              /www/

              create a script (mine is called startsrvr) with

               

              python -m CGIHTTPServer > pythonsrvr.out 2> pythonsrvr.err &

               

              and

               

              b) in the sketch

               

              void setup() 
              {
                ...
                system("cd /media/mmcblk0p1/www; ./startsrvr"); 
                ...
              

               

              and you'll have a CGI enabled HTTP server, which also executes .py CGI scripts. Take into account that /www and startsrvr are user chosen (mine, in this case) names. I have a

              /www/cgi-bin

              with several .py scripts.

               

              HTH,

               

              Fernando.

              • 4. Re: How to support cgi on lighttpd
                BMEJ

                I do have "full" yocto. 

                 

                So this line:  python -m CGIHTTPServer > pythonsrvr.out 2> pythonsrvr.err &

                 

                creates a script? 

                 

                And for 2):  Is that an arduino sketch? 

                 

                are you just executing scripts from an arduino sketch?

                 

                What I'm hoping to do is execute scripts on the server by clicking a button on my website.

                 

                Thanks for the help

                • 5. Re: How to support cgi on lighttpd
                  FGT

                  BMEJ wrote:

                   

                  So this line:  python -m CGIHTTPServer > pythonsrvr.out 2> pythonsrvr.err &

                   

                  creates a script?

                   

                  Sorry, I didn't explain correctly.

                   

                  I created a file

                        /www/startsrvr

                  containing

                        python -m CGIHTTPServer > pythonsrvr.out 2> pythonsrvr.err &

                  (the /www/startsrvr is a Linux script file, with a bash command, maybe I'm using too old terminology...)

                   

                  If you run the /www/startsrvr from the command line or from a sketch there will be "a CGI enabled HTTP server, which also executes .py CGI scripts" as I wrote in my previous post.

                  And for 2):  Is that an arduino sketch?

                   

                  are you just executing scripts from an arduino sketch?

                   

                  I execute Linux scripts from Arduino sketches (using "system"), but not python sketches, if that is what you are asking about... or keep asking, I'm not good at understanding/explaining...

                   

                  I choose to execute /www/startsrvr from an Arduino sketch because:

                  a) In this way (executing /www/startsrvr from the sketch) the CGI enabled server is executed as soon as Galileo boots (by the end of booting, actually).

                  b) I had to use other Arduino code (not python code), so I had to use an Arduino sketch anyway.

                   

                  You may execute /www/startsrvr (or its command line) in the way you decide.

                   

                  Python scripts are executed by the CGI enabled HTTP server.

                   

                  What I'm hoping to do is execute scripts on the server by clicking a button on my website.

                   

                   

                  Just as a step-by-step simple start, in a "full" yocto:

                  mkdir /www

                  cd /www

                  mkdir cgi-bin

                  # create a simple .py script in /www/cgi-bin

                  # create a simple .html with a form referring to the .py script

                  # run the server with the two following lines:

                  cd /www

                  python -m CGIHTTPServer > pythonsrvr.out 2> pythonsrvr.err &


                  and you should have the server "up and running". I had to use the Arduino sketch because, as I wrote above, I had to execute other (non python) Arduino code.

                   

                  If something fails, please let me know

                  HTH,

                   

                  Fernando.

                  • 6. Re: How to support cgi on lighttpd
                    BMEJ

                    thanks fgt,

                    Ive give this a shot again later,

                     

                     

                    question,  Does your directory have /www/pages/ directory?


                    That's where all mine is so it's a bit different than most references I've read.

                     

                    Thanks

                    • 7. Re: How to support cgi on lighttpd
                      FGT

                      BMEJ wrote:

                       

                      question,  Does your directory have /www/pages/ directory?



                      You can have the directory structure you want/need/decide to have, it's your setting.

                       

                      I had only a /www directory with a few (one or two, maybe) .html pages, and I started the python HTTP server in the same directory so that I didn't have to configure many things. All the .py scripts (maybe 3 or 4, it was a very simple proof of concept example application) where in the /www/cgi-bin just for "tradition", nothing more.

                       

                      I suggest you try the simple first (as in the step-by-step simple start above) and add complexity as you understand what happens and how things work.

                       

                      HTH,

                       

                      Fernando.

                      • 8. Re: How to support cgi on lighttpd
                        BMEJ

                        Ok I've tried it.  It still doesn't run the py script.  My browser just tries to download it to my PC. 

                         

                        here is the result of running that line:

                         

                        root@galileo:/www# python -m CGIHTTPServer > pythonsrvr.out 2> pythonsrvr.err &

                        [1] 746

                         


                        I then tried to run my .py script from the web (www.address.com/cgi-bin/script.py) but it just tries to download it.

                         

                        then if I hit enter back on putty:

                         

                        root@galileo:/www#

                        [1]+  Done(255)               python -m CGIHTTPServer > pythonsrvr.out 2> pythonsrvr.err

                        • 9. Re: How to support cgi on lighttpd
                          FGT

                          Sorry, I forgot that the default port in which the python server listens to requests is 8000, so you should use something like

                           

                          www.address.com:8000

                           

                          I don't have enough time right now, but I'll try to make a more specific example. Anyways, adding the port number should work

                           

                          BTW, please post the contents of the files pythonsrvr.out and pythonsrvr.err

                           

                          HTH,

                           

                          Fernando.

                          • 10. Re: How to support cgi on lighttpd
                            BMEJ

                            Oh no worries FGT, you've helped enough.  Once I get it going I'll report back.  Thanks again.

                            • 11. Re: How to support cgi on lighttpd
                              FGT

                              Ok, thanks for letting me know.

                               

                              Maybe you should start another thread, we're a little bit off topic from the original question at this point...

                               

                              Fernando.