编译PHP7

PHP7

PHP7都出了,自然我是要安装的,上次写的那个编译开发环境并没有针对性的说PHP,就这次说清楚吧。

Download

$ wget http://cn2.php.net/get/php-7.0.0.tar.gz/from/this/mirror
$ mv mirror php7.tar.gz
$ tar -zxvf php7.tar.gz
$ cd php7

libxml

$ sudo apt-get install libxml2-dev

OpenSSL

$ sudo apt-get install libcurl4-openssl-dev

mcrypt

$ sudo apt-get install curl libmcrypt-dev

GD

configure: error: png.h not found.

$ sudo apt-get install libpng12-dev

configure

$ ./configure --enable-debug --with-mysql --with-gd --with-curl --enable-zip --prefix=/opt/php7 --enable-fpm --enable-cgi --with-openssl --enable-mbstring --with-mcrypt --with-pdo-mysql --with-mysql-sock=/var/run/mysqld/mysqld.sock
$ make -j 4
$ sudo make install

PATH

$ cd ~
$ vim .zshrc # 或者其他的.bashrc什么的

加入以下内容在最后一行:

$PATH=$PATH:/opt/php7/bin;

php-fpm

这个步骤比较长,得有点儿耐心

首先找个php.ini文件,然后放到/opt/php7/lib里面,然后

给两个文件改名

$ suo cp /opt/php7/etc/php-fpm.conf.default /opt/php7/etc/php-fpm.conf
$ suo cp /opt/php7/etc/php-fpm.d/www.conf.default /opt/php7/etc/php-fpm.d/www.conf

然后调整里面的东西

先给个pid取消注释

$ sudo vim /opt/php7/etc/php7-fpm.conf
[...]
pid = run/php-fpm.pid
[...]

改一下用户组和用户

$ sudo vim /opt/php7/etc/php7-fpm.d/www.conf
user = YourUsername
group = YourGroup

然后创建文件:

$ sudo vim /etc/init.d/php7-fpm

写入以下内容,注意一些文件名

#! /bin/sh
### BEGIN INIT INFO
# Provides:          php7-fpm
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php7-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
php_fpm_BIN=/opt/php7/sbin/php-fpm
php_fpm_CONF=/opt/php7/etc/php-fpm.conf
php_fpm_PID=/opt/php7/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF"
wait_for_pid () {
        try=0
        while test $try -lt 35 ; do
                case "$1" in
                        'created')
                        if [ -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                        'removed')
                        if [ ! -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                esac
                echo -n .
                try=`expr $try + 1`
                sleep 1
        done
}
case "$1" in
        start)
                echo -n "Starting php-fpm "
                $php_fpm_BIN $php_opts
                if [ "$?" != 0 ] ; then
                        echo " failed"
                        exit 1
                fi
                wait_for_pid created $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        stop)
                echo -n "Gracefully shutting down php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -QUIT `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed. Use force-exit"
                        exit 1
                else
                        echo " done"
                       echo " done"
                fi
        ;;
        force-quit)
                echo -n "Terminating php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -TERM `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        restart)
                $0 stop
                $0 start
        ;;
        reload)
                echo -n "Reload service php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -USR2 `cat $php_fpm_PID`
                echo " done"
        ;;
        *)
                echo "Usage: $0 {start|stop|force-quit|restart|reload}"
                exit 1
        ;;
esac

赋权:

$ sudo chmod 755 /etc/init.d/php7-fpm
$ sudo vim /lib/systemd/system/php7-fpm.service

然后写入下面的内容:

[Unit]
Description=The PHP 7 FastCGI Process Manager
After=network.target

[Service]
Type=simple
PIDFile=/opt/php7/var/run/php-fpm.pid
ExecStart=/opt/php7/sbin/php-fpm --nodaemonize --fpm-config /opt/php7/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

Run

试试看行不行:

$ php -v
$ sudo /etc/init.d/php-fpm start

没有出错就对了,出错了请在下面留言,我看看怎么回事。

好了,到这里其实已经没有问题了。但是剩下的Nginx也得稍微调整一下

$ sudo vim /etc/nginx/site-available/default

改成这样

fastcgi_pass 127.0.0.1:9000

重启服务,走起!

$ sudo service nginx restart
$ sudo service php7-fpm restart

Swoole

按照他们的指导做就行了

$ git clone https://github.com/swoole/swoole-src.git
$ cd swoole-src
$ phpize
$ ./configure
$ make && sudo make install

在php.ini里面写上扩展

$ sudo vim /opt/php7/lib/php.ini

随便找个地方,不过我还是建议放到extension那边,大概第900行左右

extension=swoole.so

checkout it

试试看swoole有没有安装好

$ php -m | grep swoole

有输出就是没问题,没输出就是有问题。

References