Home > Hosting > Program

How to solve the problem that Apache can't recognize PHP in Linux?

2024-04-10 14:57:18

In the process of using Linux operating system, Apache often fails to recognize PHP, which is caused by the lack of PHP module in the server environment or the incorrect configuration of PHP module. This article will introduce how to solve the problem that Apache can't recognize PHP in Linux.

First, confirm the problem

Before starting to solve the problem, you need to confirm whether Apache is installed correctly and running.

Check whether Apache is installed You can check whether Apache is installed by the following command:

sudo systemctl status apache2

If Apache is not installed, use the following command to install it:

sudo apt-get updatesudo apt-get install apache2

Check whether Apache is running You can check whether Apache is running by the following command:

sudo systemctl status apache2

If Apache is not started, it can be started by the following command:

sudo systemctl start apache2

Second, install PHP

After confirming that Apache is installed and running correctly, you need to install the PHP module. The following is the command to install PHP under Ubuntu:

sudo apt-get updatesudo apt-get install php libapache2-mod-php

After the installation is complete, you need to restart Apache:

sudo systemctl restart apache2

Third, adjust the Apache configuration file

After installing the PHP module, you need to adjust the configuration file of Apache to make it recognize PHP normally. The following are the commands to adjust Apache configuration files:

sudo nano /etc/apache2/mods-enabled/dir.conf

In the opened file, you can see the following:

DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

Index.php needs to be moved to the front of the directory list, and the modified content is as follows:

DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

After the modification is completed, Apache needs to be restarted for the configuration to take effect:

sudo systemctl restart apache2

Fourth, testing

After completing the above steps, you need to verify whether Apache can recognize PHP normally.

Creating a PHP file You can create a PHP file in Apache's default website directory by the following command:

sudo nano /var/www/html/info.php

Enter the following in the open file:

Save and close the file.

Access in the Browser Enter the following address in the browser:

http://localhost/info.php

If the following page appears, it means that Apache has been able to recognize PHP normally:

If an inaccessible page appears, it may be caused by security rules configured in the Apache configuration file. You can solve this problem by modifying Apache's security rules:

sudo nano /etc/apache2/conf-available/security.conf

In the open file, comment out the following:

Options FollowSymLinks AllowOverride None Require all denied AllowOverride None Require all granted Options Indexes FollowSymLinks AllowOverride None Require all granted

After modification, restart Apache:

sudo systemctl restart apache2

V. Summary

This paper introduces the solution that Apache can't recognize PHP in Linux. By confirming the problem, installing PHP module, adjusting Apache configuration file and testing, Apache can recognize PHP normally. At the same time, it also introduces some possible problems and solutions, hoping to help you solve the problems smoothly.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us