为 WordPress 编译安装 PHP 7.0.3 亲测全攻略
PHP7 有什么好处?
首先,比 PHP5 占用更低内存;其次,性能快一倍以上;最后,WordPress 的 QPS 可以提升到原来的 3 倍左右。
WordPress 4.4 的 QPS 比较:数字越大,QPS 越高
本例的主机 VPS 配置
- 带宽:1Mbps
- CPU:1核
- 操作系统:Ubuntu 12.04 64位
- 内存:1GB
- 云盾:是
- 环境:Linux + Nginx + Memcached + PHP-FPM
为什么要编译安装
并不是为了酷,本站使用的 Ubuntu 12.04 版本太低,apt-get install 安装不到最新版的 GCC 和 PHP7 。
检查 GCC 版本
@Laruence 推荐用 GCC 4.8 以上编译 PHP7 ,检查一下 GCC 版本:
1 |
|
若低于 4.8 ,推荐编译升级 GCC,请参考 linux下升级gcc的方法 – 亲测可用 – C++爱好者博客 。
截止 2016年2月15日, GCC 的最新版是 5.3.0 ,请自行到 GCC 官网 查看和 选择下载地址 。
本例从 GCC 4.6.3 升级到了 GCC 5.3.0,步骤如下:
1 |
|
运行 download_prerequisites 脚本,这个脚本会自动帮你下载所需要的依赖文件和库:
1 |
|
建立输出目录,将所有的中间文件都放到该目录,
1 |
|
运行
1 |
|
特别注意
编译时间依据机器性能而定,并消耗较多的内存。如果你的服务器内存也很紧张,请开启 500M 的 SWAP 交换区,否则 Make 阶段内存不足会报错 。本例服务器 IO 性能非常差,所以结束后再关掉 SWAP 交换区即可。
1 |
|
本例编译 GCC 5.3.0 用了 2-3 个小时。PHP7 编译安装也结束后,本例关掉了 SWAP 交换区:
1 |
|
开始编译安装 PHP7
截止 2016年2月15日 ,PHP7 的最新 stable 版本是 PHP 7.0.3 ,最新版本信息: http://php.net/downloads.php 。下载最新版:
1 |
|
可以先看看配置帮助:
1 |
|
本例使用的配置,其中 /usr/local/php 是安装目标位置
1 |
|
如果提示某些模块找不到,可能需要单独安装它们,例如
1 |
|
您可能需要安装的模块有 libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel mysql pcre-devel curl-devel libxslt-devel 等等……
接下来运行:
1 |
|
就开始编译安装了。
PHP7 编译安装完成的画面
PHP7 的配置和优化
复制配置文件
完成后还需要做一些配置和优化。首先,复制配置文件:
1 |
|
测试启动 PHP7-FPM
线上服务器一般已有 PHP5-FPM 服务在运行,请修改 www.conf 中的 user 和 group 为线上使用的用户名和用户组,并改掉默认 9000 端口号避免冲突。
启动 PHP7-FPM:
1 |
|
启动成功后可以查看 phpinfo() 等等。
本例 PHP7-FPM 的测试端口是 9008,假设线上 nginx 配置是 bokeyy,那么:
1 |
|
把
1 |
|
的 9000 改成 9008,并开过一个端口。然后
1 |
|
就可以测试一下新端口的网站各方面是否正常。
性能对比测试
接下来直接和 PHP5.3.10 对比测试一下效率如何( 来源 )。
没开Opache的PHP7.0.3和PHP5.3.10的性能比较,PHP7大约快一倍但不是很明显
开了Opache的PHP7.0.3和PHP5.3.10的性能比较,性能提升非常显著
test.php 的内容
1 |
|
配置 opcache
所以 Opcache ( http://php.net/opcache )是一定要启用的。而且早已有图证明,它能让 WordPress 提升不少 QPS。
WordPress 4.4 有无 Opache 的 QPS 比较
本例结合 Opache 官方推荐的配置 和 @Laruence 推荐的 Opache 配置 ,在 php.ini 加入:
1 |
|
其中 /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/opcache.so 是 opcache.so 所在的地址。
重启 PHP7-FPM 即可:
1 |
|
所有安装和配置到此就结束了。
亚一程 @Laruence 有一篇 让PHP7达到最高性能的几个Tips | 04 Dec 15 ,提了不少让 PHP7 更快的有用的建议。
本例只采纳了一部分。对于天天被 php5-fpm 进程打满可怜的 1G 小内存的服务器来说,HugePage 和 Opcache file cache 都需要一定冗余内存,不太合适。
让你的PHP7更快(GCC PGO) 建议先训练一下 GCC 再编译针对 WordPress 的定制化 PHP7 版本似乎也值得采纳。但是,由于服务器资源严重不足,基本上所有 WordPress 页面都不是即时生成的,而是走了时效为半天的 WP Super Cache 缓存。所以,生成 WordPress 首页的速度提升 7-10% 对本例的服务器来说意义很有限,所以也没有做了。
因此最后只采纳了“开启Opache”、“使用 GCC 4.8 以上的编译器”这两项。
清理环境
如果测试 PHP7-FPM 没有任何问题,就可以删掉测试网站
1 |
|
编辑 www.conf 把 PHP7-FPM 的端口改成 9000
1 |
|
最后关掉 PHP5-FPM,用 PHP7-FPM 来替代:
1 |
|
增加软链接
为了无论在哪个目录下输入 php 都能调用刚刚安装的 /usr/local/php/bin/php ,而不用输入这么长,建立几个软链接
1 |
|
现在只要
1 |
|
就能查看刚刚安装的 php 版本了。以后安装 PHP 插件也可以直接 phpize 和 php-config 了。
本例还有 WordPress 网站需要 Memcache,所以还没有结束。
安装并配置 PHP7-MEMCACHED
你一定好奇 PHP 的 memcached 和 memcache 插件有什么区别。事实上,它们功能差不多,都依赖默认在 11211 端口的 memcached 服务,只是封装方法不同,而 php-memcached 更新更勤快一些,功能也更多。
最重要的是, 在安装 PHP 7.0.3 你会发现,你只能装 php-memcached ! 注意有这个 d (截至 2016年2月15日)。让我们开始吧:
请到 github 下载 php-memcached 插件的 php7 分支( https://github.com/php-memcached-dev/php-memcached/tree/php7 ):
1 |
|
解压并安装:
1 |
|
这一步可能会提示你没有 libmemcached 库:
1 |
|
可能还提示不支持 memcache sasl ,推荐禁用,好吧!
1 |
|
终于可以运行
1 |
|
最后将 memcached.so 的地址加入 php.ini 就装好了:
1 |
|
加入
1 |
|
但是还有一个坑前面没有提及: WordPress 官方 Memcached 插件 只支持 php-memcache ,注意没有 d。所以我们用这个支持 php-memcached 的插件 https://github.com/tollmanz/wordpress-pecl-memcached-object-cache 。按照链接所示一步步配置来就行了。
感兴趣的还可以看看插件作者关于 php-memcache 和 php-memcached 两个 PHP 插件区别的文章: WordPress Object Cache Driven By The PECL Memcache(d) Extension 。
为什么只能用 php-memcached (有d)
截止 2015年2月15日,PHP7.0.3 下直接试图安装 php-memcache 会报错:
1 |
|
查了一下原因 ,是因为 PHP7 里面,这个文件改成了 php_smart_string.h 。博主尝试过手动将文件名改成正确的,然而也没有用,还会报错:
1 |
|
所以这是插件暂不兼容 PHP7 。只能等待,直到 php-memcache 插件更新。所以博主最后放弃,按照上文,使用了 php-memcached 插件作为替代。
参考的博客文章
- The Definitive PHP 7.0 & HHVM Benchmark
- linux下升级gcc的方法 – 亲测可用 – C++爱好者博客
- 让PHP7达到最高性能的几个Tips | 04 Dec 15
- Linux环境PHP7.0安装

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.
