PHP Setup

Last Edit: 2024.07.30

Debian / Ubuntu

Fedora / Rocky / RHEL

Overview

Install PHP for web applications on a Linux server.

Assumptions

Update

Before getting started, update package repositories and apply upgrades for the latest patches.

# Debian
sudo apt update
sudo apt upgrade
# Fedora
sudo dnf check-update
sudo dnf upgrade

Install PHP

The PHP version available in most default package repositories can be outdated. Add a PHP repository to your system to install more recent versions of PHP. For Debian distributions this will be the Ondřej Surý PPA, and for Fedora distros this will be the Remi repository.

Debian

Verify that the required packages software-properties-common and apt-transport-https are installed.

sudo apt install software-properties-common apt-transport-https

Some systems may require the python3-launchpadlib package.

Add the ondrej/php PPA to the system sources.

# Debian
sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
# Ubuntu
sudo add-apt-repository ppa:ondrej/php

Update package sources and install PHP. The latest PHP release will be installed by default. To install a specific version, append the version number: php8.1 php7.4.

sudo apt install php

Fedora

Verify that the required package dnf-utils is installed.

sudo dnf install dnf-utils

Add the remirepo.net repository to the system sources.

sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

Select the latest version of PHP, or the version required by your application. View available module versions using dnf module list php.

sudo dnf module enable php:remi-8.1

Install the selected PHP version.

sudo dnf install php

PHP Extensions

Refer to the PHP Manual Extension List for available packages. To install an extension, install the system package with the naming scheme php-extensionname.

# Debian
sudo apt install php-openssl
# Fedora
sudo dnf install php-openssl

Common Dependencies

Common packages required to integrate PHP with other applications.

Apache

# Debian
sudo apt install libapache2-mod-php

Nginx

# Debian
sudo apt install php-fpm
# Fedora
sudo dnf install php-fpm

MariaDB / MySQL

# Debian
sudo apt install php-mysql
# Fedora
sudo dnf install php-mysqlnd