How to Install PHP on Ubuntu 22.04
The apt package manager is the easiest way to install PHP on Ubuntu 22.04, but the version included in the Ubuntu repositories may not be the most recent. For the latest version, use the Ondřej Surý repository. Advanced users can also build PHP from the source.
Installing PHP is only the first step. You must also configure your web server (Apache or Nginx) to handle and manage PHP processes.
Apache Handler (libapache2-mod-php), FPM, or CGI is the server API used by Apache. FastCGI Process Manager (php-fpm) is PHP’s implementation of FastCGI and is the recommended option for NGINX.
In this tutorial, I’ll explain how to install PHP on Ubuntu 22.04 using both methods and cover further configuration.
Before You Begin
In order to follow this tutorial, you need a Ubuntu system and a non-root account with sudo privileges. A web server like Apache or Nginx must also be already installed and running.
Before installing PHP, ensure that your Ubuntu installation is up-to-date.
sudo apt update && sudo apt upgrade -y
Easiest Way to Install PHP on Ubuntu 22.04
The easiest way to install PHP on Ubuntu is from the official repository itself. The default command shown below will install a recent PHP version with the libapache2-mod-php plugin.
sudo apt install php -y
If you want to install PHP with the FPM module instead,
sudo apt install php-fpm -y
This will install the php8.1-fpm package on a ubuntu 22.04 system. You can add a specific version after php to install the version of your choice.
Install Latest PHP with Apache on Ubuntu
If you’ve already installed PHP, you can skip this step. But for those who haven’t, you can install the latest PHP version from the Ondrej PPA. I’ll start by adding the ondrej/php PPA as a software repository.
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Next, install the desired PHP version. I’ll install the current latest version (8.2) with some required modules for this tutorial.
sudo apt install php8.2 php8.2-common php8.2-mbstring php8.2-xmlrpc php8.2-soap php8.2-gd php8.2-xml php8.2-intl php8.2-mysql php8.2-cli php8.2-zip php8.2-curl -y
If you want to use php-fpm instead with Apache, you can run
sudo apt install php8.2-fpm -y
enable Apache to use php-fpm
sudo a2enmod proxy_fcgi
sudo a2enconf php8.2-fpm
To change the default directory index
sudo nano /etc/apache2/mods-enabled/dir.conf
Move index.php to the start of the DirectoryIndex directive as shown below:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Save the changes and exit.
Restart the Apache server to apply the changes.
sudo systemctl restart apache2
Install Latest PHP with Nginx on Ubuntu
Once again, you can skip ahead if you’ve already installed PHP.
But if you need to install a different version for instance, you can do so by adding the Ondrej PPA. Start by adding the PPA to your apt sources list.
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
Then, you can install the PHP version you want with
sudo apt-get install php-fpm -y
In my case, I’ll install version 8.2 with some important modules such as MySQL. You can decide which modules to install depending on your own needs. You can also install additional modules after the installation as I’ve covered later in this guide.
sudo apt-get install -y php8.2-fpm php8.2-common php8.2-mbstring php8.2-xmlrpc php8.2-soap php8.2-gd php8.2-xml php8.2-intl php8.2-mysql php8.2-cli php8.2-zip php8.2-curl
After installing PHP, you’ll need to modify site-specific server blocks to use PHP with Nginx. On Ubuntu, these config files are stored in the /etc/nginx/sites-available/ directory. In my case, I’ll edit the default config file. Replace this with your own server block as appropriate.
To start, open the config file with a text editor:
sudo nano /etc/nginx/sites-available/default
Add index.php at the start index directive to allow the server block to process .php files.
Then, add additional location blocks after the first one to ensure that .php files requests are handled by php-fpm.
location ~* \.php$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
Save the changes and exit.
Verify that the configuration changes were valid with:
sudo nginx -t
If no syntax errors are reported, reload Nginx to apply the changes:
sudo systemctl reload nginx
Verifying the Installation
Before you get started with PHP, you can run
php -v
to check the version number of your current PHP installation. This will also help confirm that the installation went well. After this, I’ll directly test the PHP installation to ensure everything works.
Testing the PHP Installation
Create a file called test.php in your document root
sudo nano /var/www/html/test.php
with the following content
<?php phpinfo(); ?>
Save the changes and exit.
Now, open a web browser and enter the server’s address, followed by the filename, as such:
http://your_server_address/test.php
The PHP information page should be displayed, indicating PHP is properly installed. It will display the version of PHP and any enabled configuration, settings and extensions.
Installing Additional Modules
If you want to install additional modules, you can check the available ones with:
sudo apt search php<version>-*
From this list, you can install the module you want with it’s name.
sudo apt install <fullmodulename>
You can also install multiple modules together if you want.
sudo apt install php<version>-{modulenames}
Finally, after installing the modules, you can check that they’re loaded with php -m
.
The phpenmod
and phpdismod
commands can be used to enable or disable PHP modules, respectively.
Configuring PHP
The phpinfo();
function will return information about your PHP configuration, including the location of the php.ini file. You can edit this file to optimize your PHP configuration.
To locate the php.ini file on your Ubuntu system, you can use the following command:
sudo find / -name php.ini
Do keep in mind that there’s no one size fits all configuration. In fact, the default configuration already comes closest to that. Instead, the optimal configuration for you will depend on your specific needs and available resources.
In most cases, you should be okay with the default values or anything close. However, you should monitor server performance on a regular basis and adjust the limits as necessary.
Using excessively high values might result in increased server load, decreased system performance, and various security risks.
With that said, here are some good starting points. Search inside the editor and update the values.
memory_limit = 256M
max_execution_time = 30
max_input_time = 60
max_input_vars = 1000
upload_max_filesize = 256M
post_max_size = 256M
session.gc_maxlifetime = 1440
disable_functions = exec,shell_exec,system,getmyuid,passthru,leak
Managing PHP Using the CLI
While I’m mostly focusing on configuring PHP for web servers in this article, it’s also worth knowing some basic management steps for the CLI.
As covered earlier, running php -v
in the CLI returns the current version number while php -m
displays the installed modules. Similarly, here are some other useful command-line options
php -i #outputs the configuration information.
php -l <file> #checks the syntax.
php -c <path>|<file> #lets you specify where to look for the php.ini file.
php -n #specifies that no config files will be used.
php -f <some-script.php> #lets us run the specified script in CLI.
php -h #displays the help page where you’ll find the full list of options.
Installing Previous PHP Versions
It’s possible to have multiple php versions installed at the same time. It can be useful if you have applications that require different versions of php.
If you want to install a specific PHP version, , simply install the desired version with:
sudo apt install php<version>
If the version you want isn’t available, you can follow the steps above for adding the Ondrej PPA and install the package from there instead.
Switching Multiple PHP Versions
Assuming you’ve installed multiple PHP versions, you can check the available versions with this command.
sudo update-alternatives --config php
Here, you can use the selection number to change the default PHP version for the CLI.
Alternatively, you can also run sudo update-alternatives --set php /usr/bin/php<version>
to directly change the default version. You can use php -v
to confirm the change.
Similarly, you can change the PHP version for Apache with the following process:
sudo a2dismod php<versiontodisable>
sudo a2enmod php<versiontoenable>
sudo systemctl restart apache2
For NGINX, I’ll need to edit my server block configuration and change the PHP version there.
First, I’ll open the config file.
sudo nano /etc/nginx/sites-available/default
In the location block, change the PHP version to your liking:
fastcgi_pass unix:/run/php/php<version>-fpm.sock;
Save the changes and exit the editor.
Reload nginx to apply the changes.
sudo systemctl nginx reload
Managing & Monitoring PHP
mod-php runs as a shared Apache module, while php-fpm runs as its own process. This means you can manage the latter with systemd commands. For instance, you can check the status of the PHP service with
sudo systemctl status php<version>-fpm
You can manage the PHP service in the same manner by replacing status with start, stop, restart, enable, disable, and so on.
In the case of Apache, you’ll need to manage the Apache service itself. For instance, to reload the PHP module, reload Apache with
sudo systemctl reload apache2
Uninstalling PHP
If you only want to remove the PHP package, use either of the following:
sudo apt remove php<version>
sudo apt remove php<version>-fpm
Sometimes you’ll need to remove the configuration files as well. Use purge instead for this:
sudo apt purge php<version>
sudo apt purge php<version>-fpm
Afterward, remove any lingering libraries with:
sudo apt autoremove
Important Files and Directories
PHP uses different initialization files depending on how it’s run. The config file for PHP when it runs from the Apache module is /etc/php/<phpversion>/apache2/php.ini.
When it runs from the FPM module, it uses /etc/php/<phpversion>/fpm/php.ini instead.
And when it’s run from the CLI, it uses /etc/php/cli/<phpversion>/php.ini.
The installed modules are located in the /etc/php/<version>/mods-available directory.
The loaded PHP modules can be found in the following directories depending on how PHP was run:
Apache: /etc/php/<version>/apache2/conf.d/
PHP FPM: /etc/php/<version>/fpm/conf.d/
PHP CLI: /etc/php/<version>/cli/conf.d/