LAMP安装顺序:

apache-->mysql-->php

由于php要与mysql建立关系,php在编译的时候要指明mysql路径,所以mysql要先于php安装。

注意:千万不要偷懒图省事,不安装Apache,就直接开始编译php,然后遇到过不去的,就将参数删除,这是不可行的,不信,你试试就知道了。

一定要记住这些步骤都是不可省的。


一、编译安装httpd-2.4.6

三个依赖的包


apr-1.4.6.tar.gz ------------http://apache.dataguru.cn/apr/apr-1.4.6.tar.gz


apr-util-1.5.2.tar.gz-----http://apache.dataguru.cn/apr/apr-util-1.5.2.tar.gz


Pcre-devel直接yum install


1,先编译安装apr


#tar xf  apr-1.4.6
#cd apr-1.4.6
#./configure --prefix=/usr/local/apr
#make && make install

2,编译安装apr-util


#tar xf apr-util-1.5.2.tar.bz2
#cd apr-util-1.5.2
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
#make && make install

3,编译安装httpd-2.4.6


# tar xf httpd-2.4.6.tar.bz2
# cd httpd-2.4.6
#./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event

由于需要配置的东西很多,这里将所有依赖的和可能用的包做了个汇总。


4,编译安装完了之后,给他配置服务脚本。由于之前装了httpdrpm包,所以在服务目录/etc/rc.d/init.d/下已经有了httpd服务样本,只需要copy过来就行,然后编辑修改。


#cp /etc/init.d/httpd /etc/init.d/httpd24-------init.d

rc.d下的init.d的连接文件,为了却别安装过的httpd程序,cphttpd24



#vim /etc/init.d/httpd24

修改以下三行


apachectl=/usr/local/apache/bin/apachectl


也可以作为服务脚本,不过要将httpd24下的二进制程序source进当前shell才能直接使用。


httpd=${HTTPD-/usr/local/apache/bin/httpd}


httpd24的二进制程序,可以用来检测语法错误与否


pidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid}


进程号文件



服务脚本再现


#!/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-/usr/local/apache/logs/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


5source二进制程序文件


# vim /etc/profile.d/httpd24.sh
Export PATH=/usr/local/apache/bin/:$PATH
# . /etc/profile.d/httpd24.sh

6,连接头文件


# ln -sv /usr/local/httpd24/include/ /usr/include/httpd24

在系统启动的时候可以扫面读取这些文件。

7,导出库文件,有了导,没有就不用导了。一般2.4以后的就没有了。


# vim /etc/ld.so.conf.d/httpd24.conf
/usr/local/apache/lib/
# ldconfig ---------扫描读取刷新系统所有的库文件

8,添加man文件


# vim /etc/man.config
MANPATH /usr/local/apache/man

9,启动httpd服务


#chkconfig --add httpd24
#chkconfig httpd24 on
#service httpd24 start

Apache编译安装完成。





spacer.gif

二、Mysql通用二进制包安装

通用二进制包就是我们所说的绿色软件,不需要安装,只需哟解压后,手动配置,即可使用。


要对数据库进行管理,建议奖数据库文件单独拿出来放在一个逻辑卷中,这样数据库容量不过了,还可以随时扩容。这里实现创建了一个逻辑卷mysql,编辑/etc/fstab让它开机自动挂在到了/mysql/下,由于在编译的时候要指定数据存放位置,这里在mysql下再创建一个目录data,数据库是高度机密的,所以应该提高它的权限,专门添加一个用户来管理。


安装步骤


1下载mysql-5.5.33-linux2.6-x86_64.tar.gz


2,最好将mysql的数据文件放在一个独立的逻辑卷中,所以,新建逻辑卷mysql


# mkdir /mysql
# mount /dev/myvg/mysql /mysql

新建目录mysql,并挂在逻辑卷到该目录下


# mkdir /mysql/data

创建目录,用来存放数据但需要修改目录的权限,属主,属组都为mysql,这时需要新建一个用户,mysql,不需要mysql登录系统,所以,新建mysql用户为系统用户。


#useradd -r mysql
# chown -R mysql:mysql data -----R 递归

3,解压mysql-5.5.33-linux2.6-x86_64.tar.gz包到/usr/local


# tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local/

4,到/usr/local下,为解压后的包创建连接文件


# cd /usr/local
# ln -sv mysql-5.5.33-linux2.6-x86_64 mysql

5,查看一下mysql


# cd mysql
# ls
bin  COPYING  data  docs  include  INSTALL-BINARY  lib  man  mysql-test  README  scripts  share  sql-bench  support-files

Datamysql包原有的数据目录,但我们现在是将它另外放了一个地方,即/mysql/data.因此,这个目录没用了。


6,修改mysql包下的文件的文件属性


# chown -R root:mysql *

7,配置文件


# cd support-files/-----------样例配置文件
# ls
binary-configure   config.small.ini  my-innodb-heavy-4G.cnf  my-small.cnf         mysql.server
config.huge.ini    magic             my-large.cnf            mysqld_multi.server  ndb-config-2-node.ini
config.medium.ini  my-huge.cnf       my-medium.cnf           mysql-log-rotate     solaris
# cp my-large.cnf /etc/my.cnf----------mysql的配置文件独特的格式

8,编辑配置文件,添加属性


# vim /etc/my.cnf
thread_concurrency = 2------------------单核两线程,单核为2,双核就给4
datadir = /mysql/data-----数据库文件目录
socket          = /tmp/mysql.sock-------这个记录的功能是让一台机子可以自己当服务器,自己当客户端。

9,装载服务程序


# cp mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# service mysqld start
Starting MySQL....The server quit without updating PID file[FAILED]/data/luliming.icy.pid).
启动报错。

10,排错


错误原因,服务初始化失败,修改初始化步骤。这种安装步骤系统不会自动初始化,需要手动进行。


这里还需要注意,出现在这种情况的原因还有mysqld服务的端口已经请用,即3306端口已被占用,这时请ps aux | grep mysqld查看是否3306端口已经被占用,然后杀死这个进程,再执行初始化,启动进程。


杀死进程:# kill 进程号


11,手动初始化


# cd /usr/local/mysql/scripts/
# ls
mysql_install_db---------------------用来生成mysql数据元数据的脚本文件,即初始化程序文件
# ./mysql_install_db --help-----------可以看到要指定的选项,有两个选项需要指定
# cd ..
# scripts/mysql_install_db --user=mysql --datadir=/mysql/data----------注意,这里必须在/mysql目录下执行,否则,会报错。

12,初始化成功,启动服务


# service mysqld start
Starting MySQL...                                          [  OK  ]

13,再看数据文件/mysql/data


# ls /mysql/data
mysql  mysql-bin.000001  mysql-bin.000002  mysql-bin.index  performance_schema  test
mysql performance_schema  test-----------这三个文件其实就是数据库了

14,启动客户端


# mysql
-bash: mysql: command not found

报错,说明客户端工具还不能使用。


15,启用客户端工具


# ls bin
innochecksum       mysqlaccess.conf            mysqld                mysqlhotcopy               mysql_tzinfo_to_sql
msql2mysql         mysqladmin                  mysqld-debug          mysqlimport                mysql_upgrade
myisamchk          mysqlbinlog                 mysqld_multi          mysql_plugin               mysql_waitpid
myisam_ftdump      mysqlbug                    mysqld_safe           mysql_secure_installation  mysql_zap
myisamlog          mysqlcheck                  mysqldump             mysql_setpermission        perror
myisampack         mysql_client_test           mysqldumpslow         mysqlshow                  replace
my_print_defaults  mysql_client_test_embedded  mysql_embedded        mysqlslap                  resolveip
mysql              mysql_config                mysql_find_rows       mysqltest                  resolve_stack_dump
mysqlaccess        mysql_convert_table_format  mysql_fix_extensions  mysqltest_embedded

这里可以看到mysql的各种命令,只需要修改环境变量,就可以将它们全部唤醒。


16,唤醒mysql客户端工具


# vim /etc/profile.d/mysql.sh

在里面写上


Export PATH=/usr/local/mysql/bin/:$PATH


保存退出


# . /etc/profile.d/mysql.sh

这样,mysql客户端工具就可以使用了。


# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.33-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

17,连接头文件,便于访问


# ln -sv /usr/local/mysql/include /usr/include/mysql

18,设置帮助文件


# vim /etc/man.config

写上MANPATH /usr/local/mysql/man保存退出


19,导出库文件


# vim /etc/ld.so.conf.d/mysql.conf

写上/usr/local/mysql/lib


# ldconfig -v | grep mysql----------------刷新系统库文件,显示与mysql有关的信息。

可以看到库文件已经导出


到此,mysql通用二进制程序包配置完毕。







spacer.gif

三、Php服务安装配置php-5.4.13



下载源码包php-5.4.13.tar.bz2


果想让编译的php支持mcrypt扩展,还需要下载libmcrypt,libmcrypt-devel


libmcrypt-2.5.7-5.el5.i386.rpm


libmcrypt-devel-2.5.7-5.el5.i386.rpm


1,安装依赖的mcrypt

# yum -y install libmcrypt libmcrypt-devel libxml2-devel bzip2-devel openssl-devel

提供一个yum仓库地址


Baseurl=http://mirrors.sohu.com/fedora-epel/6Server/x86_64/


2,配置编译环境


# tar xf php-5.4.13.tar.bz2
# cd php-5.4.13
#.configure --prefix=/usr/local/php --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --enable-xml --with-freetype-dir  --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=usr --with-mhash --with-mcrypt --with-config-file-path=etc --with-config-file-scan-dir=etcphp.d --with-bz2 --with-curl --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-apxs2=/usr/local/httpd24/bin/apxs

--with-apxs=/usr/local/httpd24/bin/apxs

这一项要根据你安装的Apache的名字来定义


--with-mysql=mysqlnd --with-mysqli=mysqlnd 同一台虚拟机配置php和mysql高可用必须有这两项。

注意,如果我们没有重新编译安装apache而使用的是系统原有的httpd,那么这里肯定会报错,--with-apxs2=/usr/local/apache/bin/apxs首先这个路径就不对,而且,这一项也是必须的,因为如果这一项没有,那么我们编译php就没有什么意义了,下面有对这一项的解释


--prefix=/usr/local/php 安装目录


--with-mysql=/usr/local/mysql 依赖的mysql在什么地方


--with-openssl openssl在什么地方


--with-mysqli=/usr/local/mysql/bin/mysql_config mysql高级接口


--enable-mbstring------------------


--with-freetype-dir-----------php创建动态图片的时候用到


--with-jpeg-dir


--with-png-dir---------开发库相同上三者,desktop 。。。。


--with-zlib---------------压缩库


--with-libxml-dir=/usr-----------libxml


--enable-xml----------解析xml格式的页面程序


--enable-sockets-----------有些监控程序依赖


--with-apxs2=/usr/local/apache/bin/apxs--------------Apache钩子,将php当作模块装载到Apache中的接口


--with-mcrypt-------开发库,安装上libmcryptmhash--epel上下载---php能够支持更强机密的工具


--with-config-file-path=/etc----------------php的配置文件位置


--with-config-file-scan-dir=/etc/php.d---------到上级目录找php.ini,到该目录下找所有以.ini结尾的文件,当作配置文件的一部分


--with-bz2--------------压缩工具


--enable-maintainer-zts-------------Apache配置安装如果以workerevent模式,就需要使用这个功能,很明显,上边我们配置Apache编译环境的时候使用了,--with-mpm=event,所以,这里就需要启用这个功能。


prefork:支持的模块mod_php


worker, event:支持的模块 mod_php_zts

出现

phpMyAdmin - Error

The mbstring extension is missing. Please check your PHP configuration.


# yum -y install php-mbstring

3,编译安装


# make && make install

4,php提供配置文件,当前目录/usr/local/php-5.4.19 /


# cp php.ini-production /etc/php.ini

5,编辑apache配置文件,让apache支持php


# vim /etc/httpd/httpd.conf


添加如下二行


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


定位至DirectoryIndex index.html


修改为:


DirectoryIndex index.php index.html


6,重启apache,可测试,配置结果。


7,用ab做下 压力测试


# ab -c 1000 -n 10000 www.web.com/index.php


This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.web.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: Apache/2.4.6
Server Hostname: www.web.com
Server Port: 80
Document Path: /index.php
Document Length: 46750 bytes
Concurrency Level: 1000
Time taken for tests: 13.801 seconds
Complete requests: 10000
Failed requests: 1001
 (Connect: 0, Receive: 0, Length: 1001, Exceptions: 0)
Write errors: 0
Total transferred: 469158884 bytes
HTML transferred: 467498884 bytes
Requests per second: 724.57 [#/sec] (mean)
Time per request: 1380.122 [ms] (mean)
Time per request: 1.380 [ms] (mean, across all concurrent requests)
Transfer rate: 33197.27 [Kbytes/sec] received
Connection Times (ms)
 min mean[+/-sd] median max
Connect: 2 377 451.3 248 3184
Processing: 38 684 745.1 529 9293
Waiting: 2 437 720.3 288 9293
Total: 236 1060 818.0 908 9349
Percentage of the requests served within a certain time (ms)
 50% 908
 66% 983
 75% 1005
 80% 1051
 90% 1947
 95% 2257
 98% 4201
 99% 4398
 100% 9349 (longest request)
下面做下xcache,来比较装了xcache后的速度是否变快。






spacer.gif

php安装xcache模块,加速php的运行。



1,下载xcache源码包xcache-3.0.1.tar.gz


2,编译安装


# tar xf xcache-3.0.1.tar.gz
# cd xcache-3.0.1
# /usr/local/php/bin/phpize 编译模块的时候都需要,用来识别模块


Configuring for:


PHP Api Version: 20100412


Zend Module Api No: 20100525


Zend Extension Api No: 220100525


# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install


注释:当我们编译php的时候,./configure 后边跟了好多选项,那些都是php依赖的功能模块,随着工作的需要,我们还需要额外的一些模块,而安装这些模块,就需要用到phpize了,而不用在重新编译php


3安装结束时,会出现类似如下行:


Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/


4编辑php.ini,整合phpxcache


首先将xcache提供的样例配置导入php.ini


# mkdir /etc/php.d
# cp xcache.ini /etc/php.d/ 程序在执行的时候会去扫描配置文件,并将其作为配置文件使用。

说明:xcache.ini文件在xcache的源码目录中。


接下来编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:


zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so


5,ab压力测试,看看安装了xcache后的速度是否提升。


# ab -c 1000 -n 10000 www.web.com/index.php


This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.web.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: Apache/2.4.6
Server Hostname: www.web.com
Server Port: 80
Document Path: /index.php
Document Length: 46766 bytes
Concurrency Level: 1000
Time taken for tests: 11.736 seconds
Complete requests: 10000
Failed requests: 1041
 (Connect: 0, Receive: 0, Length: 1041, Exceptions: 0)
Write errors: 0
Total transferred: 478996380 bytes
HTML transferred: 477300524 bytes
Requests per second: 852.08 [#/sec] (mean)
Time per request: 1173.596 [ms] (mean)
Time per request: 1.174 [ms] (mean, across all concurrent requests)
Transfer rate: 39857.82 [Kbytes/sec] received
Connection Times (ms)
 min mean[+/-sd] median max
Connect: 7 370 423.3 222 3124
Processing: 148 722 682.7 579 6271
Waiting: 4 408 661.4 256 6051
Total: 410 1092 778.6 840 6656
Percentage of the requests served within a certain time (ms)
 50% 840
 66% 1032
 75% 1098
 80% 1201
 90% 1731
 95% 2277
 98% 3839
 99% 5034
 100% 6656 (longest request)

spacer.gif

四、Apache2.4以上版本虚拟主机启动流程



1,注释掉主机配置的DocumentRoot


#DocumentRoot "/usr/local/apache/htdocs"

2,开启虚拟主机的连接配置


Include /etc/httpd24/extra/httpd-vhosts.conf


3,在虚拟主机配置中配置虚拟机


#<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot "/usr/local/apache/docs/dummy-host.example.com"
# ServerName dummy-host.example.com
# ServerAlias www.dummy-host.example.com
# ErrorLog "logs/dummy-host.example.com-error_log"
# CustomLog "logs/dummy-host.example.com-access_log" common
#</VirtualHost>

以上是注释信息,我们应该启用虚拟机配置,下面是我做的配置

<VirtualHost 172.16.251.111:80>
 ServerAdmin webmaster@dummy-host2.example.com
 DocumentRoot "/www/web/"
 ServerName www.phpadmin.com
 ErrorLog "/www/web/logs/error_log"
 CustomLog "/www/web/logs/access_log" common
<Directory "/www/web">
 Options None
 require all granted
</Directory>
</VirtualHost>


注意:不用的虚拟主机配置一定要注释掉,还有,必须为虚拟主机授权,即Directory中的配置,否则,在浏览器中访问时会提示没有权限。




spacer.gif

五、配置图形化的数据库管理工具phpMyAdmin



需要下载phpMyAdmin-4.0.5-all-languages.zip


1,解压程序包


# tar xf phpMyAdmin-4.0.5-all-languages.zip


2,将解压后的文件复制到站点根目录下进行简单配置,就可以在浏览器中打开使用了。假设站点就是上边所配置的虚拟主机,站点根目录为/www/web/


# mv phpMyAdmin-4.0.5-all-languages/* /www/web/


3,cd到站点根目录


# cd /www/web


4,提供配置文件,这里有配置文件模板,只需要复制即可


# cp config.sample.inc.php config.inc.php


5,配置文件中不需要修改什么,可以为cookies改下认证码

$cfg['blowfish_secret'] = 'a8jdfjsljflsdjflkjsdfjb7c6d'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

注意,如果在浏览器中无法访问mysql,就需要将配置文件中的主机名localhost改为127.0.0.1,还不行再将allownopassword 改为true

6,在浏览器中输入站点名称,就可以看到下面登录界面。


142859587.jpg


7,因为是mysql数据库的管理工具,复制过来的phpmyadmin数据库管理工具需要手动为它设置一个账户密码。


8,为phpmyadmin设置账号密码



# mysql -uroot -hlocalhost -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.33-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>


若之前有设置密码,这里就必须写明用户,登录的主机,和密码才能登录。


mysql> set password for root@localhost=password(redhat);


Query OK, 0 rows affected (0.00 sec)


账户密码设置成功,用户root,密码redhat刷新数据库


mysql> flush privileges;


Query OK, 0 rows affected (0.01 sec)


9,登录phpMyadmin,就可以对数据库进行管理操作了。



142838495.jpg


通过下图可以看出,左边列出的就是我们数据库中的几个数据可文件


mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)


就此,一个简单的LAMP的实例已全部呈现,后续还会补充php的代理功能fastcgimemcache服务器的应用,敬请期待。




spacer.gif

六、配置apache-2.4.4fpm方式的php-5.4.13



1,php-fpm提供Sysv init脚本,并将其添加至服务列表:


上文中已经编译安装了php,要配置fpm服务器,还需要借助php的解压包,在解压后的php包中可以看到sapi目录,fpm的服务程序就在其中。


1,启用php-fpm服务

# cd php-5.4.13
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on

2php-fpm提供配置文件:


# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf


3编辑php-fpm的配置文件:


# vim /usr/local/php/etc/php-fpm.conf


配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):


pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php/var/run/php-fpm.pid

4接下来就可以启动php-fpm了:


# service php-fpm start

5使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):


# ps aux | grep php-fpm


6默认情况下,fpm监听在127.0.0.19000端口,也可以使用如下命令验正其是否已经监听在相应的套接字。


# netstat -tnlp | grep php-fpm
tcp  0  0 127.0.0.1:9000    0.0.0.0:*     LISTEN  689/php-fpm


7php-fpm服务启动了,但apache还不能够支持,所以还需要对Apache做一些配置


启用httpd的相关模块


Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩充,因此,这两个模块都要加载


LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so


配置虚拟主机支持使用fcgi


在相应的虚拟主机中添加类似如下两行。


ProxyRequests Off   取消正向代理
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1

例如:


<VirtualHost *:80>
 DocumentRoot "/www/web"
 ServerName www.phpamdin.com
 ProxyRequests Off
 ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/magedu.com/$1
 <Directory "/www/web">
 Options none
 AllowOverride none
 Require all granted
 </Directory>
</VirtualHost>

注释:


1ProxyRequests Off是关闭正向代理的意思,因为fastcgi是为web服务器做代理,所以他是反向代理


154235471.jpg


2 ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/web/$1


把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://127.0.0.1:9000后指明了这两个参数,其它的参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。


再注释:fastcgicgi的功能是一样的,只是前者比后者速度更快一筹,它们都是进程处理请求,但cgifork-and-excute模式,而fastcgi则是long-live模式,所以fastcgi性能要比cgi强。


ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/web/$1

格式比较固定,其中127.0.0.1不能随意改动,若要改动,必须与php目录下的配置文件php-fpm中一句listen = 172.16.251.141:9000相同,否则报错

Service Unavailable

The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.



这样,php-fpm模式的Apache配置完毕。



将session信息保存在memcached中

编辑php.ini文件,确保如下两个参数的值分别如下所示:

session.save_handler = memcache

session.save_path = "tcp://172.16.200.11:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

两个测试页面


<?php

session_start();

if (!isset($_SESSION['www.MageEdu.com'])) {

$_SESSION['www.MageEdu.com'] = time();

}

print $_SESSION['www.MageEdu.com'];

print "<br><br>";

print "Session ID: " . session_id();

?>


<?php

session_start();

$memcache_obj = new Memcache;

$memcache_obj->connect('172.16.200.11', 11211);

$mysess=session_id();

var_dump($memcache_obj->get($mysess));

$memcache_obj->close();

?>