RHEL BasedRocky Linux

How To Install PHP on Rocky Linux 10

Install PHP on Rocky Linux 10

In this tutorial, we will show you how to install PHP on Rocky Linux 10. Installing PHP on Rocky Linux represents a crucial step for web developers and system administrators looking to deploy robust web applications. Rocky Linux 10, as a community-driven enterprise-grade distribution, provides exceptional stability and security for hosting PHP-based applications. This comprehensive guide covers multiple installation methods, web server integration, and optimization techniques to ensure your PHP environment runs efficiently and securely. Whether you’re setting up a development environment or preparing production servers, this tutorial provides everything needed for a successful PHP deployment on Rocky Linux 10.

Prerequisites and System Requirements

Before beginning the PHP installation process, ensure your Rocky Linux 10 system meets specific requirements and dependencies. Your system should have sudo or root access privileges, as most installation commands require elevated permissions. Network connectivity is essential for downloading packages from repositories and obtaining necessary dependencies.

The minimum hardware requirements include at least 2GB of RAM for basic PHP operations, though 4GB or more is recommended for production environments. Storage requirements vary depending on installed modules, but allocating at least 10GB of free space ensures adequate room for PHP, web server components, and future updates.

Update your system packages before proceeding with PHP installation:

sudo dnf update -y

This command ensures all existing packages are current and reduces potential compatibility issues during installation. Consider firewall configurations if you plan to serve web content, as ports 80 (HTTP) and 443 (HTTPS) may need opening.

Understanding PHP Versions and Selection

Rocky Linux 10 supports multiple PHP versions through its modular repository system, allowing administrators to choose versions best suited for their applications. The default AppStream repository typically includes PHP versions that prioritize stability over cutting-edge features.

Current PHP ecosystem includes PHP 8.2, 8.3, and 8.4, with PHP 8.4 representing the latest stable release offering improved performance and security features. When selecting PHP versions, consider application compatibility requirements and long-term support considerations.

Check available PHP versions using:

sudo dnf module list php

This command displays available PHP streams and their status indicators, helping you understand which versions are available for installation. Production environments typically benefit from stable, well-tested versions rather than bleeding-edge releases.

Version selection should align with your application requirements. Legacy applications may require older PHP versions, while new projects benefit from modern PHP features and security improvements. Consider migration paths when choosing versions, as upgrading PHP versions may require application modifications.

Repository Setup and Configuration

Rocky Linux 10’s default repositories provide stable PHP versions, but accessing the latest releases requires additional repository configuration. The EPEL (Extra Packages for Enterprise Linux) repository provides extended package collections not available in base repositories.

Install EPEL repository:

sudo dnf install epel-release -y

For accessing newer PHP versions, the Remi repository offers cutting-edge PHP releases and extensions. Install Remi repository with:

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

Enable the Remi repository:

sudo dnf config-manager --set-enabled remi

Repository security involves verifying GPG keys during installation. The system prompts for key acceptance when installing packages from new repositories. Always verify repository authenticity before accepting keys, as compromised repositories pose security risks.

Verify repository configuration:

dnf repolist

This command confirms active repositories and their status, ensuring proper configuration before proceeding with PHP installation.

PHP Installation Methods

Standard Repository Installation

The standard installation method uses Rocky Linux’s package manager for straightforward PHP deployment. This approach ensures dependency resolution and maintains system integrity through tested package combinations.

Reset existing PHP module configurations:

sudo dnf module reset php

Enable your desired PHP version. For PHP 8.4 from Remi repository:

sudo dnf module install php:remi-8.4 -y

This command installs PHP core components and essential modules required for basic functionality. The modular approach allows selecting specific PHP versions without affecting other system components.

Source Code Compilation

Source compilation provides maximum customization control but requires more technical expertise and time investment. This method suits environments requiring specific PHP configurations or custom extensions not available through package managers.

Download PHP source code from official archives and extract to a working directory. Configure compilation options based on your requirements:

./configure --prefix=/usr/local/php --enable-fpm --with-mysql
make && sudo make install

Compilation time varies based on system resources and selected options. Custom installations require manual maintenance for security updates and dependency management.

Web Server Integration

Apache Integration

Apache remains a popular web server choice for PHP applications due to its extensive module ecosystem and flexible configuration options. Install Apache alongside PHP for seamless integration.

Install Apache web server:

sudo dnf install httpd

Install PHP with Apache support:

sudo dnf install php php-cli -y

Configure Apache to start automatically and enable the service:

sudo systemctl enable httpd
sudo systemctl start httpd

Verify Apache status:

sudo systemctl status httpd

Apache automatically recognizes PHP files and processes them through the installed PHP module. Create a test PHP file to verify integration:

echo "" | sudo tee /var/www/html/info.php

Configure firewall access for web traffic:

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload

Nginx Integration

Nginx offers excellent performance characteristics and efficient resource utilization, making it ideal for high-traffic PHP applications. Nginx requires PHP-FPM (FastCGI Process Manager) for PHP processing.

Install PHP-FPM specifically for Nginx:

sudo dnf install php-fpm php-cli -y

Configure PHP-FPM service:

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

Nginx configuration requires specific directives for PHP file processing. Edit your site configuration to include PHP handling:

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Alternative socket-based configuration offers better performance:

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

PHP-FPM Configuration and Optimization

PHP-FPM provides superior resource management and performance characteristics compared to traditional PHP SAPI implementations. Understanding PHP-FPM configuration enables fine-tuning for specific workload requirements.

Main PHP-FPM configuration resides in /etc/php-fpm.d/www.conf. Key configuration parameters include process management settings and resource limits.

Modify user and group settings for web server compatibility:

sudo sed -i 's/^user = apache/user = nginx/' /etc/php-fpm.d/www.conf
sudo sed -i 's/^group = apache/group = nginx/' /etc/php-fpm.d/www.conf

Configure process management strategy. Static allocation provides predictable resource usage:

pm = static
pm.max_children = 20

Dynamic allocation adjusts based on demand:

pm = dynamic
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 10
pm.max_children = 20

Apply configuration changes:

sudo systemctl restart php-fpm

Monitor PHP-FPM status:

sudo systemctl status php-fpm

Essential PHP Modules and Extensions

PHP’s modular architecture allows installing specific functionality through extensions, optimizing system resources and security by including only necessary components.

Search available PHP modules:

sudo dnf search php | more

Install essential modules for web development:

sudo dnf install php-mysqlnd php-pdo-mysql php-gd php-curl php-mbstring php-zip php-xml php-json -y

Database connectivity modules include php-mysqlnd for MySQL/MariaDB connections and php-pdo-mysql for PDO support. Web development essentials comprise php-gd for image processing, php-curl for HTTP requests, and php-mbstring for multibyte string handling.

Security-focused modules like php-openssl provide cryptographic functionality, while php-filter offers input validation capabilities. Performance modules such as php-opcache significantly improve execution speed through bytecode caching.

Module installation syntax follows the pattern:

sudo dnf install [module_name]

Restart web services after installing new modules to ensure proper loading:

sudo systemctl restart httpd
sudo systemctl restart php-fpm

Installation Verification and Testing

Comprehensive testing ensures PHP installation functions correctly across all components and integrations. Multiple verification methods provide different perspectives on installation success.

Check PHP version and configuration:

php -v

Expected output shows PHP version, build information, and loaded extensions. Command-line testing verifies basic PHP functionality independent of web server integration.

Create a comprehensive test file for web-based verification:

sudo tee /var/www/html/test.php << 'EOF'
<?php
echo "<h1>PHP Installation Test</h1>";
echo "<p>PHP Version: " . phpversion() . "</p>";
echo "<p>Server Software: " . $_SERVER['SERVER_SOFTWARE'] . "</p>";
echo "<h2>Loaded Extensions:</h2>";
print_r(get_loaded_extensions());
phpinfo();
?>
EOF

Access the test file through your web browser at http://your-server-ip/test.php to verify web server integration.

Service status verification ensures all components run correctly:

sudo systemctl status httpd
sudo systemctl status php-fpm

Log file monitoring helps identify potential issues:

sudo tail -f /var/log/httpd/error_log
sudo tail -f /var/log/php-fpm/www-error.log

Troubleshooting Common Issues

Installation problems often stem from permission conflicts, service configuration errors, or repository issues. Systematic troubleshooting approaches help identify and resolve problems efficiently.

Permission-related problems frequently occur when web server processes cannot access PHP files or sockets. Verify file ownership and permissions:

sudo chown -R apache:apache /var/www/html
sudo chmod -R 755 /var/www/html

Service startup failures require examining system logs for specific error messages:

sudo journalctl -u httpd -f
sudo journalctl -u php-fpm -f

Module loading errors typically indicate missing dependencies or configuration conflicts. Verify module installation and dependencies:

php -m | grep module_name

SELinux policies may restrict web server and PHP-FPM interactions. Temporarily disable SELinux for testing:

sudo setenforce 0

If this resolves issues, configure appropriate SELinux policies rather than permanently disabling security features.

Port binding conflicts occur when multiple services attempt to use the same network ports. Identify port usage:

sudo netstat -tlnp | grep :80

Security Configuration

PHP security requires attention to configuration settings, regular updates, and security best practices. Proper security configuration protects against common vulnerabilities and attack vectors.

Edit PHP configuration file /etc/php.ini for security enhancements:

sudo nano /etc/php.ini

Disable dangerous functions:

disable_functions = exec,passthru,shell_exec,system,proc_open,popen

Configure secure session settings:

session.cookie_httponly = On
session.cookie_secure = On
session.use_strict_mode = On

Set production-appropriate error reporting:

display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log

Implement file upload restrictions:

file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20

Configure open_basedir restrictions to limit file system access:

open_basedir = /var/www/html:/tmp

Apply security updates regularly:

sudo dnf update php* -y

Performance Optimization

Performance optimization significantly impacts user experience and server resource utilization. Multiple optimization strategies address different performance bottlenecks.

Install and configure OPcache for bytecode caching:

sudo dnf install php-opcache -y

Configure OPcache in /etc/php.d/10-opcache.ini:

opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.validate_timestamps=0
opcache.enable_cli=1

Optimize PHP-FPM pool settings based on server resources and expected load. Monitor resource usage and adjust accordingly:

sudo systemctl status php-fpm

Configure memory limits appropriately for your applications:

memory_limit = 256M

Set realistic execution time limits:

max_execution_time = 30

Implement caching strategies using Redis or Memcached for session storage and application-level caching.

Monitor performance metrics regularly using tools like htop, iostat, and application-specific monitoring solutions.

Congratulations! You have successfully installed PHP. Thanks for using this tutorial for installing PHP 8 on your Rocky Linux 10 system. For additional help 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