CentOS6升级PHP7.0及安装ZIP扩展

我使用了 laravel-backup 来备份 悟禅小书童 文件和数据,本地开发测试正常,更新到线上后,composer update 时报错了,线上服务器的PHP版本是 5.6,但/laravel-backup 最新版要求 7.0+,没办法,只好重新安装PHP,并安装对应的扩展。

安装PHP7.0

1. 移除旧版php及php扩展

1
$ yum remove php* php-common

2. 安装php7的yum源

1
$ rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

实际安装时报了如下错误:

1
2
3
4
5
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
Retrieving http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
warning: /var/tmp/rpm-tmp.yZ1q8y: Header V3 DSA/SHA1 Signature, key ID 00f97f56: NOKEY
error: Failed dependencies:
epel-release >= 6 is needed by remi-release-6.6-1.el6.remi.noarch

解决方式是先安装 epel-release-6-8.noarch.rpm,再安装 remi-release-6.rpm

1
2
$ rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm 
$ rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

3. 修改yum源

修改 /etc/yum.repos.d/remi.repo 文件,将 [remi] 段中的 enabled=0 改为 enabled=1

1
$ vim /etc/yum.repos.d/remi.repo
1
2
3
4
5
6
7
8
[remi]
name=Remi's RPM repository for Enterprise Linux 6 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/6/remi/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/6/remi/httpsmirror
mirrorlist=http://rpms.remirepo.net/enterprise/6/remi/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

修改 /etc/yum.repos.d/remi-php70.repo 文件,将 [remi-php70] 段中的 enabled=0 改为 enabled=1

1
$ vim /etc/yum.repos.d/remi-php70.repo
1
2
3
4
5
6
7
8
9
10
...
[remi-php70]
name=Remi's PHP 7.0 RPM repository for Enterprise Linux 6 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/6/php70/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/6/php70/httpsmirror
mirrorlist=http://rpms.remirepo.net/enterprise/6/php70/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
...

4. yum安装php7及扩展

1
$ yum install php php-fpm php-cli php-pdo php-mysql php-gd php-bcmath php-xml php-mbstring php-mcrypt php-redis php-pear php-devel

记得一定要安装 php-pearphp-devel,后续安装 zip 扩展要用到。

安装完成后检测下 php版本php扩展

1
2
3
4
$ php -v
PHP 7.0.17 (cli) (built: Mar 14 2017 15:23:16) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
$ php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
igbinary
json
libxml
mbstring
mcrypt
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
zlib

[Zend Modules]

5. 配置php并设置开机自启

修改配置

1
$ vim /etc/php.ini
1
2
3
4
5
date.timezone = Asia/Shanghai
upload_max_filesize = 20M
post_max_size = 20M
display_errors = Off
expose_php = Off

重启php

1
$ service php-fpm restart

设置开机自启

1
$ chkconfig php-fpm on

至此,PHP就成功升级到 7.0 了。

安装ZIP扩展

使用 laravel-backup 备份数据库时,报错了:

1
2
3
4
5
6
7
Starting backup...
Dumping database blog...
Determining files to backup...
Zipping 1 files...

[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'ZipArchive' not found

很明显,错误原因时没有安装 zip 扩展,安装即可。

1. 下载 PHP7.0.17 的源码

1
2
3
4
$ cd ~
$ wget http://php.net/get/php-7.0.17.tar.gz/from/this/mirror
$ tar zxvf php-7.0.17.tar.gz
$ cd php-7.0.17/ext/zip/

2. 编译安装扩展

1
2
3
$ phpize
$ ./configure
$ make && make install

安装成功后 zip.so 会保存在 /usr/lib64/php/modules/ 目录下。

3. 启用zip扩展

1
vim /etc/php.ini

Dynamic Extensions 部分增加 extension=zip.so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

; If you wish to have an extension loaded automatically, use the following
; syntax:
;
; extension=modulename.extension
;
; For example, on Windows:
;
; extension=msql.dll
;
; ... or under UNIX:
;
; extension=msql.so
;
; ... or with a path:
;
; extension=/path/to/extension/msql.so
;
; If you only provide the name of the extension, PHP will look for it in its
; default extension directory.

extension=zip.so

4. 重启apache

1
/etc/init.d/httpd restart

至此,zip扩展就安装好了,用 php -m 检测下,可以看到已经有了 zip

1
2
3
4
5
6
7
8
$ php -m
[PHP Modules]
...
xsl
zip
zlib

[Zend Modules]

接下来重新测试备份数据库,可以看到已经备份成功。

1
2
3
4
5
6
7
8
9
10
11
$ php artisan backup:run --only-db
Starting backup...
Dumping database blog...
Determining files to backup...
Zipping 1 files...
Created zip containing 1 files. Size is 41.62 KB
Copying zip to disk named local...
Successfully copied zip to disk named local.
Copying zip to disk named qiniu...
Successfully copied zip to disk named qiniu.
Backup completed!

本文作者:Jormin
本文地址https://blog.lerzen.com/2017/03/29/centos6升级php7-0及安装zip扩展/
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!

----- 到这结束咯 感谢您的阅读 -----