CentOS is a Linux-based operating system widely used by web developers. PHP, a popular server-side programming language, is often installed on CentOS. In this article, we'll show you how to install PHP5 and PHP7 on CentOS and provide instructions for switching between PHP versions.
Before installing PHP5, you need to install Apache server, MySQL database, and PHP dependencies. You can do this by running the following command:
sudo yum install httpd mysql-server php php-mysql
Here, httpd is the Apache server, mysql-server is the MySQL database, and php and php-mysql are the PHP dependencies.
After installation, restart the Apache server to apply the changes:
sudo service httpd restart
Next, create a simple PHP file to test if PHP5 was installed successfully. Run the following command:
sudo vi /var/www/html/info.php
Insert the following code:
<?php phpinfo(); ?>
Save and close the file. Then, go to your browser and visit `http://localhost/info.php`. If the page shows the PHP version and relevant information, PHP5 has been successfully installed.
PHP7 offers higher performance and better application results compared to PHP5. To install PHP7, you first need to install EPEL (Extra Packages for Enterprise Linux). Run the following command:
sudo yum install epel-release
Next, install the Webtatic repository:
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Run the following command to install PHP7:
sudo yum install php70w
After installation, restart the server.
Similar to PHP5, create an info.php file and visit `http://localhost/info.php` in your browser. If the page shows PHP7-related information, PHP7 has been successfully installed.
If you want to switch between different PHP versions on the same server, you can use a multi-PHP version manager. First, install the required software:
sudo yum install scl-utils-build scl-utils
Next, install the necessary PHP version packages:
sudo yum install centos-release-scl-rh sudo yum-config-manager --enable rhel-server-rhscl-7-rpms sudo yum install rh-php71 # Install PHP 7.1 sudo yum install rh-php56 # Install PHP 5.6
After installation, you can switch PHP versions by running the following command:
scl enable rh-php71 bashOr
scl enable rh-php56 bash
After choosing the desired PHP version, you can use that version of PHP in your terminal.
This article explained how to install PHP5 and PHP7 on CentOS and provided instructions for switching between PHP versions. Mastering these installation and switching techniques will help you manage your PHP environment more effectively.