When deploying a web application, issues with PHP not parsing correctly on CentOS can disrupt your website’s functionality. These problems are often caused by misconfigurations, missing modules, or improper file permissions. This guide will help you systematically troubleshoot and resolve these common issues.
First, make sure PHP is installed on your system. Run the following command in the terminal:
php -v
If you see the PHP version information, it means PHP is installed. If it returns "command not found", you need to install PHP first.
You can install PHP and its commonly used extensions using the YUM package manager with the command below:
sudo yum install php php-cli php-mysqlnd php-gd
After installation, confirm it worked by checking the PHP version again.
The main PHP configuration file is usually located at /etc/php.ini. Use a text editor to open it:
sudo vi /etc/php.ini
Pay close attention to the following settings:
If you're using Apache, make sure the relevant PHP modules are installed:
sudo yum install php-mbstring php-xml
Then, check Apache’s main configuration file at /etc/httpd/conf/httpd.conf to ensure PHP is enabled:
LoadModule php7_module modules/libphp7.so
After making changes, don’t forget to restart Apache.
Incorrect file ownership or permissions can prevent PHP files from being executed. Make sure the web server user (e.g., apache) has access to your PHP files:
sudo chown apache:apache /path/to/your/phpfile.php
Then, set the proper file permissions:
sudo chmod 644 /path/to/your/phpfile.php
After making the changes, restart the Apache service to apply them:
sudo systemctl restart httpd
If PHP still doesn't parse, examine Apache’s error log for more detailed information. By default, it's located at:
/var/log/httpd/error_log
Use this command to monitor the log output:
sudo tail -f /var/log/httpd/error_log
PHP not parsing on CentOS is typically caused by installation issues, missing modules, or misconfigured settings. By following the steps outlined in this guide, you should be able to identify the root cause and resolve the issue. Regularly reviewing your server configuration helps ensure stable and secure operation.