Check if PHP is Working on Apache in Ubuntu

In this guide, you can follow the instruction that is provided for you to check if PHP is working on Apache in an Ubuntu server. If you try to set up a server through the command line, you may find out that your PHP is not enabled with Apache. So you can follow the steps below to see how you can check this.

Check if PHP is Working on Apache in Ubuntu

As you may know, on Ubuntu and Debian distros the Apache configuration files are stored under /etc/apache2. In the Apache config file, there are two files named mods-available and mods-enabled.

When you install an Apache module, it will put it into the /etc/apache2/mods-available directory.

Also, when you enable an Apache module using a2enmod, it will create a symbolic link in /etc/apache2/mods-enabled for each of the matching files from /etc/apache2/mods-available.

At this point you can check the PHP, if you enabled Apache PHP modules, you should see something similar to this:

# cd /etc/apache2
# ls -l mods-*/*php*
Output
-rw-r--r-- 1 root root 998 Jun  9 15:37 mods-available/php8.2.conf
-rw-r--r-- 1 root root 101 Jun  9 15:37 mods-available/php8.2.load
lrwxrwxrwx 1 root root  29 Oct 16 05:50 mods-enabled/php8.2.conf -> ../mods-available/php8.2.conf
lrwxrwxrwx 1 root root  29 Oct 16 05:50 mods-enabled/php8.2.load -> ../mods-available/php8.2.load

If you don’t see these results in your output, you should enable the Apache module with PHP on Ubuntu. To do this, you can follow the steps below.

How do I enable PHP in Apache?

As an example, we consider that we don’t see the PHP 8 config files in the mods-enabled directory. To fix this, we need to enable it with the command below:

# sudo a2enmod php8.2
# sudo systemctl restart apache2

Also, you can check your PHP configuration by creating a sample PHP info file in Ubuntu. To do this, you can use the commands below:

# echo "<?php phpinfo(); ?>" | sudo tee /var/www/test.php
# sudo chown www-data:www-data /var/www/test.php
# sudo chmod 755 /var/www/test.php

At this point, you can browse to /test.php on the server and see the PHP configuration data.

Conclusion

At this point, you have learned to check if PHP is working on Apache in an Ubuntu server. If it is not working, you can use the a2enmod utility for Apache to enable it.

Hope you enjoy it.

You can get more guides and articles by visiting the Apache webserver Tutorials.

Also, you may like these articles:

Disable or Hide Error Messages in PHP

Find php.ini File Location on Linux

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!