Home » Code » Ubuntu上安装nginx 1.10.0 + PHP 7.0.6

Ubuntu上安装nginx 1.10.0 + PHP 7.0.6

之前使用Cent OS为主,试试Ubuntu,版本是14.04.2  LTS。

一开始apt-get无法使用,apt-get update一下即可。先是使用apt-get install nginx,很快完成了,service nginx start,启动一切正常。执行apt-cache show nginx,信息发现是1.4的版本。作为版本控,这么老的实在不能忍受。于是service nginx stop,卸载,使用命令sudo apt-get remove –purge nginx,看着还给删了一些依赖库,可能会出问题,估计单remove一下即可。

到nginx官网查看,最新稳定版是1.10.0了,当然得它了。下载下来解压进入目录直接configure,提示缺少pcre库,估计是缺少一些依赖。参考网上的,安装以下东西可以满足接下来nginx和php的所有依赖。当然,configure参数不同,所需要的依赖也不同,百度谷歌一下都有答案的。

apt-get install libgd2-xpm-dev libgeoip-dev libpcre3 libpcre3-dev libssl-dev openssl libxslt-dev libbz2-dev libcurl4-openssl-dev libc-client2007e-dev libmcrypt-dev

如果依然提示mcrypt找不到,估计是apt-get源中没有,执行以下命令即可:

aptitude install libmcrypt-dev

然后再configure,显示如下:

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + md5: using system crypto library
  + sha1: using system crypto library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

其中的OpenSSL Library is not used,我不太懂,上边也安装了,为何没有用呢,这样是不是无法支持SSL?不管了,就这样安装吧,其实默认配置也挺好的感觉。sudo make && make install,成功。/usr/local/nginx/sbin/nginx即启动,打开网址即可看到欢迎页。nginx -s quit是平滑退出。这个nginx还有基本的启动关闭命令,php-fpm就没有了,得自己整shell script,比较麻烦。

接着安装PHP 7。官网最新是7.0.6,下载解压后,configure命令是参考网上并适当修改,实测了N次可用。

./configure --prefix=/usr/local/php-7.0.6 \
--with-config-file-path=/usr/local/php-7.0.6/etc \
--with-config-file-scan-dir=/usr/local/php-7.0.6/etc/php.d \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-soap \
--enable-calendar \
--with-curl \
--with-mcrypt \
--with-zlib \
--with-gd \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--enable-exif \
--enable-bcmath \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-pdo-mysql \
--with-mysqli \
--with-mysql-sock=/run/mysqld/mysqld.sock \
--enable-mysqlnd \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--enable-gd-native-ttf \
--with-openssl \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--with-libdir=/lib/x86_64-linux-gnu \
--enable-ftp \
--with-kerberos \
--with-gettext \
--with-xmlrpc \
--with-xsl \
--enable-opcache \
--enable-fpm

其中,prefix和confit-file-path,如果二者都不存在,后者必须是前者的子文件夹,切忌不能分别属于不同的文件夹,否则启动php-fpm后不能自动加载后者指定文件夹下的php.ini,除非使用-c参数手动指定。就是犯了这么一个小问题,将prefix写为php-7.0.6,config-file-path写为php7.0.6,不断的恢复磁盘鼓捣了一天!少一个中划线,坑爹至极!这里还指定了mysql-sock,因为之前apt-get install mysql-server mysql-client了,有5.5版本挺新的,不编译安装了,文件太大。然后configure成功,make,make install都没问题了。

Installing shared extensions:     /usr/local/php-7.0.6/lib/php/extensions/no-debug-non-zts-20151012/
Installing PHP CLI binary:        /usr/local/php-7.0.6/bin/
Installing PHP CLI man page:      /usr/local/php-7.0.6/php/man/man1/
Installing PHP FPM binary:        /usr/local/php-7.0.6/sbin/
Installing PHP FPM config:        /usr/local/php-7.0.6/etc/
Installing PHP FPM man page:      /usr/local/php-7.0.6/php/man/man8/
Installing PHP FPM status page:   /usr/local/php-7.0.6/php/php/fpm/
Installing phpdbg binary:         /usr/local/php-7.0.6/bin/
Installing phpdbg man page:       /usr/local/php-7.0.6/php/man/man1/
Installing PHP CGI binary:        /usr/local/php-7.0.6/bin/
Installing PHP CGI man page:      /usr/local/php-7.0.6/php/man/man1/
Installing build environment:     /usr/local/php-7.0.6/lib/php/build/
Installing header files:           /usr/local/php-7.0.6/include/php/
Installing helper programs:       /usr/local/php-7.0.6/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php-7.0.6/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php-7.0.6/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.3.0
[PEAR] PEAR           - installed: 1.10.1
Wrote PEAR system config file at: /usr/local/php-7.0.6/etc/pear.conf
You may want to add: /usr/local/php-7.0.6/lib/php to your php.ini include_path
/usr/local/src/php-7.0.6/build/shtool install -c ext/phar/phar.phar /usr/local/php-7.0.6/bin
ln -s -f phar.phar /usr/local/php-7.0.6/bin/phar
Installing PDO headers:           /usr/local/php-7.0.6/include/php/ext/pdo/

这时却发现nginx给死掉了,难道是版本太新了不稳定?执行sbin/nginx -s reload报错找不到pid文件,或者是数值错误之类的,百度找到解决方案指定配置文件:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

然后执行sbin/nginx可以正常启动。

接下来就是启动php-fpm了,直接运行安装目录下sbin/php-fpm能启动,关闭与重启之类的就没有相应命令,得自己写shell脚本。首先,复制php.ini和php-fpm.conf相应位置。

cp /usr/local/php-7.0.6/etc/php-fpm.conf.default /usr/local/php-7.0.6/etc/php-fpm.conf
cp /usr/local/php-7.0.6/etc/php-fpm.d/www.conf.default /usr/local/php-7.0.6/etc/php-fpm.d/www.conf
cp /usr/local/src/php-7.0.6/php.ini-development /usr/local/php-7.0.6/etc/php.ini

也就是把安装目录etc下边的php-fpm.conf.default复制一份,etc/php-fpm.d下的www.conf.default也复制一份,并把源码目录下的php.ini-development复制到相同目录(configure下指定的目录,如果已经有php.ini删除之,因为不知道它是生产环境的配置还是开发环境的,我们需要开发环境的)。然后编辑php-fpm.conf,把第17行#pid = run/php-fpm.pid前边的#去掉,指定pid路径,因为写启动shell脚本需要这个路径。在/etc/init.d/目录下创建名为php-7.0.6-fpm内容如下的一个文件:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-7.0.6-fpm
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-7.0.6-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
php_fpm_BIN=/usr/local/php-7.0.6/sbin/php-fpm
php_fpm_CONF=/usr/local/php-7.0.6/etc/php-fpm.conf
php_fpm_PID=/usr/local/php-7.0.6/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

脚本都通用的,就是需要把### END INIT INFO下边那三个路径改一下即可(说明文件这些不重要)。然后执行

chmod 755 /etc/init.d/php-7.0.6-fpm
update-rc.d php-7.0.6-fpm defaults

其中前一句是添加执行权限,后一句是加入开机启动,不需要不用执行。这样/etc/init.d/php-7.0.6-fpm start即可启动php-fpm。执行/usr/local/php-7.0.6/bin/php -v信息如下:

PHP 7.0.6 (cli) (built: May 16 2016 19:30:47) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

最后,添加nginx解析PHP功能。先是修改默认的 location 块,使其支持 .php 文件:

location / {
    root   html;
    index  index.html index.htm index.php;
}

下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并修改为下面的内容:

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

重启nginx,/usr/local/nginx/sbin/nginx -s reload,在/usr/local/nginx/html目录下写一个info.php来输出phpinfo()即可。

参考链接:
https://segmentfault.com/a/1190000004123048
http://php.net/manual/zh/install.unix.nginx.php
http://jcutrer.com/howto/linux/how-to-compile-php7-on-ubuntu-14-04
https://www.howtoforge.com/how-to-use-multiple-php-versions-php-fpm-and-fastcgi-with-ispconfig-3-ubuntu-12.04-lts-p3

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.