一、安装环境说明

系统环境:

  • CentOS 6.5-x86_64

  • httpd-2.2.27.tar.gz

  • mysql-5.5.38-linux2.6-i686.tar.gz

  • php-5.5.14.tar.gz


编译安装的原则:对于我们来说,需要定制的才需要直接编译,其余的一切皆 yum / apt-get搞定,方便省心。


1、同步系统时间

# yum -y install ntpdate
# /usr/sbin/ntpdate time.nist.gov
# clock -w

2、安装开发工具包

## 添加 epel源
# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

## 方式一
[root@localhost ~]# yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype*

## 方式二:或者更简单粗暴的,我更喜欢这种方式
[root@localhost ~]# yum -y groupinstall "Development Tools"  
[root@localhost ~]# yum install -y openssl openssl-devel pcre pcre-devel zlib zlib-devel

3、检查并卸载系统自带的apache,mysql,php 软件包

# 检查系统是否已经安装
[root@localhost ~]# rpm -qa | grep -i http
[root@localhost ~]# rpm -qa | grep -i mysql
[root@localhost ~]# rpm -qa | grep -i php

# 如果安装,需要卸载。卸载前,要关闭启动的服务
[root@localhost ~]# service httpd stop
[root@localhost ~]# service mysqld stop

# 卸载
[root@localhost ~]# yum remove httpd
[root@localhost ~]# yum remove mysql
[root@localhost ~]# yum remove php

# yum 安装的则使用yum卸载,rpm安装的就用rpm卸载
# 建议使用 rpm -e http --nodeps (--nodeps 选项)

4、关闭SELinux 以及 iptables

# 临时关闭SELinux
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive

# 永久关闭SELinux
[root@localhost ~]# vi /etc/selinux/config
SELINUX=disabled

若安装时没有禁用SELinux,将enforcing改为disabled,修改后需重新启动Linux方可生效!

# 直接关闭iptables
[root@localhost ~]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]


二、编译 httpd-2.2.27

    注意,httpd-2.4.x需要较新版本的apr和apr-util,因此需要事先对其进行升级。建议使用源码编译安装,因为系统自带apr,但是版本较低,但不能卸载,存在依赖关系。所以我们把新版本的apr与老版本的apr不安装在同一个地方即可。

官方文档描述:

  • apr/apr-util >= 1.2

    apr and apr-util are bundled with the Apache HTTPd source releases, and will be used without any problems in almost all circumstances. However, if apr orapr-util, versions 1.0 or 1.1, are installed on your system, you must either upgrade your apr/apr-util installations to 1.2, force the use of the bundled libraries or have httpd use separate builds. To use the bundled apr/apr-util sources specify the --with-included-apr option to configure:

# Build and install apr 1.2
cd srclib/apr
./configure --prefix=/usr/local/apr-httpd/
make
make install

# Build and install apr-util 1.2
cd ../apr-util
./configure --prefix=/usr/local/apr-util-httpd/ --with-apr=/usr/local/apr-httpd/
make
make install

# Configure httpd
cd ../../
./configure --with-apr=/usr/local/apr-httpd/ --with-apr-util=/usr/local/apr-util-httpd/

1、编译httpd

[root@localhost ~]# tar xf httpd-2.2.27.tar.gz -C /usr/local/src
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# cd httpd-2.2.27/
[root@localhost httpd-2.2.27]# ./configure \
--prefix=/usr/local/apache \
--sysconfdir=/etc/httpd \
--enable-so \
--enable-ssl \
--enable-cgi \
--with-zlib \
--with-pcre \
--enable-rewrite \
--enable-mods-shared=most \
--enable-mpms-shared=all \
--with-mpm=event
--with-apr=/usr/local/apr-httpd \
--with-apr-util=/usr/local/apr-util-httpd

[root@localhost httpd-2.2.27]# make && make install

编译选项解释:
--sysconfdir=/etc/httpd      # 指定配置文件安装位置
--enable-so                  # 支持动态共享模块如果没有这个模块PHP将无法与apache结合工作
--enable-ssl                 # 启用支持ssl
--enable-cgi                 # 支持cgi
--enable-rewrite             # 支持URL重写
 --with-zlib                 # 压缩库,在互联网上传播时可节约带宽
--with-apr=/usr/local/apr-httpd     # 指定apr路径
--with-apr-util=/usr/local/apr-util-httpd     # 指定apr-util路径
--enable-mpms-shared=all     # 支持多道处理模块

## 编译选项非常多,我们只需要记住常用的就可以。后续如果需要加入其它模块,需要重新编译,最好和开发沟通,哪些参数对你是必需的。然后参考 ./configure --help 
模块选项
有两种使用模块的方法:一是静态连接进核心,二是作为DSO模块动态加载;如果编译中包含任何DSO模块,则mod_so会被自动包含进核心。如果希望核心能够装载DSO,但不实际编译任何DSO模块,则需明确指定”–-enable-so”。

一般情况下你可以使用如下语法启用或者禁用某个模块:
–-disable-MODULE
禁用MODULE模块(仅用于基本模块)
–-enable-MODULE=shared
将MODULE编译为DSO(可用于所有模块)
–-enable-MODULE
将MODULE静态连接进核心(仅用于扩展和实验模块)
–-enable-mods-shared=MODULE-LIST
将MODULE-LIST中的所有模块都编译成DSO(可用于所有模块)
–-enable-modules=MODULE-LIST
将MODULE-LIST静态连接进核心(可用于所有模块)

上述 MODULE-LIST 可以是:
(1)用引号界定并且用空格分隔的模块名列表
–-enable-mods-shared=’headers rewrite dav’
(2)”most”(大多数模块[遇见错误模块时忽略该模块而不中断配置过程])
(3)”all”(所有模块[遇见错误模块时中断配置过程并报错])

总结三点:
1) 如果编译参数中有--enable-mods-shared=all 或者--enable-mods-shared=most,那么所有--enable-[module]都将会以动态模式来编译,除非特别指定--enable-[module]=static 为静态编译 。
2)如果编译参数中有--enable-modules=all 指定所有静态方式的话,还可以用--enable-[module]=shared来动态加载模块。
3)特别注意如果编译参数中既有--enable-modules=all静态编译又有--enable-mods-shared=all动态编译,那么最终会使用动态编译。

2、启动httpd服务

    编译完成后,在/etc/init.d/目录下是没有httpd这个服务启动脚本的。那么有两种方式

  • 通过 bin/apachectl 启动

  • 提供一个httpd启动脚本

来看看第一种方式:

# /usr/local/apache/bin/apachectl {start|stop|restart}
[root@localhost ~]# /usr/local/apache/bin/apachectl start
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[root@localhost ~]# ps -ef | grep httpd
root     29954     1  0 09:29 ?        00:00:00 /usr/local/apache/bin/httpd -k start
daemon   29955 29954  0 09:29 ?        00:00:00 /usr/local/apache/bin/httpd -k start
daemon   29956 29954  0 09:29 ?        00:00:00 /usr/local/apache/bin/httpd -k start
daemon   29957 29954  0 09:29 ?        00:00:00 /usr/local/apache/bin/httpd -k start

# OK, 我们看到服务已经启动

为了方便管理, 我们进行一些额外的配置


3、更改pid的位置

    我们看到httpd是以daemon用户身份运行的,按照惯例我们使用apache用户。编译安装httpd.pid默认存放在/usr/local/apache/logs目录下,这不符合我们的习惯。

[root@localhost ~]# groupadd -r apache
[root@localhost ~]# useradd -r -g apache -s /sbin/nologin apache

# 编辑httpd.conf配置文件
[root@localhost ~]# vi /etc/httpd/httpd.conf 
Pidfile "/var/run/httpd.pid"
User apache
Group apache

# 重启httpd服务
[root@localhost ~]# /usr/local/apache/bin/apachectl restart

但是, 使用apachectl不方便, 而且提示非常不友好,所以我们还是提供httpd服务启动脚本:

在 httpd 源码包中,给我们提供了一个脚本示例。 httpd-2.x/build/rpm/httpd.init

[root@localhost httpd-2.2.27]# cp build/rpm/httpd.init /etc/init.d/httpd
[root@localhost ~]# vi /etc/init.d/httpd

#!/bin/bash  
#  
# httpd        Startup script for the Apache HTTP Server  
#  
# chkconfig: - 85 15  
# description: Apache is a World Wide Web server. It is used to serve \  
#            HTML files and CGI.  
# processname: httpd  
# config: /etc/httpd/conf/httpd.conf  
# config: /etc/sysconfig/httpd  
# pidfile: /var/run/httpd.pid  
   
# Source function library.  
. /etc/rc.d/init.d/functions  
   
if [ -f /etc/sysconfig/httpd ]; then  
        . /etc/sysconfig/httpd  
fi  
   
# Start httpd in the C locale by default.  
HTTPD_LANG=${HTTPD_LANG-"C"}  
   
# This will prevent initlog from swallowing up a pass-phrase prompt if  
# mod_ssl needs a pass-phrase from the user.  
INITLOG_ARGS="" 
   
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server  
# with the thread-based "worker" MPM; BE WARNED that some modules may not  
# work correctly with a thread-based MPM; notably PHP will refuse to start.  
   
# Path to the apachectl script, server binary, and short-form for messages.  
############################################
apachectl=/usr/local/apache/bin/apachectl  
httpd=${HTTPD-/usr/local/apache/bin/httpd}  
prog=httpd 
pidfile=${PIDFILE-/var/run/httpd.pid}  
lockfile=${LOCKFILE-/var/lock/subsys/httpd}  
RETVAL=0 
###########################################
   
start() {  
        echo -n $"Starting $prog: "  
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS  
        RETVAL=$?  
        echo  
        [ $RETVAL = 0 ] && touch ${lockfile}  
        return $RETVAL  
}  
   
stop() {  
       echo -n $"Stopping $prog: "  
       killproc -p ${pidfile} -d 10 $httpd  
       RETVAL=$?  
       echo  
       [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}  
}  
reload() {  
    echo -n $"Reloading $prog: "  
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then  
        RETVAL=$?  
        echo $"not reloading due to configuration syntax error"  
        failure $"not reloading $httpd due to configuration syntax error"  
    else  
        killproc -p ${pidfile} $httpd -HUP  
        RETVAL=$?  
    fi  
    echo  
}  
   
# See how we were called.  
case "$1" in  
 start)  
       start  
       ;;  
 stop)  
       stop  
       ;;  
 status)  
        status -p ${pidfile} $httpd  
       RETVAL=$?  
       ;;  
 restart)  
       stop  
       start  
       ;;  
 condrestart)  
       if [ -f ${pidfile} ] ; then  
              stop  
              start  
       fi  
       ;;  
 reload)  
        reload  
       ;;  
 graceful|help|configtest|fullstatus)  
       $apachectl $@  
       RETVAL=$?  
       ;;  
 *)  
       echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"  
       exit 1  
esac  
   
exit $RETVAL

测试一下我们的启动脚本:

[root@localhost ~]# chmod +x /etc/init.d/httpd
[root@localhost ~]# service httpd status
httpd (pid  30050) is running...
[root@localhost ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

把httpd添加到服务列表,使其能开机启动

[root@localhost ~]# chkconfig --add httpd
[root@localhost ~]# chkconfig httpd on

4、把httpd的bin目录添加到PATH

[root@localhost ~]# vi /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
[root@localhost ~]# . /etc/profile.d/httpd.sh

# OK
# 测试httpd.conf配置文件语法
[root@localhost ~]# httpd -t
[root@localhost ~]# httpd -l
[root@localhost ~]# httpd -M
[root@localhost ~]# httpd -S


三、安装 MySQL-5.5.38

    MySQL提供多种安装方式:源码编译安装、通用二进制包、rpm/yum 安装

我们这里使用通用二进制包:mysql-5.5.38-linux2.6-i686.tar.gz


1、添加mysql用户和组

[root@localhost ~]# groupadd -r mysql
[root@localhost ~]# useradd -r -g mysql -s /sbin/nologin mysql

2、数据目录规划

[root@localhost ~]# mkdir -pv /mydata/data
mkdir: created directory `/mydata'
mkdir: created directory `/mydata/data'
[root@localhost ~]# chown -R mysql:mysql /mydata/data

3、解压

[root@localhost ~]# tar xf mysql-5.5.38-linux2.6-i686.tar.gz -C /usr/local/src
[root@localhost ~]# cd /usr/local
[root@localhost local]# ln -sv src/mysql-5.5.38-linux2.6-i686 mysql
`mysql' -> `src/mysql-5.5.38-linux2.6-i686'

4、初始化数据库

[root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data --user=mysql
[root@localhost mysql]# chown -R root:root /usr/local/src/mysql-5.5.38-linux2.6-i686/

5、提供配置文件和启动脚本

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig mysqld on

[root@localhost mysql]# cp support-files/my-medium.cnf /etc/my.cnf

## 编辑配置文件my.cnf

# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket          = /tmp/mysql.sock
character-set-server = utf8

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
pid-file         = /mydata/data/mysqld.pid
character-set-server = utf8
collation-server = utf8_unicode_ci
basedir         = /usr/local/mysql
datadir         = /mydata/data
user            = mysql
skip-name-resolve

6、启动mysql

[root@localhost ~]# service mysqld start
Starting MySQL... SUCCESS! 
[root@localhost ~]# netstat -tulpn  | grep 3306
tcp        0      0 0.0.0.0:3306        0.0.0.0:*          LISTEN      30997/mysqld      

# 添加到PATH
[root@localhost ~]# vi /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost ~]# . /etc/profile.d/mysql.sh

### 进行安全性设置
[root@localhost ~]# /usr/local/mysql/bin/mysql_secure_installation

7、其他设置(导出man手册以及头文件)

[root@localhost ~]# vim /etc/man.config 
MANPATH /usr/local/mysql/man

[root@localhost ~]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
`/usr/include/mysql' -> `/usr/local/mysql/include/'

[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib

[root@localhost ~]# ldconfig

至此, MySQL安装已经完成。

8、当然,也可以编译安装mysql (可选)

# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS:STRING=all \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/var/mysql/data

# make && make install

# mkdir -p /var/mysql
# mkdir -p /var/mysql/data
# mkdir -p /var/mysql/log

# chown -R mysql:mysql /usr/local/mysql
# chown -R mysql:mysql /var/mysql

(多说两句:在启动MySQL服务时,会按照一定次序搜索my.cnf,先在/etc目录下找,找不到则会搜索"$basedir/my.cnf",在本例中就是 /usr/local/mysql/my.cnf,这是新版MySQL的配置文件的默认位置!注意:在CentOS 6.4版操作系统的最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。)


1、mysql 5.6.16时 运行 cmake报错Curses library not found,怎么解决?

答:是因为 curses库没有安装,执行下面的安装命令即可:yum -y install ncurses ncurses-devel


2、如果出现 Warning: Bison executable not found in PATH 则需要你安装 Bison ,可以源码编译安装,也可通过yum install bison命令快速安装。


四、安装 php (模块方式)

    前面我们已经安装了 httpd 以及 MySQL,那么接下来演示如何安装php。我们知道, httpd 与 php 工作的模式有三种: cgi, fastcgi, module。 通常, apache和php都是以模块的方式组合工作的,鉴于fastcgi有更优的效率,待会儿也会讲解。

1、安装 php 所需要的扩展包

php需要实现一些特别的功能,可能需要调用扩展(具体需要开发人员确定)

# yum install -y gd-devel libjpeg libjpeg-devel libpng libpng-devel bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel

如果提示没有相应的包,可以在http://dl.fedoraproject.org/pub/epel/6/x86_64/ 中下载,然后使用 rpm -Uvh  xxx.rpm 手动安装即可。

2、编译安装 php

如果你的 Web Server 使用的 Apache, 并且需要以模块化方式加载php,请添加类似:--with-apxs2=/usr/local/apache-xx/bin/apxs  参数。该选项 基于 apxs 实现让php编译成 apache 模块

# tar xf php-5.5.18.tar.gz -C /usr/local/src
# cd /usr/local/src/php-5.5.18/

# ./configure \
--prefix=/usr/local/php55 \
--with-config-file-path=/usr/local/php55/etc \
--with-apxs2=/usr/local/apache/bin/apxs \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-opcache \
--enable-fpm \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gettext \
--enable-mbstring \
--with-iconv \
--with-mcrypt \
--with-mhash \
--with-openssl \
--enable-bcmath \
--enable-soap \
--with-libxml-dir \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--with-curl \
--with-zlib \
--enable-zip \
--with-bz2 \
--with-readline

# make -j8 && make install

更多编译参数请使用 ./configure --help 查看。

""" 安装路径 """
--prefix=/usr/local/php56 \

""" php.ini 配置文件路径 """
--with-config-file-path=/usr/local/php56/etc \

""" 优化选项 """
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \

""" 启用 opcache,默认为 ZendOptimizer+(ZendOpcache) """
--enable-opcache \

""" FPM FastCGI"""
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \

""" MySQL """
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \

""" 国际化与字符编码支持 """
--with-gettext \
--enable-mbstring \
--with-iconv \

""" 加密扩展 """
--with-mcrypt \
--with-mhash \
--with-openssl \

""" 数学扩展 """
--enable-bcmath \

""" Web 服务,soap 依赖 libxml """
--enable-soap \
--with-libxml-dir \

""" 进程,信号及内存 """
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \

""" socket & curl """
--enable-sockets \
--with-curl \

""" 压缩与归档 """
--with-zlib \
--enable-zip \
--with-bz2 \

""" GNU Readline 命令行快捷键绑定 """
--with-readline

如果想重新安装:

# make clean
# make clean all

# ./configure ...
# make -j8
# make install

3、配置 php

配置文件:

# cp php.ini-production /usr/local/php55/etc/php.ini

4、整合 php 与 apache:  与apache 关联

    单纯的WEB服务器仅支持静态网页内容,那么apache是如何支持动态网页呢? 比如 php 网站。 当apache发现处理的页面是动态网页时(文件名后缀),那么它就在 配置文件中查看哪种应用程序(handler)能够编译处理这种类型的文件,然后把页面交由该应用程序处理。应用程序处理完成之后,再把生成的静态文件传递给apache, apache在返回给客户端。

    由于我们在编译 php 的时候,指定了 --with-apxs2=/usr/local/apache/bin/apxs 选项,它会自动帮我们完成加载模块。

# vim /etc/httpd/httpd.conf
LoadModule php5_module modules/libphp5.so


AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

<IfModule dir_module>
    DirectoryIndex index.php index.html        # 修改apache默认页面类型
</IfModule>

5、测试

# httpd -t
# service httpd restart

# vi /usr/local/apache/htdocs/index.php
<?php
	phpinfo();
?>

通过浏览器访问,如果能够看到 php 配置详情页面,说明配置成功。

6、测试 php 与 mysql

# vi /usr/local/apache/htdocs/index.php

<?php
        $conn = mysql_connect('localhost', 'root', '123456');
        if ($conn)
                echo "Connect MySQL Success";
        else
                echo "Failure, can't connect mysql";
?>


五、安装 php (Fast-CGI方式)

配置 apache(fastCGI)

如果要通过fastCGI,不仅 php需要支持fastCGI, 而且apache同样也要支持fastCGI, 两者必须同时支持才可以。

为apache安装 mod_fastcgi模块

## apache 2.2 如下安装即可
# wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
# tar xf mod_fastcgi-2.4.6.tar.gz
# cd mod_fastcgi-2.4.6
# cp Makefile.AP2 Makefile
# vim Makefile 

## 将Makefile中的top_dir 路径改成你的apache的安装路径
top_dir = /usr/local/apache

# make && make install 

### 安装成功后,会自动把mod_fastcgi.so复制到/usr/local/apache/modules目录下
# cd /usr/local/apache/modules
# ls | grep fast
mod_fastcgi.so

########################################################################
## apache 2.4  安装 mod_fastcgi 2.4.6 需要打补丁
# wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
# tar -zxvf mod_fastcgi-2.4.6.tar.gz
# cd mod_fastcgi-2.4.6

# wget http://leeon.me/upload/other/byte-compile-against-apache24.diff
# patch -p1 < byte-compile-against-apache24.diff  //apache2.4下安装mod_fastcgi 2.4.6 ,需要打补丁
# cp Makefile.AP2 Makefile
# vim Makefile       将Makefile中的路径改成你的apache的安装路径

# make && make install 
//安装成功后,会自动把mod_fastcgi.so复制到/usr/local/apache/modules目录

修改 httpd.conf 配置文件

# vim /usr/local/apache/conf/httpd.conf
LoadModule actions_module modules/mod_actions.so  //删除这行前面的#注释
LoadModule fastcgi_module modules/mod_fastcgi.so  //添加这行,mod_fastcgi.so为上一步mod_fastcgi生成的

配置 PHP(fastCGI)

    编译php时需要指定 --enable-fpm 选项,以启用 fastCGI。

# ./configure --prefix=/usr/local/php
--enable-fpm                                # fastCGI
--with-apxs2=/usr/local/apache/bin/apxs     # module
  • --enable-fpm

fastCGI模式。

--with-apxs2

        有些文章错误的介绍: 如果要配置 fastCGI模式,就不能使用该选项。  其实不是这样, --with-apxs2 的作用主要有2个。第一: 把 php 的解释编译成 so 文件并添加到apache的modules目录中。 第二: 编辑 httpd.conf 配置文件,添加 LoadModule  php5_module  modules/libphp5.so。

如果不想使用 module 模式的话, 把下面这句注释掉就行了。

#LoadModule php5_module modules/libphp5.so


编译php的过程和 第四步 一致,此处不再累述。

fastCGI 优点:

  • 从稳定性上看, fastcgi是以独立的进程池运行来cgi,单独一个进程死掉,系统可以很轻易的丢弃,然后重新分    配新的进程来运行逻辑.

  • 从安全性上看,Fastcgi支持分布式运算. fastcgi和宿主的server完全独立, fastcgi怎么down也不会把server搞垮.

  • 从性能上看, fastcgi把动态逻辑的处理从server中分离出来, 大负荷的IO处理还是留给宿主server, 这样宿主server可以一心一意作IO,对于一个普通的动态网页来说, 逻辑处理可能只有一小部分, 大量的图片等静态

  • IO处理完全不需要逻辑程序的参与.

  • 从扩展性上讲, fastcgi是一个中立的技术标准, 完全可以支持任何语言写的处理程序   (php,java,perl,ruby,c++,python…)

  • 适用操作系统,可在任何平台上http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz使用


配置文件

# cd /usr/local/src/php-5.5.18

# cp php.ini-production /usr/local/php55/etc/php.ini

配置 php-fpm 服务

# cp /usr/local/php55/etc/php-fpm.conf.default /usr/local/php55/etc/php-fpm.conf
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm55
# chmod +x /etc/init.d/php-fpm55

启动 php-fpm

# service php-fpm55 start

php-fpm 可用参数 start|stop|force-quit|restart|reload|status

# ss -tulpn | grep php
tcp  LISTEN   0   128  127.0.0.1:9000   *:*   users:(("php-fpm",64349,7),("php-fpm",64350,0),("php-fpm",64351,0))

# ps -ef | grep php
root     64349     1  0 10:21 ?        00:00:00 php-fpm: master process (/usr/local/php55/etc/php-fpm.conf)                                                 
nobody   64350 64349  0 10:21 ?        00:00:00 php-fpm: pool www                                                                                           
nobody   64351 64349  0 10:21 ?        00:00:00 php-fpm: pool www

php-fpm 监听在 127.0.0.1:9000端口上。

php-fpm 有一个主进程(master process),  它负责其他php-fpm子进程的创建和销毁工作。可以编辑 php-fpm.conf 对其工作属性进行配置。


配置 httpd.conf 以及 在 /etc/httpd/conf.d/fastcgi.php.conf

# cp /usr/local/php55/sbin/php-fpm /usr/local/apache/cgi-bin/

## 编辑httpd.conf, 注释
#LoadModule php5_module        modules/libphp5.so


## 编辑fastcgi.php.conf
<IfModule fastcgi_module>
   FastCgiExternalServer /usr/local/php55/sbin/php-fpm -host 127.0.0.1:9000
   AddType application/x-httpd-php .php
   AddHandler php-fastcgi .php
   Action php-fastcgi /cgi-bin/php-fpm
</IfModule>

重启 httpd 进程

# httpd -t
# service httpd restart


测试phpinfo()页面,配置成功则显示为:
...
Server API        FPM/FastCGI
...  


参考文章:

https://segmentfault.com/q/1010000000256516

http://coolnull.com/1293.html

http://www.nowamagic.net/librarys/veda/detail/1319