PHP 8.1

Installation

Add PHP Repository

# Update system
sudo apt update
 
# Add Ondrej PHP PPA (for multiple PHP versions)
sudo add-apt-repository ppa:ondrej/php -y
 
# Update package list
sudo apt update

Install PHP 8.1 and Extensions

# Install PHP 8.1 CLI and common extensions
sudo apt install php8.1 php8.1-mysql php8.1-curl php8.1-gd php8.1-xml php8.1-mbstring -y
 
# Install additional common extensions
sudo apt install php8.1-zip php8.1-json php8.1-bcmath php8.1-opcache -y
 
# Verify installation
php8.1 --version

Install PHP-FPM

# Install PHP-FPM
sudo apt install php8.1-fpm -y
 
# Start and enable PHP-FPM
sudo systemctl start php8.1-fpm
sudo systemctl enable php8.1-fpm
 
# Verify service is running
sudo systemctl status php8.1-fpm
 
# Check PHP-FPM socket
ls -la /run/php/php8.1-fpm.sock

Ionium Cube Loader (Optional)

Installation

# Download Ionium Cube loader
sudo wget http://downloads3.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz
 
# Extract to /usr/local
sudo tar xzf ioncube_loaders_lin_x86-64.tar.gz -C /usr/local
 
# Clean up
sudo rm -rf ioncube_loaders_lin_x86-64.tar.gz
 
# Verify installation
ls /usr/local/ioncube

Configure for PHP-FPM

# Edit PHP-FPM configuration
sudo nano /etc/php/8.1/fpm/php.ini
 
# Add to the file (find the [Zend] section):
zend_extension = /usr/local/ioncube/ioncube_loader_lin_8.1.so
 
# Restart PHP-FPM
sudo systemctl restart php8.1-fpm
 
# Verify Ionium Cube is loaded
php8.1 -m | grep ionCube

Configure for Apache (if using Apache)

# Edit Apache PHP configuration
sudo nano /etc/php/8.1/apache2/php.ini
 
# Add to file:
zend_extension = /usr/local/ioncube/ioncube_loader_lin_8.1.so
 
# Restart Apache
sudo service apache2 restart

Configure for PHP-CLI

# Edit CLI PHP configuration
sudo nano /etc/php/8.1/cli/php.ini
 
# Add to file:
zend_extension = /usr/local/ioncube/ioncube_loader_lin_8.1.so
 
# Test in CLI
php8.1 -v
php8.1 -m | grep ionCube

Configuration

Edit PHP Configuration

# For PHP-FPM
sudo nano /etc/php/8.1/fpm/php.ini
 
# For CLI
sudo nano /etc/php/8.1/cli/php.ini
 
# For Apache (if using)
sudo nano /etc/php/8.1/apache2/php.ini

Common Settings

# Memory limit
memory_limit = 256M
 
# Max upload size
upload_max_filesize = 100M
post_max_size = 100M
 
# Execution time
max_execution_time = 300
 
# Display errors (development only)
display_errors = On
display_startup_errors = On
 
# Error reporting
error_reporting = E_ALL
log_errors = On
error_log = /var/log/php8.1-fpm.log
 
# Timezone
date.timezone = UTC
 
# Short open tags (if needed)
short_open_tag = On

PHP-FPM Configuration

Edit FPM Pool Configuration

# Edit www pool
sudo nano /etc/php/8.1/fpm/pool.d/www.conf

Optimization Settings

; Process management
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
 
; Request timeout
request_terminate_timeout = 60
 
; Slow request logging
slowlog = /var/log/php8.1-fpm/slow.log
request_slowlog_timeout = 5
 
; Error logging
error_log = /var/log/php8.1-fpm/error.log
 
; User and group
user = www-data
group = www-data
 
; Listen socket
listen = /run/php/php8.1-fpm.sock
listen.owner = www-data
listen.group = www-data

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
 
# Check status
sudo systemctl status php8.1-fpm
 
# View logs
tail -f /var/log/php8.1-fpm/error.log
tail -f /var/log/php8.1-fpm/slow.log

Nginx Configuration with PHP 8.1

server {
    listen 80;
    server_name example.com;
    root /var/www/example.com/public;
 
    index index.php index.html;
 
    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    location ~ /\. {
        deny all;
    }
}

Extensions Installation

Install Additional Extensions

# Database
sudo apt install php8.1-pgsql php8.1-sqlite3 -y
 
# Image processing
sudo apt install php8.1-imagick -y
 
# Caching
sudo apt install php8.1-redis php8.1-memcached -y
 
# Other useful
sudo apt install php8.1-soap php8.1-xmlrpc php8.1-intl -y

List Installed Extensions

# Via command line
php8.1 -m
 
# Via info page
php8.1 -i | grep -i "extension"
 
# Check specific extension
php8.1 -m | grep mysql

Composer with PHP 8.1

# Install Composer (if not already installed)
curl -sS https://getcomposer.org/installer | php8.1 -- --install-dir=/usr/local/bin --filename=composer
 
# Verify
composer --version
 
# Use with PHP 8.1
php8.1 composer.phar install

Troubleshooting

# Check PHP-FPM socket permissions
ls -la /run/php/php8.1-fpm.sock
 
# Fix permissions if needed
sudo chown www-data:www-data /run/php/php8.1-fpm.sock
sudo chmod 660 /run/php/php8.1-fpm.sock
 
# Test PHP syntax
php8.1 -l index.php
 
# Test FPM connectivity
sudo -u www-data php8.1 -r 'phpinfo();'
 
# Check loaded modules
php8.1 -m
 
# View full PHP info
php8.1 -i

Uninstall

# Remove PHP 8.1
sudo apt remove php8.1 php8.1-fpm -y
sudo apt autoremove -y
 
# Remove configuration
sudo rm -rf /etc/php/8.1