编译安装lamp框架

一、 先说说啥叫lamp

    1. lamp简介: Linux Apache Mysql PHP(Python, Perl)的简称,下面说说他们之间的关系

        Linux:系统运行平台 

        Apache:httpd网页服务器,不会和后端数据库发生直接联系 

        Mysql:mysql数据服务器,用来为前端应用程序提供数据,例如php脚本 

        PHP:动态网页脚本的解释器,通常联系httpd和mysqld,如果httpd遇到php脚本时,会交给php解析。脚本运行所需要的数据将向mysqld获取。 

二、 两种lamp构架实现方式

    1. lamp构架的实现方式之一, 基于php模块实现。 此时PHP是作为一个httpd的动态模块存在的,由httpd直接管理调度。

                                        lamp_php_module.png 

    操作过程如下 

            1) 编译安装apache 

      ## 安装编译环境 
        # yum -y groupinstall 'Development tools'
        # yum -y groupinstall 'Desktop Platform Development'
        # yum -y install pcre-devel 
        
      ## 编译安装apr
        # tar -xf apr-1.5.2.tar.gz
        # cd apr-1.5.2 
        # ./configure --prefix=/usr/local/apr
        # make && make install
        
      ## 编译安装apr-util 
        # tar -xf apr-util-1.5.4.tar.gz
        # cd apr-util-1.5.4
        # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
        # make && make install
        
      ## 编译安装httpd2.4.12 
        # tar -xf httpd-2.4.12.tar.gz
        # cd httpd-2.4.12 
        # ./configure --prefix=/usr/local/httpd24 --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 --enables-modules=most --enable-mpms-shared=all --with-mpm=event
        # make && make install
        
      ## 修改配置文件
      ## vim /etc/httpd24/httpd.conf 修改 
        PidFile "/var/run/httpd.pid"
        
      ## 提供服务脚本,可以把系统自带httpd服务脚本改名后,稍加修改后使用。 主要需改
      ## apachectl, httpd, prog, pidfile, lockfile 这几项 其他几乎不用动,然后给个权限,存成/etc/rc.d/init.d/httpd24
        
        #!/bin/bash 
        #
        #!/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/httpd24/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/httpd24/bin/apachectl
        httpd=${HTTPD-/usr/local/httpd24/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
# chkconfig --add httpd24

## 至于导入导出头文件,帮助文件和二进制文件,详见之前关于网络服务的博客。此处不详述。

             2) 安装mysql,这里使用的版本为 mysql-5.5.44的二进制文件 

      ## 安装mysql-5.5.44 到 /usr/local/下面                 
        # tar -xf mysql-5.5.44-linux2.6-x86_64.tar.gz -C /usr/local/ 
        # ln -sv /usr/local/mysql-5.5.44 /usr/local/mysql 
        
      ## 创建mysql需要的运行用户 
        # groupadd mysql 
        # useradd -g mysql -r -s /sbin/nologin -M -d mysql 
        
      ## 修改mysql相关文件的属主属组  
        # chown -R root:mysql /usr/local/mysql/* 
        
      ## 初始化mysql数据库 
      ## 先创建/data/sqldata 这里最好使用逻辑卷,方便演示只要创建一个目录就好了
        # mkdir /data/mysql 
        # /usr/local/mysql/mysql_instal_db --user=mysql --datadir=/data/sqldata  
        
      ## 导出mysql的库文件,二进制文件,头文件
        # echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf 
        # ldconfig 
        # echo 'export $PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh 
        # vim /etc/man.config 添加如下行 
        MANPATH /usr/local/mysql/man

            3)编译安装php-5.6.9

      ## 解决依赖关系 
        # yum -y groupinstall 'Development tools'
        # yum -y groupinstall 'Desktop Platform Development'
      
      ## 编译安装 
        # tar -xf php-5.6.9.tar.xz        
        # cd php-5.6.9
        # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --with-apxs2=/usr/local/httpd24/bin/apxs --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --enable-maintainer-zts 
        ## --with-apxs2=/usr/local/httpd24/bin/apxs 这个选项定义了,php将被当做模块调用
        
        # make 
        # make test 
        # make install
        
      ## 提供php配置文件 
        #  cp php.ini-production /etc/php.ini 
        
      ## 修改httpd.conf,使其支持php,并且可以识别index.php结尾作为首页  
        AddType application/x-httpd-php  .php
        AddType application/x-httpd-php-source  .phps
        DirectoryIndex  index.php  index.html

            4)性能测试。 使用phpMyAdmin作为事例。

      ## 安装phpMyAdmin
          # tar -xf phpMyAdmin-4.0.10.10-all-languages.tar.xz -C /var/httpd24/htdocs 
          # cp config.sample.inc.php config.inc.php'
          ## 此时如果使用本机ip访问的话,应该可以看到phpMyAdmin首页
          
      ## 使用ab进行压力测试 
          # ab -c 100 -n 500 192.168.98.128/index.php  
          下面结果我们看到,每秒钟响应次数99个。 一会我们安装完xcache后,在次进行尝试

                                                    ab_no_xchache.png

            5) 安装xcache-3.2.0

      ## 编译安装
        # tar xf xcache-3.2.0.tar.gz
        # cd xcache-3.2.0
        # /usr/local/php/bin/phpize
        # ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
        # make && make install
      ## 配置xcache整合到php上 
        # mkdir /etc/php.d 
        # cp xcache.ini /etc/php.d
        # vim /etc/php.d/xcache.ini 修改下面指令 
        extension = /usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so
      ## 再次进行测试 
         # ab -c 100 -n 500 192.168.98.128/index.php 
         可以看到每秒响应次数变为541个, 性能提升超过五倍

                                                     ab_xchache.png

        2. lamp构架实现方式之二, php作为一个独立服务,可以与httpd服务器分开部署 

                                                    php-fpm.png

在此种模型中, php以php-fpm服务的形式运行,单独调度。如果分开部署可以大大减轻httpd服务器的压力。缺点网络通信不畅的情况下,带宽有可能成为瓶颈。下面实验中,另开一个主机,配置ip 192.168.98.129, 把这个作为php-fpm服务器主机。 mysql 和 httpd全部在之前的主机上,地址为192.168.98.128不用重新安装。下面只在新的主机上重新编译php作为一个服务。

            1) 编译php-fpm服务

      ## 在新主机上配置安装的环境,此地不详述。 
      ## 编译安装php
        # tar -xf php-5.6.9.tar.xz        
        # cd php-5.6.9
       ## 如果没有事先安装mysql则按照以下编译 
        # ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --enable-maintainer-zts 
       ## 如果预先安装mysql则
        # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --enable-maintainer-zts
        php编译模块不同点只是把 --with-apxs2=/usr/local/httpd24/bin/apxs替换为 --enable-fpm而已

          2) 配置php-fpm

      ## 提供服务脚本 
         # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
      编辑配置文件, 大部分都不需要修改。  如果已经提供了,就不要再多加。重复的指令会产生错误。 
        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
        listen = 192.168.98.129:9000 
        
     ## 启动服务
        # service php-fpm start 
        # ss -tnlp 
        LISTEN     0      128                                                                                        192.168.98.129:9000                                                                                                   *:*      users:(("php-fpm",30927,9),("php-fpm",30943,0),("php-fpm",30944,0),("php-fpm",30946,0))

         3) 配置httpd服务器,使其支持反向代理 

      ## 在配置文件中启用反向代理模块
      LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
      LoadModule proxy_module modules/mod_proxy.so
      
      ## 配置个虚拟主机
      <VirtualHost *:80>
            DocumentRoot "/var/httpd24/htdocs/phpMyAdmin"
            ServerName playground.com
            ProxyRequests off
            ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.98.129:9000/var/htdocs/phpMyAdmin/$1
            <Directory "/var/httpd24/htdocs/phpMyAdmin">
                    Options none
                    AllowOverride none
                    Require all granted
             </Directory>
       </VirtualHost>
       
      ## 我们把phpMyAdmin放在 192.168.98.129主机,/var/htdocs/phpMyAdmin下面就可以了,当然没有的话要创建 
      现在当访问192.168.98.128/index.php时,将会通过fcgi协议反向代理到192.168.98.129主机的相应目录下面。

        4) 安装xcache 前后的压力测试 

            安装xcache之前:即使没有安装,也比模块化略快。每秒钟128个请求。

                                          ab_proxy.png  

            安装以后 每秒526个请求

                                           ab_proxy_xcache.png  

原创文章,作者:以马内利,如若转载,请注明出处:http://www.178linux.com/5187

(0)
上一篇 2015-06-18 10:20
下一篇 2015-06-18 10:22

相关推荐

  • Linux Basics-Linux Bash历史和其概念名词解释part1

    Linux Basics-Linux Bash历史和其概念名词解释part1 阅读本文你将知道:查看更多BashFAQ.pdf   Bash的历史及其特性   Bash的如何工作   Bash的概念解释   前提知识:      对linux有一定基础而且了解Bash的…

    Linux干货 2016-10-29
  • Bash的基础特性之命令执行状态返回值和命令行展开

    Bash的基础特性之命令的执行状态 Linux的命令执行结果状态有两种,分别为:1、成功2、失败bash使用特殊变量 $? 保存最近一条命令的执行状态结果使用echo $? 命令来查看命令执行状态返回值:0:成功1-255:失败 示例:         [root@localho…

    Linux干货 2016-11-04
  • 二叉树的应用详解 – 数据结构

    概述: 平衡树——特点:所有结点左右子树深度差≤1 排序树——特点:所有结点“左小右大字典树——由字符串构成的二叉排序树判定树——特点:分支查找树(例如12个球如何只称3次便分出轻重)带权树——特点:路径带权值(例如长度) 最优树——是带权路径长度最短的树,又称 Huffman树,用途之一是通信中的压缩编码。 1. 二叉排序树(二叉查找树 Bina…

    Linux干货 2015-04-07
  • Centos不重启添加新硬盘

     显示当前磁盘分区: [root@centos7 ~]# fdisk -l</p>< p>    Disk /dev/sda: 32.2 GB, 32212254720 bytes, 6…

    Linux干货 2016-03-27
  • 第三周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# who user1    tty1         2016-12-27&nb…

    Linux干货 2016-12-28
  • mysql进阶之MySQL查询

    一、MySQL多表查询和子查询 别名:as        表别名        字段别名        查询结果亦可命名别名   联结查询:事先将两张或多张表join,根…

    2016-11-18