全新CentOS安装 PHP7升级笔记

PHP7已经到7.0.6版本了,是时候来尝试下小象飞的时候了,哈哈,那么就来配个PHP7的环境来玩玩…

更换时区

1
2
$ sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
$ ntpdate time.windows.com && hwclock -w

查看CentOS 版本

$ cat /etc/redhat-release
顺便看看linux环境内核、gcc信息
$ cat /proc/version

更换163 yum 源

  • 登录mirrors.163.com
  • 点击centos后面的“centos使用帮助”
  • $ mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  • 下载对应版本repo文件, 放入/etc/yum.repos.d/(操作前请做好相应备份),我的是CentOS6
1
2
3
$ cd /etc/yum.repos.d/&&wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
$ yum clean all
$ yum makecache

升级gcc版本

  • 先升级到4.7版本
1
2
3
4
5
6
7
8
9
10
$ cd /etc/yum.repos.d
$ sudo wget http://people.centos.org/tru/devtools-1.1/devtools-1.1.repo
$ sudo yum --enablerepo=testing-1.1-devtools-6 install devtoolset-1.1-gcc devtoolset-1.1-gcc-c++
$ mv /usr/bin/gcc /usr/bin/gcc-4.4.7
$ mv /usr/bin/g++ /usr/bin/g++-4.4.7
$ mv /usr/bin/c++ /usr/bin/c++-4.4.7
$ ln -s /opt/centos/devtoolset-1.1/root/usr/bin/gcc /usr/bin/gcc
$ ln -s /opt/centos/devtoolset-1.1/root/usr/bin/c++ /usr/bin/c++
$ ln -s /opt/centos/devtoolset-1.1/root/usr/bin/g++ /usr/bin/g++
$ gcc --version
  • 升级到4.8
1
2
3
4
5
6
7
8
9
10
$ cd /etc/yum.repos.d
$ wget http://people.centos.org/tru/devtools-2/devtools-2.repo
$ yum install devtoolset-2-gcc devtoolset-2-binutils devtoolset-2-gcc-c++
$ mv /usr/bin/gcc /usr/bin/gcc-4.4.7
$ mv /usr/bin/g++ /usr/bin/g++-4.4.7
$ mv /usr/bin/c++ /usr/bin/c++-4.4.7
$ ln -s /opt/rh/devtoolset-2/root/usr/bin/gcc /usr/bin/gcc
$ ln -s /opt/rh/devtoolset-2/root/usr/bin/c++ /usr/bin/c++
$ ln -s /opt/rh/devtoolset-2/root/usr/bin/g++ /usr/bin/g++
$ gcc --version

安装先导库

1
$ yum install php-mcrypt libmcrypt libmcrypt-devel  libxml2-devel  openssl-devel  libcurl-devel libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64  libjpeg-turbo-devel   libmcrypt-devel   mysql-devel  -y

安装php7

  • 下载php7最新源码
1
$ cd ~&&wget http://cn2.php.net/distributions/php-7.0.6.tar.gz
  • 解压
1
$ tar zxvf php-7.0.6.tar.gz&&cd cd php-7.0.6
  • 编译安装
1
2
$ ./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc --with-mcrypt=/usr/include --enable-mysqlnd --with-gd --with-iconv --with-zlib --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --with-curl --with-jpeg-dir --with-freetype-dir --with-mysqli --enable-embedded-mysqli  --with-pdo-mysql --with-config-file-scan-dir=/usr/local/php7/etc/conf.d
$make&&sudo make install
  • 建立软连接到/usr/local/bin/
1
$ sudo ln -s /usr/local/php7/bin/php* /usr/local/bin/
  • 配置php
1
2
3
4
5
$ cp php.ini-production /usr/local/php7/etc/php.ini
$ cp sapi/fpm/init.d.php-fpm /etc/init.d/php7-fpm
$ chmod +x /etc/init.d/php7-fpm
$ cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
$ cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
  • 加入opcache
1
$ sudo vim /usr/local/php7/etc/conf.d/opcache.ini

加入: zend_extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/opcache.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
26
27
28
29
30
31
32
33
34
35
36
[opcache]
zend_extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/opcache.so
; Determines if Zend OPCache is enabled 启用opcache
opcache.enable=1
; Determines if Zend OPCache is enabled for the CLI version of PHP 在cli下启用opcache
opcache.enable_cli=1
; The OPcache shared memory storage size. opcache使用的内存,单位MB
opcache.memory_consumption=256
; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=8
; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 100000 are allowed. 存储最大的对象数目
opcache.max_accelerated_files=100000
; The maximum percentage of "wasted" memory until a restart is scheduled. 最大浪费内存百分比,超出将会GC
opcache.max_wasted_percentage=5
; When this directive is enabled, the OPcache appends the current working
; directory to the script key, thus eliminating possible collisions between
; files with the same name (basename). Disabling the directive improves
; performance, but may break existing applications.
opcache.use_cwd=1
; When disabled, you must reset the OPcache manually or restart the
; webserver for changes to the filesystem to take effect. 验证文件改动时间错,开启之后将由validate_timestamps来提供刷新频率,对于加载文件很多的程序性能有极大影响,关闭之后将会杜绝验证文件的时候造成的抖动,但是更新文件之后需要手动重启服务或者刷新全部内容
opcache.validate_timestamps=0
; How often (in seconds) to check file timestamps for changes to the shared
; memory storage allocation. ("1" means validate once per second, but only
; once per request. "0" means always validate) 验证时间间隔,1表示1秒,0表示总是验证
opcache.revalidate_freq=5
; Enables or disables file search in include_path optimization
opcache.revalidate_path=1
; If disabled, all PHPDoc comments are dropped from the code to reduce the
; size of the optimized code. 缓存中保存文档信息
opcache.save_comments=1
; If disabled, PHPDoc comments are not loaded from SHM, so "Doc Comments"
; may be always stored (save_comments=1), but not loaded by applications
; that don't need them anyway. 加载文档信息
opcache.load_comments=1
  • 启动
1
$ sudo /etc/init.d/php7-fpm start
  • ==为了安全最好是去掉头信息 X-Powered-By: PHP/7.则修改 php.ini 文件 设置 expose_php = Off==
1
$ vim /usr/local/php7/etc/php.ini

找到 expose_php = On
改为 expose_php = Off

安装redis扩展

  • 首先要安装依赖 igbinary
1
2
3
4
5
6
$ cd ~&&wget https://github.com/igbinary/igbinary7/archive/master.zip -O igbinary7.zip&&unzip igbinary7.zip&&cd igbinary7-master
$ phpize
$./configure
$ make&&sudo make install
$ cd /usr/local/php7/etc/conf.d/&&sudo cp opcache.ini igbinary.ini
$ vim igbinary.ini加入:extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/igbinary.so
  • 安装redis
    安装git

    1
    $ sudo yum install git
  • 检出redis

1
2
3
4
5
6
7
8
$ git clone https://github.com/phpredis/phpredis.git
$ cd phpredis&&git branch -r
$ git branch --track php7 origin/php7
$ git checkout php7
$ phpize&&./configure --enable-redis-igbinary
$ make &&sudo make install
$ cd /usr/local/php7/etc/conf.d&&sudo cp igbinary.ini redis.ini
$ vim redis.ini加入:extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/redis.so

安装mongodb扩展

1
2
3
4
5
6
7
$ git clone https://github.com/mongodb/mongo-php-driver.git &&cd mongo-php-driver
$ git submodule sync && git submodule update --init
$ phpize
$ ./configure --with-php-config=/usr/local/php7/bin/php-config
$ make all -j 5&& sudo make install
$ cd /usr/local/php7/etc/conf.d&&sudo cp redis.ini mongodb.ini
$ vim mongodb.ini加入:extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/mongodb.so

安装mamcache扩展

  • 检出git仓库
1
2
3
4
5
6
7
8
9
$ git clone git clone https://github.com/websupport-sk/pecl-memcache.git
$ cd pecl-memcache&git branch -r
$ git branch --track php7 origin/php7
$ git checkout php7
$ phpize
$ ./configure&&make
$ sudo make install
$ cd /usr/local/php7/etc/conf.d&&sudo cp redis.ini memcache.ini
$ sudo vim memcache.ini加入:extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/memcache.so

安装mamcached扩展

  • 源码安装libmemcached
1
2
3
4
$ wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz&&tar zxvf libmemcached-1.0.18.tar.gz
$ cd libmemcached-1.0.18
$ ./configure --prefix=/usr/local/libmemcached --enable-sasl
$make && sudo make install
  • 检出git仓库
1
2
3
4
5
6
7
$ git clone https://github.com/php-memcached-dev/php-memcached.git
$ cd php-memcached&&git checkout php7 orgin/php7&&git pull
$ phpize
$ ./configure --with-libmemcached-dir=/usr/local/libmemcached
$ sudo make &&make install
$ cd /usr/local/php7/etc/conf.d&&sudo cp memcache.ini memcached.ini
$ sudo vim memcached.ini加入:extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/memcached.so

安装apcu扩展

1
2
3
4
5
6
7
8
9
10
11
12
13
$ wget https://github.com/krakjoe/apcu/archive/v5.1.4.zip -O apcu-v5.1.4.zip&&unzip apcu-v5.1.4.zip&& cd apcu-5.1.4/
$ php phpize&& ./configure
$ make&&&&sudo make install
$ cd /usr/local/php7/etc/conf.d&&sudo cp redis.ini apcu.ini
$ sudo vim apcu.ini
加入:
extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/apcu.so
[apcu]
apc.enabled=1
apc.shm_segments=1
apc.shm_size=64M
apc.ttl=0
apc.enable_cli=1apcu.so