Configuring Apache2 and PHP on a Fresh Installation

Introduction

Setting up Apache2 with PHP is essential for anyone looking to host a web server for applications and websites. Whether you’re setting up a local development environment or a production server, configuring Apache2 with PHP correctly is vital to ensure smooth performance and compatibility.

Prerequisites

  • A server running a Debian-based distribution such as Ubuntu.
  • A user account with sudo privileges.
  • Basic knowledge of the command line.

Step 1: Update the System

Before we begin, ensure your system’s package list is up-to-date to avoid any issues with outdated packages.

sudo apt update
sudo apt upgrade -y

This command updates the list of available packages and their versions and then upgrades any existing packages to their latest versions.

Step 2: Install Apache2

To install Apache2, run the following command:

sudo apt install apache2 -y

Once the installation is complete, check the status of Apache2 to ensure it is running:

sudo systemctl status apache2

You should see a message indicating that Apache2 is active and running.

Step 3: Adjust Firewall Settings

If your server uses ufw (Uncomplicated Firewall), you’ll need to allow HTTP and HTTPS traffic.

sudo ufw allow 'Apache Full'
sudo ufw reload

Verify that the firewall is active and the rules have been updated:

sudo ufw status

Step 4: Install PHP

Apache2 alone won’t be able to process PHP scripts; we need to install PHP and some commonly used PHP modules.

sudo apt install php libapache2-mod-php php-cli php-mbstring php-xml php-curl php-zip -y

Step 5: Test PHP Installation

To confirm that PHP is working, create a simple PHP file in the Apache document root:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Now, open your web browser and go to http://your_server_ip/info.php. You should see the PHP information page, confirming PHP is correctly installed and configured.

Important: Once you confirm PHP is working, remove the info.php file to avoid exposing sensitive information.

sudo rm /var/www/html/info.php

Step 6: Modify Apache2 Configuration (Optional)

By default, Apache2 uses index.html as the first file to serve. To prioritise index.php, modify the Apache configuration file.

  • Open the configuration file:
sudo nano /etc/apache2/mods-enabled/dir.conf
  • Move index.php to the front:
<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
  • Save and close the file (CTRL + X, then Y and Enter).
  • Restart Apache2 to apply the changes:
sudo systemctl restart apache2

Step 7: Verify Apache2 and PHP Configuration

Ensure everything is working correctly by creating and hosting a simple PHP file or accessing an existing PHP-based application.

This guide covered the essential steps for configuring Apache2 with PHP on a Debian-based server. By following these steps, you now have a basic web server environment ready to serve PHP applications.

Related posts

Weekly Tech Digest | 06 Jul 2026

Stay updated with the latest in tech! This digest covers AI ethics, auto industry shifts, and the impact of politics on technology, exploring today's pressing issues.

wolfCOSE zero-allocation parsing in embedded C

wolfCOSE looks sensible only if you care about what your firmware actually has to carry. I like that, because on small targets the wrong crypto feature can cost more than the message itself, and there...

restic | v0.19.1

restic v0 19 1: safer FUSE mounts and mountpoint checks, robust backup source and exclude handling, clearer CLI JSON output, Windows SFTP deletion fixes