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
- Check the exact error message. Run
sudo apachectl configtest(on most Linux systems) orhttpd -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. - Open the PHP module file. The error often points to a
.sofile (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 theconf/httpd.confunderLoadModule. - 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. Runls -l /usr/lib/apache2/modules/libphp.soto confirm. If the file is missing, reinstall the PHP module package (e.g.,sudo apt install libapache2-mod-php). - 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-devorsudo yum install libxml2-devel). - 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 -vandphp -v. - 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) andsudo a2enmod phpX.Y(new version). On CentOS, comment out duplicate lines in the conf file. - Look for syntax errors in PHP config files. If the error mentions a
.inifile, runphp -i | grep 'Loaded Configuration File'to find it, then runphp -l /path/to/php.inito check for syntax errors. Fix any typos. - Restart Apache. After making changes, run
sudo apachectl configtestagain. If it says “Syntax OK”, start Apache withsudo systemctl restart apache2(orhttpdon 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
LoadModuleline must point to the exact version you installed. A path likelibphp7.sowhen 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.conffiles. Useapachectl -Vto see the actualSERVER_CONFIG_FILEpath.
Tips
- Always run
sudo apachectl configtestbefore 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.logor/var/log/httpd/error_log. On Windows, it’s in the Apache installation directory underlogs/error.log. The log often has more detail than the config test. - Try a minimal config. Temporarily comment out the PHP
LoadModuleline. 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*thensudo 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 0to test (temporary). On Ubuntu, check AppArmor status withsudo 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
- Blockchain vs Database: When Does Distributed Trust Win?
- GTA 6 and Call of Duty on Same Console?
- MCP in Production: Security Failures Behind the Backdoor Label
- Nothing Phone 2 Waterproof? IP54 Rating Explained
- Satellite Internet Equipment & Installation Guide
- Infinix GT 20 Pro vs AI Nova 2 Ultra 5G: Which Fits?