Table of Contents
PHP7 有什么好处?
本例的主机 VPS 配置
为什么要编译安装
检查 GCC 版本
特别注意
开始编译安装 PHP7
PHP7 的配置和优化
复制配置文件
测试启动 PHP7-FPM
性能对比测试
配置 opcache
所有安装和配置到此就结束了。
因此最后只采纳了“开启Opache”、“使用 GCC 4.8 以上的编译器”这两项。
清理环境
增加软链接
安装并配置 PHP7-MEMCACHED
为什么只能用 php-memcached (有d)
参考的博客文章
Home Backend Development PHP Tutorial 为 WordPress 编译安装 PHP 7.0.3 亲测全攻略

为 WordPress 编译安装 PHP 7.0.3 亲测全攻略

Jun 23, 2016 pm 01:16 PM

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

$ gcc -v

Copy after login

若低于 4.8 ,推荐编译升级 GCC,请参考 linux下升级gcc的方法 – 亲测可用 – C++爱好者博客 。

截止 2016年2月15日, GCC 的最新版是 5.3.0 ,请自行到 GCC 官网 查看和 选择下载地址 。

本例从 GCC 4.6.3 升级到了 GCC 5.3.0,步骤如下:

1

$ wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-5.3.0/gcc-5.3.0.tar.gz$ tar -xf gcc-5.3.0.tar.gz$ cd gcc-5.3.0

Copy after login

运行 download_prerequisites 脚本,这个脚本会自动帮你下载所需要的依赖文件和库:

1

$ ./contrib/download_prerequisites

Copy after login

建立输出目录,将所有的中间文件都放到该目录,

1

$ mkdir gcc_temp$ cd gcc_temp

Copy after login

运行

1

$ ../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib$ make && make install

Copy after login

特别注意

编译时间依据机器性能而定,并消耗较多的内存。如果你的服务器内存也很紧张,请开启 500M 的 SWAP 交换区,否则 Make 阶段内存不足会报错 。本例服务器 IO 性能非常差,所以结束后再关掉 SWAP 交换区即可。

1

$ SWAP=/tmp/swap$ dd if=/dev/zero of=$SWAP bs=1M count=500$ mkswap $SWAP$ sudo swapon $SWAP

Copy after login

本例编译 GCC 5.3.0 用了 2-3 个小时。PHP7 编译安装也结束后,本例关掉了 SWAP 交换区:

1

$ sudo swapoff -a

Copy after login

开始编译安装 PHP7

截止 2016年2月15日 ,PHP7 的最新 stable 版本是 PHP 7.0.3 ,最新版本信息: http://php.net/downloads.php 。下载最新版:

1

$ wget http://cn2.php.net/get/php-7.0.3.tar.bz2/from/this/mirror$ tar jxvf mirror$ cd php-7.0.3/

Copy after login

可以先看看配置帮助:

1

./configure --help

Copy after login

本例使用的配置,其中 /usr/local/php 是安装目标位置

1

./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip

Copy after login

如果提示某些模块找不到,可能需要单独安装它们,例如

1

$ sudo apt-get install libcurl4-gnutls-dev

Copy after login

您可能需要安装的模块有 libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel mysql pcre-devel curl-devel libxslt-devel 等等……

接下来运行:

1

$ make && make install

Copy after login
Copy after login

就开始编译安装了。

PHP7 编译安装完成的画面

PHP7 的配置和优化

复制配置文件

完成后还需要做一些配置和优化。首先,复制配置文件:

1

$ cp php.ini-development /usr/local/php/lib/php.ini$ cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf$ cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf$ cp -R ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

Copy after login

测试启动 PHP7-FPM

线上服务器一般已有 PHP5-FPM 服务在运行,请修改 www.conf 中的 user 和 group 为线上使用的用户名和用户组,并改掉默认 9000 端口号避免冲突。

启动 PHP7-FPM:

1

$ service php-fpm start

Copy after login

启动成功后可以查看 phpinfo() 等等。

本例 PHP7-FPM 的测试端口是 9008,假设线上 nginx 配置是 bokeyy,那么:

1

$ cp /etc/nginx/site-available/bokeyy /etc/nginx/site-enabled/bokeyy_php7$ vim /etc/nginx/site-enabled/bokeyy_php7

Copy after login

1

fastcgi_pass 127.0.0.1:9000;

Copy after login

的 9000 改成 9008,并开过一个端口。然后

1

$ service nginx configtest$ service nginx reload

Copy after login

就可以测试一下新端口的网站各方面是否正常。

性能对比测试

接下来直接和 PHP5.3.10 对比测试一下效率如何( 来源 )。

没开Opache的PHP7.0.3和PHP5.3.10的性能比较,PHP7大约快一倍但不是很明显

开了Opache的PHP7.0.3和PHP5.3.10的性能比较,性能提升非常显著

test.php 的内容

1

<?php$a = array();    for($i=0;$i<600000;$i++){        $a[$i] = $i;        }    foreach($a as $i)    {        array_key_exists($i, $a);    }?>

Copy after login

配置 opcache

所以 Opcache ( http://php.net/opcache )是一定要启用的。而且早已有图证明,它能让 WordPress 提升不少 QPS。

WordPress 4.4 有无 Opache 的 QPS 比较

本例结合 Opache 官方推荐的配置 和 @Laruence 推荐的 Opache 配置 ,在 php.ini 加入:

1

zend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/opcache.soopcache.memory_consumption=128opcache.interned_strings_buffer=8opcache.max_accelerated_files=4000opcache.revalidate_freq=60opcache.fast_shutdown=1opcache.enable=1opcache.enable_cli=1opcache.file_cache=/tmp

Copy after login

其中 /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/opcache.so 是 opcache.so 所在的地址。

重启 PHP7-FPM 即可:

1

$ service php-fpm restart

Copy after login

所有安装和配置到此就结束了。

亚一程 @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

$ rm /etc/nginx/site-enabled/bokeyy_php7

Copy after login

编辑 www.conf 把 PHP7-FPM 的端口改成 9000

1

$ vim /usr/local/php/etc/php-fpm.d/www.conf

Copy after login

最后关掉 PHP5-FPM,用 PHP7-FPM 来替代:

1

$ service php-fpm configtest$ service php5-fpm stop$ service php-fpm restart$ service nginx configtest$ service nginx reload

Copy after login

增加软链接

为了无论在哪个目录下输入 php 都能调用刚刚安装的 /usr/local/php/bin/php ,而不用输入这么长,建立几个软链接

1

$ cd /usr/bin$ ln -s /usr/local/php/bin/php php$ ln -s /usr/local/php/bin/phpize phpize$ ln -s /usr/local/php/bin/php-config php-config

Copy after login

现在只要

1

$ php -v

Copy after login

就能查看刚刚安装的 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

$ wget https://github.com/php-memcached-dev/php-memcached/archive/php7.zip

Copy after login

解压并安装:

1

$ unzip php7.zip$ cd php-memcached-php7/$ root=/usr/local/php$ $root/bin/phpize$ ./configure --with-php-config=$root/bin/php-config

Copy after login

这一步可能会提示你没有 libmemcached 库:

1

$ cd ..$ wget  https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz$ tar xvf libmemcached-1.0.18.tar.gz$ cd libmemcached-1.0.18$ ./configure$ cd ../php-memcached-php7/$ ./configure --with-php-config=$root/bin/php-config

Copy after login

可能还提示不支持 memcache sasl ,推荐禁用,好吧!

1

$ ./configure --with-php-config=$root/bin/php-config --disable-memcached-sasl

Copy after login

终于可以运行

1

$ make && make install

Copy after login
Copy after login

最后将 memcached.so 的地址加入 php.ini 就装好了:

1

$ vim /usr/local/php/lib/php.ini

Copy after login

加入

1

extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/memcached.so

Copy after login

但是还有一个坑前面没有提及: 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

fatal error: ext/standard/php_smart_str.h: No such file or directory

Copy after login

查了一下原因 ,是因为 PHP7 里面,这个文件改成了 php_smart_string.h 。博主尝试过手动将文件名改成正确的,然而也没有用,还会报错:

1

error: 'RETURN STRING' undeclared(first use in this function)

Copy after login

所以这是插件暂不兼容 PHP7 。只能等待,直到 php-memcache 插件更新。所以博主最后放弃,按照上文,使用了 php-memcached 插件作为替代。

参考的博客文章

  • The Definitive PHP 7.0 & HHVM Benchmark
  • linux下升级gcc的方法 – 亲测可用 – C++爱好者博客
  • 让PHP7达到最高性能的几个Tips | 04 Dec 15
  • Linux环境PHP7.0安装
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

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.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

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 and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

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.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO) How do you prevent SQL Injection in PHP? (Prepared statements, PDO) Apr 15, 2025 am 12:15 AM

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: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

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's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

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.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

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.

See all articles