Problems with executing CGI  programs

Posted on 2005-04-27
Last Modified: 2013-12-25

I have Linux fedora 3 installed with apache webserver and mysql database .I wrote the following script to connect to teh mysql database :


use DBI;

#### main program
   $dbpath = "dbi:mysql:database=maxsponder;host=localhost";
   $dbh = DBI->connect($dbpath, "user", "pass")
     or die "Can't open database: $DBI::errstr";
print "Content-type:text/html", "\n\n";
print "Successful MySQL DB Connection";
# Create the statement.
$stmt = "SELECT * FROM adfarmerusage";
print "OK";
# Prepare and execute the SQL query
$sth = $dbh->prepare($stmt);
print "OK";
# Get the first record
# If more than one record will be returned put
# the fetchrow in a while loop
@record = $sth->fetchrow();
# Get the value of the first field returned.
$telephone = $record[0];
print $telephone;
# Clean up the record set and the database connection    
exit 0;

I changed teh permissions to this file and ran under my user account (./dbconn.cgi) and gave results  but when I copied over the file to /var/www/cgi-bin , and ran it there it did not work and in the browser it gave an error message "Internal Server Error" . I am doing something wrong here , why doesnt it work ?Any clues
Question by:janu_s
    LVL 48

    Expert Comment

    Remember that when it is running as a CGI process, it will be running as the 'nobody' or 'apache' user.

    If you add a:

    use CGI::Carp qw(fatalsToBrowser);

    in your script and run it again from your browser, I'll bet you'll see the error message

    Can't open database: ........

    Author Comment

    Yes , now I get the Cant open .... error , but how come under my account , that is when I just type ./dbconn.cgi in my local directory not under /var/www/cgi-bin  I get the connection passed through and the same file when tried with ./ when in teh directory /var/www/cgi-bin does not work , why?

    Author Comment

    And this the error message Can't open database: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13) .
    LVL 51

    Expert Comment

    the user running httpd has another .my.cnf as you when logged in
    check your ~/.my.cnf what you find as socket there, and the httpd's user aslo (which seems to be /var/lib/mysql/mysql.sock)

    If you cannot change the system (httpd.conf or .my.cnf) then you can try following in your code:

    $dbh->sock('/tmp/mysql.sock'); # <== write the proper path to the socket here
    LVL 2

    Expert Comment

    change this lines

    $dbpath = "dbi:mysql:database=maxsponder;host=localhost";
       $dbh = DBI->connect($dbpath, "user", "pass")
       or die "Can't open database: $DBI::errstr";


    $dbh = DBI->connect('DBI:mysql:maxsponder','user','pass') || die "Connect Error: $DBI::errstr";

    I think the problem is with the 'localhost' definition. Your name to your client is not 'localhost'. So don't use it.
    LVL 51

    Expert Comment

    disagreed, sorry.
    http:#13882056 does not give a solution, just a hint how you can *probably* get more diagnostic messages easyly
    http:#14059283 also gives no solition, the assumption about localhost is very vage 'cause localhost is most likely defined in any /etc/hosts file and the initial question does not tell us that this is a *special* installation (for example a virtual server like some web hosters use)

    according the information given by the questioner, I still believe that my suggestion http:#13887225 is most closest to a solution

    If the questioner does not return, I'd suggest a PAQ with 0 points and no refund.
    LVL 2

    Expert Comment

    Here is another solution i got

    Go to System settings
    On the SELinux tab choose Transition at the list box
    Select "Disable SELinux protection for httpd daemon"
    Save/Close the configuration

    Go to /var/lib/mysql and delete the mysql.sock file.

    From the Services panel, stop the httpd and mysqld daemons.
    Start the mysqld daemon
    Start the httpd daemon

    All should be working fine.

    Accepted Solution

    PAQed with no points refunded (of 50)

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now