AlmaLinux/Rocky Linux's default repositories often ship an older PHP version than actively developed applications need. This guide covers installing a current PHP version using the Remi repository.
Prerequisites
- AlmaLinux 9 or Rocky Linux 9 VPS
- Root or sudo access
Step 1 — Enable EPEL and Remi Repositories
sudo dnf install epel-release -y
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
Step 2 — List Available PHP Versions
sudo dnf module list php
Step 3 — Reset the Default PHP Module and Select a Version
sudo dnf module reset php -y
sudo dnf module enable php:remi-8.3 -y
Step 4 — Install PHP
sudo dnf install php php-cli php-fpm php-mysqlnd php-gd php-mbstring php-xml php-zip php-curl -y
Step 5 — Verify Installation
php -v
Step 6 — Enable and Start PHP-FPM
sudo systemctl enable --now php-fpm
Step 7 — Connect PHP-FPM to Nginx
server {
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Verify the exact socket path matches your PHP-FPM pool configuration:
cat /etc/php-fpm.d/www.conf | grep listen
Step 8 — SELinux Considerations for PHP-FPM
If PHP-FPM needs to connect to a database or make network calls:
sudo setsebool -P httpd_can_network_connect_db 1
Step 9 — Test PHP
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Visit http://YOUR_SERVER_IP/info.php, then remove it:
sudo rm /var/www/html/info.php
Switching PHP Versions Later
sudo dnf module reset php -y
sudo dnf module enable php:remi-8.2 -y
sudo dnf install php -y
sudo systemctl restart php-fpm
Common Errors
"Error: Problem: package php conflicts with..." — multiple PHP module streams enabled simultaneously; reset with dnf module reset php before enabling a different stream.
PHP files download instead of executing (Nginx) — verify the socket path in your Nginx config matches PHP-FPM's actual listen directive.
Database connection fails from PHP despite correct credentials — likely SELinux blocking the network connection; apply setsebool httpd_can_network_connect_db as shown above.
Best Practices
- Use the Remi repository for access to current, actively supported PHP versions
- Install only the PHP extensions your application actually needs
- Check SELinux booleans (
getsebool -a | grep httpd) when troubleshooting unexpected permission errors
Continue Reading
- How to Install Nginx on AlmaLinux/Rocky Linux
- Understanding SELinux Basics on AlmaLinux/Rocky Linux
- How to Install MySQL/MariaDB on AlmaLinux/Rocky Linux
Browse more articles in AlmaLinux & Rocky Linux.
