Personal computing discussed

Moderators: renee, SecretSquirrel, notfred

 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

can't get cgi-bin working

Mon Jul 20, 2009 2:40 pm

I've created the directory /var/www_cgi-bin

In httpd.conf I have added the directive: ScriptAlias /cgi-bin/ /var/www_cgi-bin/

Then I did: /etc/init.d/apache2 restart

I created: hello.pl

The contents of which are:
#!/usr/bin/perl
print 'content-type: "text/html"' , "\n";
print "Hello Perl!\n";

When I run the file from the command line it works fine, even with "use strict;"

When I enter the URL: http://localhost/cgi-bin/hello.pl
I get 404 not found.

Anyone know where I'm going wrong?

Edit: this is on ubuntu 804, apache 2.2, perl5
 
titan
Grand Gerbil Poohbah
Posts: 3376
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains
Contact:

Re: can't get cgi-bin working

Mon Jul 20, 2009 2:58 pm

Did you make hello.pl executable?

chmod 655 hello.pl

I'm sure there's a better permissions to make it executable, but that works well enough for me.
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

Re: can't get cgi-bin working

Mon Jul 20, 2009 3:13 pm

Yes, forgot to mention that I had already done: chmod +x hello.pl

So, that is not the problem.
 
titan
Grand Gerbil Poohbah
Posts: 3376
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains
Contact:

Re: can't get cgi-bin working

Mon Jul 20, 2009 4:25 pm

Is Apache being started with "-D SUEXEC"?

I don't know how Ubuntu handles its initialization scripts, but that's the option Gentoo has. That option enables running CGI scripts.
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: can't get cgi-bin working

Mon Jul 20, 2009 9:07 pm

Ubuntu's default Apache configuration can be a bit strange if you're not familiar with how they do it. The "main" site is actually set up as a virtual host out of the box; the upshot of this is that putting your configuration directives in the top level httpd.conf file probably isn't doing what you expect (since it doesn't affect the virtual host).

CGI scripts are enabled by default, with Apache's /cgi-bin aliased to the /usr/lib/cgi-bin directory. If you want to point it elsewhere, edit the ScriptAlias block in the /etc/apache2/sites-available/default file, and restart Apache.

Edit: You're probably better off just putting your own scripts in /usr/lib/cgi-bin (or symlinking to them from there). Other optional Ubuntu packages expect /usr/lib/cgi-bin to be the default script directory, so you may cause other problems down the road if you change it. Alternatively, you could configure a second virtual host with its own script area... or add a block to the default site configuration file along the lines of:
ScriptAlias /mike-cgi/ /home/mike/cgi-bin/
<Directory "/home/mike/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
</Directory>

Whereby you have a second CGI folder (which in this example sits at /mike-cgi/ in Apache's document tree, and is an alias to the cgi-bin directory in user mike's home area), with your custom scripts in it.

Hope that made sense... if not, keep asking questions until we get it sorted! :D
Nostalgia isn't what it used to be.
 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

Re: can't get cgi-bin working

Wed Jul 22, 2009 7:43 am

JBI, I owe you a bag of hops. Thanks! That was probably in the Ubuntu documentation? Shame on me. I was looking in Apache's documentation.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: can't get cgi-bin working

Wed Jul 22, 2009 8:56 am

Don't feel too bad about not finding it; the Ubuntu-specific Apache documentation is not all that well organized. The way Ubuntu does their Apache configuration files is inherited from Debian; our Debian guru here at the office explained it to me a few months ago. As I noted above, it is different from the default Apache way of doing things; if you were trying to configure it based on the Apache documents I can understand why you were a bit confused.

You may want to take a look at the man pages for a2ensite and a2enmod, and poke around in the /etc/apache2 area until you understand how Debian has reorganized things. It's actually pretty slick, and makes the Web server configuration a lot more modular. But it can be rather confusing at first.
Nostalgia isn't what it used to be.
 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

Re: can't get cgi-bin working

Thu Jul 23, 2009 9:19 am

Still fail do I. I put a simple hello.pl script in /usr/lib/cgi-bin/ and get "Internal server error: The server encountered an internal error or misconfiguration and was unable to complete your request." The file has execute permissions.
 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

Re: can't get cgi-bin working

Thu Jul 23, 2009 9:30 am

here is my apache2.conf file (I have made no changes to it, it is all default):

#
# Based upon the NCSA server configuration files originally by Rob McCool.
#
# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See http://httpd.apache.org/docs/2.2/ for detailed information about
# the directives.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
# consult the online docs. You have been warned. 
#
# The configuration directives are grouped into three basic sections:
#  1. Directives that control the operation of the Apache server process as a
#     whole (the 'global environment').
#  2. Directives that define the parameters of the 'main' or 'default' server,
#     which responds to requests that aren't handled by a virtual host.
#     These directives also provide default values for the settings
#     of all virtual hosts.
#  3. Settings for virtual hosts, which allow Web requests to be sent to
#     different IP addresses or hostnames and have them handled by the
#     same Apache server process.
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path.  If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so "/var/log/apache2/foo.log"
# with ServerRoot set to "" will be interpreted by the
# server as "//var/log/apache2/foo.log".
#

### Section 1: Global Environment
#
# The directives in this section affect the overall operation of Apache,
# such as the number of concurrent requests it can handle or where it
# can find its configuration files.
#

#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# NOTE!  If you intend to place this on an NFS (or otherwise network)
# mounted filesystem then please read the LockFile documentation (available
# at <URL:http://httpd.apache.org/docs-2.1/mod/mpm_common.html#lockfile>);
# you will save yourself a lot of trouble.
#
# Do NOT add a slash at the end of the directory path.
#
ServerRoot "/etc/apache2"

#
# The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
#
#<IfModule !mpm_winnt.c>
#<IfModule !mpm_netware.c>
LockFile /var/lock/apache2/accept.lock
#</IfModule>
#</IfModule>

#
# PidFile: The file in which the server should record its process
# identification number when it starts.
# This needs to be set in /etc/apache2/envvars
#
PidFile ${APACHE_PID_FILE}

#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 300

#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 15

##
## Server-Pool Size Regulation (MPM specific)
##

# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_worker_module>
    StartServers          2
    MaxClients          150
    MinSpareThreads      25
    MaxSpareThreads      75
    ThreadsPerChild      25
    MaxRequestsPerChild   0
</IfModule>

# These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

#
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives.  See also the AllowOverride
# directive.
#

AccessFileName .htaccess

#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
</Files>

#
# DefaultType is the default MIME type the server will use for a document
# if it cannot otherwise determine one, such as from filename extensions.
# If your server contains mostly text or HTML documents, "text/plain" is
# a good value.  If most of your content is binary, such as applications
# or images, you may want to use "application/octet-stream" instead to
# keep browsers from trying to display binary files as though they are
# text.
#
DefaultType text/plain


#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
HostnameLookups Off

# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog /var/log/apache2/error.log

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn

# Include module configuration:
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf

# Include all the user configurations:
Include /etc/apache2/httpd.conf

# Include ports listing
Include /etc/apache2/ports.conf

#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
# If you are behind a reverse proxy, you might want to change %h into %{X-Forwarded-For}i
#
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

#
# Define an access log for VirtualHosts that don't define their own logfile
CustomLog /var/log/apache2/other_vhosts_access.log vhost_combined

#
# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
#
# Some examples:
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html
#

#
# Putting this all together, we can internationalize error responses.
#
# We use Alias to redirect any /error/HTTP_<error>.html.var response to
# our collection of by-error message multi-language collections.  We use
# includes to substitute the appropriate text.
#
# You can modify the messages' appearance without changing any of the
# default HTTP_<error>.html.var files by adding the line:
#
#   Alias /error/include/ "/your/include/path/"
#
# which allows you to create your own set of files by starting with the
# /usr/share/apache2/error/include/ files and copying them to /your/include/path/,
# even on a per-VirtualHost basis.  The default include files will display
# your Apache version number and your ServerAdmin email address regardless
# of the setting of ServerSignature.
#
# The internationalized error documents require mod_alias, mod_include
# and mod_negotiation.  To activate them, uncomment the following 30 lines.

#    Alias /error/ "/usr/share/apache2/error/"
#
#    <Directory "/usr/share/apache2/error">
#        AllowOverride None
#        Options IncludesNoExec
#        AddOutputFilter Includes html
#        AddHandler type-map var
#        Order allow,deny
#        Allow from all
#        LanguagePriority en cs de es fr it nl sv pt-br ro
#        ForceLanguagePriority Prefer Fallback
#    </Directory>
#
#    ErrorDocument 400 /error/HTTP_BAD_REQUEST.html.var
#    ErrorDocument 401 /error/HTTP_UNAUTHORIZED.html.var
#    ErrorDocument 403 /error/HTTP_FORBIDDEN.html.var
#    ErrorDocument 404 /error/HTTP_NOT_FOUND.html.var
#    ErrorDocument 405 /error/HTTP_METHOD_NOT_ALLOWED.html.var
#    ErrorDocument 408 /error/HTTP_REQUEST_TIME_OUT.html.var
#    ErrorDocument 410 /error/HTTP_GONE.html.var
#    ErrorDocument 411 /error/HTTP_LENGTH_REQUIRED.html.var
#    ErrorDocument 412 /error/HTTP_PRECONDITION_FAILED.html.var
#    ErrorDocument 413 /error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
#    ErrorDocument 414 /error/HTTP_REQUEST_URI_TOO_LARGE.html.var
#    ErrorDocument 415 /error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
#    ErrorDocument 500 /error/HTTP_INTERNAL_SERVER_ERROR.html.var
#    ErrorDocument 501 /error/HTTP_NOT_IMPLEMENTED.html.var
#    ErrorDocument 502 /error/HTTP_BAD_GATEWAY.html.var
#    ErrorDocument 503 /error/HTTP_SERVICE_UNAVAILABLE.html.var
#    ErrorDocument 506 /error/HTTP_VARIANT_ALSO_VARIES.html.var



# Include of directories ignores editors' and dpkg's backup files,
# see README.Debian for details.

# Include generic snippets of statements
Include /etc/apache2/conf.d/

# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/


This is a totally clean install of apache2 on a clean install of Ubuntu I am working with - in a virtual machine.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: can't get cgi-bin working

Thu Jul 23, 2009 10:20 am

You need to have a blank line to tell Apache where the headers end and the actual content begins. Modify your script so that it outputs an extra newline after the content type header.
Nostalgia isn't what it used to be.
 
titan
Grand Gerbil Poohbah
Posts: 3376
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains
Contact:

Re: can't get cgi-bin working

Thu Jul 23, 2009 5:14 pm

Yup, the first two lines of any Perl script to be displayed in a browser need to look like so:

#!/usr/bin/perl
print "Content-type: text/html \n\n";


It looks like you have a monolithic configuration file going on. There is a line you need to add to httpd.conf:
AddHandler cgi-script .cgi .pl

Really, this could appear anywhere, but I have several different sites that use Perl scripts that it just made sense to add that in the server config context.


And then make sure you have permissions allowed for scripts to be run in a directory. Here's an example from my machine that I use to run scripts before I submit them to my school for grading:
<VirtualHost *:80>
  ServerName learnsql
  DocumentRoot /home/titan/www/LearnSQL
  <Directory /home/titan/www/LearnSQL>
    Order deny,allow
    Allow from localhost
    Deny from all
    Options ExecCGI
  </Directory>
</VirtualHost>

Pay special attention to "Options ExecCGI". If that line isn't stated explicitly, you'll never get a script to run within that dirctory and it'll only be served as a plain text file. It doesn't have to be a virtual host either, you could just remove the virtual host tags, the ServerName and place the remaining lines directly into httpd.conf.
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

Re: can't get cgi-bin working

Thu Jul 23, 2009 6:02 pm

Wonderful guys, it's working! I had everything proper except: there needs to be two "/n" at the end of the content-type statement (just like Titian said - thanks dude) and I only had one.

Hot dog, now I can hack some Perl!

Edit: Titian, FYI, I didn't have to add any of those directives you mentioned, all I had to do (beyond putting the script in the proper folder and making sure it was executable) was get the content-type statement done proper.
 
titan
Grand Gerbil Poohbah
Posts: 3376
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains
Contact:

Re: can't get cgi-bin working

Thu Jul 23, 2009 8:08 pm

I'm sure the directives were somewhere in the files that are "Incude"'d into the httpd.conf.

Glad to know it's working now. I can't tell you how many times that second newline stumped me.

My solution: have a &print_header subroutine that handles it automatically.

So now, the start of my files look like this:
#!/usr/bin/perl
require "libcgi.pl";
&print_header("titan\'s Example");
# Other lines of code
&print_footer;
exit;


And libcgi.pl looks like this:
# Sets the content type and starts the HTML.
# $_[0] is the page title.
sub print_header {
    print "Content-type: text/html \n\n";
    print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n";
    print "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n";
    print "<head>\n";
    print "<title>$_[0]</title>\n";
    print "<link rel=\"stylesheet\" type=\"text/css\" href=\"linkto/ff.css\" />\n";
    print "</head>\n";
    print "<body>\n";
}

#Finishes the HTML. This function should be the last line before "exit;".
sub print_footer {
    print "\n</body>\n";
    print "<\html>";
}

With some other things in there for my commonly used stuff that are way more sensitive than what I just posted. (And, the stuff I've posted is actually much neater than what I actually have in my script. I might get around to really cleaning it up one day.)
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On