keepcalculator

06 Numbers and Powers

Fix Apache Not Restarting After Installing PHP

Card No. 06·32028 · Updated · Standard formula, shown below

After installing PHP, Apache often fails to restart with an error like Syntax error on line ... of ... or Cannot load ... into server. This guide walks you through the most common causes and fixes, from module path issues to missing libraries, so you can get your web server running again quickly.

What You’ll Need

  • Access to the terminal or command line on your server (SSH or local).
  • Root or sudo privileges.
  • The exact PHP version you installed (e.g., 8.2, 8.3).
  • Your operating system (Ubuntu/Debian, CentOS/RHEL, or Windows).

Step-by-Step

  1. Check the exact error message. Run sudo apachectl configtest (on most Linux systems) or httpd -t (on Windows or some Unix). This will show the precise syntax error that prevents Apache from starting. Write down the file path and line number.
  2. Open the PHP module file. The error often points to a .so file (Linux) or .dll (Windows) that Apache cannot load. On Debian/Ubuntu, the file is usually /etc/apache2/mods-available/phpX.Y.load. On CentOS/RHEL, it’s typically /etc/httpd/conf.modules.d/XX-php.conf. On Windows, it’s in the conf/httpd.conf under LoadModule.
  3. Verify the module path exists. Check if the path in that file points to an actual file. For example, on Ubuntu, the line might be LoadModule php_module /usr/lib/apache2/modules/libphp.so. Run ls -l /usr/lib/apache2/modules/libphp.so to confirm. If the file is missing, reinstall the PHP module package (e.g., sudo apt install libapache2-mod-php).
  4. Check for missing shared libraries. If the file exists but Apache still fails, the module may depend on libraries not installed. Run ldd /path/to/php_module.so (Linux) to see if any dependencies are listed as “not found”. Install the missing packages (e.g., sudo apt install libonig-dev or sudo yum install libxml2-devel).
  5. Ensure PHP and Apache are compatible. PHP modules are compiled against a specific Apache version (e.g., Apache 2.4). If you upgraded Apache recently, you may need to reinstall the PHP module to match. Check with apache2 -v and php -v.
  6. Check for duplicate or conflicting LoadModule lines. Sometimes an old PHP module is left enabled. On Debian/Ubuntu, run sudo a2dismod phpX.Y (old version) and sudo a2enmod phpX.Y (new version). On CentOS, comment out duplicate lines in the conf file.
  7. Look for syntax errors in PHP config files. If the error mentions a .ini file, run php -i | grep 'Loaded Configuration File' to find it, then run php -l /path/to/php.ini to check for syntax errors. Fix any typos.
  8. Restart Apache. After making changes, run sudo apachectl configtest again. If it says “Syntax OK”, start Apache with sudo systemctl restart apache2 (or httpd on RHEL/Windows).

Common Mistakes

  • Forgetting to enable the module. Just installing PHP doesn’t always enable it. On Debian/Ubuntu, you must run sudo a2enmod phpX.Y.
  • Using the wrong PHP version path. The LoadModule line must point to the exact version you installed. A path like libphp7.so when you installed PHP 8.2 will fail.
  • Mixing 32-bit and 64-bit binaries. Ensure your PHP module matches Apache’s architecture. On Windows, a 64-bit Apache needs a 64-bit PHP DLL.
  • Editing the wrong config file. On some systems, there are multiple httpd.conf files. Use apachectl -V to see the actual SERVER_CONFIG_FILE path.

Tips

  • Always run sudo apachectl configtest before restarting. It catches errors without taking down your server.
  • Keep a backup of your config files: sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak.
  • Use the package manager to install PHP modules instead of manually compiling. It handles dependencies and paths automatically.
  • On Windows, use the Apache Monitor tool to test config and restart. It shows errors in a dialog box.

Troubleshooting

If the above steps don’t resolve the issue:

  • Check the Apache error log. On Linux, it’s usually /var/log/apache2/error.log or /var/log/httpd/error_log. On Windows, it’s in the Apache installation directory under logs/error.log. The log often has more detail than the config test.
  • Try a minimal config. Temporarily comment out the PHP LoadModule line. If Apache restarts, the problem is definitely with PHP. Then re-add it and test again.
  • Reinstall PHP completely. Sometimes a partial installation leaves broken files. On Debian/Ubuntu: sudo apt purge php* then sudo apt install php libapache2-mod-php. On CentOS: sudo yum remove php* then reinstall.
  • Check for SELinux or AppArmor. On CentOS, SELinux might block the module. Run sudo setenforce 0 to test (temporary). On Ubuntu, check AppArmor status with sudo aa-status.
  • Verify the PHP module is actually a valid shared object. Run file /path/to/module.so; it should say “ELF 64-bit LSB shared object”. If it says “data”, the file is corrupt.

Frequently Asked Questions

Why does Apache say “Cannot load … into server”?

This means Apache cannot find or read the PHP module file. Check the path, file permissions, and that the file exists. Also ensure the module is compiled for the same Apache version and architecture.

What does “Syntax error on line …” mean?

It means Apache found an invalid directive in its config files. The error message includes the file and line number. Open that file and look for typos, missing quotes, or incorrect paths.

I installed PHP via a package manager, but Apache still won’t start. What now?

Run sudo apachectl configtest to see the exact error. It’s often a missing dependency. Use ldd on the module file to find missing libraries and install them.

Do I need to set DirectoryIndex to include index.php?

Not for Apache to start, but to serve PHP files as directory index, you should add DirectoryIndex index.php index.html in your site config or .htaccess.

Can I use both PHP 7 and PHP 8 modules at the same time?

No, Apache can only load one PHP module per MPM. You must disable the old one and enable the new one. On Debian/Ubuntu, use a2dismod and a2enmod.

How do I change the PHP version Apache uses?

On Debian/Ubuntu, use sudo a2dismod php7.4 && sudo a2enmod php8.2 (adjust versions). On CentOS, edit the LoadModule line in the PHP config file to point to the new version’s .so file.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →