Installing the Latest Official PHP on Rocky Linux 9

If you’re setting up a WordPress site on Rocky Linux 9, it’s crucial to have a reliable and secure server environment. This guide will walk you through the installation of the latest official PHP version, the necessary PHP extensions, and configuring Nginx to work with PHP using Unix sockets.

πŸš€ Step 1: Install the Latest PHP

Rocky Linux 9 repositories do not include PHP 8.4 by default. We’ll use the Remi repository, a trusted source for the latest PHP versions.

    1. Enable EPEL and Remi Repository:
      sudo dnf install -y epel-release
      sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm
      sudo dnf module reset php
      sudo dnf module list php
      sudo dnf module enable php:remi-8.4
  1. Install PHP 8.4:
    sudo dnf install php php-fpm

🧩 Step 2: Install PHP Extensions for WordPress

To ensure WordPress runs smoothly, you need to install several PHP extensions.

sudo dnf install -y php-mysqlnd php-opcache php-xml php-json php-gd php-mbstring

Other PHP extensions

sudo dnf install -y php-cli php-curl php-zip php-intl php-common php-bcmath php-imap php-imagick php-xmlrpc php-readline php-memcached php-redis php-apcu

Check PHP version and List installed modules:

php -v
php --modules

βš™οΈ Step 3: Configure PHP-FPM

Edit the PHP configuration to optimize performance and security.

  • PHP-FPM Configuration Path: /etc/php-fpm.d/www.conf
  • Key Configurations: Adjust user and group to nginx, listen to a Unix socket, and set appropriate permissions.
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
sudo sed -i 's/;listen.owner = nobody/listen.owner = nginx/' /etc/php-fpm.d/www.conf
sudo sed -i 's/;listen.group = nobody/listen.group = nginx/' /etc/php-fpm.d/www.conf
sudo sed -i 's/;listen.mode = 0660/listen.mode = 0660/' /etc/php-fpm.d/www.conf

#sudo sed -i ‘s!listen = 127.0.0.1:9000!listen = /run/php-fpm/www.sock!’ /etc/php-fpm.d/www.conf
Enable/Status PHP fpm service

sudo systemctl enable php-fpm.service
sudo systemctl start php-fpm.service

Start/Stop/Restart PHP fpm service

sudo systemctl start php-fpm.service
sudo systemctl stop php-fpm.service
sudo systemctl restart php-fpm.service

🌐 Step 4: Configure Nginx

Set up Nginx to use PHP-FPM via the Unix socket.

  • Nginx Configuration Path: /etc/nginx/conf.d/default.conf
  • Optimized Nginx Configuration for WordPress:

    location ~ \.php$ {
        root   /usr/share/nginx/html;
        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;
        fastcgi_read_timeout 120;
    }

πŸ”„ Step 5: Restart Services

After configuration, restart Nginx andPHP-FPM to apply changes.

sudo systemctl restart nginx
sudo systemctl restart php-fpm

πŸ§ͺ Step 6: Test PHP Configuration

Create a PHP info file to verify the setup.

echo '<?php phpinfo(); ?>' | sudo tee /usr/share/nginx/html/22.php

Now, navigate to http://your-server-ip/22.php in your browser to see the PHP configuration details.

This setup provides a solid foundation for hosting WordPress on Rocky Linux 9 with Nginx and the latest PHP version. Follow these steps to ensure a secure and optimized server environment for your WordPress site. 🌟

Scroll to Top