As we all know, PHP is a widely used programming language, which is usually used for server-side programming. Many websites run on PHP, so it's important to know how to set up your server's PHP version information.
This article will introduce how to set the PHP version information of the server. First, you need to confirm whether your server has PHP installed. If you are not sure whether PHP is installed, you can run the following command:
php -v
If the system prompts that the php command is not found, you need to install php first. For Ubuntu systems, you can install it with the following command:
sudo apt-get install php
For CentOS systems, you can install it with the following command:
sudo yum install php
After the installation is complete, you can check the PHP version with the following command Information:
php -v
Next, we will describe how to set the PHP version information of the server.
Method 1: Modify the PHP configuration file
First, you need to find the PHP configuration file. For Ubuntu systems, the location of the PHP configuration file is usually /etc/php/7.4/cli/php.ini; for CentOS systems, it is usually /etc/php.ini.
Use an editor such as vi or nano to open the PHP configuration file and find the following line:
; version=PHP_VERSION
Remove the comment from this line and modify PHP_VERSION to the PHP version you need, for example:
version=7.4.0
Save the file and exit the editor. Next, restart the PHP service for the changes to take effect:
For Ubuntu systems:
sudo systemctl restart php7.4-fpm
For CentOS systems:
sudo systemctl restart php-fpm
Now, you can confirm whether the PHP version is correct by running the following command Changed:
php -v
Method 2: Using a PHP version management tool
Another way to set the server's PHP version information is to use a PHP version management tool, such as phpenv or phpbrew. These tools allow you to run multiple PHP versions simultaneously on the same server and switch between them easily.
The methods of using these tools are different, depending on which tool you are using. Here we take phpenv as an example.
First, install phpenv:
git clone https://github.com/phpenv/phpenv.git ~/.phpenv
Then, add the following code to the ~/.bash_profile file:
export PATH="$HOME/.phpenv/bin:$PATH" eval "$(phpenv init -)"
Save the file and exit the editor, then execute the following command to Let the changes take effect:
source ~/.bash_profile
Next, install the required PHP version. You can view all available PHP versions on phpenv's GitHub page:
phpenv install -l
Select the PHP version you need, for example:
phpenv install 7.4.0
After the installation is complete, you can confirm it with the following command Multiple PHP versions have been installed in the system:
phpenv versions
Next, set the required PHP version as the global default version:
phpenv global 7.4.0
Now, you can use the php command on the command line to Running PHP 7.4.0 version:
php -v
If you need to use different PHP versions in different applications, you can set the corresponding PHP version as a local version, for example:
phpenv local 7.3.0
Then , create a file named .php-version in the application directory, fill in the PHP version you need to use:
echo "7.3.0" > .php-version
After saving the file, reopen the console window for the changes to take effect.
Summary
The above are two methods on how to set the PHP version information of the server. You can choose one or both methods according to your needs. No matter which method you choose, restarting the PHP service or reopening the console window is necessary for the changes to take effect.
The above is the detailed content of How to set the server's PHP version information. For more information, please follow other related articles on the PHP Chinese website!