PHP is a popular general-purpose scripting language especially suited to web development. It is fast, flexible, and pragmatic, powering everything from blogs to the most popular websites in the world. PHP offers extensive support for various databases, making it an essential tool for server-side scripting.
The following guide will demonstrate the steps to install PHP on Rocky Linux 9 or 8 using command-line commands. The installation method involves using the Remi RPM repository for the latest stable builds of PHP 8.1, 8.2, and 8.3.
Import Remi PHP RPM Repo
Update Rocky Linux Before PHP Installation
Begin by updating your Rocky Linux system to prepare for PHP installation. Open the terminal and enter:
sudo dnf upgrade --refresh
Import PHP Remi Repository
EPEL Repository Setup
EPEL (Extra Packages for Enterprise Linux) is crucial for additional software packages in Rocky Linux. Below are two options for installing EPEL on Rocky Linux for the latest EL9 release or EL8.
Import EPEL for Rocky Linux 9
Enable CRB
CRB provides access to more development tools. Use:
sudo dnf config-manager --set-enabled crb
Install EPEL Repositories
To get the latest EPEL releases for Rocky Linux 9:
sudo dnf install \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm \
https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-9.noarch.rpm
Incorporate the Remi repository designed for EL 9:
sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
Import EPEL for Rocky Linux 8
Enable PowerTools Repository
PowerTools is essential for PHP development tools.
sudo dnf config-manager --set-enabled powertools
Enable EPEL for Rocky Linux 8
Install EPEL repositories for Rocky Linux 8:
sudo dnf install \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm \
https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-8.noarch.rpm
Import Remi EL 8 Repository
With EPEL installed, import the Remi RPM containing PHP for EL 8:
sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
Enable PHP (8.x) from Remi Repository
Verifying PHP Versions
Start by confirming the available PHP versions in the Remi repository. This ensures you have access to the desired PHP version.
Execute in the terminal:
dnf module list php
If prompted, import the GPG key for Remi’s repository by typing (Y).
Selecting Your PHP Version
Enable PHP 8.3
To install PHP 8.3, a stable and recent version, use:
sudo dnf module enable php:remi-8.3 -y
Enable PHP 8.2
For PHP 8.2, suitable for applications requiring this specific version:
sudo dnf module enable php:remi-8.2 -y
Enable PHP 8.1
To enable PHP 8.1, ideal for compatibility with certain applications:
sudo dnf module enable php:remi-8.1 -y
Finalize PHP Installation
Installing PHP for Apache or Nginx Support
After enabling the Remi PHP repository, you can install PHP tailored to your web server needs, either Apache or Nginx.
Apache (httpd) PHP Installation
sudo dnf install php php-cli -y
Nginx PHP Installation
sudo dnf install php-fpm php-cli -y
Verifying PHP Installation
Confirm PHP installation by running:
php -v
PHP Extensions Installation
Install essential PHP extensions based on your project needs. Avoid installing all extensions to maintain optimal performance and security.
Standard Installation
sudo dnf install php-cli php-fpm php-curl php-mysqlnd php-gd php-opcache php-zip php-intl php-common php-bcmath php-imagick php-xmlrpc php-json php-readline php-memcached php-redis php-mbstring php-apcu php-xml php-dom php-redis php-memcached php-memcache
Simplified Installation
For a compact installation command:
sudo dnf install php-{cli,fpm,curl,mysqlnd,gd,opcache,zip,intl,common,bcmath,imagick,xmlrpc,json,readline,memcached,redis,mbstring,apcu,xml,dom,memcache}
Now, let’s break down what each of these PHP extensions does:
- php-cli: Command Line Interface for PHP, helpful in running PHP scripts in the shell.
- php-fpm: FastCGI Process Manager, useful for handling and serving PHP requests.
- php-curl: Allows you to connect and communicate with different types of servers and protocols.
- php-mysqlnd: MySQL Native Driver, used for MySQL database connections.
- php-gd: Graphics Draw, useful for creating and manipulating image files.
- php-opcache: Opcode Cache, used to speed up PHP execution.
- php-zip: Allows you to work with zip archives.
- php-intl: Internationalization extension for working with international character sets and locales.
- php-common: Common files and settings used by multiple PHP modules.
- php-bcmath: Binary Calculator allows arbitrary precision mathematics.
- php-imagick: Provides methods to create and modify images using the ImageMagick library.
- php-xmlrpc: Enables creating an XML-RPC server and makes sending XML-RPC requests easy.
- php-json: JSON (JavaScript Object Notation) extension for encoding and decoding JSON data.
- php-readline: Allows for reading a line of data from the terminal.
- php-memcached: Provides fast, distributed memory object caching.
- php-redis: PHP extension for interfacing with Redis.
- php-mbstring: Multi-Byte String for handling non-ASCII strings.
- php-apcu: APC User Cache, a userland caching extension based on APC.
- php-xml: XML Parser for parsing XML documents.
- php-dom: Document Object Model, to manipulate XML documents in an object-oriented way.
- php-memcache: Provides handy procedural and object-oriented interface to memcached.
- php-soap: Used for writing SOAP Servers and Clients.
- php-ldap: Lightweight Directory Access Protocol used for accessing and managing directory information.
- php-pdo: PHP Data Objects, a database access layer providing a uniform access method to multiple databases.
You can use the following command to view modules loaded at any time.
php -m
Regular monitoring and removal of unnecessary modules are recommended for performance efficiency.
Installing PHP Development Tools
For development purposes, install the PHP development branch and debugging tools:
PHP Development Branch
sudo dnf install php-devel
PHP Debugging Tools
sudo dnf install php-xdebug php-pcov
It’s important to note that installing this version will bring in multiple dependencies, and it’s not recommended to do so unless you have a specific need for it in your PHP development or any special requirement.
Configure PHP-FPM Nginx User on Rocky Linux
Adjusting PHP-FPM for Nginx
RHEL-based distributions like Rocky Linux default to using the “Apache” user for PHP-FPM, which is not suitable for Nginx setups. It’s essential to modify this for optimal functioning with Nginx.
Editing PHP-FPM Configuration
Open Configuration File
Access the www.conf
file for editing:
sudo nano /etc/php-fpm.d/www.conf
Modify User and Group Settings
Change the Apache user and group to Nginx within the file.
Post modification:
Save your changes by pressing (CTRL+O), then exit with (CTRL+X).
Restart PHP-FPM Service
After modifying the configuration, restart PHP-FPM to apply changes:
sudo systemctl restart php-fpm
Example Nginx PHP-FPM Server Block Code
Configuring Nginx for PHP Processing
To enable Nginx to handle PHP files, the server block must be configured correctly. The following example demonstrates the necessary setup within the server{}
block for processing PHP files.
Nginx PHP-FPM Configuration
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Validating Nginx Configuration
After updating the configuration, validate it to ensure no syntax errors.
sudo nginx -t
Expected output should confirm the configuration is correct:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restarting Nginx Service
To apply the changes, restart the Nginx service:
sudo systemctl restart nginx
This restart is essential for the changes and for Nginx to start processing PHP files with the new configuration.
Conclusion
Alright, that wraps up our journey! We’ve successfully set up PHP on Rocky Linux, tweaking it for Apache or Nginx and fine-tuning PHP-FPM for optimal performance. Remember, regularly checking your PHP modules and configurations keeps things running smoothly. And don’t forget to restart your services after any changes.