Home > Article > Backend Development > How to solve the problem that apache cannot find the php file
When you use Apache as the web server and PHP as the server-side script interpreter, you may encounter the "PHP file not found" problem. This will make your PHP files inaccessible in your web browser. This article will show you how to diagnose and fix this problem.
First of all, we need to confirm whether you have installed Apache and PHP correctly. If you are not sure, you can check by following these steps:
Open a terminal or command prompt and enter the following command:
apache2 -v
If you see the output, you have installed Apache and your Apache version number will be displayed.
Open a terminal or command prompt and enter the following command:
php -v
If you see the output, it means you have installed it PHP and your PHP version number will be displayed.
If you don't see the output, you need to install Apache and PHP. You can follow the steps below to install it:
Open a terminal and run the following command:
sudo apt update sudo apt install apache2
Open a terminal and run the following command:
sudo apt update sudo apt install php libapache2-mod-php
After completing the installation, we can start troubleshooting the "PHP file not found" problem .
Open a terminal or command prompt and enter the following command to edit the Apache configuration file:
sudo nano /etc/apache2/apache2.conf
Check this Does the end of the file contain the following lines:
<FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
If not, add these lines to the end of the file.
Open a terminal or command prompt and enter the following command to enable Apache's PHP module:
sudo a2enmod php7.4
Please note that you need Replace the version number with your PHP version number.
Check if your PHP file is located in Apache's web root directory or virtual host root directory. You can find your root directory with the following command:
sudo nano /etc/apache2/sites-available/000-default.conf
In that file you will find the following line:
DocumentRoot /var/www/html
This is where your root directory is. Make sure your PHP files are located in this directory.
Restart Apache for the changes to take effect:
sudo service apache2 restart
After completing the above steps, you should be able to access PHP in your web browser file. If you are still experiencing "PHP file not found" issues, try using a different browser or clearing your browser cache.
The above is the detailed content of How to solve the problem that apache cannot find the php file. For more information, please follow other related articles on the PHP Chinese website!