PHP

Installation

Ubuntu/Debian

Add PHP Repository (for multiple versions)

# Add Ondrej PHP PPA (supports multiple PHP versions)
sudo apt update
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

Install PHP (Latest)

# Install PHP with common extensions
sudo apt install php php-fpm php-mysql php-curl php-gd php-zip php-xml php-mbstring -y
 
# Verify installation
php --version

Install Specific PHP Version

# Install PHP 7.4
sudo apt install php7.4 php7.4-fpm php7.4-mysql php7.4-curl php7.4-gd -y
 
# Install PHP 8.0
sudo apt install php8.0 php8.0-fpm php8.0-mysql php8.0-curl php8.0-gd -y
 
# Install PHP 8.1
sudo apt install php8.1 php8.1-fpm php8.1-mysql php8.1-curl php8.1-gd -y
 
# Install PHP 8.2
sudo apt install php8.2 php8.2-fpm php8.2-mysql php8.2-curl php8.2-gd -y

CentOS/RHEL

# Enable REMI repository
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
 
# Enable PHP 8.1 from REMI
sudo yum-config-manager --enable remi-php81
 
# Install PHP
sudo yum install php php-fpm php-mysql php-curl php-gd php-zip php-xml -y
 
# Verify installation
php -v

PHP Extensions

Install Additional Extensions

# Ubuntu/Debian
sudo apt install php-{extension-name} -y
 
# Common extensions:
# php-mysql, php-pgsql, php-sqlite3
# php-curl, php-http
# php-gd, php-imagick
# php-zip, php-rar
# php-xml, php-json
# php-mbstring, php-iconv
# php-soap, php-xmlrpc
# php-opcache, php-xdebug

Check Installed Extensions

# List all loaded extensions
php -m
 
# Get extension info
php -i | grep -i "extension_dir"
 
# Check specific extension
php -m | grep mysql

PHP Configuration

Configuration Files

# Main configuration
/etc/php/{version}/fpm/php.ini      # PHP-FPM
/etc/php/{version}/apache2/php.ini  # Apache
/etc/php/{version}/cli/php.ini      # CLI
 
# FPM pool configuration
/etc/php/{version}/fpm/pool.d/www.conf

Common Settings

# Memory limit
memory_limit = 256M
 
# Max upload size
upload_max_filesize = 100M
post_max_size = 100M
 
# Max execution time
max_execution_time = 300
 
# Display errors (development only)
display_errors = On
display_startup_errors = On
 
# Logging
error_reporting = E_ALL
log_errors = On
error_log = /var/log/php/error.log
 
# Timezone
date.timezone = UTC

PHP-FPM Service Management

# Start PHP-FPM
sudo systemctl start php8.1-fpm
 
# Stop PHP-FPM
sudo systemctl stop php8.1-fpm
 
# Restart PHP-FPM
sudo systemctl restart php8.1-fpm
 
# Enable on boot
sudo systemctl enable php8.1-fpm
 
# Check status
sudo systemctl status php8.1-fpm

Composer Installation

# Download Composer installer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
 
# Verify installer
php -r "if (hash_file('sha384', 'composer-setup.php') === 'xxx') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
 
# Install Composer
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
 
# Verify installation
composer --version
 
# Make global
sudo mv /usr/local/bin/composer /usr/local/bin/composer

Multiple PHP Versions

Switch Between Versions (CLI)

# List installed versions
update-alternatives --list php
 
# Set default PHP version
sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.1 81
sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.2 82
 
# Switch default version
sudo update-alternatives --config php

Switch FPM Pool

# Edit Nginx configuration to use different PHP version
# Change:
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
# To:
fastcgi_pass unix:/run/php/php8.2-fpm.sock;