Important Disclaimer#
PHP 7.4 reached its End of Life (EOL) in November 2022. This means it no longer receives security updates or bug fixes from the PHP project. We strongly recommend upgrading to a supported PHP version (8.1+) for production environments. Use this guide only if you have a legacy application that requires PHP 7.4.
Table of Contents#
- Prerequisites
- Step 1: Update System Packages
- Step 2: Enable EPEL and Remi Repositories
- Step 3: Enable Remi PHP 7.4 Module
- Step 4: Install PHP 7.4 and Core Packages
- Step 5: Verify PHP 7.4 Installation
- Step 6: Configure PHP 7.4 (php.ini)
- Step 7: Install PHP 7.4 Extensions
- Step 8: Test PHP with Web Servers
- Troubleshooting Common Issues
- Conclusion
- References
Prerequisites#
Before you begin, ensure you have:
- A server running Rocky Linux 8.x or AlmaLinux 8.x (this guide does not work for 9.x, as PHP 7.4 is unavailable in Remi for RHEL 9).
- A user account with
sudoprivileges (to run administrative commands). - An active internet connection (to download packages).
Step 1: Update System Packages#
Always start by updating your system to ensure you have the latest security patches and dependencies:
sudo dnf update -yThe -y flag auto-confirms prompts to speed up the process.
Step 2: Enable EPEL and Remi Repositories#
Rocky Linux/AlmaLinux’s default repositories do not include PHP 7.4 (they ship with PHP 7.2 or 8.0). We’ll use two third-party repositories:
What Are These Repositories?#
- EPEL (Extra Packages for Enterprise Linux): A free repository with additional open-source software for RHEL-based systems.
- Remi: A trusted repository that provides up-to-date PHP versions (including legacy releases like 7.4) for Enterprise Linux.
Install EPEL#
Install the EPEL repository first—Remi depends on it:
sudo dnf install epel-release -yInstall Remi Repository#
Next, install the Remi repository for RHEL 8:
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -yStep 3: Enable Remi PHP 7.4 Module#
RHEL-based systems use module streams to manage multiple versions of software. To use PHP 7.4 from Remi, we need to enable its module stream:
sudo dnf module enable php:remi-7.4 -yThis command tells your system to prioritize Remi’s PHP 7.4 packages over the default (older) PHP versions.
Step 4: Install PHP 7.4 and Core Packages#
Now install PHP 7.4 and essential packages for web development:
sudo dnf install php php-cli php-fpm php-common -yLet’s break down what each package does:
- php: The core PHP runtime.
- php-cli: Enables PHP execution from the command line (useful for scripts or debugging).
- php-fpm: The FastCGI Process Manager (required for Nginx or high-performance Apache setups).
- php-common: Shared files used by other PHP extensions.
Step 5: Verify PHP 7.4 Installation#
Confirm PHP 7.4 is installed correctly:
php -vYou should see output like this (version number may vary slightly):
PHP 7.4.33 (cli) (built: Oct 3 2023 18:30:00) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
To verify the php-fpm service (used by Nginx/Apache):
sudo systemctl status php-fpmIf it’s not running, start and enable it to launch on boot:
sudo systemctl start php-fpm
sudo systemctl enable php-fpmStep 6: Configure PHP 7.4 (php.ini)#
The main PHP configuration file is php.ini. To find its location:
php --iniLook for the Loaded Configuration File line (typically /etc/php.ini for CLI or /etc/php-fpm.d/www.conf for FPM).
Common Configuration Changes#
Edit php.ini with a text editor like nano (replace with vi if you prefer):
sudo nano /etc/php.iniHere are key settings to adjust for most web applications:
| Setting | Default | Recommended (Example) | Purpose |
|---|---|---|---|
memory_limit | 128M | 256M | Maximum memory PHP can use per script (increase for complex apps like WordPress). |
upload_max_filesize | 2M | 64M | Largest file size users can upload. |
post_max_size | 8M | 64M | Maximum size of POST data (must be ≥ upload_max_filesize). |
max_execution_time | 30 | 60 | Maximum time (in seconds) PHP waits for a script to finish (increase for slow tasks). |
display_errors | Off | Off | Hide PHP errors in the browser (set to On only for development). |
log_errors | On | On | Log errors to a file (instead of showing them to users). |
After making changes, save the file (Ctrl+O, then Enter) and exit (Ctrl+X).
For changes to take effect:
- If using Apache: Restart Apache (
sudo systemctl restart httpd). - If using Nginx: Restart PHP-FPM (
sudo systemctl restart php-fpm).
Step 7: Install PHP 7.4 Extensions#
PHP extensions add functionality like database connections, image processing, or XML parsing. Use this command to install common extensions:
sudo dnf install php-mysqlnd php-gd php-xml php-mbstring php-json php-curl php-zip -yLet’s explain each extension:
- php-mysqlnd: Native MySQL driver (connects PHP to MySQL/MariaDB databases).
- php-gd: Image processing (resizes images, creates thumbnails).
- php-xml: Parses XML data (used by WordPress, Laravel, etc.).
- php-mbstring: Handles multi-byte characters (essential for non-English languages).
- php-json: Encodes/decodes JSON data (common in APIs).
- php-curl: Makes HTTP requests (fetches data from external APIs).
- php-zip: Reads/writes ZIP files (for file uploads or backups).
To list all installed PHP extensions:
php -mTo install a specific extension later (e.g., php-redis for Redis caching):
sudo dnf install php-redis -yStep 8: Test PHP with Web Servers#
Now we’ll test PHP with two popular web servers: Apache (common for traditional LAMP stacks) and Nginx (modern, high-performance choice).
Option A: Apache HTTP Server#
Apache is preconfigured to work with PHP via the mod_php module (included when you install php).
-
Install Apache:
sudo dnf install httpd -y -
Start and enable Apache:
sudo systemctl start httpd sudo systemctl enable httpd -
Allow HTTP/HTTPS through the firewall:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload -
Create a PHP test file:
Apache serves files from/var/www/htmlby default. Create a file calledinfo.php:echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php -
Test PHP:
Open a browser and visithttp://your-server-ip/info.php(replaceyour-server-ipwith your server’s public/private IP).If PHP is working, you’ll see a page with PHP configuration details (e.g., version, extensions, and settings).
Option B: Nginx Web Server#
Nginx does not process PHP directly—instead, it forwards PHP requests to php-fpm (which we installed earlier).
-
Install Nginx:
sudo dnf install nginx -y -
Start and enable Nginx:
sudo systemctl start nginx sudo systemctl enable nginx -
Allow Nginx through the firewall:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload -
Configure Nginx to use PHP-FPM:
Nginx’s default configuration file is/etc/nginx/nginx.conf, but we’ll create a new server block (virtual host) for clarity.Create a file called
example.confin/etc/nginx/conf.d/:sudo nano /etc/nginx/conf.d/example.confPaste this configuration (replace
your-domain.comwith your domain or server IP):server { listen 80; server_name your-domain.com; root /usr/share/nginx/html; index index.php index.html; location / { try_files $uri $uri/ =404; } # Pass PHP requests to PHP-FPM 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; } }Key lines:
- fastcgi_pass: Points to the PHP-FPM socket (Remi’s default is
/run/php-fpm/www.sock). - SCRIPT_FILENAME: Tells PHP-FPM where to find the PHP file.
- fastcgi_pass: Points to the PHP-FPM socket (Remi’s default is
-
Test Nginx configuration:
Ensure there are no syntax errors:sudo nginx -tIf you see
nginx: configuration file /etc/nginx/nginx.conf test is successful, proceed. -
Restart Nginx:
sudo systemctl restart nginx -
Create a PHP test file:
Nginx serves files from/usr/share/nginx/htmlby default. Createinfo.php:echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/info.php -
Test PHP:
Visithttp://your-server-ip/info.phpin a browser. If you see the PHP info page, Nginx and PHP-FPM are working together.
Troubleshooting Common Issues#
Here are fixes for frequent problems:
1. PHP Not Parsing in Browser#
- For Apache: Ensure the
phpmodule is enabled:sudo dnf install php-mod_php -y sudo systemctl restart httpd - For Nginx: Verify the
location ~ \.php$block in your server configuration is correct. Check if PHP-FPM is running:sudo systemctl status php-fpm
2. Permission Denied Errors#
- Apache: The
apacheuser must own the web root (/var/www/html):sudo chown -R apache:apache /var/www/html - Nginx: The
nginxuser must own the web root (/usr/share/nginx/html):sudo chown -R nginx:nginx /usr/share/nginx/html
3. PHP-FPM Service Not Running#
Start and enable PHP-FPM:
sudo systemctl start php-fpm
sudo systemctl enable php-fpmCheck logs for errors:
sudo journalctl -u php-fpm -fConclusion#
You’ve successfully installed PHP 7.4 on Rocky Linux or AlmaLinux! While this gets your legacy application up and running, remember PHP 7.4 is no longer supported. For production environments, we strongly recommend:
- Upgrading your application to a supported PHP version (8.1+).
- Migrating to Rocky Linux 9/AlmaLinux 9 (which includes newer PHP versions).
If you need help upgrading PHP, check the PHP Upgrade Guide for breaking changes.
References#
- Remi Repository Official Site
- PHP Supported Versions
- Rocky Linux Package Management
- AlmaLinux EPEL Installation
- Apache HTTP Server Documentation
- Nginx PHP-FPM Configuration
Let us know in the comments if you encountered any issues—we’re happy to help!