How To

Install PHP on Ubuntu 24.04 for Apache and Nginx

PHP remains one of the most widely used backend technologies for websites and web applications. If you want to Install PHP on Ubuntu systems for platforms like WordPress, Laravel, or Magento, Ubuntu 24.04 makes the process fast and straightforward with official repositories and updated packages.

This guide explains how to install PHP 8.3 on Ubuntu 24.04, configure it with Apache or Nginx, add common extensions, and manage multiple PHP versions efficiently.

Why Install PHP on Ubuntu Systems?

Ubuntu 24.04 includes PHP 8.3 in its default repositories, which means you can deploy modern PHP applications without adding third-party sources. PHP works seamlessly with popular web servers and offers strong performance for dynamic websites.

Whether you run a development machine or a production server, Ubuntu provides a stable environment for PHP applications.

Install PHP on Ubuntu with Apache

If your server uses Apache, installing PHP is very simple. Start by updating the package index and installing the required packages:

sudo apt updatesudo apt install php libapache2-mod-php

Once installation finishes, restart Apache to activate the PHP module:

sudo systemctl restart apache2

You can verify the installation using:

php -v

The output should display the installed PHP version.

Install PHP on Ubuntu with Nginx

Unlike Apache, Nginx does not process PHP files directly. Instead, it relies on PHP-FPM.

Install PHP and PHP-FPM with the following command:

sudo apt updatesudo apt install php-fpm

After installation, confirm that PHP-FPM is running:

sudo systemctl status php8.3-fpm

Next, configure your Nginx server block to handle PHP files:

location ~ \.php$ {    include snippets/fastcgi-php.conf;    fastcgi_pass unix:/run/php/php8.3-fpm.sock;}

Restart Nginx afterward:

sudo systemctl restart nginx

Installing PHP Extensions

Most PHP applications require additional extensions for databases, image processing, or caching.

You can install extensions individually:

sudo apt install php-mysql php-gd php-curl

Some commonly used extensions include:

  • php-mysql for MySQL databases
  • php-curl for HTTP requests
  • php-xml for XML support
  • php-mbstring for multibyte text handling
  • php-zip for archive management

To list installed PHP modules:

php -m

After adding extensions, restart Apache or PHP-FPM.

Test PHP Processing

Create a test file inside your web root:

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

Open your browser and visit:

http://your_server_ip/info.php

If PHP is configured correctly, a detailed PHP information page will appear.

For security reasons, remove the file after testing:

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

Install Other PHP Versions

Some applications require older PHP releases. Ubuntu supports multiple PHP versions through the popular Ondřej Surý repository.

Add the repository:

sudo add-apt-repository ppa:ondrej/phpsudo apt update

Then install a specific version:

sudo apt install php8.1

You can switch between installed versions using:

sudo update-alternatives --config php

Conclusion

Learning how to Install PHP on Ubuntu servers is essential for developers, administrators, and hosting environments. Ubuntu 24.04 simplifies PHP deployment with official repositories, PHP-FPM support, and flexible version management.

Once PHP is installed, you can add extensions, optimize performance, and begin deploying modern web applications with confidence.

Cyber Defence

Recent Posts

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

14 hours ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

15 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

19 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

20 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

21 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

22 hours ago