How to Install Apache and PHP on Ubuntu 24.04

When setting up a web server on Ubuntu, Apache and PHP are two essential components. Apache is a widely used web server, while PHP is a scripting language often required for dynamic websites. In this guide, I’ll show you how to install Apache and PHP on Ubuntu 24.04, step-by-step.

Prerequisites

Before we start, ensure that:

  • You have a system running Ubuntu 24.04
  • You have root or sudo access to the server
  • Your system is up-to-date

Step 1: Update the System

First, ensure your package list is up-to-date. Open a terminal and run the following command:

sudo apt update && sudo apt upgrade -y

This will ensure you have the latest packages and security patches installed.

Step 2: Install Apache

Apache is one of the most reliable and widely-used web servers. You can install it using the following command:

sudo apt install apache2 -y

Once installed, you can start the Apache service:

sudo systemctl start apache2

To ensure Apache starts automatically when the system reboots, enable the service:

sudo systemctl enable apache2

Verify the Apache Installation

After the installation, you can check whether Apache is running by visiting your server’s IP address in a web browser:

http://your_server_ip

If installed correctly, you should see the Apache default page.

Step 3: Install PHP

Now, we’ll install PHP. Ubuntu 24.04 typically includes the latest PHP version in its repositories. Run the following command to install PHP:

sudo apt install php libapache2-mod-php -y

This will install PHP and the necessary Apache module to handle PHP files.

Verify PHP Installation

To verify that PHP is working correctly, create a test file. First, navigate to the web root directory:

cd /var/www/html

Then create a PHP file:

sudo nano info.php

Add the following PHP code to the file:

<?php
phpinfo();
?>

Save the file, and then access it via your web browser by navigating to:

http://your_server_ip/info.php

You should see the PHP information page, confirming PHP is properly installed.

Step 4: Configure Apache to Prefer PHP Files

By default, Apache might load HTML files first. To prioritize PHP files, you can adjust the dir.conf file. Open it with the following command:

sudo nano /etc/apache2/mods-enabled/dir.conf

Move index.php to the first position, like this:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Save the file and restart Apache to apply the changes:

sudo systemctl restart apache2

Step 5: Install Additional PHP Modules (Optional)

Depending on your application, you might need additional PHP modules. You can search for PHP modules with this command:

apt search php-

To install a module, use the following syntax:

sudo apt install php-[module_name] -y

For example, to install the php-mysql module, you would run:

sudo apt install php-mysql -y

Step 6: Secure Apache

It’s important to secure your Apache installation. Here are a few basic steps to improve security:

  • Firewall Configuration: Ensure only essential traffic is allowed:
    bash sudo ufw allow 'Apache' sudo ufw enable
  • Disable Directory Listing: Prevent directory contents from being displayed by adjusting the Apache configuration file:
    bash sudo nano /etc/apache2/apache2.conf
    Find the section with the <Directory /var/www/> directive and set Options -Indexes.

Conclusion

By following these steps, you now have a fully functional web server running Apache and PHP on Ubuntu 24.04. You can now deploy your PHP applications and start building your web projects. If you run into any issues, consult the Apache and PHP logs for more details:

  • Apache logs: /var/log/apache2/error.log
  • PHP logs: /var/log/php-errors.log

Make sure to keep your server updated and secure, and enjoy building with Apache and PHP!