Home Programming How to Install PHP on Ubuntu 24.04 LTS: A Complete Guide

How to Install PHP on Ubuntu 24.04 LTS: A Complete Guide

Learn how to install PHP on Ubuntu 24.04 LTS with this detailed guide. Follow our step-by-step instructions to set up PHP, configure your server, and ensure optimal performance.

by Arun Kumar
install php on ubuntu

In the world of web development, PHP is a popular server-side scripting language that’s used to develop dynamic web applications. If you’re running a web server on Ubuntu 24.04 LTS and want to use PHP, you’re in the right place. In this guide, I’ll take you through the steps to install PHP on Ubuntu 24.04 LTS. I’ll include detailed steps, sample commands, and expected outputs to ensure you have a smooth installation process.

Installing PHP on Ubuntu

Prerequisites

Before we dive into the installation process, make sure you have the following:

  1. A running Ubuntu 24.04 LTS system.
  2. Sudo privileges to install packages.

Update the package list

The first step in any installation process on Ubuntu is to update the package list. This ensures that you are installing the latest versions of the software.

sudo apt update

Install Apache2

While it’s possible to run PHP with different web servers, Apache2 is a common choice due to its robustness and ease of use. If you don’t have Apache2 installed, you can do so with the following command:

sudo apt install apache2

Output:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,236 kB of archives.
After this operation, 5,771 kB of additional disk space will be used.
Do you want to continue? [Y/n] y

Install PHP

Now that we have our web server ready, we can install PHP. We’ll also install some common PHP extensions that are often needed.

sudo apt install php libapache2-mod-php php-mysql

Output:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  php-common php7.4-cli php7.4-common php7.4-json php7.4-mysql php7.4-opcache php7.4-readline
Suggested packages:
  php-pear php7.4-bcmath php7.4-bz2 php7.4-curl php7.4-dba php7.4-enchant php7.4-fpm php7.4-gd php7.4-gmp php7.4-imap php7.4-interbase php7.4-intl php7.4-ldap php7.4-mbstring php7.4-odbc php7.4-pgsql php7.4-phpdbg php7.4-pspell php7.4-recode php7.4-snmp php7.4-soap php7.4-sybase php7.4-tidy php7.4-xml php7.4-xmlrpc php7.4-xsl php7.4-zip
The following NEW packages will be installed:
  libapache2-mod-php php php-common php-mysql php7.4-cli php7.4-common php7.4-json php7.4-mysql php7.4-opcache php7.4-readline
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 5,717 kB of archives.
After this operation, 23.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Verify PHP installation

To verify that PHP is installed correctly, you can create a simple PHP file and access it through your web browser.

  1. Create a PHP file in the web server root directory:
sudo nano /var/www/html/info.php
  1. Add the following content to the file:
<?php
phpinfo();
?>
  1. Save and close the file.
  2. Open your web browser and navigate to http://your_server_ip/info.php. You should see a page displaying information about your PHP installation.

Configure Apache to prioritize PHP

By default, Apache might not prioritize PHP files. To ensure that PHP files are served before others, we need to modify the dir.conf file.

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

Change the order of the index files to:

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

Save and close the file.

Restart Apache

For the changes to take effect, you need to restart the Apache service.

sudo systemctl restart apache2

Output:

Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.

If you encounter an error, it might be due to a syntax error in the configuration file. Double-check your changes and try restarting the service again.

Install additional PHP extensions

Depending on your needs, you might require additional PHP extensions. Here are some common ones:

sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc

Output:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  php7.4-curl php7.4-gd php7.4-mbstring php7.4-xml php7.4-xmlrpc
The following NEW packages will be installed:
  php-curl php-gd php-mbstring php-xml php-xmlrpc php7.4-curl php7.4-gd php7.4-mbstring php7.4-xml php7.4-xmlrpc
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,252 kB of archives.
After this operation, 14.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Conclusion

Congratulations! You have successfully installed PHP on your Ubuntu 24.04 LTS system. Now you can start developing dynamic web applications with PHP.  Note that you should remove the info.php file after verifying your installation to prevent unauthorized access to your server configuration. Here are the steps:

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

Output:

rm: remove write-protected regular file '/var/www/html/info.php'? y

Feel free to explore the vast array of PHP extensions and configurations to tailor your development environment to your specific needs.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.