FedoraRHEL Based

How To Install PHP on Fedora 42

Install PHP on Fedora 42

PHP 8.4 represents the latest stable release in the PHP programming language, bringing significant improvements and new features to enhance web development capabilities. As Fedora 42 includes PHP 8.4 as its default PHP version, understanding how to properly install and configure this powerful scripting language is essential for developers and system administrators alike. This comprehensive guide will walk you through every aspect of installing PHP 8.4 on Fedora 42, from basic setup to advanced configurations.

Table of Contents

Understanding PHP 8.4 Features

PHP 8.4 introduces several exciting new features and improvements that make it worth upgrading from previous versions. Released as a feature-rich update, this version brings substantial enhancements to the language’s capabilities.

Key New Features

PHP 8.4 includes property hooks for computed properties, allowing developers more control over property behaviors. It also introduces independent control of property read/write scope, providing more granular access controls. The new #[\Deprecated] attribute makes it easier to mark functions, methods, and classes as deprecated, improving code maintenance practices.

Array Function Improvements

The language now offers several new array manipulation functions that streamline common operations:

  • array_find() – Efficiently locates elements in arrays
  • array_find_key() – Searches for specific keys
  • array_any() – Tests if any element meets a condition
  • array_all() – Checks if all elements satisfy a condition

Database Improvements

PHP 8.4 introduces dedicated PDO subclasses for different database systems, including Pdo\Dblib, Pdo\Firebird, and Pdo\MySql. This changes enhance type safety and improve IDE integration for database operations.

Performance and Security

Beyond new features, PHP 8.4 delivers significant performance improvements over previous versions. The password hashing default Bcrypt cost has been increased from 10 to 12, enhancing security by default. Additionally, the minimum required OpenSSL version has been increased to 1.1.1, ensuring better cryptographic capabilities.

Prerequisites for Installation

Before installing PHP 8.4 on your Fedora 42 system, ensure you meet all necessary requirements and prepare your environment properly.

System Requirements

Fedora 42 comes with built-in support for PHP 8.4, making it an ideal platform for running the latest PHP version. Verify you have sufficient disk space (at least 1GB free) and ensure your system is up-to-date.

Checking Existing PHP Installations

If you’ve previously installed PHP on your system, it’s important to check which version is currently active. Run the following command to check:

php -v

Backing Up Existing PHP Applications

Before making any changes to your PHP installation, back up any existing PHP applications and configuration files. This preventive measure ensures you can recover your environment if anything goes wrong during the installation process.

Update Your System

It’s essential to update your Fedora system before installing PHP 8.4:

sudo dnf update -y

This ensures compatibility and prevents potential conflicts during installation.

Installation Methods Overview

Fedora 42 offers multiple ways to install PHP 8.4, each with its own advantages. Understanding these options helps you choose the most appropriate method for your specific needs.

Default Fedora Repositories vs. Remi Repository

PHP 8.4 comes as the default version in Fedora 42, available through the standard repositories. However, the Remi repository offers alternative installation options that may include more up-to-date packages and extensions.

Module-Based vs. Package-Based Installation

Fedora supports both module-based and package-based PHP installations. The module-based approach offers better version management, while package-based installations provide more granular control over components.

Considerations for Different Environments

For production environments, stability is paramount, making the default repositories a solid choice. Development environments might benefit from Remi’s repository for access to the latest updates and extensions.

Method 1: Installing PHP 8.4 from Default Fedora Repositories

Installing PHP 8.4 from Fedora’s default repositories is straightforward and ensures official support from the Fedora Project.

Basic Installation

Since PHP 8.4 is the default version in Fedora 42, you can install it with a simple DNF command:

sudo dnf install php

This command installs the core PHP package. To verify the installation:

php -v

You should see output indicating PHP 8.4 is now installed.

Installing Additional Packages

For a more complete PHP installation, include additional packages that provide common functionalities:

sudo dnf install php php-cli php-common php-fpm

Advantages of Default Repositories

Using the default repositories ensures you receive regular updates through Fedora’s standard update mechanisms. Additionally, these packages undergo thorough testing by the Fedora community, enhancing stability and reliability.

Method 2: Installing PHP 8.4 using Remi Repository

The Remi repository offers an alternative installation method that may provide more up-to-date packages and additional extensions not available in the default repositories.

Adding the Remi Repository

First, install the Remi repository configuration package:

sudo dnf install https://rpms.remirepo.net/fedora/remi-release-42.rpm

Enabling PHP 8.4 Module

After adding the repository, you can reset any existing PHP module and install PHP 8.4:

sudo dnf module reset php
sudo dnf module install php:remi-8.4

This command switches to the PHP 8.4 module from the Remi repository.

Alternative: Parallel Installation

If you want to test PHP 8.4 alongside an existing PHP installation, you can install it as a Software Collection:

sudo dnf install php84

This method creates a parallel installation that doesn’t affect your system’s default PHP version.

Verifying Installation

Check that PHP 8.4 has been properly installed:

php -v

The output should confirm you’re running PHP 8.4 from the Remi repository.

Configuring PHP 8.4 with Apache Web Server

Apache is a popular web server choice for PHP applications. Configuring PHP 8.4 to work with Apache requires a few specific steps.

Installing Apache and PHP Module

Install Apache and the PHP module for Apache:

sudo dnf install httpd php php-fpm libapache2-mod-php

Configuring Apache for PHP

Edit the Apache configuration to properly handle PHP files. Open the configuration file:

sudo nano /etc/httpd/conf.d/php.conf

Ensure the following directives are included:

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Restart Apache Service

After making configuration changes, restart Apache:

sudo systemctl restart httpd

Testing PHP with Apache

Create a test PHP file to verify the integration:

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

Access this file through your web browser at http://your_server_ip/info.php. You should see the PHP information page, confirming Apache is correctly processing PHP files.

Configuring PHP 8.4 with Nginx Web Server

Nginx offers excellent performance characteristics and is increasingly popular for PHP applications. Setting up PHP 8.4 with Nginx involves configuring PHP-FPM.

Installing Nginx and PHP-FPM

Install Nginx and PHP-FPM:

sudo dnf install nginx php-fpm

Configuring PHP-FPM

PHP-FPM is required for Nginx to process PHP files. Ensure PHP-FPM is properly configured:

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

Configuring Nginx for PHP Processing

Edit the Nginx server block configuration:

sudo nano /etc/nginx/conf.d/default.conf

Add or modify the PHP processing section:

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php-fpm/www.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}

Restart Services

Apply the changes by restarting both Nginx and PHP-FPM:

sudo systemctl restart nginx
sudo systemctl restart php-fpm

Testing PHP with Nginx

Create a test PHP file and access it through your web browser to verify the configuration is working correctly.

Installing Common PHP Extensions

PHP extensions enhance functionality and enable integration with databases, image processing, and other services. Installing the right extensions is crucial for most web applications.

Essential Extensions

Install common PHP extensions needed by most applications:

sudo dnf install php-mysqlnd php-pgsql php-redis php-curl php-json php-gd php-xml php-mbstring php-zip

This installs support for MySQL, PostgreSQL, Redis, cURL, JSON processing, image manipulation, XML parsing, multibyte string handling, and ZIP file operations.

Database Connectivity Extensions

For database connectivity, install the appropriate extensions:

sudo dnf install php-mysqlnd php-pgsql

The php-mysqlnd package enables PHP to connect to MySQL databases, while php-pgsql provides PostgreSQL support.

Web Development Extensions

For web development, additional extensions are useful:

sudo dnf install php-gd php-xml php-mbstring php-zip

These extensions handle image processing, XML parsing, multibyte character encoding, and ZIP file operations.

Verifying Installed Extensions

Check which extensions are installed and active:

php -m

This command lists all loaded PHP modules, allowing you to confirm your extensions are properly installed.

Optimizing PHP 8.4 Configuration

Proper configuration of PHP is essential for optimal performance and security. Understanding key configuration parameters helps you tailor PHP to your specific needs.

Locating and Editing php.ini

The main PHP configuration file is located at /etc/php.ini. Open it for editing:

sudo nano /etc/php.ini

Critical Performance Settings

Adjust these settings based on your server resources and application requirements:

  • memory_limit = 256M – Increases available memory for PHP scripts
  • max_execution_time = 120 – Allows scripts to run longer
  • post_max_size = 64M – Increases maximum POST data size
  • upload_max_filesize = 64M – Increases maximum file upload size

Opcache Configuration

Enable and optimize Opcache for better performance:

opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1

Error Handling

For production environments, configure error handling to log errors rather than displaying them:

display_errors = Off
log_errors = On
error_log = /var/log/php/error.log

Create the log directory if it doesn’t exist:

sudo mkdir -p /var/log/php
sudo chmod 777 /var/log/php

Testing PHP Installation

After installation and configuration, thorough testing ensures everything is working correctly.

Creating a Test File

Create a simple PHP info file:

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

Command-Line Testing

Verify PHP is working from the command line:

php -v
php -m

These commands display the PHP version and loaded modules.

Web Server Testing

Access your test file through a web browser:

http://your_server_ip/info.php

This page displays detailed information about your PHP installation, including version, configuration settings, and loaded extensions.

Common Issues and Solutions

If PHP files download instead of executing, check your web server configuration. For Apache, ensure the PHP module is enabled. For Nginx, verify the PHP-FPM configuration.

If extensions don’t load, check for error messages in the PHP and web server logs. You might need to install additional dependencies or adjust permission settings.

Switching Between Multiple PHP Versions

Managing multiple PHP versions gives you flexibility to support different applications with specific version requirements.

Installing Multiple Versions

Using the Remi repository, you can install multiple PHP versions simultaneously:

sudo dnf install php84 php83

Switching CLI Version

Switch between versions for command-line usage:

# For PHP 8.3
scl enable php83 bash

# For PHP 8.4
scl enable php84 bash

Web Server Configuration

For Apache, you can switch versions by enabling the appropriate module:

sudo a2dismod php8.3
sudo a2enmod php8.4
sudo systemctl restart httpd

For Nginx, modify your PHP-FPM socket path in the server block configuration to point to the desired PHP version.

Maintenance and Updates

Regular maintenance ensures your PHP installation remains secure and performs optimally.

Updating PHP

Keep PHP updated with security patches:

sudo dnf update php

Monitoring Logs

Regularly check PHP logs for errors or issues:

sudo tail -f /var/log/php/error.log

Backup Strategies

Create regular backups of your PHP configuration:

sudo cp /etc/php.ini /etc/php.ini.backup

Future Upgrade Path

As PHP 8.4 may be the last 8.x release before PHP 9.0, it’s important to stay informed about future developments. PHP 8.4 is expected to be supported in Fedora 42, 43, and 44.

Congratulations! You have successfully installed PHP. Thanks for using this tutorial for installing the PHP popular general-purpose scripting language on Fedora 42 Linux system. For additional Apache or useful information, we recommend you check the official PHP website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button