Share this:

Today we are going to learn Step-by-Step Instructions for Installing PHP 8.x on CentOS. PHP is a widely used server-side scripting language, particularly for web development. The release of PHP 8.x brings a host of new features, performance improvements, and security enhancements. In this detailed blog, we will cover the step-by-step process for installing PHP 8.x on CentOS.

Before we dive into the installation process, let’s first understand the new features and improvements in PHP 8.x.

New Features in PHP 8.x

  1. Just-In-Time (JIT) Compiler: The JIT compiler is one of the most significant improvements in PHP 8.x. It increases the performance of PHP code by compiling parts of the code at runtime.
  2. Attributes: Attributes provide a more elegant and efficient way to add metadata to classes, functions, properties, and other PHP elements.
  3. Named Arguments: This feature allows you to pass arguments to a function by specifying the parameter names, making the code more readable and less error-prone.
  4. Constructor Property Promotion: This feature simplifies the syntax for declaring and initializing class properties in the constructor.
  5. Union Types: Union types allow you to specify multiple types for a single variable or parameter, making the code more flexible and expressive.
  6. Match Expression: The match expression is an improvement over the traditional switch statement, offering a more concise syntax and better type safety.

Now, let’s proceed with the installation of PHP 8.x on CentOS.

Step 1: Update Your System

Before starting the installation, it’s essential to update your CentOS system to the latest packages and security patches. To do this, run the following command:

sudo yum update -y

Step 2: Install EPEL and Remi Repositories

To install PHP 8.x on CentOS, you need to enable the EPEL (Extra Packages for Enterprise Linux) and Remi repositories. These repositories provide additional packages, including the latest PHP versions. Run the following commands to install and enable the repositories:

sudo yum install -y epel-release
sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm

Step 3: Enable the Remi Repository for PHP 8.x

Now, enable the Remi repository for PHP 8.x by running the following command:

sudo yum-config-manager --enable remi-php80

Step 4: Install PHP 8.x

After enabling the Remi repository for PHP 8.x, install PHP by running the following command:

sudo yum install -y php

Wait for the installation to complete. You can verify the PHP version installed by running the following command:

php -v

You should see output similar to the following:

PHP 8.x.x (cli) (built: xxx xx 20xx xx:xx:xx) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.x.x, Copyright (c), by Zend Technologies

Step 5: Install PHP Extensions

To enhance the functionality of PHP, you may need to install additional PHP extensions. Use the following command to search for available extensions:

sudo yum search php-

To install a specific PHP extension, use the following command:

sudo yum install -y php-extension_name

For example, to install the MySQL extension, run:

sudo yum install -y php-mysqlnd

Step 6: Configure PHP (Optional)

The default PHP configuration file is located at /etc/php.ini. You can edit this file to modify various PHP settings, such as memory limits, error reporting, and more. To open the file for editing, run the following command:

sudo nano /etc/php.ini

Some common configuration changes include:

  1. Adjusting the memory limit:
memory_limit = 256M
  1. Configuring the maximum execution time:
max_execution_time = 300
  1. Setting the maximum file upload size:
upload_max_filesize = 64M
  1. Enabling or disabling error reporting:
error_reporting = E_ALL
display_errors = On

Once you have made the desired changes, save the file and exit the editor. To apply the changes, restart the PHP-FPM service or the web server running PHP.

Step 7: Install and Configure PHP-FPM (Optional)

PHP-FPM (FastCGI Process Manager) is a high-performance alternative to the traditional CGI-based PHP processing. It is recommended for use with modern web servers like Nginx. To install PHP-FPM, run the following command:

sudo yum install -y php-fpm

After installation, edit the PHP-FPM configuration file located at /etc/php-fpm.d/www.conf. Modify the following settings according to your requirements:

  1. Set the user and group for running PHP-FPM processes:
user = your_user
group = your_group
  1. Configure the listen directive:
listen = /run/php-fpm/www.sock
  1. Set the listen.owner and listen.group:
listen.owner = your_user
listen.group = your_group

Save the file and exit the editor. To start and enable the PHP-FPM service, run the following commands:

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

Step 8: Configure Your Web Server

Depending on the web server you are using (Apache or Nginx), you need to configure it to use the installed PHP version. Below are the instructions for both web servers:

For Apache:

Install the Apache web server, if not already installed:

sudo yum install -y httpd

Enable and start the Apache service:

sudo systemctl enable httpd
sudo systemctl start httpd

Create a sample PHP file in the Apache document root (/var/www/html):

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Now, access the file in your browser by navigating to http://your_server_IP/info.php. You should see the PHP information page.

For Nginx:

Install the Nginx web server, if not already installed:

sudo yum install -y nginx

Enable and start the Nginx service:

sudo systemctl enable nginx
sudo systemctl start nginx

Edit the Nginx configuration file located at /etc/nginx/conf.d/default.conf and add the following server block:

server {
    listen 80;
    server_name your_domain_or_IP;
    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.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;
    }
}

Save the file and exit the editor. Reload the Nginx configuration:

sudo systemctl reload nginx

Create a sample PHP file in the Nginx document root (/usr/share/nginx/html):

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

Now, access the file in your browser by navigating to http://your_server_IP/info.php. You should see the PHP information page.

Step 9: Test Your PHP Installation

To test your PHP installation, create a PHP script named test.php with the following content:

<?php
echo 'Hello, PHP 8.x!';
?>

Place the test.php file in your web server’s document root (e.g., /var/www/html for Apache or /usr/share/nginx/html for Nginx). Access the script in your browser by navigating to http://your_server_IP/test.php. You should see the message “Hello, PHP 8.x!” displayed, confirming that PHP is working correctly.

Step 10: Secure Your PHP Installation

To enhance the security of your PHP installation, consider implementing the following best practices:

  1. Disable potentially dangerous PHP functions by adding the following line to your /etc/php.ini file:
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
  1. Limit the exposure of PHP error messages in a production environment by disabling error display:
display_errors = Off
  1. Keep your PHP installation up to date by regularly applying updates and security patches.

Conclusion

In this blog post, we have provided detailed, step-by-step instructions for installing PHP 8.x on CentOS. By following these steps, you can take advantage of the latest features, performance improvements, and security enhancements offered by PHP 8.x.

Remember to keep your system and PHP installation updated to ensure optimal performance and security. Additionally, consider implementing PHP best practices and securing your installation to minimize vulnerabilities and ensure a stable environment for your web applications.

Share this:

Categorized in: