Learn to troubleshoot Apache 2 when it isn’t executing PHP recordsdata with this easy-to-follow, step-by-step tutorial.
What number of occasions have you ever arrange an online server with Apache 2 solely to seek out out that if you try to view a web page with a .php extension, the web page both makes an attempt to save lots of 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 purpose to keep away from working with PHP and Apache 2. However, how do you repair it? Let me present you.
Notice: I reveal this course of on Ubuntu Desktop 23.04, working the most recent releases of Apache 2 and PHP 8.1. This course of can work in most fashionable Linux distributions.
Leap 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 straightforward. 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
Notice: 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 obligatory to start out Apache 2 and allow Apache 2. For those who begin Apache 2 however don’t allow it, then Apache 2 is not going to restart if you reboot your system:
sudo systemctl begin apache2
sudo systemctl allow apache2
You may test the standing of Apache 2 utilizing the command:
sudo systemctl standing apache2
This command will inform you if Apache 2 is working 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 may take away this with the command:
sudo rm /var/www/html/index.html
The sudo
command is important as a result of this file is owned by root.
Now create a brand new file, /var/www/html/index.php
, instead:
sudo nano /var/www/html/index.php
And on this file, place the next code:
<?php
phpinfo();
?>
In case you have a desktop surroundings and an online browser put in in your server, you may browse to the set up by way of the URL http://localhost. In any other case, you may browse to the set up from one other laptop in your community utilizing the IP tackle of the server on which you carried out the set up.
For those who see a display screen that appears just like the one under, then congratulations! You could 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 positively sort out the phpinfo()
command, particularly if in case you have this working on a public-facing server:
<?php
//phpinfo();
?>
Subsequent steps: Securing your Apache 2 and PHP setup
Usually, lets say “that’s it!” almost about your Apache 2 and PHP setup, however that’s most definitely not the case. For starters, the default configuration for Apache 2 is very 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: