LochStudios  /  Help Centre  /  VPS & Linux  /  Install PHP and Common Extensions

Install PHP and Common Extensions

Install PHP 8.x with essential extensions (MySQL, cURL, JSON, GD) on Ubuntu/Debian or RHEL/CentOS.

Updated

PHP is a popular server-side scripting language. This guide walks you through installing PHP and the most commonly needed extensions (MySQL, cURL, JSON, GD, Zip) on your VPS.

Step 1: Update your package manager

First, refresh your package lists:

sudo apt update

On RHEL/CentOS:

sudo dnf update -y

Step 2: Add the PHP repository (optional but recommended)

Ubuntu's default repos often include older PHP versions. To get the latest stable PHP (8.2 or 8.3), add the Ondrej PPA:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

On RHEL/CentOS, PHP 8+ is available in the default repos or via EPEL/Remi.

Step 3: Install PHP and common extensions

Ubuntu/Debian:

sudo apt install php8.3 php8.3-cli php8.3-fpm php8.3-mysql php8.3-curl php8.3-gd php8.3-zip php8.3-json php8.3-mbstring php8.3-xml -y

(Replace 8.3 with 8.2 or 8.1 if you prefer an older version.)

RHEL/CentOS:

sudo dnf install php php-cli php-fpm php-mysql php-curl php-gd php-zip php-json php-mbstring php-xml -y

Step 4: Verify the installation

Check the PHP version:

php -v

List installed modules:

php -m

You should see curl, json, gd, zip, mysql, mbstring, and xml listed.

Step 5: Start and enable PHP-FPM (optional, if using Nginx)

If you're running Nginx, enable the PHP FastCGI Process Manager:

sudo systemctl start php8.3-fpm
sudo systemctl enable php8.3-fpm
sudo systemctl status php8.3-fpm

On RHEL/CentOS:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm
sudo systemctl status php-fpm

Step 6: Configure PHP (optional)

Edit the PHP configuration file to adjust memory limits, max upload size, or timezone:

sudo nano /etc/php/8.3/fpm/php.ini

Common settings to adjust:

memory_limit = 256M          # Default is 128M
upload_max_filesize = 100M   # Default is 2M
post_max_size = 100M         # Should match upload_max_filesize
date.timezone = UTC          # Set to your timezone (e.g., Australia/Sydney)

Save and exit (Ctrl+X, then Y, then Enter).

On RHEL/CentOS, the file is at /etc/php.ini.

Restart PHP-FPM to apply changes:

sudo systemctl restart php8.3-fpm

Step 7: Test PHP with a simple script

Create a test file in your web root:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php

Visit http://your-server-ip/phpinfo.php in your browser. You should see a detailed PHP information page with all loaded modules.

Once confirmed, remove the test file for security:

sudo rm /var/www/html/phpinfo.php

Step 8: Install additional extensions (if needed)

Common optional extensions include:

sudo apt install php8.3-redis php8.3-imagick php8.3-soap -y

For database support beyond MySQL, add:

sudo apt install php8.3-pgsql php8.3-sqlite3 -y

Always restart PHP-FPM after installing new extensions:

sudo systemctl restart php8.3-fpm

Tips

  • PHP with Apache: If using Apache, install libapache2-mod-php8.3 instead of php8.3-fpm, then reload Apache (sudo systemctl reload apache2).
  • Multiple PHP versions: You can install and run multiple PHP versions on the same server (e.g., 8.1, 8.2, 8.3) and switch per project.
  • Check loaded extensions: Use php -i | grep -i <extension-name> to verify a specific extension is active.
  • View current config: Run php -i to see all configuration values.
  • Troubleshooting: If extensions don't load after install, check /var/log/php8.3-fpm.log for errors.

Was this article helpful?

← Back to VPS & Linux