lamp组合一种是基于linux平台的web应用部署:      
       
    L: Linux, A: apache (httpd), M: MySQL (MariaDB), P (php, python, perl, ruby)

    工作流程:

web进程接受客户端请求,客户端请求的资源有动态的也有静态,静态资源请求web进程直接从磁盘读取,动态资源请求需要服务器提供一个PHP语言的环境进行运行,运行过程可能需要读取数据(由数据库提供数据提高效率),将动态资源运行的结果给web进程,由web进程响应给用户。

为了提高响应速度,可将web进程,php环境,数据库分别部署在不同的服务器上。(这就是fcgi模式)

    早期有客户端自行处理动态资源,获得运行结果(如浏览器的插件,flash等)

关于动态资源请求的工作图

 

 

CGI协议:Common Gateway Interface,通用网关接口      
        简化版的http协议,用于web服务器与应用程序进程通信;(在lamp体系中httpd进程调动php语言等其他的进程)httpd需要CGI模块才能成为cgi协议的客户端和其他应用程序进程交流

        httpd进程还将URL转换成本地文件路径发给编程语言环境进程,由该进程自己调用文件      
        PHP语言有一套自己完整的解释器,无需依赖linux操作系统的特别命令

Database:

        PHP应用程序直接读取磁盘上的数据(文件形式存储的数据),影响应用进程本身运行,将功能下放交给数据库读取磁盘上的数据,数据库进程读取并组织成结构完后再传递给PHP应用程序提高应用程序计算运行的效率。

        数据库为PHP等应用程序提供数据库接口(API),在应用程序的代码中调用此接口。

a与p的结合方式有三种:

           Module:将应用程序作为第三方模块,加载在httpd的进程中

           cgi:web服务器根据客户端的动态请求开启一个子进程,该子进程就是PHP负责运算,运算完成后由httpd进程销毁

           fcgi:应用程序自己起一个进程,监听着与httpd进程的请求(基于套接字可以跨主机),工作模式类似httpd的prework。此种方法可以完全把应用程序独立成服务器。

    请求流程:

        Client --> (http) --> httpd --> (somefile.php, cgi) --> application server --> (mysql) --> mysql server

 

php程序与MySQL如何交互:      
        解释器无须与MySQL对接, 需要处理数据其实是解释器上运行的程序;      
        存储系统有多种:      
            NoSQL: redis, mongodb等等      
            SQL: MariaDB, MySQL, Oracle等      
            NewSQL:      
            分布式存储:      
        程序与存储系统交互需要通过专用的接口(驱动)进行

    CentOS 6.6 AMP(apache,mysql,php)      
        需要安装基本程序包:      
            httpd, php, php-mysql, mysql-server      
        rpm包安装的PHP为模块可能被httpd进程所加载,PHP可以嵌入html代码中 web服务器只会将PHP封装的那部分代码提交给PHP解释器      
       
        PHP代码连接mysql的扩展(称之为API接口或驱动),在centos中被做成一个RPM包(php-mysql),安装完成需要重新加载!

 

部署(Module方式)步骤:

一、在1.11上编译安装apache2.4.9

1.前期准备

httpd-2.4.9需要较新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包。这里选择使用编译源代码的方式进行。想服务器上传三个源代码包apr-1.5.0.tar.bz2  apr-util-1.5.3.tar.bz2  httpd-2.4.9.tar.bz2

2.安装apache细节

(1)编译安装apr

[root@student1 mnt]# tar xf apr-1.5.0.tar.bz2        
[root@student1 mnt]# cd apr-1.5.0         
[root@student1 apr-1.5.0]# ./configure --prefix=/usr/local/apr       checking build system type... x86_64-unknown-linux-gnu         
checking host system type... x86_64-unknown-linux-gnu         
checking target system type... x86_64-unknown-linux-gnu
[root@student1 apr-1.5.0]# make && make install

 

(2)编译安装apr-until

[root@student1 mnt]# tar xf apr-util-1.5.3.tar.bz2        
[root@student1 mnt]# cd apr-util-1.5.3         
[root@student1 apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@student1 apr-util-1.5.3]# make && make install

 

(3)编译安装apache2.4.9

httpd-2.4.9编译过程也要依赖于pcre-devel软件包,需要事先安装。此软件包系统光盘自带,使用yum本地源安装。

[root@student1 Packages]# yum install pcre-devel-7.8-6.el6.x86_64.rpm

之后开始安装apapche,执行如下配置

./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

 

遇到configure: WARNING: OpenSSL version is too old   no

checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures

解决方法:使用yum安装新的openssl-devel就能解决,安装完毕后再次执行配置,顺利通过

 

[root@student1 httpd-2.4.9]# make && make install

顺利安装完毕

3、修改httpd的主配置文件,设置其Pid文件的路径

编辑/etc/httpd24/httpd.conf,添加如下行即可:      
PidFile  "/var/run/httpd.pid"(本次自己编写的)

4、提供SysV服务脚本/etc/rc.d/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

而后为此脚本赋予执行权限:      
# chmod +x /etc/rc.d/init.d/httpd

加入服务列表:      
# chkconfig --add httpd

二、在3.12安装mysql-5.5.33

1、准备数据存放的文件目录

创建/var/mydata/data目录做为mysql数据的存放目录。

2、新建用户以安全方式运行进程:

# groupadd -r mysql       
# useradd -g mysql -r -s /sbin/nologin -M -d /var/mydata/data mysql        
# chown -R mysql:mysql /var/mydata/data


3、安装并初始化mysql-5.5.33

# tar xf mysql-5.5.33-linux2.6-i686.tar.gz -C /usr/local       
# cd /usr/local/        
# ln -sv mysql-5.5.33-linux2.6-i686  mysql        
# cd mysql 
# chown -R mysql:mysql  .       
# scripts/mysql_install_db --user=mysql --datadir=/var/mydata/data        
# chown -R root  .


4、为mysql提供主配置文件:

# cd /usr/local/mysql       
# cp support-files/my-large.cnf  /etc/my.cnf


并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:      
thread_concurrency = 2

另外还需要添加如下行指定mysql数据文件的存放位置:      
datadir = /var/mydata/data


5、为mysql提供sysv服务脚本:

# cd /usr/local/mysql       
# cp support-files/mysql.server  /etc/rc.d/init.d/mysqld        
# chmod +x /etc/rc.d/init.d/mysqld


添加至服务列表:      

# chkconfig --add mysqld        
# chkconfig mysqld on


而后就可以启动服务测试使用了。


为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:      
6、输出mysql的man手册至man命令的查找路径:

编辑/etc/man.config,添加如下行即可:      
MANPATH  /usr/local/mysql/man

7、输出mysql的头文件至系统头文件路径/usr/include:

这可以通过简单的创建链接实现:      

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


8、输出mysql的库文件给系统库查找路径:

# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf


而后让系统重新载入系统库:      
# ldconfig

9、修改PATH环境变量,让系统可以直接使用mysql的相关命令。

连接数据库并为数据库创建用户以供后面PHP测试

[root@MariaDB ~]# mysql -p     
Enter password:       Welcome to the MariaDB monitor.  Commands end with ; or \g.      Your MariaDB connection id is 13      Server version: 5.5.36-MariaDB-log MariaDB Server
Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create user 'test'@'192.168.%.%' identified by 'ewq321';     Query OK, 0 rows affected (0.00 sec)

三、在1.11上编译安装php-5.4.26

1、解决依赖关系:

配置好yum源为阿里云上的源(系统安装源及epel源)后执行如下命令:      

# yum -y groupinstall "Desktop Platform Development"(本机安装系统时选择桌面版了,桌面开发环境完整)         
# yum -y install bzip2-devel libmcrypt-devel        
#


2、编译安装php-5.4.26

# tar xf php-5.4.26.tar.bz2       
# cd php-5.4.26        
# ./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/apache/bin/apxs --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --enable-maintainer-zts



说明:      
1、这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。


报错1:configure: error: xml2-config not found. Please check your libxml2 installation.      
解决方法:        
        在多次安装libxml2-2.7.6-14.el6.x86_64时,出现如下提醒        
        Examining libxml2-2.7.6-14.el6.x86_64.rpm: libxml2-2.7.6-14.el6.x86_64        
        libxml2-2.7.6-14.el6.x86_64.rpm: does not update installed package.        
        Error: Nothing to do        
        结合报错判定可能是libxm12的扩展包未安装,yum -y install libxm12-devel报错1解决        
       
报错2:configure: error: Cannot find libmysqlclient_r under/usr/local/mysql. Note that the MySQL client library is not bundledanymore!        
解决方法:        
    如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定PHP连接mysql的相关参数设置指定为mysqlnd函数库(它是专门为PHP而写的一个库,用了PHP的内在管理函数以及一些网络流的函数。),这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。        
        指定连接php新的函数库mysqlnd方法:./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd

# make       
# make test        
# make intall


为php提供配置文件:      
# cp php.ini-production /etc/php.ini

3、 编辑apache配置文件httpd.conf,以apache支持php      
 
# vim /etc/httpd/httpd.conf        
1、添加如下二行        
   AddType application/x-httpd-php  .php        
   AddType application/x-httpd-php-source  .phps

2、定位至DirectoryIndex index.html        
   修改为:        
    DirectoryIndex  index.php  index.html

而后重新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。


测试页面index.php示例如下:      
    <?php        
      $link = mysql_connect('192.168.3.12,'test',‘ewq321');      
      if ($link)        
        echo "Success...";        
      else        
        echo "Failure...";

      mysql_close();      
    ?>

报错3:/tmp/php-5.4.26/ext/standard/info.c:878: undefined reference to `executor_globals_id'      
       collect2: ld returned 1 exit status        
       make: *** [sapi/cli/php] Error 1        
解决方法:造成此错误的原因是编译PHP时漏加了一个参数设置发现后重新编译,但没有事先进行make clean清理,应先make clean而后重新编译./configure再make&&make install

发现打开index。php页面一直报"Failure...",后在web服务器上安装mysql客户端,连接测试报错如下

[root@student1 Packages]# mysql -h 192.168.3.12 -u root -p    
Enter password:      
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.3.12' (113)      
[root@student1 Packages]# mysql -h 192.168.3.12 -u root –p

 

经检查发现数据库服务器防火墙没有关闭,关闭后正常连接。

LAMP_linux