How To Install PHP 7.3 on Centos 7

In this article, we want to teach you How To Install PHP 7.3 on Centos 7.

PHP is a server-side scripting language. that is used to develop Static websites or Dynamic websites or Web applications. It stands for Hypertext Pre-processor, which earlier stood for Personal Home Pages.

As you know, Centos 7 comes with PHP 5.4 and it is no longer supported.

By using PHP 7 your applications will load faster and consume fewer system resources.

How To Install PHP 7.3 on Centos 7

Before you start to set up PHP 7.3, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Centos 7.

Now follow the steps below to install PHP on your server.

Set up PHP 7.3

Here we use the Remi repository to install PHP 7.3. And it depends on the Epel repository.

First, install the Epel release on Centos 7 with the following command:

sudo yum install epel-release yum-utils

Then, you can use the following command to install the Remi repo:

sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

With this command, you will be asked to import the GPG key, and press y to complete your installation.

Now enable the Remi repo on Centos 7 with the command below:

sudo yum-config-manager --enable remi-php73

At this point, you can run the following command to install PHP 7.3 and its common modules on your server:

sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd

Verify your installation by checking the PHP version:

php -v

In your output you will see:

Output
PHP 7.3.33 (cli) (built: Nov 16 2021 11:18:28) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.33, Copyright (c) 1999-2018, by Zend Technologies

At this point, we will show you how to configure PHP 7.3 to work with Apache and Nginx.

Configure PHP 7.x to Work with Apache

If you use Apache as your Web server, you just need to restart the Apache service on Centos 7 with the command below:

sudo systemctl restart httpd

Configure PHP 7.x to work with Nginx

To configure PHP to work with Nginx, you need to install a separate application like PHP FPM to handle the PHP files.

First, install the PHP FPM package on Centos 7 with the command below:

sudo yum install php-fpm

By default PHP FPM will run as an Apache user on port 9000. You need to change the user to Nginx and switch from TCP socket to Unix socket.

Now open the PHP FPM configuration file with your favorite text editor, here we use vi:

sudo vi /etc/php-fpm.d/www.conf
At the file, find the lines below and change them to:
...
user = nginx
...
group = nginx
...
listen = /run/php-fpm/www.sock
...

Also, find the lines below and uncomment them by removing the “;” from the beginning of the line and setting them to:

listen.owner = nginx
listen.group = nginx

When you are done, save and close the file.

Then, set the correct permissions for PHP on Centos 7 with the following command:

sudo chown -R root:nginx /var/lib/php

Now enable and start PHP FPM service with the commands below:

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

Next, you need to edit the Nginx virtual host directive.  Open your Nginx server block file with your favorite text editor, here we use vi:

vi /etc/nginx/conf.d/example.com

Add the following location block so that Nginx can process PHP files:

server {
    # . . . other code
    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;
    }
}

When you are done, save and close the file.

Then, restart Nginx on Centos 7 to apply the changes:

sudo systemctl restart nginx

Conclusion

At this point, you learn to set up PHP 7.3 on Centos 7. Also, you learn to configure PHP to work with Apache and Nginx.

Hope you enjoy it.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular