首页 > 编程技术 > php

PHP 7安装和开启opcache的教程

发布时间:2016-11-25 15:27

PHP 5.5+版本以上的,可以使用PHP自带的opcache开启性能加速(默认是关闭的),但如果没有安装opcache的话可以和小编一起来看看PHP 7安装和开启opcache的教程,希望文章能够帮助到大家哦。


鸟哥在博客中说,提高PHP 7性能的几个tips,第一条就是开启opache,引用下原文:

记得启用Zend Opcache, 因为PHP7即使不启用Opcache速度也比PHP-5.6启用了Opcache快, 所以之前测试时期就发生了有人一直没有启用Opcache的事情

我的阿里云服务器是通过一个叫remi的centos源提供的PHP 7,默认没装opcache,用yum list yum70*命令搜索一下,果然有,于是install下来:

yum install php70-php-opcache.x86_64

然后重启php-fpm,使用service php70-php-fpm restart命令。(不同的系统和环境根据自己的情况安装和开启opache)

opcache

新建一个php文件,用phpinfo()函数显示php.ini信息,如果可以找到OPcache项,说明成功启用了。安装和启用PHP 7,就是给PHP 7再加一个翅膀!

PHP7在windows环境中安装已经不像低版本的php了它需要安装其它的一些插件才可以正常的运行了,下面我们来看一篇关于Windows服务器安装配置PHP7.0环境图文教程了,希望对各位有帮助。

在Windows环境下载及配置PHP7.0,2015年9月可下载PHP5.4至7.0,这次使用最新PHP版本7.0。而「PHP 7.0.0 RC 1」、「PHP 7.0.0 RC 2」是属于开发当中的版本,在生产环境使用需注意,稳定版是PHP5.6.13。

确认Microsoft Visual C++版本

Microsoft Visual C++的版本至关重要,根据Microsoft Visual C++版本下载的PHP不同。

PHP官网的左侧栏有以下注释。

VC9, VC11 & VC14
More recent versions of PHP are built with VC9, VC11 or VC14 (Visual Studio 2008, 2012 or 2015 compiler respectively) and include improvements in performance and stability.
– The VC9 builds require you to have the Visual C++ Redistributable for Visual Studio 2008 SP1 x86 or x64 installed
– The VC11 builds require to have the Visual C++ Redistributable for Visual Studio 2012 x86 or x64 installed
– The VC14 builds require to have the Visual C++ Redistributable for Visual Studio 2015 x86 or x64 installed
根据Microsoft Visual C++版本选择下载的PHP版本。

VC9:Microsoft Visual C++ 2008
VC11:Microsoft Visual C++ 2012
VC14:Microsoft Visual C++ 2015

点击「开始」->「控制面板」>->「卸载程序」,可以查看到现在的Microsoft Visual C++的版本。


MicrosoftVisual

Microsoft Visual C++版本是2015,因此必须选择VC14开始的PHP文件。

下载PHP7.0

从以下PHP官网进行下载。

PHP7.0.0RC2Released

「PHP 7.0.0 RC 2」处写着「Windows source and binaries can be found on windows.php.net/qa/」。

WindowsPHPNet-QA

下载源码访问如下URL。
DownLoad-PHP7.0

PHP7.0(7.0.0RC2)的32位有「VC14 x86 Non Thread Safe」、「VC14 x86 Thread Safe」,64位有「VC14 x64 Non Thread Safe」、「VC14 x64 Thread Safe」。这次使用环境是Windows Server2008 64位因此选择下载64位的「VC14 x64 Thread Safe」,大家根据自己的实际环境选择下载的源文件(下载之前再次提醒大家注意VC9、VC11、VC14是根据Windows环境的Microsoft Visual C++版本选择,如果Microsoft Visual C++ 2012的话,就不可以下载VC14的PHP文件。)。

重要的是在左侧栏,有以下注意事项。

IIS
If you are using PHP as FastCGI with IIS you should use the Non-Thread Safe (NTS) versions of PHP.
Apache
Please use the Apache builds provided by Apache Lounge. They provide VC9, VC11 and VC14 builds of Apache for x86 and x64. We use their binaries to build the Apache SAPIs.
If you are using PHP as module with Apache builds from apache.org (not recommended) you need to use the older VC6 versions of PHP compiled with the legacy Visual Studio 6 compiler. Do NOT use VC9+ versions of PHP with the apache.org binaries.
With Apache you have to use the Thread Safe (TS) versions of PHP.
这次是在以Apache为Web服务器的环境下使用PHP。Apache可在Apache Lounge进行下载(具体的下载方法参照下载Apache2.4.16,安装方法参照安装Apache2.4.16)。
PHP有「Non Thread Safe」、「Thread Safe」版本,在这里使用「Thread Safe」版本。

点击「VC14 x64 Thread Safe (2015-Sep-04 02:40:39)」的Zip。

DownLoad-VC14 x64 Thread Safe

点击「保存」下载到任意的目录下,就完成PHP的下载步骤了。

SavePHP7.0

安装PHP7.0

接着来进行PHP的安装步骤。把下载的文件解压以后,存放到任意的目录下就完成安装了。这次安装到「C:\MiddleWare\PHP」。

PHP7.0

运行PHP所需的所有文件都在该目录下。

1) 添加PHP模块

首先确认PHP模块的路径。

php7apache2_4

在「httpd.conf」文件,寻找「Dynamic Shared Object (DSO) Support」的部分。默认是在61行附近。
并在最后追加如下一行。
LoadModule php7_module C:/MiddleWare/PHP/php7apache2_4.dll
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
LoadModule access_compat_module modules/mod_access_compat.so
LoadModule actions_module modules/mod_actions.so
・・・
・・・
#LoadModule xml2enc_module modules/mod_xml2enc.so
LoadModule php7_module C:/MiddleWare/PHP/php7apache2_4.dll
追加以上配置以后Apache启动时,会读取PHP模块。

2) 登录扩展名

把PHP的扩展名(.php)和PHP关联起来。例如访问「http://www.example.com/sample.php」时,返回给浏览器的不是sample.php的内容,而是sample.php记载的PHP脚本的执行结果。

在「httpd.conf」文件里搜索「AddType」。默认是在404行附近,并追加如下2行。
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
#
    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    #
    TypesConfig conf/mime.types

    #
    # AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
    #
    # AddEncoding allows you to have certain browsers uncompress
    # information on the fly. Note: Not all browsers support this.
    #
    #AddEncoding x-compress .Z
    #AddEncoding x-gzip .gz .tgz
    #
    # If the AddEncoding directives above are commented-out, then you
    # probably should define those extensions to indicate media types:
    #
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
  AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
・・・
・・・
</IfModule>
追加以上2行之后当访问扩展名为.php文件时,PHP程序会进行处理。

3) 指定php.ini配置文件路径(PHPIniDir)

首先确认存放php.ini文件目录。

phpini
在「httpd.conf」文件里指定PHP配置文件php.ini的目录,在这里添加到「httpd.conf」文件的最后。
PHPIniDir “c:/MiddleWare/PHP”
Apache会参照PHPIniDir目录下的php.ini文件。

4) 确认PHP运行情况

PHP相关的Apache配置已经完成。在这里作一个简单的PHP脚本文件确认配置是否正确。变更配置文件之后别忘了重启Apache。

打开文本编辑器,追加如下内容。
<?php
phpinfo();
?>

文静名为「phpinfo.php」,保存到Apache的htdocs目录下。

phpinfo

打开浏览器输入「http://localhost/phpinfo.php」。如以下页面显示PHP相关信息的话,Apache可以正常调用PHP了。

phpinof


好了以上就是关于Windows服务器安装配置PHP7.0环境图文教程了,这里介绍了安装那个版本的插件可以支持php 7正常在windows中运行了。

Zend Opcache 已经集成在了PHP 5.5里面,编译安装PHP5.5的时候加上--enable-opcache就行了。但也支持低版本的PHP 5.2.*, 5.3.*, 5.4.*,未来会取消对5.2的支持


Optimizer+ 是 Zend 开发的闭源但可以免费使用的 PHP 优化加速组件,是第一个也是最快的 opcode 缓存工具。现在,Zend 科技公司将 Optimizer+ 在 PHP License 下开源成为 Zend Opcache。

Zend OPcache 通过 opcode 缓存和优化提供更快的 PHP 执行过程。它将预编译的脚本文件存储在共享内存中供以后使用,从而避免了从
磁盘读取代码并进行编译的时间消耗。同时,它还应用了一些代码优化模式,使得代码执行更快。


官方网站:http://pecl.php.net/package/ZendOpcache

依次执行下面的命令

wget http://pecl.php.net/get/zendopcache-7.0.3.tgz
tar xzf zendopcache-7.0.3.tgz
cd zendopcache-7.0.3
phpize
###如果找不到phpize 的话自己找PHP路径,我的在/usr/local/php/bin/phpize,下面这行也要按你的php.ini路径自行修改
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

如果显示Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/ 表示安装完成,下面要修改php的配置文件让它生效

2、启用Zend Opcache

注:如果使用Windows开发环境,或者使用brew或apt-get等命令安装的PHP可以略过编译步骤。
默认情况下,Zend Opcache没有开启,需要我们在编译时使用--enable-opcache指定启用Zend Opcache。

编译好PHP后还需要在php.ini中指定Opcache扩展路径:

zend_extension=/path/to/opcache.so

一般而言PHP编译成功后会显示Zend Opcache扩展路径,但如果想不起来,可以使用如下命令找到PHP扩展所在目录:

php-config --extension-dir

注:如果你使用Xdebug,需要在php.ini中先加载Zend Opcache,再加载Xdebug。
更新php.ini后重启PHP进程并查看是否启用成功:

php-zendopcache

3、配置Zend Opcache

启用Zend Opcache后还需要在php.ini中配置Zend Opcache,下面是一份配置示例作为参考:

opcache.validate_timestamps=1    //生产环境中配置为0
opcache.revalidate_freq=0    //检查脚本时间戳是否有更新时间
opcache.memory_consumption=64    //Opcache的共享内存大小,以M为单位
opcache.interned_strings_buffer=16    //用来存储临时字符串的内存大小,以M为单位
opcache.max_accelerated_files=4000    //Opcache哈希表可以存储的脚本文件数量上限
opcache.fast_shutdown=1         //使用快速停止续发事件
注:后续我们还会进一步介绍Zend Opcache的配置,PHP官网中列出了Zend Opcache的全部设置:http://ua2.php.net/manual/zh/opcache.configuration.php。

4、使用Zend Opcache

Zend Opcache使用起来很简单,因为启用之后它会自动运行,Zend Opcache会自动在内存中缓存预先编译好的PHP字节码,如果缓存了某个文件的字节码,就执行对应的字节码。

如果php.ini中配置了opcache.validate_timestamps值为0,需要小心,因为Zend Opcache将不能觉察PHP脚本的变化,必须手动清空Zend OPcache缓存的字节码,才能让它发现PHP文件的变动。这个配置适合在生产环境中设置为0,但在开发环境会带来不便,我们可以在开发环境中这样配置启用自动验证缓存功能:

opcache.validate_timestamps=1
opcache.revalidate_freq=0

编译安装软件我们可以更好的提升php 7性能了,下面我们为各位介绍PHP 7 的编译安装的方法了,希望这篇文章能够为各位带来帮助。

php 的编译安装相当简单,且每个版本差异不大,正因为如此之前版本的编译安装过程也可以参考,当然,需要注意的是,每个版本的编译参数存在部分差异,以 ./configure --help 中具体描述的编译配置参数为准。

本文中系统环境为 CentOS 7 64bit,如使用其他系统环境的,请注意在某些 依赖安装环节 的差异。

准备工作

对于 CentOS ,yum 我向来是用作安装大批量依赖的工具,所以,一股脑安装这些依赖库吧。

yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl openssl-devel gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel

当然,个别依赖库可能需要手动编译安装,因此需要注意这个问题。在下文中,对于某些依赖库、组件我会做一些说明。

除了上述依赖的,我们还需要查看 php 编译安装说明文档,以确认还是否需要其他组件。

在源码包中的 INSTALL 文件里,可以看到相关说明:

Prerequisite knowledge and software for compiling:
Basic Unix skills (being able to operate "make" and a C compiler)
An ANSI C compiler
A web server
Any module specific components (such as GD, PDF libs, etc.)

When building directly from Git sources or after custom modifications

you might also need:
autoconf: 2.13+ (for PHP < 5.4.0), 2.59+ (for PHP >= 5.4.0)
automake: 1.4+
libtool: 1.4.x+ (except 1.4.2)
re2c: Version 0.13.4 or newer
flex: Version 2.5.4 (for PHP <= 5.2)
bison: Version 1.28 (preferred), 1.35, or 1.75

之前我们已经安装了 gcc 、 automake 、 autoconf 等,还需要 re2c 和 bison 这两个。这两个都是 PHP 的语法分析器。(突然发现写这篇文章的 9 小时前 RE2C 发布了新的版本)。两者的下载地址:

RE2C http://sourceforge.net/projects/re2c/files/0.15.3/re2c-0.15.3.tar.gz

BSION http://ftp.gnu.org/gnu/bison/bison-3.0.4.tar.gz

然后分别编译安装,这两个编译安装不需要什么特别的配置参数,很直接。

当然,还有 libmcrypt 这个东西,下载地址: ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz 。这玩意儿必须的。

PHP 的编译安装

首先是从 github 下载源码包: https://github.com/php/php-src/archive/php-7.0.0.tar.gz

下载后解压,进入 PHP 源码目录:

tar zxvf php-7.0.0.tar.gz
cd php-src-php-7.0.0
调用 buildconf 生成 configure 文件:

./buildconf --force
生成了 configure 文件后,我们就需要着手开始进行编译安装的配置,如果你想知道有哪些编译配置选项,仅需 ./configure --help 即可查看。

参考配置如下:

./configure \
--prefix=/usr/local/php/7.0.0 \
--with-config-file-path=/usr/local/php/7.0.0/etc \
--with-config-file-scan-dir=/usr/local/php/7.0.0/etc/conf.d \
--enable-fpm \
--with-fpm-user=web \
--with-fpm-group=www \
--enable-soap \
--with-openssl \
--with-openssl-dir \
--with-mcrypt \
--with-pcre-regex \
--with-zlib \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-mbstring \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-pdo-mysql \
--with-zlib-dir \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd \
--with-mysqli \
--without-pear
配置执行完毕后,执行编译安装:

make && make install
大功告成!现在只需要将 /usr/local/php/7.0.0/bin 中的可执行文件软连接到 /bin 下即可,当然也可以自行选择 PATH 环境变量指定的路径中,这样就可以全局访问 PHP 解析执行器了。

完成后,执行 php -v 输出:

PHP 7.0.0 (cli) (built: Dec  3 2015 11:29:49) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies

标签:[!--infotagslink--]

您可能感兴趣的文章: