[关闭]
@gabe 2015-12-20T11:35:30.000000Z 字数 3901 阅读 7863

centos7下安装php7

php7 centos7


距离php7发布已经有近10天的时间了,关于php7超强的性能提升这里就不再阐述了,这篇文章主要记录下在centos7上安装php7,nginx,php-memcached的整个过程,不得不说在centos7上安装php7比在centos6.5上顺利太多了,整个过程也就10分钟多点。

安装centos7,nginx

安装PHP7

  1. yum install -y \
  2. gcc-c++ autoconf \
  3. libjpeg libjpeg-devel libpng \
  4. libpng-devel freetype freetype-devel \
  5. libpng libpng-devel libxml2 libxml2-devel \
  6. zlib zlib-devel glibc glibc-devel \
  7. glib2 glib2-devel bzip2 bzip2-devel \
  8. ncurses curl openssl-devel \
  9. gdbm-devel db4-devel libXpm-devel \
  10. libX11-devel gd-devel gmp-devel \
  11. readline-devel libxslt-devel \
  12. expat-devel xmlrpc-c xmlrpc-c-devel \
  13. libicu-devel libmcrypt-devel \
  14. libmemcached-devel
  1. $ cd /usr/src/
  2. $ wget http://cn2.php.net/distributions/php-7.0.0.tar.gz
  3. #解压
  4. $ tar -xzxvf php-7.0.0.tar.gz
  5. $ cd php-7.0.0.0
  1. $ ./configure --prefix=/usr/local/php7 \
  2. --with-mysql-sock --with-mysqli \
  3. --enable-fpm --enable-soap \
  4. --with-libxml-dir --with-openssl \
  5. --with-mcrypt --with-mhash \
  6. --with-pcre-regex --with-zlib \
  7. --enable-bcmath --with-iconv \
  8. --with-bz2 --enable-calendar \
  9. --with-curl --with-cdb --enable-dom \
  10. --enable-exif --enable-fileinfo \
  11. --enable-filter --with-pcre-dir \
  12. --enable-ftp --with-gd \
  13. --with-openssl-dir --with-jpeg-dir \
  14. --with-png-dir --with-zlib-dir \
  15. --with-freetype-dir \
  16. --enable-gd-native-ttf \
  17. --enable-gd-jis-conv --with-gettext \
  18. --with-gmp --with-mhash \
  19. --enable-json --enable-mbstring \
  20. --enable-mbregex \
  21. --enable-mbregex-backtrack \
  22. --with-libmbfl --with-onig \
  23. --enable-pdo --with-pdo-mysql \
  24. --with-zlib-dir --with-readline \
  25. --enable-session --enable-shmop \
  26. --enable-simplexml --enable-sockets \
  27. --enable-sysvmsg --enable-sysvsem \
  28. --enable-sysvshm --enable-wddx \
  29. --with-libxml-dir --with-xsl \
  30. --enable-zip \
  31. --enable-mysqlnd-compression-support \
  32. --with-pear --enable-intl
  33. $ make
  34. $ make install
  1. $ /usr/local/php7/bin/php -v
  2. PHP 7.0.0 (cli) (built: Dec 13 2015 22:28:12) ( NTS )
  3. Copyright (c) 1997-2015 The PHP Group
  4. Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
  1. $ ln -sf /usr/local/php7/bin/php /usr/local/bin/php
  2. $ php -v #和之前的/usr/local/php7/bin/php一样

添加php.ini,如果是生产环境则cp对应的php.ini-production

  1. $ cp php.ini-development /usr/local/php7/lib/php.ini
  1. $ cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
  2. $ cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
  3. $ cp /usr/src/php-7.0.0/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  4. $ chmod +x /etc/init.d/php-fpm

配置文件添加成功后,我们启动php-fpm

  1. $ service php-fpm start

php-fpm启动后我们还需要配置nginx才能通过访问php页面,首先确认nginx是启动的。service nginx restart

  1. $ echo '<?php phpinfo();' > /usr/share/nginx/html/info.php

然后访问 http://127.0.0.1/info.php,应该看到一个提示下载的页面。这是因为为nginx现在无法处理php文件,接下来我们配置nginx使其将php访问交给php-fpm处理。在/etc/nginx/nginx.confserver块里面添加下面的配置

  1. location ~ \.php(/|$) {
  2. fastcgi_pass 127.0.0.1:9000;
  3. fastcgi_index index.php;
  4. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  5. include fastcgi_params;
  6. }

保存退出,并重启nginx

  1. $ service nginx restart

现在重新访问 http://127.0.0.1/info.php应该能看到我们熟悉的页面了。

安装php-memcached

下载php-memcached,从github上下载,记住一定要选择对应的php7分支

  1. $ cd /usr/src
  2. $ git clone https://github.com/php-memcached-dev/php-memcached.git
  3. $ cd php-memcached/
  4. $ git checkout php7
  5. $ /usr/local/php7/bin/phpize
  6. $ ./configure --with-php-config=/usr/local/php7/bin/php-config
  7. $ make
  8. $ make install

修改php.ini

  1. $ vi /usr/local/php7/lib/php.ini
  2. #在最下面加上
  3. extension=memcached.so

保存并退出,重启php-fpm

  1. $ service php-fpm start

再次访问http://127.0.0.0/info.php,就可以看到memcached已经安装成功了。
以上php7,php7-memcached,nginx就已经安装完成了。


优化

  1. $ vi /usr/local/php7/lib/php.ini
  2. [opcache]
  3. zend_extension=opcache.so #新加此行
  4. opcache.enable=1 #删除此行前面的注释,并将0改为1,启用opcache
  5. opcache.enable_cli=1 #删除此行前面的注释,并将0改为1,在cli模式下启用opcache
  6. opcache.revalidate_freq=10 #可选,设置10s检查一次文件变化

通过 http://127.0.0.1/info.phpphp -i可以看到opcache在web和cli模式下都已经启用了。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注