Rocky Linux has emerged as one of the most reliable and stable enterprise-grade Linux distributions available today. Born from the discontinuation of CentOS, Rocky Linux provides a robust, community-driven alternative that maintains 100% bug-for-bug compatibility with Red Hat Enterprise Linux (RHEL). With over 600,000 active installations worldwide as of 2025, it’s become the go-to choice for developers and system administrators who need enterprise-level stability without the associated costs.
Why Choose PHP for Web Development?
PHP continues to power approximately 78.9% of all websites whose server-side programming language is known, according to recent W3Techs statistics. Its popularity stems from its simplicity, extensive documentation, massive community support, and seamless integration with various databases and web servers. Whether you’re building a simple blog or a complex e-commerce platform, PHP provides the flexibility and performance needed for modern web applications.
Prerequisites for Installing PHP on Rocky Linux
System Requirements
Before diving into the PHP installation process, let’s ensure your Rocky Linux system meets the necessary requirements:
- Operating System: Rocky Linux 8.x or 9.x (64-bit recommended)
- RAM: Minimum 1GB (2GB+ recommended for development environments)
- Storage: At least 2GB free space for PHP and its dependencies
- Network Connection: Active internet connection for downloading packages
Required Permissions and Access
You’ll need root or sudo privileges to install PHP and configure system settings. Most commands in this guide will require administrative access, so ensure you can either log in as root or use the sudo
command.
Checking Your Current System
Let’s start by checking your current Rocky Linux version and system status:
cat /etc/os-release
uname -a
This information helps determine which installation method will work best for your specific setup.
Understanding PHP Versions and Repositories
Available PHP Versions on Rocky Linux
Rocky Linux offers multiple PHP versions through different repositories. As of 2025, you can install:
- PHP 7.4: Still supported but nearing end-of-life
- PHP 8.0: Stable with significant performance improvements
- PHP 8.1: Current stable version with enhanced features
- PHP 8.2: Latest stable release with cutting-edge features
- PHP 8.3: Newest version with experimental features
Default Repositories vs Third-Party Repositories
The default Rocky Linux repositories typically include older, more stable PHP versions. For newer versions, you’ll need to enable additional repositories like:
- Remi Repository: Offers the latest PHP versions
- EPEL (Extra Packages for Enterprise Linux): Provides additional packages
- PowerTools/CRB Repository: Contains development tools
Choosing the Right PHP Version
Consider these factors when selecting a PHP version:
- Application compatibility: Ensure your applications support the chosen version
- Long-term support: PHP 8.1 and 8.2 offer extended support periods
- Performance requirements: Newer versions generally offer better performance
- Security updates: Always choose actively supported versions
Method 1: Installing PHP from Default Rocky Linux Repositories
Updating Your System
Always start with a system update to ensure you have the latest package information:
sudo dnf update -y
This command updates your package lists and installs any available system updates.
Installing PHP Using DNF Package Manager
The DNF package manager is Rocky Linux’s default package management tool, replacing YUM from earlier versions.
Installing Basic PHP Packages
Install the core PHP package and essential modules:
sudo dnf install php php-cli php-fpm php-common -y
This installs:
- php: The core PHP runtime
- php-cli: Command-line interface for PHP
- php-fpm: FastCGI Process Manager for better performance
- php-common: Common PHP files and utilities
Installing Common PHP Extensions
Most web applications require additional PHP extensions. Install the most commonly used ones:
sudo dnf install php-mysqlnd php-pdo php-gd php-mbstring php-xml php-curl php-zip php-json -y
These extensions provide:
- Database connectivity (MySQL, PDO)
- Image processing (GD)
- String handling (mbstring)
- XML processing
- HTTP requests (cURL)
- Archive handling (ZIP)
Method 2: Installing PHP from Remi Repository
Adding the Remi Repository
The Remi repository provides the latest PHP versions and is maintained by Remi Collet, a Fedora and Red Hat contributor.
First, install the EPEL repository (required dependency):
sudo dnf install epel-release -y
Then add the Remi repository:
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
Installing Specific PHP Versions
With Remi repository enabled, you can install specific PHP versions:
For PHP 8.2:
sudo dnf module enable php:remi-8.2 -y
sudo dnf install php php-cli php-fpm php-common -y
For PHP 8.1:
sudo dnf module enable php:remi-8.1 -y
sudo dnf install php php-cli php-fpm php-common -y
Configuring Multiple PHP Versions
Advanced users might need multiple PHP versions for different projects. The Remi repository supports this through module streams:
# List available PHP modules
sudo dnf module list php
# Install PHP 8.2 alongside existing versions
sudo dnf install php82-php php82-php-cli php82-php-fpm -y
Method 3: Installing PHP from EPEL Repository
Enabling EPEL Repository
EPEL provides additional packages compatible with RHEL-based distributions:
sudo dnf install epel-release -y
sudo dnf config-manager --set-enabled powertools
Installing PHP Packages from EPEL
After enabling EPEL, install PHP:
sudo dnf install php php-cli php-fpm php-common --enablerepo=epel -y
Configuring PHP After Installation
Locating PHP Configuration Files
PHP configuration files are typically located at:
- Main config:
/etc/php.ini
- FPM config:
/etc/php-fpm.conf
- Additional configs:
/etc/php.d/
Essential PHP Configuration Settings
Edit the main PHP configuration file:
sudo nano /etc/php.ini
Memory Limits and Execution Time
Adjust these key settings based on your application needs:
memory_limit = 256M
max_execution_time = 300
max_input_vars = 3000
post_max_size = 32M
upload_max_filesize = 32M
Error Reporting and Logging
For development environments, enable detailed error reporting:
display_errors = On
error_reporting = E_ALL
log_errors = On
error_log = /var/log/php_errors.log
For production environments, disable error display:
display_errors = Off
error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED
Installing and Configuring a Web Server
Installing Apache HTTP Server
Apache remains the most popular web server, powering over 35% of all websites:
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
Installing Nginx (Alternative Option)
Nginx offers better performance for high-traffic websites:
sudo dnf install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
Integrating PHP with Apache
Apache integration requires additional configuration. Install the Apache PHP module:
sudo dnf install php-apache -y
sudo systemctl restart httpd
Testing PHP Installation
Create a test PHP file to verify your installation:
sudo nano /var/www/html/phpinfo.php
Add this content:
<?php
phpinfo();
?>
Visit http://your-server-ip/phpinfo.php
to see your PHP configuration. Remember to remove this file after testing for security reasons.
Managing PHP Extensions and Modules
Viewing Installed PHP Extensions
Check which extensions are currently installed:
php -m
Or create a PHP file with:
<?php
print_r(get_loaded_extensions());
?>
Installing Additional PHP Extensions
Database Extensions (MySQL, PostgreSQL)
For MySQL/MariaDB support:
sudo dnf install php-mysqlnd php-pdo -y
For PostgreSQL support:
sudo dnf install php-pgsql -y
Security and Performance Extensions
Install security and performance-enhancing extensions:
sudo dnf install php-opcache php-mcrypt php-intl php-ldap -y
These extensions provide:
- OPcache: Improves performance by caching compiled PHP code
- Intl: International functions
- LDAP: Directory service support
Troubleshooting Common PHP Installation Issues
Permission Problems
If you encounter permission errors, ensure proper ownership:
sudo chown -R apache:apache /var/www/html/
sudo chmod -R 755 /var/www/html/
Repository Conflicts
Sometimes different repositories provide conflicting packages. To resolve:
sudo dnf clean all
sudo dnf makecache
Extension Dependencies
Missing dependencies can cause extension installation failures. Always check error messages and install required packages:
sudo dnf install gcc gcc-c++ make autoconf -y
Security Best Practices for PHP on Rocky Linux
Securing PHP Configuration
Implement these security measures in your PHP configuration:
expose_php = Off
allow_url_fopen = Off
allow_url_include = Off
enable_dl = Off
These settings:
- Hide PHP version information
- Prevent remote file inclusion attacks
- Disable dynamic library loading
Regular Updates and Maintenance
Keep your PHP installation secure with regular updates:
sudo dnf update php* -y
Set up automatic security updates:
sudo dnf install dnf-automatic -y
sudo systemctl enable --now dnf-automatic.timer
Performance Optimization Tips
Optimize PHP performance with these configurations:
- Enable OPcache: Dramatically improves performance by caching compiled PHP code
- Tune PHP-FPM: Adjust process manager settings based on your server resources
- Configure proper memory limits: Balance between functionality and resource usage
- Use the latest PHP version: Each new version typically offers performance improvements
Example PHP-FPM optimization:
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
Frequently Asked Questions (FAQs)
1. What’s the difference between installing PHP from default repositories versus Remi repository?
Default Rocky Linux repositories offer older, more stable PHP versions that undergo extensive testing. Remi repository provides the latest PHP versions with newer features but may have less stability testing. Choose default repositories for production environments requiring maximum stability, and Remi for development or when you need cutting-edge PHP features.
2. Can I install multiple PHP versions simultaneously on Rocky Linux?
Yes, you can install multiple PHP versions using the Remi repository’s module streams or by installing version-specific packages (like php82-php, php81-php). This is useful for testing applications across different PHP versions or maintaining legacy applications while developing with newer versions.
3. How do I switch between different PHP versions after installation?
You can switch PHP versions using the alternatives
system or by enabling different module streams. For module streams, use sudo dnf module enable php:remi-8.2
to switch to PHP 8.2. For command-line usage, you can use update-alternatives
to manage different PHP CLI versions.
4. What should I do if PHP extensions fail to install due to dependency issues?
First, ensure you have development tools installed (sudo dnf groupinstall "Development Tools"
). Check which specific dependencies are missing in the error message and install them manually. Sometimes enabling additional repositories like PowerTools/CRB resolves dependency issues: sudo dnf config-manager --set-enabled powertools
.
5. How can I verify that PHP is working correctly with my web server after installation?
Create a simple PHP info file (<?php phpinfo(); ?>
) in your web server’s document root and access it through a browser. You should see a detailed PHP configuration page. Additionally, check that your web server service is running (sudo systemctl status httpd
or sudo systemctl status nginx
) and that PHP modules are properly loaded (php -m
from command line).