User avatar
richiepp
Posts: 150
Joined: Wed Dec 19, 2012 4:56 pm

Apache cgi-bin config

Sun May 15, 2016 1:55 pm

Hey All,
So I've got my LAMP server up and running, it was relatively easy as there are many tutorials on the web. But things become difficult when it comes to properly configuring cgi-bin. I want to use .sh .cgi and .py files. If anyone knows of a good tutorial please pass it along.

My understanding is that 3 things need to be done to get it working.
  • configure apache2 to point to the cgi-bin and execute file extensions, in my case; .sh, .cgi and .py
    change ownership of /var/www/html to www-data
    Set proper file and folder permissions (chmod)
1) edit the Apache config file located at

Code: Select all

/etc/apache2/sites-enabled/000-default.conf
Here's what mine currently looks like

Code: Select all

<VirtualHost *:80>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
        AddHandler cgi-script .py
        AddHandler cgi-script .cgi
        AddHandler cgi-script .sh
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
</Directory>

        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
So this gets Apache pointing to /usr/lib/cgi-bin but the files don't execute. I think this is because of what group the files and folders belong to. I've set up my /var/www/html directory to be owned by www-data. I've added the pi user to www-data. I've done some chmod-ing but nothing seems to work.

What do I need to do to get my cgi-bin properly configured?

Thanks
Rich

User avatar
richiepp
Posts: 150
Joined: Wed Dec 19, 2012 4:56 pm

Re: Apache cgi-bin config

Mon May 16, 2016 11:47 am

I'm still looking for an answer to this. Does anyone know how to get cgi scripts running from Apache?

Thanks
Rich

User avatar
DougieLawson
Posts: 42214
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Apache cgi-bin config

Mon May 16, 2016 6:48 pm

Take a look at /etc/apache2/conf-available/serve-cgi-bin.conf enable that with sudo a2enconf serve-cgi-bin restart Apache2 and a program called /usr/lib/cgi-bin/foo.cgi should run as http://localhost/cgi-bin/foo.cgi

Here's a sample program to test things with

Code: Select all

#!/usr/bin/python
import os
import cgi
cgi.test()
cgi.print_environ()
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

User avatar
richiepp
Posts: 150
Joined: Wed Dec 19, 2012 4:56 pm

Re: Apache cgi-bin config

Tue May 17, 2016 3:39 pm

Hmmm,
I ran

Code: Select all

sudo a2enconf serve-cgi-bin
and it said it was already running.

I currently have /var/www/html owned by www-data. I also have /usr/lib/cgi-bin owned by www-data.

Rght now when I call the foo.cgi you suggest above I get the following Apache error

Code: Select all

You don't have permission to access /cgi-bin/foo.py on this server.
Note that I tried this with two different file extensions .py and .cgi.
So I changed the permissions of /usr/lib/cgi-bin and foo.cgi to 777 and I still get he same error.

still stiuck :(

knute
Posts: 808
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas

Re: Apache cgi-bin config

Wed May 18, 2016 4:18 am

You also need to put cgid.conf and cgid.load in /etc/apache2/mods-enabled.

sudo a2enmod cgid

I would leave the ownership of /usr/lib/cgi-bin or /var/www/html as root:root. The cgi-bin programs I would chmod 750 and have them owned by www-data:www-data. Your html files can be owned by pi if you want.

User avatar
richiepp
Posts: 150
Joined: Wed Dec 19, 2012 4:56 pm

Re: Apache cgi-bin config

Wed May 18, 2016 12:21 pm

ok, that got me a little closer. Now I'm getting a different error message. This time from the Apache2 log

Code: Select all

10.0.0.9 - - [18/May/2016:07:20:17 -0500] "GET /cgi-bin/on.py HTTP/1.1" 500 803 "http://10.0.0.12/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.10$
And here is the error code displayed in the web browser

Code: Select all

Internal Server Error 500

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

knute
Posts: 808
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas

Re: Apache cgi-bin config

Wed May 18, 2016 2:34 pm

Try taking all the edits you did to 000-default.conf out. All of the cgi-bin info is already in serve-cgi-bin.conf. Maybe you are having a conflict there. Also try using the script that Dougie Lawson posted. It works fine on my Pi.

User avatar
richiepp
Posts: 150
Joined: Wed Dec 19, 2012 4:56 pm

Re: Apache cgi-bin config

Thu May 19, 2016 11:48 am

Thanks Knute! that was it!

klintkrossa
Posts: 83
Joined: Tue Nov 10, 2015 3:06 pm

Re: Apache cgi-bin config

Fri Dec 04, 2020 10:20 pm

This worked for me

Code: Select all

sudo chmod -R 775 /var/www/html/
sudo chmod -R 775 /usr/lib/cgi-bin/
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

Return to “Other programming languages”