Home > Backend Development > PHP7 > body text

The correct way to install PHP7 on CentOS

coldplay.xixi
Release: 2023-02-17 16:32:01
forward
3485 people have browsed it

The correct way to install PHP7 on CentOS

##The PHP versions on CentOS are very old and cannot meet the requirements of some frameworks. PHP version required. As a result, many third-party software libraries have emerged, such as EPEL, RPM Fusion, Remi, etc., which provide new versions of PHP. Let our system keep pace with the times.

However, third-party software libraries have several disadvantages: First, the software they provide has not been officially tested by CentOS. When installing the software, some core files of the system may be replaced, causing system instability. . Second, the software installed by third-party libraries may not guarantee compatibility, and upgrading the system may render certain software unusable.

So this article recommends using the SCL (Software Collections) software library to install a higher version of PHP. SCL belongs to the official software library of CentOS. It has been fully tested and will not replace the core files of the system when installing the software, ensuring the stability of the system.

Installing SCL is very simple, just one command is enough:

[root@localhost]# yum install centos-release-scl-rh
Copy after login
Copy after login

Then you can search for the new version of PHP in yum:

[root@localhost]# yum search php...php54-runtime.x86_64
php55-runtime.x86_64
rh-php56-runtime.x86_64
rh-php70-runtime.x86_64...
Copy after login
Copy after login

rh- The prefix means RedHat, telling you that this is the official PHP provided and not provided by a third-party library. The author guesses that this prefix specification was formulated around 2015, and PHP5.4 & 5.5 were released before that. In order to ensure the compatibility of your program (for example, some of your programs have already written yum install php55, add a prefix The program hangs), without the rh- prefix.

Next, install PHP7.0:

[root@localhost]# yum install rh-php70
Copy after login

After installation, PHP will actually be installed in the /opt/rh directory.

If you run the php command at this time, the system will still prompt you command not found. This is because the style of SCL is to minimize the impact of software on the system. Even after PHP is installed, the php command will not be added to the

$PATH variable, so you cannot directly execute the software. of the command. You need to display the execution through the scl enable command:

First look at what software is installed by SCL:

[root@localhost]# scl -lrh-php70
Copy after login
Copy after login

You can see that we have installed a rh-php70 in the system. First Enable it and execute the command:

[root@localhost]# scl enable rh-php70 "php -v"PHP 7.0.10 (cli) (built: Nov  3 2016 08:06:03) ( NTS )Copyright (c) 1997-2016 The PHP GroupZend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
Copy after login
Copy after login

The command was executed successfully. It would be too troublesome if every command had to be executed like this! It doesn't matter, you don't need to execute one command each time, but directly execute the

bash command, then the newly opened shell will automatically recognize php:

[root@localhost]# scl enable rh-php70 bash[root@localhost]# php -vPHP 7.0.10 (cli) (built: Nov  3 2016 08:06:03) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

[root@localhost ~]# php -m[PHP Modules]
bz2
calendar...
Copy after login
Copy after login

SCL to

scl enable This method of management software adds a little trouble to use, but it absolutely guarantees system stability. And there is another advantage to doing this: multiple PHP versions can coexist on the system without conflicting with each other. It is convenient for you to test the compatibility of your code or framework on various versions of PHP.

In short, if you want to do a rigorous operation and maintenance, this article still highly recommends using SCL to manage the software on your system.

The PHP version on CentOS is very old and cannot meet the PHP version requirements of some frameworks. As a result, many third-party software libraries have emerged, such as EPEL, RPM Fusion, Remi, etc., which provide new versions of PHP. Let our system keep pace with the times.

However, third-party software libraries have several disadvantages: First, the software they provide has not been officially tested by CentOS. When installing the software, some core files of the system may be replaced, causing system instability. . Second, the software installed by third-party libraries may not guarantee compatibility, and upgrading the system may render certain software unusable.

So this article recommends using the SCL (Software Collections) software library to install a higher version of PHP. SCL belongs to the official software library of CentOS. It has been fully tested and will not replace the core files of the system when installing the software, ensuring the stability of the system.

Installing SCL is very simple, just one command is enough:

[root@localhost]# yum install centos-release-scl-rh
Copy after login
Copy after login

Then you can search for the new version of PHP in yum:

[root@localhost]# yum search php...php54-runtime.x86_64
php55-runtime.x86_64
rh-php56-runtime.x86_64
rh-php70-runtime.x86_64...
Copy after login
Copy after login

rh- The prefix means RedHat, telling you that this is the official PHP provided and not provided by a third-party library. The author guesses that this prefix specification was formulated around 2015, and PHP5.4 & 5.5 were released before that. In order to ensure the compatibility of your program (for example, some of your programs have already written yum install php55, add a prefix The program hangs), without the rh- prefix.

Next, install PHP7.0:

[root@localhost]# yum install rh-php70
Copy after login

After installation, PHP will actually be installed in the /opt/rh directory.

此时如果你运行php命令,系统依然会提示你command not found。这是因为,SCL的风格就是把软件对系统的影响减少到最小,甚至安装完PHP,php命令都不会被添加到 $PATH 变量中,所以你没法直接执行软件中的命令的。需要通过 scl enable 命令显示执行:

先看看SCL安装了哪些软件:

[root@localhost]# scl -lrh-php70
Copy after login
Copy after login

可以看到我们在系统安装了一个rh-php70,先启用它并执行命令:

[root@localhost]# scl enable rh-php70 "php -v"PHP 7.0.10 (cli) (built: Nov  3 2016 08:06:03) ( NTS )Copyright (c) 1997-2016 The PHP GroupZend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
Copy after login
Copy after login

命令成功执行了。要是每一条命令都要这么执行,太麻烦了!不要紧,你可以不用每次执行一条命令,而是直接执行 bash 命令,那么新开的shell就能自动识别php了:

[root@localhost]# scl enable rh-php70 bash[root@localhost]# php -vPHP 7.0.10 (cli) (built: Nov  3 2016 08:06:03) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

[root@localhost ~]# php -m[PHP Modules]
bz2
calendar...
Copy after login
Copy after login

SCL以 scl enable 方式管理软件,虽然在使用上增加了一点点的麻烦,但这绝对地保证了系统稳定性。而且这么做还有一个好处:就是系统上可以多个PHP版本共存而互相不冲突。方便你测试代码或者框架,在各个版本PHP上的兼容性。

总之,要做一个严谨的运维,本篇还是十分推荐用SCL管理你系统上的软件。

推荐教程:《php视频教程

The above is the detailed content of The correct way to install PHP7 on CentOS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template