编译安装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
下一篇 2015-06-18

相关推荐

  • 编程真难啊

    上周,在Sun的Java论坛上出现了一个这样的帖子,这个贴子的链接如下:http://forums.sun.com/thread.jspa?threadID=5404590&start=0&tstart=0 LZ的贴子翻译如下: 大家好,我是一个Java的新手,我有一个简单的问题:请问我怎么才能反转一个整数的符号啊。比如把-12转成+12。是…

    Linux干货 2015-04-03
  • Linux高级文件系统管理之磁盘配额、软RAID及LVM

    高级文件系统管理之磁盘配额、软RAID及LVM   本章内容: 设定文件系统配额 设定和管理软RAID设备 配置逻辑卷   一、文件系统配额:     执行软限制(soft limit) 硬限制(hard limit)     注:磁盘配额只能针对分区控制有效,不能对整个磁盘控制…

    Linux干货 2016-09-01
  • 语言的歧义

    语言是人与人相互沟通的途径,而计算机语言则是人和计算机沟通的途径。就算是任何再完美的自然语言都会有歧义,但是又是什么让人和计算计算机间产生了歧义呢?下面这篇文章来自Gowri Kumar的Puzzle C一文。我做了一些整理,挑选了其中的一些问题,并在之后配上相应的答案(这些答案是我加的,如果需要原版的答案可以直接和本文作者Gowri Kumar联系,作者的…

    Linux干货 2016-05-08
  • Linux文件系统初识

    Linux文件系统初识        文件系统是一种用于向用户提供数据访问的机制,我们的硬盘,U盘等存储设备会被文件系统分割为特定大小的块,系统中的文件就被存储在数据块中,而我们平常接触到的分区,目录,文件等正是文件系统通过组织整合之后才呈现在我们面前的,我们无需去关心我们的数据到底存储在硬盘的那个…

    2017-07-15
  • 马哥教育网络班22期+第6周课程练习

    week6: 请详细总结vim编辑器的使用并完成以下练习题 vim编辑器小结: 1. vim编辑器的模式: vim编辑器有很多模式,常用模式有:Normal,Insert,Command; 通常,打开一个文件,此时所处的就是normal模式;normal模式下可以浏览,修改文件内容; 在任何模式下,只要按ESC就可以返回到Normal模式; Ins…

    Linux干货 2016-09-26
  • 学习宣言

    努力不只是为了更好的生活,更是为了证明自己! 只有逼自己一把,才能知道自己是可以做到的! 路漫漫其修远兮,吾将上下而求索。

    Linux干货 2016-12-26