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:
- A server running Ubuntu 22.04 (Jammy Jellyfish) with a non-root user that has
sudoprivileges. See our initial server setup guide for Ubuntu 22.04 (Jammy Jellyfish) if needed. - Apache installed and running. Follow our How to Install Apache on Ubuntu 22.04 (Jammy Jellyfish) guide first.
Installing PHP
Install PHP and the Apache module that allows Apache to process PHP files:
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:
php -v
You should see output similar to:
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:
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.
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:
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:
| Extension | Purpose | Required By |
|---|---|---|
php-mysql | MySQL/MariaDB database connectivity | WordPress, Laravel, Drupal, most CMS |
php-curl | HTTP requests, API integrations | WordPress plugins, payment gateways |
php-gd | Image processing and manipulation | WordPress media, image galleries |
php-mbstring | Multibyte string handling (UTF-8) | Any multilingual application |
php-xml | XML parsing (RSS, sitemaps, SOAP) | WordPress, Drupal, API integrations |
php-zip | Archive compression/extraction | Plugin/theme installers |
php-intl | Internationalization (i18n) | Symfony, Laravel, Magento |
php-bcmath | Arbitrary precision mathematics | E-commerce, financial applications |
Restart Apache to load the new extensions:
sudo systemctl restart apache2
Verify the extensions are loaded:
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.
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:
sudo nano /etc/apache2/mods-enabled/dir.conf
You’ll see a line similar to:
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
Move index.php to the front:
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:
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:
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:
; 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)
128M memory; WooCommerce and page builders often require 256M or more. Restart Apache to apply configuration changes:
sudo systemctl restart apache2
Production Error Handling
In production, disable error display to prevent exposing sensitive information while still logging errors for debugging:
; 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)
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.logunless 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):
sudo nano /var/www/example.com/index.php
Add the following content:
<?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.
Comments