Home >Backend Development >PHP Problem >How to solve the problem that apache cannot find the php file

How to solve the problem that apache cannot find the php file

PHPz
PHPzOriginal
2023-03-21 11:10:351772browse

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:

  1. Check the Apache version

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.

  1. Check PHP version

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:

  1. Install Apache on Ubuntu

Open a terminal and run the following command:

sudo apt update
sudo apt install apache2
  1. Install PHP on Ubuntu

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 .

  1. Check the Apache configuration file

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.

  1. Check the Apache module

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.

  1. Check PHP file path

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.

  1. Restart Apache

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn