How to Install PHP on Ubuntu VPS – a Comprehensive Guide

Installing PHP on Linux is a straightforward process, but doing it on a VPS gives you more control and flexibility. Whether you’re setting up a new server for a project or migrating an existing site, a Linux PHP install ensures you can run frameworks and content management systems exactly the way you want. Using a PHP VPS means you can fine-tune performance, adjust configurations, and keep your environment secure.

This guide walks you through how to install PHP on Linux distributions like Ubuntu, covering everything from updating package lists to configuring it with Nginx or Apache. You’ll also learn how to add extensions, test your installation, and set up the server for long-term stability and performance.

How to Install PHP on Ubuntu

When it comes to installing PHP, Ubuntu offers a reliable and straightforward process. The official repositories include multiple PHP versions, so you can choose the one that best fits your project. Setting up PHP on Ubuntu is quick, and the same steps work across most modern Ubuntu releases.

In the next subsections, we’ll update the system’s package list, install PHP itself, run command line tests, and set it up with popular web servers like Nginx and Apache. You’ll also see how to install PHP extensions to add extra functionality for your applications.

Package list update

Before installing any software, it’s best to make sure your system knows about the latest available packages. On Ubuntu, this is done with the apt update command. Open your terminal and run:

sudo apt update

This refreshes the package index so your VPS can pull the newest versions from the repositories. Skipping this step might leave you with outdated dependencies, which could cause issues during or after the PHP installation.

Install PHP on Ubuntu

Once your package list is up to date, you can proceed with the installation. Ubuntu makes this easy with the apt package manager. To perform an Ubuntu PHP install, run:

sudo apt install php

This will install the default PHP version provided by Ubuntu’s repositories, along with the basic modules needed for most applications.

If you need a specific version, such as PHP 8.2, you can specify it explicitly:

sudo apt install php8.2

After the process finishes, PHP is ready to use on your VPS. In the next step, we’ll verify the installation from the command line.

Command line PHP tests

After installation, it’s a good idea to confirm that PHP is working correctly from the terminal. This helps verify that the PHP command line interface is available and running the expected version.

Check your PHP version with:

php -v

You should see details about the installed version, along with build information. To run a quick test, you can also execute:

php -r 'echo "PHP is running\n";'

If you see the output without errors, your command line PHP setup is working as intended.

Install PHP with Nginx

If you’re using Nginx as your web server, you’ll need to install PHP along with PHP-FPM (FastCGI Process Manager) to handle dynamic content. Unlike Apache, Nginx doesn’t process PHP natively, so PHP-FPM acts as the bridge between the server and your scripts.

Install PHP-FPM with:

sudo apt install php-fpm

Once installed, configure Nginx to pass PHP requests to PHP-FPM. Open your site’s Nginx configuration file (usually in /etc/nginx/sites-available/) and update the server block with:

location ~ \.php$ {

    include snippets/fastcgi-php.conf;

    fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;

}

Replace php8.2-fpm.sock with the correct version for your installation. After you configure Nginx, restart the server:

sudo systemctl restart nginx

Your Nginx setup can now serve PHP pages without issues.

Install PHP with Apache

Apache can process PHP directly through the libapache2-mod-php module, making setup simple. To install PHP with Apache, run:

sudo apt install php libapache2-mod-php

This will enable PHP support for your Apache server automatically. If you need a specific PHP version, include it in the command, for example:

sudo apt install php8.2 libapache2-mod-php8.2

After installation, Apache will recognize and execute .php files without extra configuration. If you want to tweak settings or configure Apache for better performance, you can edit the PHP settings in /etc/php/8.2/apache2/php.ini and then restart Apache:

sudo systemctl restart apache2

Your server is now ready to serve PHP content through Apache.

Install PHP extensions

Many PHP applications require extra functionality beyond the core installation. This is where PHP extensions come in — they add features such as database drivers, image processing, or encryption support.

To install PHP extensions, use:

sudo apt install php-[extension_name]

For example, to enable MySQL support:

sudo apt install php-mysql

You can install multiple extensions at once:

sudo apt install php-mysql php-xml php-curl

After installing, restart your web server or PHP-FPM so the changes take effect. You can check which extensions are active by running:

php -m

With the right extensions in place, PHP on your Ubuntu VPS can run a wider range of applications without compatibility issues.

What to do after installing PHP on Ubuntu VPS

Once PHP is installed, there are a few important steps to prepare your server for real-world use.

  1. Install a database server
    Most web applications need a database. You can install MySQL with:
sudo apt install mysql-server

To learn more about configuring and using MySQL, check our guide: Open-Source Databases Series – MySQL.

Or, if you prefer MariaDB, run:

sudo apt install mariadb-server

We also have a dedicated article for MariaDB here: Open-Source Databases Series – MariaDB.[JN1] [TM2] [JN3] 

  • Set up a Mail Transfer Agent (MTA)


An MTA allows your applications to send emails, such as password resets or notifications. Popular choices include Postfix and Exim. Install Postfix with:

sudo apt install postfix

You can follow our complete mail server setup tutorial here: Linux Mail Server Setup and Configuration.

  • Secure your installation

Configure database user permissions, enable firewalls, and review your php.ini settings to disable unsafe functions and set proper limits. For additional security, consider using Fail2Ban (What Is Fail2Ban and How to Use It on a VPS) and setting up a software firewall (How to Setup a Software-Firewall in Linux and Windows).

  • Test your full stack

Run a test PHP script connected to your database to make sure PHP, the web server, and your database work together smoothly. Completing these steps will ensure your VPS is ready for production workloads.

Install PHP on Ubuntu with Contabo – Step-by-Step

If you’re hosting on a Contabo VPS, the process of installing PHP on Ubuntu follows the same steps we’ve covered, but with the added benefit of reliable infrastructure and full root access.

  1. Log in to your VPS
    Use SSH to connect:
ssh username@your_server_ip
  • Update package lists
sudo apt update
  • Install PHP
sudo apt install php
  • Set up with your web server
    If you’re running Nginx, install PHP-FPM and configure Nginx. For Apache, enable the libapache2-mod-php module.
  • Add required PHP extensions
    Install any extensions your application needs, then restart your web server.

If you don’t have a VPS yet or want to upgrade, you can explore the available options on Linux VPS Hosting from Contabo.

FAQ on how to install PHP on Ubuntu VPS

Q1: How do I install PHP extensions on Ubuntu?
Use the package manager with the extension name. For example:

sudo apt install php-mysql

Replace php-mysql with the specific extension you need.

Q2: How can I check or increase the PHP memory limit?
Open your php.ini file (location depends on your PHP version and server setup) and look for:

memory_limit = 128M

Adjust the value, save the file with CTRL+O, exit the editor with CTRL+X,[IS4]  and restart your web server or PHP-FPM with systemctl restart apache2 or systemctl restart php-fpm[IS5] .

Q3: How do I switch between PHP versions?
You can install multiple versions (e.g., PHP 7.4, PHP 8.2) and use update-alternatives to switch between them:

sudo update-alternatives –config php

This will display a list of installed versions and let you choose the default:[IS6] 

Install PHP on Ubuntu VPS - Switch between PHP versions

Q4: How do I secure PHP for production?
To secure PHP for production, disable unsafe functions like exec() and phpinfo() by adding the line disable_functions = exec,phpinfo to your php.ini file, limit resource usage by setting directives like memory_limit = 128M in the same file, and keep your installation up to date using server commands like sudo apt upgrade php and project commands like composer update. Consider using Fail2Ban and a firewall for extra protection.

Q5: What is PHP-FPM and when should I use it?
PHP-FPM (FastCGI Process Manager) is a PHP process manager designed for high performance, especially when paired with Nginx. It can handle more concurrent requests efficiently compared to the default PHP module in Apache, making it ideal for high-traffic e-commerce sites during a sale, or for applications processing long-running tasks like a video upload without slowing down the site for other users.

Scroll to Top