Learn to troubleshoot Apache 2 when it is not executing PHP information with this easy-to-follow, step-by-step tutorial.

What number of instances have you ever arrange an internet server with Apache 2 solely to search out out that once you try and view a web page with a .php extension, the web page both makes an attempt to avoid wasting onto the native drive or it shows the PHP code within the browser? That is dangerous on a number of ranges. First off, your web site isn’t functioning correctly. Even worse, somebody may get a first-hand glimpse of your code, which isn’t solely a frustration however a potential safety problem.
Contemplating that is a simple repair, there’s no cause to keep away from working with PHP and Apache 2. However, how do you repair it? Let me present you.
Be aware: I reveal this course of on Ubuntu Desktop 23.04, operating the most recent releases of Apache 2 and PHP 8.1. This course of can work in most trendy Linux distributions.
Bounce to:
Putting in Apache 2 and PHP 8.1
Package deal administration makes the set up and configuration of Apache 2 and PHP 8.1 simple. Use the next instructions within the terminal to put in Apache 2, PHP 8.1, the PHP 8.1 command-line interpreter, and the module which configures Apache 2 and PHP 8.1 to work collectively:
sudo apt-get set up apache2 php8.1 php8.1-cli
libapache2-mod-php8.1 -y
Be aware: The above line is damaged up due to the margin limitation on the web page. The usage of the backslash on the finish of the road is intentional.
SEE: Benefit from this guidelines for managing and troubleshooting servers from TechRepublic Premium.
Beginning and enabling Apache 2
As soon as the set up of Apache 2 and the PHP modules is full, it will likely be mandatory to begin Apache 2 and allow Apache 2. In the event you begin Apache 2 however don’t allow it, then Apache 2 is not going to restart once you reboot your system:
sudo systemctl begin apache2
sudo systemctl allow apache2
You possibly can examine the standing of Apache 2 utilizing the command:
sudo systemctl standing apache2
This command will let you know if Apache 2 is operating and whether it is enabled.
Verifying the PHP 8.1. set up
With Apache 2 put in and PHP 8.1 configured, it will likely be essential to confirm that the set up is working. You are able to do this by making a easy PHP script within the /var/www/html
listing.
By default, the Apache 2 package deal will create an index.html
file within the /var/www/html
listing. You possibly can take away this with the command:
sudo rm /var/www/html/index.html
The sudo
command is critical as a result of this file is owned by root.
Now create a brand new file, /var/www/html/index.php
, as a replacement:
sudo nano /var/www/html/index.php
And on this file, place the next code:
<?php
phpinfo();
?>
If in case you have a desktop surroundings and an internet browser put in in your server, you’ll be able to browse to the set up by way of the URL http://localhost. In any other case, you’ll be able to browse to the set up from one other laptop in your community utilizing the IP deal with of the server on which you carried out the set up.
In the event you see a display that appears just like the one beneath, then congratulations! You will have efficiently put in Apache 2 and PHP 8.1 (Determine A).
Determine A

When you’ve verified that your Apache 2 and PHP 8.1 installations are working, it is best to undoubtedly kind out the phpinfo()
command, particularly if in case you have this operating on a public-facing server:
<?php
//phpinfo();
?>
Subsequent steps: Securing your Apache 2 and PHP setup
Usually, lets say “that’s it!” with reference to your Apache 2 and PHP setup, however that’s most definitely not the case. For starters, the default configuration for Apache 2 is extremely insecure; your subsequent plan of action must be to correctly safe it. You will get extra data on precisely how to do that from the next articles: