Home  >  Article  >  Backend Development  >  lamp installs multiple php versions

lamp installs multiple php versions

PHPz
PHPzOriginal
2023-05-07 10:34:09688browse

As web applications become more and more complex, different programmers may need to use different PHP versions to complete their respective jobs. Generally speaking, having only one version of PHP on a server is not enough, and installing multiple PHP versions can satisfy this need well.

In this article, we will introduce how to install multiple php versions in the lamp environment, and use a simple example to demonstrate how to switch php versions.

  1. Update lamp environment

Before starting to install multiple php versions, we need to ensure that the system and lamp environment are up to date. Specifically, you can use the following command to update the system and lamp environment:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install lamp-server^

During the installation process, you may be asked to set the root password for the MySQL and Apache servers.

  1. Install different versions of php

When installing the lamp environment, php5 will be installed by default. If you need to install other versions of php, you can use the following command:

sudo apt-get install php7.0 php7.1 php7.2

In the above example, we installed php7.0, php7.1 and php7.2. Different versions of php can be installed as needed.

  1. Configuring different versions of php

Each installed php version has its own configuration file, so they need to be configured separately. In the Ubuntu system, the php configuration file is located in the /etc/php/{version}/apache2/ directory.

You can use the following command to open the php7.0 configuration file and modify the configuration:

sudo nano /etc/php/7.0/apache2/php.ini

Similarly, you can also use similar commands to open other versions of php configuration files.

When editing the php configuration file, you need to pay attention to the following points:

  • Determine the configuration items to be modified, such as: memory_limit, post_max_size, upload_max_filesize, etc.
  • Change the value of the configuration item and set the appropriate value as needed
  • Save and close the file

After completing the above steps, you may need to restart the apache server to To make the changes effective:

sudo service apache2 restart
  1. Switch PHP version

When there are multiple PHP versions installed on the system, the default PHP version will be the last one installed. In order to switch php version, you can use the following command:

sudo a2dismod php7.2 
sudo a2enmod php7.1
sudo service apache2 restart

In the above example, we have disabled php7.2 and enabled php7.1. With these commands, we have successfully changed the php version from 7.2 to 7.1 and set php7.1 as the default version.

  1. Example

Now, let’s use a simple PHP script to demonstrate how to switch php versions. First, create a php file and open it using the nano editor:

nano /var/www/html/phpinfo.php

Copy the following code into the file:

<?php
phpinfo();
?>

Save and close the file. Access this file in your browser and you will see the currently enabled version of php. If you follow the instructions above to switch to another php version, after refreshing the page you will see the changed php version.

  1. Summary

In this article, we introduced how to install multiple php versions in a lamp environment and demonstrated how to switch php versions through an example. By managing different PHP versions so conveniently, programmers can complete their respective tasks efficiently, thus improving work efficiency.

The above is the detailed content of lamp installs multiple php versions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn