CentOS 7.0安装Nginx 1.10.0 + PHP 7.0.6

本文在CentOS 7环境下进行Nginx 1.10.0和PHP 7.0.6编译安装。

1. Nginx 1.0.0 编译安装

1.1 源码下载

官网下载地址:nginx-1.10.0

1.2 依赖包安装

1
2
$ yum -y install gcc gcc-c++ autoconf automake libtool make cmake
$ yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

1.3 新建nginx用户及用户组

1
2
$ groupadd nginx
$ useradd -g nginx -M nginx

useradd命令的-M参数使得不为nginx建立home目录
修改/etc/passwd,使得nginx用户无法bash登陆(nginx用户后面由/bin/bash改为/sbin/nologin)

1
2
$ sudo vi /etc/passwd
nginx:x:1002:1003::/home/nginx:/sbin/nologin

1.4 编译配置、编译、安装

1
2
3
4
5
6
7
8
9
10
11
12
$ ./configure --prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/run/nginx.pid \
--with-http_ssl_module \
--user=nginx \
--group=nginx \
--with-pcre \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module

$ make
$ sudo make install

查看安装后的程序版本:

1
2
$ /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.10.0

1.5 修改Nginx默认端口:

1
2
3
4
5
$ sudo vim /usr/local/nginx/conf/nginx.conf
35 server {
36 listen 8080;
修改配置后验证配置是否合法:
$ /usr/local/nginx/sbin/nginx -t
  • 启动Nginx程序、查看进程
    1
    2
    3
    4
    $ sudo /usr/local/nginx/sbin/nginx
    $ ps -ef | grep nginx
    root 49357 1 0 19:13 ? 00:00:00 nginx: master process ./nginx
    nginx 49358 49357 0 19:13 ? 00:00:00 nginx: worker process

1.6 nginx停止、重启

未添加nginx服务前对nginx的管理只能通过一下方式管理:

1
2
3
4
5
6
7
8
9
$ sudo /usr/local/nginx/sbin/nginx -s reload/stop/start
# 从容停止Nginx:
kill -QUIT 主进程号
# 快速停止Nginx:
kill -TERM 主进程号
# 强制停止Nginx:
pkill -9 nginx
# 平滑重启nginx
/usr/nginx/sbin/nginx -s reload

2. PHP 7.0.6编译安装

2.1 源码下载

官网地址:php Downloads
解压压缩包:

1
2
$ tar -xzvf php-7.0.6.tar.gz 
$ cd php-7.0.6

2.2 安装编译所需依赖包

1
$ yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

2.3 源码编译、安装

  • 2.3.1 通过./configure –help 查看支持的编译配置参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    $ ./configure --help
    `configure' configures this package to adapt to many kinds of systems.

    Usage: ./configure [OPTION]... [VAR=VALUE]...

    To assign environment variables (e.g., CC, CFLAGS...), specify them as
    VAR=VALUE. See below for descriptions of some of the useful variables.

    Defaults for the options are specified in brackets.

    Configuration:
    -h, --help display this help and exit
    --help=short display options specific to this package
    --help=recursive display the short help of all the included packages
    -V, --version display version information and exit
    -q, --quiet, --silent do not print `checking ...' messages
    --cache-file=FILE cache test results in FILE [disabled]
    -C, --config-cache alias for `--cache-file=config.cache'
    -n, --no-create do not create output files
    --srcdir=DIR find the sources in DIR [configure dir or `..']

    Installation directories:
    --prefix=PREFIX install architecture-independent files in PREFIX
    [/usr/local]
    --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
    [PREFIX]

    By default, `make install' will install all the files in
    `/usr/local/bin', `/usr/local/lib' etc. You can specify
    an installation prefix other than `/usr/local' using `--prefix',
    for instance `--prefix=$HOME'.

    For better control, use the options below.
  • 2.3.2 PHP+Nginx组合的编译配置命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    $ ./configure --prefix=/usr/local/php7 \
    --with-config-file-path=/usr/local/php7/etc \
    --with-config-file-scan-dir=/usr/local/php7/etc/php.d \
    --with-mcrypt=/usr/include \
    --enable-mysqlnd \
    --with-mysqli \
    --with-pdo-mysql \
    --enable-fpm \
    --with-fpm-user=nginx \
    --with-fpm-group=nginx \
    --with-gd \
    --with-iconv \
    --with-zlib \
    --enable-xml \
    --enable-shmop \
    --enable-sysvsem \
    --enable-inline-optimization \
    --enable-mbregex \
    --enable-mbstring \
    --enable-ftp \
    --enable-gd-native-ttf \
    --with-openssl \
    --enable-pcntl \
    --enable-sockets \
    --with-xmlrpc \
    --enable-zip \
    --enable-soap \
    --without-pear \
    --with-gettext \
    --enable-session \
    --with-curl \
    --with-jpeg-dir \
    --with-freetype-dir \
    --enable-opcache

    # 执行完成后的结果:
    Generating files
    configure: creating ./config.status
    creating main/internal_functions.c
    creating main/internal_functions_cli.c
    +--------------------------------------------------------------------+
    | License: |
    | This software is subject to the PHP License, available in this |
    | distribution in the file LICENSE. By continuing this installation |
    | process, you are bound by the terms of this license agreement. |
    | If you do not agree with the terms of this license, you must abort |
    | the installation process at this point. |
    +--------------------------------------------------------------------+

    Thank you for using PHP.

    config.status: creating php7.spec
    config.status: creating main/build-defs.h
    config.status: creating scripts/phpize
    config.status: creating scripts/man1/phpize.1
    config.status: creating scripts/php-config
    config.status: creating scripts/man1/php-config.1
    config.status: creating sapi/cli/php.1
    config.status: creating sapi/fpm/php-fpm.conf
    config.status: creating sapi/fpm/www.conf
    config.status: creating sapi/fpm/init.d.php-fpm
    config.status: creating sapi/fpm/php-fpm.service
    config.status: creating sapi/fpm/php-fpm.8
    config.status: creating sapi/fpm/status.html
    config.status: creating sapi/cgi/php-cgi.1
    config.status: creating ext/phar/phar.1
    config.status: creating ext/phar/phar.phar.1
    config.status: creating main/php_config.h
    config.status: executing default commands
  • 2.3.3 编译 + 安装

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    编译源码:
    $ make
    Generating phar.php
    Generating phar.phar
    PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
    directorytreeiterator.inc
    pharcommand.inc
    directorygraphiterator.inc
    invertedregexiterator.inc
    clicommand.inc
    phar.inc

    Build complete.
    Don't forget to run 'make test'.

    对编译结果进行测试:
    $ make test
    很遗憾,我这里make test报错了,已反馈php test信息。

    安装程序至指定目录:
    $ sudo make install
    $ /usr/local/php7/bin/php -v
    PHP 7.0.6 (cli) (built: May 4 2016 18:37:07) ( NTS )
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

2.4 修改配置

  • 修改php配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    查看php加载配置文件路径:
    $ /usr/local/php7/bin/php -i | grep php.ini
    Configuration File (php.ini) Path => /usr/local/php7/etc

    php-7.0.6源码目录下:
    $ ls | grep ini
    php.ini-development
    php.ini-production
    $ sudo cp php.ini-production /usr/local/php7/etc/php.ini
    根据需要对php.ini配置进行配置修改,请参考[此文](http://www.cnblogs.com/hbl/archive/2008/02/15/1069367.htm]l)
    $ ../bin/php -i | grep php.ini
    Configuration File (php.ini) Path => /usr/local/php7/etc
    Loaded Configuration File => /usr/local/php7/etc/php.ini
  • 启用php-fpm服务

  • nginx代理php实现访问

参考文献

  1. PHP 手册-官网中文2016-5-3版
  2. 编译安装nginx1.9.7+php7.0.0服务器环境
  3. CentOS 7 安裝 Apache + PHP 7 + MySQL + phpMyAdmin + FTP