Configure PHP on a Debian or Ubuntu instance

Overview

PHP is a popular server-side scripting language used to build websites and other backend functionality. This article explains how to install PHP on a DreamCompute instance running Debian or Ubuntu with Apache.

View the Getting started article and make sure you have completed the following steps before proceeding.

 

Step 1 — Add the repository

You must first run the following command to add a repository before installing PHP.

[user@instance]:$ sudo add-apt-repository ppa:ondrej/php

Step 2 — Update packages

Run the following to update your list of packages.

[user@instance]:$ sudo apt update

Step 3 — Install PHP

You have the choice to install PHP as a module or FPM.

Installing as a PHP module

Run the following commands to install and load PHP.

[user@instance]:$ sudo apt install php8.0 libapache2-mod-php8.0

Installing as FPM

Run the following commands to install, enable, and load PHP-FPM.

[user@instance]:$ sudo apt install php8.0-fpm libapache2-mod-fcgid

Step 4 — Confirm PHP is installed

Finally, run the following command to view the version you installed.

[user@instance]:$ php -v
PHP 8.0.5 (cli) (built: May  3 2021 11:30:57) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.5, Copyright (c), by Zend Technologies

Install modules (extensions)

After PHP is installed, you can proceed with installing any modules (also called extensions) needed.

View available modules you can install

This command lists all modules available for you to install.

[user@instance]:$ sudo apt search php8.0-*

Install a module

After locating the correct name of a module, you can install it using the following command.

[user@instance]:$ sudo apt install php8.0-sqlite3

You can also install several modules at once. This assumes the module names start with php8.0-.

[user@instance]:$ sudo apt install php8.0-{mysql,imap,zip}

View modules currently loaded

The following command lists all modules currently loaded by PHP.

[user@instance]:$ php -m

Configuration Files

There are a few configuration files for PHP that are used for the CLI and the Apache modules (or PHP-FPM) specifically. Change <version> to your specific version of PHP.

/etc/php/cli/<version>/php.ini

The primary configuration file for PHP when executed from the CLI. 

/etc/php/<version>/apache2/php.ini

The primary configuration file for PHP when run from the Apache module.

/etc/php/<version>/fpm/php.ini

The primary configuration file for PHP when run from as PHP-FPM.

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?