How to Install PHP on Ubuntu Server 20.04 or 22.04 or 23
PHP is a script-based server-side programming language, which can help to automate various server tasks. It handles tasks such as dynamic content, database requests, and data preprocessing/display.
PHP is used by many CMS such as Wordpress, Prestashop and others.
In order to install PHP you will need the following:
- A Linux system Linux VPS running Ubuntu 20.04 or 22.04;
- Access to an Ubuntu user account with sudo privileges;
- Access to a command line/terminal window (Ctrl+Alt+T); and
- A running web server (Apache or Nginx).
Note: PHP has several different release versions and is updated on a regular basis. Currently, PHP 7.4 is EOL and 8.1 are the recommended and supported by most used versions of the software available.
Installing PHP on Ubuntu ( 20.04 or 22.04 or 23 )
Installing PHP on Ubuntu can be accomplished using the apt package manage, you will need to follow these steps:
- Update the package list:
- Install the PHP package along with any required dependencies:
- To check if PHP is installed correctly, run the following command:
This will display the version of PHP installed on your system.
- If you need to install a specific PHP version, you can do so by specifying the version number after the package name. For example, to install PHP 7.4, you can use the following command:
- Once you have installed PHP, you can also install additional PHP extensions if needed. You can search for available extensions using the apt-cache command. For example, to search for the mysqli extension, you can use the following command:
- To install the mysqli extension, run the following command:

That's it! PHP is now installed and ready to use on your Ubuntu 20.04 or 22.04 system.
Remember that PHP works in tandem with an existing server, so you will need to install it specifically to your server software. Alternatively, you can always install a stable version from the default Ubuntu software repositories.
If you are running an Apache or Nginx web server and need to install PHP, please follow the applicable steps below.
Installing PHP with Apache on Ubuntu
- Install Apache web server:
- Install PHP and required modules:
- Restart Apache to enable the PHP module:
- Create a PHP test file to verify that PHP is working properly. For example, create a file named
info.php
in the/var/www/html
directory:
- Add the following PHP code to the file and save it:
- Now, open a web browser and navigate to
http://your_server_ip/info.php
. You should see a page with information about the PHP version and configuration.
That's it! You can now start developing and deploying PHP applications on your server.
Installing PHP with Nginx on Ubuntu
- Install Nginx web server:
- Install PHP and required modules:
- Configure Nginx to use PHP-FPM:
- Find the following block of code:
- Uncomment the block and modify the
fastcgi_pass
line to point to the PHP-FPM socket. In this example, we are using PHP 7.4:
-
Save and close the file.
-
Restart Nginx and PHP-FPM:
- Create a PHP test file to verify that PHP is working properly. For example, create a file named
info.php
in the/var/www/html
directory:
- Add the following PHP code to the file and save it:
- Now, open a web browser and navigate to
http://your_server_ip/info.php
. You should see a page with information about the PHP version and configuration.
That's it! You can now start developing and deploying PHP applications on your server.
Installing additional PHP modules on Ubuntu
To install additional PHP modules on Ubuntu 20.04 or 22.04, you can use the apt
package manager and perform the following steps:
- Search for available PHP modules using the
apt-cache
command. For example, to search for theimagick
module, you can use the following command:
- Install the desired PHP module(s) using the
apt
command. For example, to install theimagick
module, you can use the following command:
- Restart the web server to enable the new module(s):
You have now successfully installed additional PHP modules on your Ubuntu system, and can repeat these steps to install any other PHP module(s) that you may need.
How to Uninstall PHP on Ubuntu
To uninstall PHP on Ubuntu 20.04 or 22.04, you can use the following command:
This will remove the PHP packages and any dependencies that are no longer needed. If you want to completely remove all configuration files associated with PHP, you can use the following command:
This will remove the PHP packages and all configuration files associated with them. Note that this will also remove any PHP modules and extensions that you may have installed. If you only want to remove specific PHP modules, you can use the apt remove
command with the name of the module you want to remove, for example:
This will remove the php-imagick
module but leave the rest of the PHP installation intact.