基于fcgi的LAMP试验环境及其原理

wKiom1M2-lTRhhTLAAIGYXcMNW8011.jpg


基于fcgI编译安装LAMP实现层次化

一:二进制源码安装Mysql

1.将数据存储在基于Raid5的逻辑卷中;

     创建基于raid5的逻辑卷

     1.1将磁盘格式化为raid卷  

# fdisk /dev/sdb
WARNING: DOS-compatible mode isdeprecated. It's strongly recommended to
switchoff the mode (command 'c') and change display units to
sectors (command 'u').
Command (m forhelp): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (5224-15665, default5224):
Using defaultvalue 5224
Last cylinder, +cylinders or +size{K,M,G} (5224-15665, default15665): +10G
Command (m forhelp): n
Command action
e   extended
p   primary partition (1-4)
e
Selected partition 4
First cylinder (6530-15665, default6530):
Using defaultvalue 6530
Last cylinder, +cylinders or +size{K,M,G} (6530-15665, default15665): +20G
Command (m forhelp): n
First cylinder (6530-9141, default6530):
Using defaultvalue 6530
Last cylinder, +cylinders or +size{K,M,G} (6530-9141, default9141): +10G
Command (m forhelp): n
First cylinder (7836-9141, default7836):
Using defaultvalue 7836
Last cylinder, +cylinders or +size{K,M,G} (7836-9141, default9141): +10G
Command (m forhelp): t
Partition number (1-6): 3
Hex code (type L to list codes): fd
Changed system type of partition 3to fd (Linux raid autodetect)
Command (m forhelp): t
Partition number (1-6): 5
Hex code (type L to list codes): fd
Changed system type of partition 5to fd (Linux raid autodetect)
Command (m forhelp): t
Partition number (1-6): 6
Hex code (type L to list codes): fd
Changed system type of partition 6to fd (Linux raid autodetect)
Command (m forhelp): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed witherror 16: Device or resource busy.
The kernel still uses the old table. The newtable will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.


    1.2.重读分区信息(在分区表中没有所见的分区信息需要重复kpartx和partx)  

# kpartx -af /dev/sdb
# partx -a /dev/sdb
# cat /proc/partitions  验证


    1.3 创建raid5    

# mdadm -C /dev/md0 -a  yes -l 5-c 32K -n 3/dev/sdb{3,5,6}
# mdadm -D /dev/md0 验证


   1.4基于raid5创建逻辑卷,并开机自动挂载      

# pvcreate  /dev/md0                               创建物理卷
Physical volume "/dev/md0"successfully created
# vgcreate data /dev/md0                           创建卷组
Volume group "data"successfully created 
# lvcreate -L 10G -n mydata data                   创建逻辑卷
Logical volume "mydata"created
# mkdir data                                     创建挂在点
# vim /etc/fstab  添加如下一行                     自动挂载
/dev/data/mydata   /data   ext4   defaults   00
# mount -a
# mke2fs -t ext4 -L MYDATA /dev/data/mydata        创建ext4文件系统


  2.通用二进制格式安装Mysql

   2.1为Mysql数据库创建用户和组

# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin mysql
# id mysql
uid=496(mysql) gid=493(mysql) groups=493(mysql)



 2.2下载二进制格式的源码包至本地,解压至/usr/local目录  

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



  2.3 为mysql-5.5.33-linux2.6-i686创建链接方便以后更新    

# ln -sv mysql-5.5.33-linux2.6-i686 mysql
`mysql' -> `mysql-5.5.33-linux2.6-i686'



  2.4改变mysql-5.5.33-linux2.6-i686目录Mysql数据目录下所有文件的属主和属组

# chown -R root:mysql mysql/*
# cd mysql
# ll
# mkdir /data/data 创建Mysql的数据目录
chown -R mysql.mysql /data/data

 2.5初始化Mysql的系统库    

# scripts/mysql_install_db  --datadir=/data/data/ --user=mysql
# chown -R mysql  ./*         将该目录下的所有文件的属主改变为mysql

 2.6为Mysql添加启动脚本  

# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig --list mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

  2.7为Mysql提供配置文件并对其进行如下修改

# cp /usr/local/mysql/support-files/my-large.cnf  /etc/my.cnf
# vim /etc/my.cnf
thread_concurrency = 4修改为cpu核心数的2倍
datadir=/data/data                         添加一行为其指定数据目录

  2.8 Mysql安装后的后续配置

# ln -sv /usr/local/mysql/include/ /usr/include/mysql
导出头文件
`/usr/include/mysql/include' -> `/usr/local/mysql/include/'
# echo "/usr/local/mysql/lib/">/etc/ld.so.conf.d/mysql.conf
导出库文件
# ldconfig                                         
重读库文件
# ldconfig -p | grep "mysql"
libtcmalloc_minimal.so.0(libc6) => /usr/local/mysql/lib/libtcmalloc_minimal.so.0
libmysqlclient_r.so.16(libc6) => /usr/lib/mysql/libmysqlclient_r.so.16
libmysqlclient.so.18(libc6) => /usr/local/mysql/lib/libmysqlclient.so.18
libmysqlclient.so.16(libc6) => /usr/lib/mysql/libmysqlclient.so.16
libmysqlclient.so (libc6) => /usr/local/mysql/lib/libmysqlclient.so
# echo '/usr/local/mysql/bin:$PATH'> /etc/profile.d/mysql.sh
导出二进止文件
# echo $PATH
/usr/lib/qt3.3/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# vim /etc/man.config
MANPATH /usr/local/mysql/man                                         导出man文档

  2.9启动Mysql服务  

# service mysqld start
Starting MySQL                                             [  OK  ]

二: 编译安装httpd-2.4.9

  httpd-2.4.9依赖你较高版本的apr和apr-util,并且还依赖域pcre-devel开发编译安装之前首先将apr-1.5.0、apr-util-1.5.3和httpd-2.4.9的源码包下载至本地

  1.解决编译安装httpd的依赖关系  

# yum install -y prce-devel
# yum groupinstall -y \"Development Tools\"
# yum groupinstall -y \"Server Platform Development\"

  2.编译安装apr-1.5.0

# tar xf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr
# make
# make install

  3.编译安装apr-util-1.5.3    

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


  4.编译安装httpd-2.4.9(生产环境根据自己的需要启用所需要的选项)


# tar xf httpd-2.4.9.tar.bz2
# cd httpd-2.4.9
# ./configure
--prefix=/usr/local/apache                指定安装目录
--sysconfdir=/etc/httpd                   制定配置文件的目录
--enable-so                                让apache核心装载DSO,但是不实际编译任何动态模块
--enable-ssl                               启用ssl
--enable-cgi                               启用通用网关接口协议
--enable-rewrite                           重写Apache重写功能
--with-zlib                                zlib库
--with-pcre                                启用pcre库
--with-apr=/usr/local/apr                  启用apr
--with-apr-util=/usr/local/                apr-util 启用apr-util
--enables-modules=most                     启用httpd常用的模块
--enable-mpms-shared=most                  启用常用的mpm模块
--with-mpm=event                           指定mpm工作模式为event
# make
# make install



  5.编译安装httpd-2.4.9之后的配置

   

1提供服务启动脚本# vim /etc/rc.d/init.d/httpd
     
# 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/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}
# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $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=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p ${pidfile} $httpd
        RETVAL=$?
        ;;
  restart)
        stop
        start
        ;;
  condrestart|try-restart)
        if status -p ${pidfile} $httpd >&/dev/null; then
                stop
                start
        fi
        ;;
  force-reload|reload)
        reload
        ;;
  graceful|help|configtest|fullstatus)
        $apachectl $@
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
        RETVAL=2
esac
exit $RETVAL
     
2.为脚本添加可执行权限
/etc/rc.d/init.d/httpd
    
3.将Apache服务脚本添加服务列表
# chkconfig --add httpd
# chkconfig --list httpd
# chkconfig  httpd on
    
4在/etc/httpd/httpd.conf配置文件中添加一行
PidFile "/var/run/httpd/httpd.pid"
      
5.导出二进制文件
# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh



  6.启动服务

# service httpd start
Starting httpd:                                            [  OK  ]


三:编译安装php-5.4.26

  1.解决依赖关系

# yum -y groupinstall "Desktop Platform Development"
# yum -y install libmcrypt-devel
# yum -y install zipb2-devel


  2.解压至当前目录中,并切换至其目录下

# tar xf php-5.4.26.tar.bz2
# cd php-5.4.26


  3.编安装php-5.4.26 (生产环境根据自己的需要启用所需要的选项)

# ./configure
--prefix=/usr/local/php                         指定安装目录
--with-mysql=mysqlnd                            指定使用mysql
--with-pdo-mysql=mysqlnd                        启用pdo模块
--with-mysqli=mysqlnd                           启用mysqli,优化支持
--with-openssl                                  启用openssl
--enable-mbstring                               启用多字节,支持字符串
--with-freetype-dir                             启用对freetype字体库的支持                   
--with-jpeg-dir                                 启用对jpeg图片的支持
--with-png-dir                                  启用对png图片的支持
--with-zlib                                     启用对压缩库的支持
--with-libxml-dir=/usr                          启用libxml2库的支持
--enable-xml                                    启用xml
--enable-sockets                                启用对套接字的支持
--enable-fpm                                    启用fpm模块
--with-mcrypt                                   启用对加密的支持
--with-config-file-path=/etc                    指定编译安装时配置文件安装的路径
--with-config-file-scan-dir=/etc/php.d          指定php.ini文件的安装路径 
--with-bz2                                      启用b2压缩
--enable-maintainer-zts                         启用对worker和event的支持
# make                                          编译php
# make install                                  安装php

   4.为php提供配置文件  

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

  5.配置php-fpm

为php-fpm提供SysV init脚本,并将其添加至主服务列表中
# 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
# chkconfig --list php-fpm


  6.为php-fpm提供配置文件

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


   7.编辑php-fpm的配置文件

# vim /usr/local/php/etc/php-fpm.conf
配置fpm的相关选项为你所需要的值
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   启用pid文件
可以启动php-fpm了:
# service php-fpm start
来验正是否开启php-fpm
# ps aux | grep php-fpm


四:配置httpd-2.4.9使其支持fcgi

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

# vim /etc/httpd/httpd.conf 启用如下两个模块
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

  2. 编辑apache配置文件httpd.conf,让apache能识别php格式的页面,并支持php格式的主页

1
2
3
4
5
6
# vim /etc/httpd/httpd.conf
1 .定位至AddType添加如下两行
AddType application/x-httpd-php  .php
AddType application/x-httpd-php-source  .phps
2 .定位至DirectoryIndex index.html 修改为:
DirectoryIndex  index.php  index.html


  3.配置虚拟主机支持fcgi

注释掉中心主机:DocumentRoot
启用虚拟主机,编辑httpd.config将 #Include/etc/httpd/extra/httpd-dav.conf改为
Include /etc/httpd/extra/httpd-dav.conf
编辑/etc/httpd/extra/httpd-dav.conf启用虚拟主机支持fcgi
# vim /etc/httpd/extra/httpd-dav.conf
<virtualhost>      ServerAdmin webadmin@www.a.magedu.com    DocumentRoot \"/web/www.a.magedu.com/htdocs\"    ServerName www.a.magedu.com    ServerAlias www.a.magedu.com    ErrorLog \"logs/a.magedu.com-error_log\"    CustomLog \"logs/a.magedu.com-access_log\" combined                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     <directory>options none        Require all granted</directory>ProxyRequests Off    ProxyPassMatch ^/(.*\\.php)$ fcgi://172.16.19.2:9000/web/a.magedu.com/htdocs/$1</virtualhost><virtualhost>ServerAdmin webadmin@b.www.magedu.com    DocumentRoot \"/web/b.www.magedu.com/htdocs\"    ServerName www.b.magedu.com    ServerAlias www.b.magedu.com    ErrorLog \"logs/b.magedu.com-error_log\"    CustomLog \"logs/b.magedu.com-access_log\" combined                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     <directory>options none        Require all granted</directory>ProxyRequests Off    ProxyPassMatch ^/(.*\\.php)$ fcgi://172.16.19.2:9000/web/b.magedu.com/htdocs/$1</virtualhost>

注意:

       ProxyRequests Off:关闭正向代理

       ProxyPassMatch:把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运

       行 的目录和URI,所以这里直接在fcgi://172.16.19.2:9000后指明了这两个参数, 其它

       的参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。


四:测试基于fcgi模式的LAMP环境

   1.为www.a.magedu.com提供链接mysql的测试

wKioL1M2-t7DpKzzAAE1CNPAT_I722.jpg

  2. www.b.magedu.com提供测试

wKiom1M2-zDwLLlqAADDolqlVLc844.jpg

   4.基于fcgi模式的LAMP环境搭建成功

wKioL1M2-ybj3bevAAFB2rk92Sk837.jpg

wKiom1M2-2CjNm5BAAHgDMIiivk625.jpg