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

Wednesday, March 29, 2017

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

安装PHP7.0

1. 移除旧版php及php扩展

$ yum remove php* php-common

2. 安装php7的yum源

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

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

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

$ 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

$ vim /etc/yum.repos.d/remi.repo
[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

$ vim /etc/yum.repos.d/remi-php70.repo
...
[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及扩展

$ 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扩展

$ 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
$ 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并设置开机自启

修改配置

$ vim /etc/php.ini
date.timezone = Asia/Shanghai
upload_max_filesize = 20M
post_max_size = 20M
display_errors = Off
expose_php = Off

重启php

$ service php-fpm restart

设置开机自启

$ chkconfig php-fpm on

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

安装ZIP扩展

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

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 的源码

$ 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. 编译安装扩展

$ phpize
$ ./configure
$ make && make install

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

3. 启用zip扩展

 vim /etc/php.ini

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

;;;;;;;;;;;;;;;;;;;;;;
; 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

/etc/init.d/httpd restart

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

$ php -m
[PHP Modules]
...
xsl
zip
zlib

[Zend Modules]

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

$ 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!
PHP CentOS PHP ZIP

CentOS6基于Python安装SentryLet's Encrypt