Ben
Ben

Reputation: 7802

PHP Extension: Works from command line, but not in httpd (Apache) server

I'm getting the strangest problem with my PHP extension. When I use it from the command line, like so:

php -r '$mc = new MyClass("foo"); echo $mc->getField();'

it prints out "foo" as expected.

However, if I try to do the same thing from within my index.php, I get a class not found error.

Fatal error: Class 'MyClass' not found in /var/www/html/index.php on line 12

How could this be happening? I've even set httpd to run as the same user as myself when running the php -r command.

My index.php looks like so:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <p>
 <?php 
  error_reporting(E_ALL); 
  ini_set('display_errors', '1');
  $mc = new MyClass("foo"); 
  echo $mc->getField();
 ?> 
 </p>
 </body>
</html>

The php portion of my httpd.conf looks like so:

# PHP Configuration for Apache
#
# Load the apache module
#
LoadModule php5_module modules/libphp5.so
#
# Cause the PHP interpreter handle files with a .php extension.
#
<Files *.php>
SetOutputFilter PHP
SetInputFilter PHP
LimitRequestBody 9524288
</Files>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php

And obviously my php.ini is set up correctly, because my php -r command works. What could possibly be causing this issue?

Upvotes: 3

Views: 2766

Answers (3)

Tarcisio J&#250;nior
Tarcisio J&#250;nior

Reputation: 1202

I have got the same issue and solved restoring the SELinux context for the extension files, which should be ideal instead of disabling SELinux, as the following example:

# restorecon -R -v /usr/lib64/php
# restorecon -R -v /etc/php.d

Upvotes: 1

Ben
Ben

Reputation: 7802

It turns out that the issue was due to SELinux. If it is turned off like so:

echo 0 >/selinux/enforce

Everything magically works.

Upvotes: 1

Phillip B Oldham
Phillip B Oldham

Reputation: 19395

The php executable you're running on the command line is different to the libphp5.so library you're running via Apache.

Put the following into a file, upload it to your server and view it via the web and you'll probably see that your module hasn't been loaded:

<?php phpinfo(); ?>

This file will tell you what modules have been loaded, which php.ini file is being used, and lots of other information. It should help you diagnose your problem. You'll probably need to set an Apache configuration parameter to either load the correct php.ini file or load the .so file of your module directly.

Upvotes: 3

Related Questions