How to Install and Configure PHP on Ubuntu 26.04

PHP remains one of the most widely deployed server-side scripting languages on the web, powering everything from simple scripts to large-scale applications like WordPress and Laravel. If you plan to run dynamic websites or web applications on your Ubuntu 26.04 server, learning how to install and configure PHP is an essential step. In this guide, you will walk through the complete process to php install configure ubuntu 26.04, including PHP-FPM for Nginx, the Apache module, common extensions, multiple PHP versions, and php.ini tuning.
Software Requirements
| Category | Requirements, Conventions or Software Version Used |
|---|---|
| System | Ubuntu 26.04 Resolute Raccoon |
| Software | PHP 8.5, PHP-FPM, Nginx or Apache |
| Other | Privileged access to your Linux system as root or via the sudo command. |
| Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Install PHP and PHP-FPM on Ubuntu 26.04 with a few commands, then configure your web server to process PHP files.
| Step | Command/Action |
|---|---|
| 1. Install PHP and PHP-FPM | sudo apt install php php-fpm nginx |
| 2. Install common extensions | sudo apt install php-mysql php-curl php-mbstring php-xml php-zip php-gd |
| 3. Configure Nginx for PHP | Add fastcgi_pass block to your Nginx server block |
| 4. Test PHP processing | Create a phpinfo() test page and open it in a browser |
Installing PHP on Ubuntu 26.04
Ubuntu 26.04 ships with PHP 8.5 in its default repositories, so you can get started without adding any third-party sources. Begin by updating your package index and installing the core PHP package:
$ sudo apt update
$ sudo apt install php
This installs the PHP CLI interpreter along with common default modules. Once the installation completes, verify the installed version:
$ php -v
You should see output similar to:
It is important to understand that the php package alone only provides the command-line interpreter. To process PHP through a web server, you need either PHP-FPM (for Nginx) or the Apache PHP module. The following sections cover both approaches.
Installing PHP-FPM for Nginx on Ubuntu 26.04
PHP-FPM (FastCGI Process Manager) is the recommended way to run PHP with Nginx. Unlike Apache, Nginx does not have a built-in PHP module, so it relies on PHP-FPM as a separate process to handle PHP requests. Consequently, you need to install the PHP-FPM package and then configure Nginx to forward PHP requests to it.
Install and Start PHP-FPM
Install the PHP-FPM package:
$ sudo apt install php-fpm nginx
After installation, PHP-FPM starts automatically as a systemd service. Verify that it is running:
$ sudo systemctl status php8.5-fpm
The output should show the service as active (running). Additionally, PHP-FPM listens on a Unix socket by default, which Nginx will use to communicate with it. You can confirm the socket file exists:
$ ls /run/php/
You should see a socket file named php8.5-fpm.sock.
Configure Nginx to Process PHP Files
To serve PHP through Nginx, you need a server block that includes a location directive for PHP-FPM. Create a new server block configuration file:
$ sudo nano /etc/nginx/sites-available/linuxconfig.conf
Add the following complete server block. This is a minimal but fully functional configuration that serves both static files and PHP scripts:
server {
listen 80;
server_name linuxconfig.org www.linuxconfig.org;
root /var/www/linuxconfig.org/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
The key sections of this configuration are the index directive, which prioritizes index.php so that Nginx serves PHP index files by default, the location ~ \.php$ block, which matches any request ending in .php and forwards it to the PHP-FPM socket, and the location ~ /\.ht block, which denies access to hidden files such as .htaccess for security.
Now create the document root directory and enable the server block:
$ sudo mkdir -p /var/www/linuxconfig.org/html $ sudo chown -R $USER:$USER /var/www/linuxconfig.org/html $ sudo ln -s /etc/nginx/sites-available/linuxconfig.conf /etc/nginx/sites-enabled/
Test the Nginx configuration for syntax errors and reload
$ sudo nginx -t
$ sudo systemctl reload nginx
Test PHP Processing with Nginx
INSTALLATION TIPS
If you do not have a domain name pointing to your server, you can test the server block locally by adding an entry to your /etc/hosts file:
$ echo "127.0.1.1 linuxconfig.org" | sudo tee -a /etc/hosts
This maps the domain to your local machine, allowing you to access http://linuxconfig.org in your browser for testing purposes. Remove or comment out this line once you configure proper DNS.
Create a test PHP file in your web root to verify that Nginx correctly processes PHP:
$ echo "<?php phpinfo(); ?>" | sudo tee /var/www/linuxconfig.org/html/info.php
Open your browser and navigate to http://your_server_ip/info.php. You should see the PHP information page displaying the full PHP configuration. This confirms that Nginx is correctly forwarding PHP requests to PHP-FPM.
SECURITY ALERT
Remove the info.php file after testing. The phpinfo page exposes sensitive server configuration details that should never be publicly accessible on a production server:
$ sudo rm /var/www/linuxconfig.org/html/info.php
Installing PHP for Apache on Ubuntu 26.04
If you use Apache instead of Nginx, PHP integration is handled through the libapache2-mod-php module. This approach embeds PHP directly into the Apache process, so no separate service is required.
Install the Apache PHP module:
$ sudo apt install libapache2-mod-php
Apache automatically enables the PHP module during installation. You can confirm it is active:
$ apache2ctl -M | grep php
The output should include a line like php_module (shared), confirming that Apache is ready to process PHP files.
INSTALLATION TIPS
If you see the warning AH00558: apache2: Could not reliably determine the server's fully qualified domain name, suppress it by setting a global ServerName directive:
$ echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/servername.conf
$ sudo a2enconf servername
$ sudo systemctl reload apache2
To test PHP processing, create a test file in your Apache document root:
$ echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Open http://your_server_ip/info.php in your browser. If you see the PHP information page, Apache is processing PHP correctly. Remove the test file afterward:
$ sudo rm /var/www/html/info.php
INSTALLATION TIPS
Do not install both libapache2-mod-php and php-fpm on the same server unless you have a specific reason. Running both can lead to confusion about which PHP handler is active. Choose one approach based on your web server.
Installing Common PHP Extensions on Ubuntu 26.04
A base PHP installation includes only core functionality. Most web applications require additional extensions for database connectivity, string handling, image processing, and more. Therefore, installing the right extensions is a critical part of setting up PHP on Ubuntu 26.04.
Install the most commonly needed extensions in one command:
$ sudo apt install php-mysql php-curl php-mbstring php-xml php-zip php-gd php-intl
Here is what each extension provides:
| Extension | Purpose |
|---|---|
php-mysql | MySQL and MariaDB database connectivity (mysqli and PDO drivers) |
php-curl | HTTP client library for API requests and remote file fetching |
php-mbstring | Multibyte string handling for UTF-8 and internationalization |
php-xml | XML parsing and manipulation (DOM, SimpleXML, XMLReader) |
php-zip | ZIP archive creation and extraction |
php-gd | Image creation and manipulation (JPEG, PNG, GIF, WebP) |
php-intl | Internationalization functions (number formatting, date formatting, collation) |
To search for all available PHP extensions in the repositories, use:
$ apt search php- | grep "^php8.5"
You can also list all currently installed PHP modules:
$ php -m
After installing new extensions, remember to restart your PHP handler so the changes take effect:
$ sudo systemctl restart php8.5-fpm
Or for Apache:
$ sudo systemctl restart apache2
Installing Multiple PHP Versions on Ubuntu 26.04
Some projects require a specific PHP version that differs from the default. For instance, a legacy application might need PHP 8.1, while the Ubuntu 26.04 repositories provide PHP 8.5. To run multiple PHP versions side by side, you can use the Ondrej Sury PPA, which is the most widely trusted third-party PHP repository for Ubuntu.
Add the Ondrej PPA
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update
Install an Alternate PHP Version
For example, to install PHP 8.1 alongside the default version:
$ sudo apt install php8.1 php8.1-fpm php8.1-mysql php8.1-curl php8.1-mbstring php8.1-xml
Both PHP versions now coexist on the system. Each version has its own FPM service, configuration directory, and CLI binary.
Switch the Default CLI Version
Use update-alternatives to change which PHP version the php command points to:
$ sudo update-alternatives --set php /usr/bin/php8.1
Verify the switch:
$ php -v
To switch back to the default version:
$ sudo update-alternatives --set php /usr/bin/php8.5
IMPORTANT
Switching the CLI version does not change which PHP-FPM version your web server uses. To switch the web server’s PHP version, update the socket path in your Nginx configuration or disable/enable the appropriate Apache module.
Switch PHP-FPM Version for Nginx
Edit your Nginx server block and change the fastcgi_pass socket path:
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
Then reload Nginx:
$ sudo systemctl reload nginx
Switch PHP Version for Apache
Disable the current module and enable the desired one:
$ sudo a2dismod php8.5
$ sudo a2enmod php8.1
$ sudo systemctl restart apache2
Configuring php.ini on Ubuntu 26.04
The php.ini file controls PHP’s runtime behavior. On Ubuntu 26.04, PHP maintains separate php.ini files for each SAPI (Server API), meaning the CLI, FPM, and Apache each have their own configuration. Consequently, changes you make to one php.ini do not affect the others.
Locate Your php.ini Files
Find the active php.ini for the CLI:
$ php --ini | grep "Loaded Configuration"
Loaded Configuration File: /etc/php/8.5/cli/php.ini
For PHP-FPM, check the phpinfo output or look directly:
$ ls /etc/php/8.5/fpm/php.ini
For Apache:
$ ls /etc/php/8.5/apache2/php.ini
Key php.ini Directives
Open the appropriate php.ini file for your web server. For PHP-FPM:
$ sudo nano /etc/php/8.5/fpm/php.ini
The following directives are the most commonly adjusted for web applications:
| Directive | Default | Recommended | Purpose |
|---|---|---|---|
memory_limit | 128M | 256M | Maximum memory a single script can consume |
upload_max_filesize | 2M | 64M | Maximum size of an uploaded file |
post_max_size | 8M | 64M | Maximum size of POST data (must be >= upload_max_filesize) |
max_execution_time | 30 | 60 | Maximum time in seconds a script can run |
max_input_vars | 1000 | 3000 | Maximum number of input variables per request |
INSTALLATION TIPS
Always set post_max_size to a value equal to or greater than upload_max_filesize. If post_max_size is smaller, file uploads will silently fail even when the file is within the upload limit.
After editing php.ini, restart the relevant service for changes to take effect:
$ sudo systemctl restart php8.5-fpm
Or for Apache:
$ sudo systemctl restart apache2
You can confirm your changes are active by checking specific values from the command line:
$ php -i | grep memory_limit
memory_limit => 256M => 256M
IMPORTANT
The CLI php -i output reflects the CLI php.ini, not the FPM or Apache one. To verify web server values, use a phpinfo() page or check the specific php.ini file directly.
Conclusion
You have successfully installed and configured PHP on Ubuntu 26.04. This guide covered the CLI installation, PHP-FPM setup for Nginx, the Apache PHP module, common extensions, running multiple PHP versions with the Ondrej PPA, and tuning the php.ini configuration. With PHP properly configured, your Ubuntu 26.04 server is ready to host dynamic web applications. For more in-depth information on PHP configuration directives, consult the official PHP documentation.
Share this:
- Share on Facebook (Opens in new window) Facebook
- Share on X (Opens in new window) X
- Share on Bluesky (Opens in new window) Bluesky
- Share on LinkedIn (Opens in new window) LinkedIn
- Share on Reddit (Opens in new window) Reddit
- Share on Threads (Opens in new window) Threads
- Print (Opens in new window) Print
- Share on Mastodon (Opens in new window) Mastodon






