How to Configure Apache to Use a Specific PHP Version on macOS (Switching from PHP 5.2.6 to 5.2.8)


1 views

When you run php --version in terminal, it shows PHP 5.2.8 (your newly compiled version), but Apache serves pages using PHP 5.2.6. This happens because:

  • Terminal uses the PHP binary in your $PATH
  • Apache loads PHP via the libphp5.so module specified in httpd.conf

The key line in your httpd.conf:

LoadModule php5_module libexec/apache2/libphp5.so

This is a relative path. To find the absolute location:

apachectl -t -D DUMP_MODULES 2>&1 | grep php
# Or search the entire system:
sudo find / -name "libphp5.so" 2>/dev/null

After compiling PHP 5.2.8, you should have a new libphp5.so at:

/usr/local/php5/libphp5.so

Modify your httpd.conf to point to the new location:

LoadModule php5_module /usr/local/php5/libphp5.so

After making changes:

sudo apachectl configtest  # Check for syntax errors
sudo apachectl restart     # Restart Apache

Create a test file (phpinfo.php) in your web root:

<?php
phpinfo();
?>

Permission Problems:

sudo chmod 755 /usr/local/php5/libphp5.so
sudo chown root:wheel /usr/local/php5/libphp5.so

Module Conflicts: Ensure no duplicate LoadModule directives exist in:

  • /etc/apache2/httpd.conf
  • /etc/apache2/extra/httpd-php.conf (if exists)

For newer macOS versions, consider:

brew install php@5.2
brew link --force php@5.2
brew services restart httpd

When you compile a new PHP version but Apache continues serving the old one, it's typically because Apache is loading a different libphp5.so than your CLI environment. On macOS, this often stems from:

# Default Apache PHP module path (10.5 Leopard)
/usr/libexec/apache2/libphp5.so
# vs custom compiled PHP path
/usr/local/php5/libphp5.so

First identify which module Apache is actually loading:

httpd -M | grep php
# Or for full path:
sudo lsof -Pni | grep libphp

For your case where httpd.conf shows:

LoadModule php5_module libexec/apache2/libphp5.so

This is relative to Apache's server root. The absolute path would be:

/usr/libexec/apache2/libphp5.so

You have two technical approaches:

Method 1: Replace the System PHP Module

# Backup original
sudo mv /usr/libexec/apache2/libphp5.so /usr/libexec/apache2/libphp5.so.bak

# Symlink your compiled version
sudo ln -s /usr/local/php5/libphp5.so /usr/libexec/apache2/libphp5.so

# Restart Apache
sudo apachectl restart

Method 2: Direct LoadModule Path Update

Edit /etc/apache2/httpd.conf:

# Change from:
# LoadModule php5_module libexec/apache2/libphp5.so
# To absolute path:
LoadModule php5_module /usr/local/php5/libphp5.so

Create a test file (phpinfo.php):

<?php
phpinfo();
?>

After restarting Apache, check:

# CLI version
php -v

# Web version
curl http://localhost/phpinfo.php | grep "PHP Version"

If modules still aren't loading, check php.ini locations:

# For CLI
php --ini

# For Apache
Add this to your phpinfo.php:
<?php
echo "Loaded ini: " . php_ini_loaded_file() . "\n";
print_r(php_ini_scanned_files());
?>

Ensure your php.ini contains:

extension=pdo_mysql.so