W3docs

Enable PHP Apache2

To enable PHP for Apache2 on Ubuntu, follow these steps:

To enable PHP for Apache2 on Ubuntu, follow these steps:

  1. First, make sure that you have installed the Apache2 and PHP packages. You can do this by running the following command:
sudo apt-get install apache2 php libapache2-mod-php
  1. Once the installation is complete, you need to enable the PHP module for Apache2. You can do this by running the following command:
sudo a2enmod php8.2
  1. After enabling the PHP module, you need to restart Apache2 for the changes to take effect. You can do this by running the following command:
sudo systemctl restart apache2
  1. Finally, you can test whether PHP is working correctly by creating a file called "info.php" in the root directory of your Apache2 server (usually located at /var/www/html). Inside this file, put the following code:

Example of enabling PHP Apache 2.x

<?php
phpinfo();
?>

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

Then, open a web browser and go to "http://localhost/info.php". If PHP is working correctly, you should see a page with information about your PHP installation.