How to Install PHP with Apache on Ubuntu 22.04

Install PHP with Apache on Ubuntu 22.04 – DevAnswers.net

This guide covers Ubuntu 22.04. Read the Ubuntu 26.04 edition.

We may earn a referral fee at no additional cost to you when you shop through the links on this page.

Ubuntu 22.04 LTS (Jammy Jellyfish), released April 2022, is a stable long-term support release with security updates until April 2027 (extendable to 2032 with Ubuntu Pro). It ships with PHP 8.1, providing excellent compatibility with modern frameworks and applications.

The following steps cover installing PHP, configuring essential extensions for web applications, optimising PHP settings, and verifying the installation with Apache.

Prerequisites 

Before you begin, ensure you have:

Installing PHP 

Install PHP and the Apache module that allows Apache to process PHP files:

Terminal
sudo apt update && sudo apt install -y php libapache2-mod-php

This installs PHP 8.1 from the Ubuntu 22.04 (Jammy Jellyfish) repositories along with the Apache PHP module (libapache2-mod-php), which enables Apache to interpret PHP code. The module is automatically enabled and Apache is restarted during installation.

Verifying the Installation 

Check the installed PHP version:

Terminal
php -v

You should see output similar to:

Output
PHP 8.1.x (cli) (built: ...)
Copyright (c) The PHP Group
Zend Engine v4.x.x, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.x, Copyright (c), by Zend Technologies

Testing PHP with Apache 

Create a PHP test file to verify Apache processes PHP correctly:

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

Open your browser and navigate to:

http://your_server_ip/info.php

You should see the PHP information page displaying your PHP version, loaded modules, and configuration details.

Security warning: The phpinfo() page exposes sensitive server configuration details. Remove it after testing:

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

Installing Common PHP Extensions 

Most web applications require additional PHP extensions. The following command installs a robust set of extensions needed by WordPress, Laravel, Drupal, and other popular frameworks:

Terminal
sudo apt install -y php-mysql php-curl php-gd php-mbstring php-xml php-zip php-intl php-bcmath

Here’s what each extension provides:

ExtensionPurposeRequired By
php-mysqlMySQL/MariaDB database connectivityWordPress, Laravel, Drupal, most CMS
php-curlHTTP requests, API integrationsWordPress plugins, payment gateways
php-gdImage processing and manipulationWordPress media, image galleries
php-mbstringMultibyte string handling (UTF-8)Any multilingual application
php-xmlXML parsing (RSS, sitemaps, SOAP)WordPress, Drupal, API integrations
php-zipArchive compression/extractionPlugin/theme installers
php-intlInternationalization (i18n)Symfony, Laravel, Magento
php-bcmathArbitrary precision mathematicsE-commerce, financial applications

Restart Apache to load the new extensions:

Terminal
sudo systemctl restart apache2

Verify the extensions are loaded:

Terminal
php -m | grep -iE 'mysql|curl|gd|mbstring|xml|zip|intl|bcmath'

You should see each extension listed in the output. If any are missing, re-run the install command and restart Apache.

Need to find an extension? Search available packages with:

apt search php- | grep ^php

Setting PHP as Apache’s Default 

By default, Apache looks for index.html before index.php when serving a directory. For PHP applications, you typically want index.php to take priority.

Edit the Apache directory index configuration:

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

You’ll see a line similar to:

Apache
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

Move index.php to the front:

dir.conf
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

Save and exit (press CTRL + X, press Y, then press ENTER)

Test the configuration and reload Apache:

Terminal
sudo apache2ctl configtest && sudo systemctl reload apache2

Configuring PHP 

PHP’s configuration file controls memory limits, upload sizes, error reporting, and other runtime settings. The primary configuration file for Apache is:

/etc/php/8.1/apache2/php.ini

Common Configuration Changes 

Open the PHP configuration file:

Terminal
sudo nano /etc/php/8.1/apache2/php.ini

Here are recommended settings for most web applications. Find each directive (use Ctrl + W to search) and modify as needed:

php.ini
; Maximum memory a script can consume (increase for large applications)
memory_limit = 256M

; Maximum file upload size (adjust for media-heavy sites)
upload_max_filesize = 64M

; Maximum size of POST data (should be >= upload_max_filesize)
post_max_size = 64M

; Maximum script execution time in seconds
max_execution_time = 300

; Maximum time for parsing input data
max_input_time = 300

; Server timezone (important for date/time functions)
date.timezone = UTC

Save and exit (press CTRL + X, press Y, then press ENTER)

These values are suggestions. Adjust based on your application’s requirements and server resources. WordPress typically needs at least 128M memory; WooCommerce and page builders often require 256M or more.

Restart Apache to apply configuration changes:

Terminal
sudo systemctl restart apache2

Production Error Handling 

In production, disable error display to prevent exposing sensitive information while still logging errors for debugging:

php.ini
; Disable error display in browser (security)
display_errors = Off

; Log errors to file instead
log_errors = On

; Error log location (check Apache's error log or set custom path)
; error_log = /var/log/php_errors.log

Save and exit (press CTRL + X, press Y, then press ENTER)

During development, set display_errors = On temporarily to see errors in your browser. Always switch back to Off before going live.

Important File Locations 

Key paths for managing PHP on Ubuntu 22.04 (Jammy Jellyfish):

  • PHP Configuration (Apache): /etc/php/8.1/apache2/php.ini — Primary settings file.
  • PHP Configuration (CLI): /etc/php/8.1/cli/php.ini — Settings for command-line scripts.
  • Apache PHP Module Config: /etc/apache2/mods-available/php8.1.conf — Module-specific settings.
  • Additional Configuration: /etc/php/8.1/apache2/conf.d/ — Drop-in config files for extensions.
  • PHP Error Log: Typically /var/log/apache2/error.log unless configured otherwise.

Creating a Complete Test 

Create a simple PHP script to verify your setup works end-to-end. If you set up a virtual host in the Apache guide, place this in your site directory (this replaces the Apache test page you created earlier):

Terminal
sudo nano /var/www/example.com/index.php

Add the following content:

index.php
<?php
$hostname = gethostname();
$php_version = phpversion();
$server_software = $_SERVER['SERVER_SOFTWARE'];
$document_root = $_SERVER['DOCUMENT_ROOT'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP Test - <?php echo $hostname; ?></title>
    <style>
        body { font-family: system-ui, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; }
        h1 { color: #4F5D95; }
        .info { background: #f4f4f4; padding: 15px; border-radius: 5px; margin: 10px 0; }
        .success { color: #28a745; }
        code { background: #e9ecef; padding: 2px 6px; border-radius: 3px; }
    </style>
</head>
<body>
    <h1>✓ PHP is Working!</h1>
    <div class="info">
        <p><strong>PHP Version:</strong> <code><?php echo $php_version; ?></code></p>
        <p><strong>Server:</strong> <code><?php echo $server_software; ?></code></p>
        <p><strong>Hostname:</strong> <code><?php echo $hostname; ?></code></p>
        <p><strong>Document Root:</strong> <code><?php echo $document_root; ?></code></p>
    </div>
    <p class="success">Your LAMP stack's PHP component is configured correctly.</p>
    <p><small>Return to <a href="https://devanswers.net/series/lamp-stack/">LAMP Stack Documentation</a></small></p>
</body>
</html>

Save and exit (press CTRL + X, press Y, then press ENTER)

Visit http://example.com in your browser to see the test page. If you did not set up a virtual host, use http://your_server_ip instead (and place the file in /var/www/html/index.php).

Conclusion 

You now have PHP 8.1 running with Apache on Ubuntu 22.04, complete with essential extensions for modern web applications.

With PHP 8.1’s modern features and LTS support until 2027 (or 2032 with Ubuntu Pro), this provides a reliable foundation for production PHP applications. Your next step is installing MariaDB to complete your LAMP stack.

Next · step 4 of 4How to Secure Apache with Let’s Encrypt SSL on Ubuntu 22.04
Support DevAnswers
Written, tested and maintained by one developer. Your support keeps these guides accurate and ad-light.
Buy me a coffee

Comments

Leave a reply