In modern web development, configuring the coexistence of PHP5 and PHP7 has become a growing need. Many applications or frameworks may still rely on PHP5, while at the same time, we want to benefit from the performance improvements and new features brought by PHP7. Implementing such coexistence on CentOS allows developers to flexibly choose different PHP versions as needed. This article will introduce the specific steps and configuration tips, enabling you to successfully configure PHP5 and PHP7 coexistence in the CentOS environment.
Before starting, ensure that you have the following environment in place:
First, we need to enable the EPEL and Remi repositories. These repositories provide the software packages required to install different versions of PHP.
<span class="fun">yum install epel-release</span>
<span class="fun">yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm</span>
Next, run the following command to install PHP7 and its common extensions:
<span class="fun">yum --enablerepo=remi-php74 install php php-cli php-fpm php-mysqlnd</span>
Then, we need to install PHP5. Since PHP5 is not available by default in the Remi repository, we need to enable the remi-php55 repository to install it:
<span class="fun">yum --enablerepo=remi-php55 install php php-cli php-fpm php-mysql</span>
After installation, you need to configure PHP-FPM so that the two PHP versions can run on different ports. Here are the basic configuration steps:
Edit the PHP7 FPM configuration file:
<span class="fun">vi /etc/php-fpm.d/www.conf</span>
Modify the listen line to listen on a different port, for example, 9000:
<span class="fun">listen = 127.0.0.1:9000</span>
Similarly, edit the PHP5 FPM configuration file, usually located at:
<span class="fun">vi /etc/php-fpm.d/www.conf</span>
Change it to a different port, such as 9001:
<span class="fun">listen = 127.0.0.1:9001</span>
After configuring PHP-FPM, we also need to configure PHP parsing in the web server. Here, we will use Nginx as an example:
Open the Nginx configuration file and add support for different script types:
server {
listen 80;
server_name your_domain.com;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000; # This is the PHP7 port
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ \.php5$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9001; # This is the PHP5 port
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
After completing the configuration, don’t forget to restart the PHP-FPM and Nginx services to apply the changes:
<span class="fun">systemctl restart php-fpm</span>
<span class="fun">systemctl restart nginx</span>
Finally, you can create two PHP test files for different versions and verify if they work correctly by accessing them through different URLs. For example, create a PHP7 file named info.php with the following content:
<span class="fun"><?php phpinfo(); ?></span>
Similarly, create a PHP5 file named info.php5 with the same content. Depending on the file name being accessed, your web server will call the corresponding PHP version.
With the steps outlined above, you have successfully configured PHP5 and PHP7 coexistence on CentOS. This flexible configuration allows you to run different PHP versions simultaneously, offering greater compatibility and convenience for development environments and applications. In future development, you can freely switch between PHP versions based on project needs, fully utilizing the advantages of both versions.