一、什么是LAMMP?

Linux+Apache+Mysql+Memcached+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。随着开源潮流的蓬勃发展,开放源代码的LAMP已经与J2EE和.Net商业软件形成三足鼎立之势,并且该软件开发的项目在软件方面的投资成本较低,因此受到整个IT界的关注。从网站的流量上来说,70%以上的访问流量是LAMP来提供的,LAMP是最强大的网站解决方案.


二、为什么要编译安装?

这是Linux初学者经常问到的问题,编译安装可以提供更高的灵活性,自主的开启或关闭某些功能,另外,系统发行商自带的rpm包的版本比较老,而且一般是在比较通用的机器上编译的,对于使用者来说可能不是很适合,编译安装更能适合自己的机器配置、性能等要求。


三、各组件的作用:

Apache:提供http服务

Xcache:为php提供加速功能;

MySQL:为网站提供数据库支持;

Memcached:为数据库的查询提供高速缓存,优化查询速度

PHP:解释PHP程序开发的网页

四、工作原理:

编译安装LAMP_第1张图片

1、客户把请求发送到http服务器;

2、http根据请求的内容进行处理,如果请求的是静态页面,就在本地处理,如果请求的是php页面,http服务器发送到FastCGI服务器,xcache将php代码进行编译成opcode,加速php响应;

3、如果客户端发起的是一个mysql查询,PHP服务器将查询发到memcached接口;

4、memcached服务器如果没有此缓存,就向PHP服务器返回否定回答;

5、PHP服务器向MySQL发起查询请求;

6、MySQL向PHP服务器返回结果;

7、PHP如果配置了要缓存此结果,就将查询结果发送给memcached服务器进行缓存;

8、PHP将结果返回给http服务器

9、http服务器回应客户的请求;



五、配置案例:

说明:

httpd(172.16.1.1)

FastCGI(172.16.1.2)

mysql(172.16.1.3)

memcached(172.16.1.4)


(一)、编译安装httpd

准备工作:

安装开发包组 "Development tools" "Server Platform Development"

下载新的apr和pcre,此处演示用的版本如下:

httpd-2.4.6.tar.bz2

apr-1.4.6.tar.bz2

apr-util-1.5.2.tar.bz2

pcre-devel-7.8-6.el6.x86_64.rpm

# tar xf httpd-2.4.6.tar.bz2

# cd httpd-2.4.4

# ./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 选项介绍:

--prefix=/usr/local/apache ;自定义安装路径

--sysconfdir=/etc/httpd24;配置文件目录

--enabe-so;编译动态加载模块(DSO)支持到httpd二进制文件,此模块使得Apache的各功能模块可以与核心分开编译、运行时动态加载。

--enable-ssl;支持ssl功能

--enable-cgi;支持cgi

--with-pcre ;支持Perl兼容的正则表达式库

--with-apr=/usr/local/apr;为apache提供底层支持的接口库

--with-apr-until=/usr/local/apr-util

--with-zlib;支持zlib的压缩

--enable-rewrite;支持url重写

--enable-modules=most;支持大部分的模块

--enable-mpms-shared=all ;指定所有模块为共享模块

--with-mpm-event=event;选择Apache多路处理模块为事件驱动模块

MPM介绍;

1):profork

工作原理: 一个单独的控制进程(父进程)负责产生子进程,为Unix系统下默认处理模块,它将运行一个非线程型的、预派生的Web服务器,这些子进程用于监听请求并作出应答。Apache总是试图保持一些备用的(spare)或者是空闲的子进程用于迎接即将到来的请求。这样客户端就不需要在得到服务前等候子进程的产生。将MaxClients设置为一个足够大的数值以处理潜在的请求高 峰,同时又不能太大,以致需要使用的内存超出物理内存的大小。

2)、worker

每个进程可以拥有的线程数量是固定的。服务器会根据负载情况增加或减少进程数量。一个单独的控制进程(父进程)负责子进程的建立。每个子进程可 以建立ThreadsPerChild数量的服务线程和一个监听线程,该监听线程监听接入请求并将其传递给服务线程处理和应答。

3)、event

事件驱动模型,是一个进程响应多个请求,当进程向内核产生一个系统调用,内核会先让进程去响应其它请求,等内核处理完成就通知进程,采用了边缘触发的机制。在apache2.4版本中支持了这种模型。


安装

make && make install

#导出PATH,方便执行命令;

vi /etc/profile.d/apache.sh

添加:

export PATH=/usr/local/apache/bin:$PATH


# 制作启动脚本

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

# 添加httpd24到服务列表

chkconfig --add httpd24

# 开机启动httpd24

chkconfig httpd24 on


#修改原配置文件路径

vi /etc/init.d/httpd24

修改:

# 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}

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


#启动服务

service httpd24 start


如果要设置https,需要再配置文件中启用

LoadModule ssl_module modules/mod_ssl.so

# Secure (SSL/TLS) connections

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


为网站生成私钥并为网站服务器申请证书(配置详解openssl)

在配置文件/etc/httpd24/extra/httpd-ssl.conf中设定网站证书和私钥存放位置

service httpd24 restart

netstat -tnl查看

将CA证书导出到客户端,双击证书,并安装证书到“受信任的根证书颁发机构”

编译安装LAMP_第2张图片

访问测试:https://www.magedu.com;

编译安装LAMP_第3张图片



(二)编译php以FastCGI服务器的方式工作:

编译php时启用php-fpm就可以把php启动为一个FastCGI的服务器;

httpd与php进行交互必须工作在反向代理模式下,并且需要fcgi模块跟FastCGI服务器通信;


如果想让编译的php支持mcrypt扩展,需要安装如下软件

libmcrypt-2.5.7-5.el5.i386.rpm

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

mhash-0.9.9-1.el5.centos.i386.rpm

mhash-devel-0.9.9-1.el5.centos.i386.rpm


# 数据库在本地:

./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 --enable-fpm --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2


# 数据库不在本地:

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


编译参数介绍;

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

--with-apxs2=/usr/local/apache/bin/apxs

--with-config-file-path=/usr/local/PHP/etc   指定php.ini位置

--with-mysql=/usr/local/mysql   mysql安装目录,对mysql的支持

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

--with-openssl ;可以支持ssl

--enable-mbstring ;启动多字节字符支持

--with-freetype-dir ;这是让PHP支持GD库的配置选项

--with-jpeg-dir

--with-png-dir

--with-zlib;支持zlib的压缩库

--with-libxml-dir=/usr

--enable-xml ;支持xml格式

--enable-sockets;支持socket方式通信

--with-apxs2=/usr/local/apache/bin/apxs;php针对此接口来编译进apache

--with-mcrypt;支持加密工具

--with-config-file-path=/etc;配置文件文件

--with-config-file-scan-dir=/etc/php.d;找/etc/php.d/下一.ini结尾的文件作为配置文件

--with-bz2

--enable-maintainer-zts;如果MPM为event和worker模型,编译时此处须启用,如果为prefork,它是以mod_php的方式安装的。


安装

make && make install


为php提供配置文件:

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


3、配置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


为php-fpm提供配置文件:

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


编辑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


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

# service php-fpm start

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

# ps aux | grep php-fpm


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

# netstat -tnlp | grep php-fpm

tcp      0      0  127.0.0.1:9000        0.0.0.0:*        LISTEN      689/php-fpm


httpd和php不在一台主机上;需修改fastcgi监听地址

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

listen = 172.16.1.2:9000


修改httpd服务器(172.16.1.1)/etc/httpd24/httpd.conf文件中添加如下内容:

AddType application/x-httpd-php .php

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

修改默认网页:

DirectoryIndex  index.php index.html

在DocumentRoot下添加:

 ProxyRequests Off

 ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.1.2:9000/www/magedu/com/$1

service httpd24 restart


测试:

[root@stu01 php-5.4.19]# curl  -I http://172.16.1.1

HTTP/1.1 200 OK

Date: Fri, 23 Aug 2013 11:01:41 GMT

Server: Apache/2.4.6 (Unix) OpenSSL/1.0.0-fips PHP/5.4.19

X-Powered-By: PHP/5.4.19

Content-Type: text/html


注意:httpd服务器(172.16.1.1)的DocumentRoot下

fcgi://172.16.1.2:9000/中的www/magedu/com/为FastCGI服务器上文件系统的路径;

在FastCGI的服务器(172.16.1.2)上有一个目录是/www/magedu/com


例:将phpMyadmin解压在/www/magedu/com

# unzip phpMyAdmin-4.0.5-all-languages.zip

# cd phpMyAdmin-4.0.5-all-languages

# mv * /var/www/html/

# cd /var/www/html

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

vi config.inc.php

# 把blowfish算法的密码填一个随机数

$cfg['blowfish_secret'] ='alkdjflakjdf';/



出现登录界面:此时数据库尚未安装

编译安装LAMP_第4张图片


(三)使用xcache为PHP加速;

XCache 是一个开源的 opcode 缓存器/优化器, 这意味着他能够提高您服务器上的 PHP 性能. 他通过Zend引擎编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接使用缓冲区已编译的代码从而提高速度. 通常能够提高您的页面生成速率 2 到5 倍, 降低服务器负载


1、安装

# tar xf xcache-3.0.1.tar.gz

# cd xcache-3.0.1

# /usr/local/php/bin/phpize ;使php识别此模块

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

# make && make install


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

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


2、编辑php.ini,整合php和xcache:


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

# mkdir /etc/php.d

# cp xcache.ini /etc/php.d


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

在/www/magedu/com下建立一个info.php

   phpinfo();

?>

编译安装LAMP_第5张图片


性能测试工具之ab:

ab

ulimit -n 30000 限制用户同时打开的文件数;默认为1000

ab

-c     -c 100 并发连接数

-n-n 10000总共1w次。

ab -c 100 -n 10000 http://www.magedu.com/index.php


php以模块方式工作且没有配置xcache之前:

[root@stu01 ~]# ab -c 50 -n 500 http://www.magedu.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.magedu.com (be patient)

Completed 100 requests

Completed 200 requests

Completed 300 requests

Completed 400 requests

Completed 500 requests

Finished 500 requests



Server Software:        Apache/2.4.6

Server Hostname:        www.magedu.com

Server Port:            80


Document Path:          /index.php

Document Length:        8139 bytes


Concurrency Level:      50

Time taken for tests:   254.889 seconds

Complete requests:      500

Failed requests:        0

Write errors:           0

Total transferred:      4680364 bytes

HTML transferred:       4069500 bytes

Requests per second:    1.96 [#/sec] (mean)

Time per request:       25488.945 [ms] (mean)

Time per request:       509.779 [ms] (mean, across all concurrent requests)

Transfer rate:          17.93 [Kbytes/sec] received


Connection Times (ms)

             min  mean[+/-sd] median   max

Connect:        0    5  12.1      1     179

Processing:   127 24924 15913.8  22397   77363

Waiting:      122 24100 15661.8  21556   76825

Total:        127 24929 15912.8  22402   77364


Percentage of the requests served within a certain time (ms)

 50%  22402

 66%  31010

 75%  35930

 80%  39479

 90%  45833

 95%  53053

 98%  62870

 99%  66384

100%  77364 (longest request)


以模块方式配置php后,配置完xcache之后测试:

[root@stu01 ~]# ab -c 50 -n 500 http://www.magedu.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.magedu.com (be patient)

Completed 100 requests

Completed 200 requests

Completed 300 requests

Completed 400 requests

Completed 500 requests

Finished 500 requests


Server Software:        Apache/2.4.6

Server Hostname:        www.magedu.com

Server Port:            80


Document Path:          /index.php

Document Length:        8139 bytes


Concurrency Level:      50

Time taken for tests:   3.329 seconds

Complete requests:      500

Failed requests:        0

Write errors:           0

Total transferred:      4680272 bytes

HTML transferred:       4069500 bytes

Requests per second:    150.20 [#/sec] (mean)

Time per request:       332.892 [ms] (mean)

Time per request:       6.658 [ms] (mean, across all concurrent requests)

Transfer rate:          1372.99 [Kbytes/sec] received


Connection Times (ms)

             min  mean[+/-sd] median   max

Connect:        0    8  12.8      1      86

Processing:    16  304 215.0    273    1626

Waiting:       14  287 211.1    256    1523

Total:         17  312 215.1    284    1627


Percentage of the requests served within a certain time (ms)

 50%    284

 66%    362

 75%    405

 80%    442

 90%    524

 95%    620

 98%    842

 99%   1258

100%   1627 (longest request)



启用FastCGI性能测试:

[root@sta2 ~]# ab -c 50 -n 500 http://www.magedu.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.magedu.com (be patient)

Completed 100 requests

Completed 200 requests

Completed 300 requests

Completed 400 requests

Completed 500 requests

Finished 500 requests



Server Software:        Apache/2.4.6

Server Hostname:        www.magedu.com

Server Port:            80


Document Path:          /index.php

Document Length:        7792 bytes


Concurrency Level:      50

Time taken for tests:   8.186 seconds

Complete requests:      500

Failed requests:        0

Write errors:           0

Total transferred:      4444802 bytes

HTML transferred:       3896000 bytes

Requests per second:    61.08 [#/sec] (mean)

Time per request:       818.554 [ms] (mean)

Time per request:       16.371 [ms] (mean, across all concurrent requests)

Transfer rate:          530.28 [Kbytes/sec] received


Connection Times (ms)

             min  mean[+/-sd] median   max

Connect:        0    1   1.0      1       9

Processing:    72  781 131.5    809     897

Waiting:       69  777 131.2    805     894

Total:         75  782 131.2    810     898


Percentage of the requests served within a certain time (ms)

 50%    810

 66%    821

 75%    833

 80%    842

 90%    859

 95%    875

 98%    884

 99%    891

100%    898 (longest request)


# 配置FastCGI和xcache之后测试结果:


[root@sta3 run]# ab -c 50 -n 500 http://www.magedu.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.magedu.com (be patient)

Completed 100 requests

Completed 200 requests

Completed 300 requests

Completed 400 requests

Completed 500 requests

Finished 500 requests



Server Software:        Apache/2.4.6

Server Hostname:        www.magedu.com

Server Port:            80


Document Path:          /index.php

Document Length:        7792 bytes


Concurrency Level:      50

Time taken for tests:   2.476 seconds

Complete requests:      500

Failed requests:        0

Write errors:           0

Total transferred:      4444778 bytes

HTML transferred:       3896000 bytes

Requests per second:    201.94 [#/sec] (mean)

Time per request:       247.602 [ms] (mean)

Time per request:       4.952 [ms] (mean, across all concurrent requests)

Transfer rate:          1753.05 [Kbytes/sec] received


Connection Times (ms)

             min  mean[+/-sd] median   max

Connect:        0    1   0.7      0       5

Processing:    25  236  39.3    246     282

Waiting:       22  233  39.2    243     272

Total:         26  237  38.9    246     282

WARNING: The median and mean for the initial connection time are not within a normal deviation

       These results are probably not that reliable.


Percentage of the requests served within a certain time (ms)

 50%    246

 66%    250

 75%    252

 80%    253

 90%    257

 95%    263

 98%    269

 99%    272

100%    282 (longest request)


通过对比,PHP以FastCGI方式要比以模块方式工作的性能要好很多;



(四)配置Memcached

Memcached是一款开源、高性能、分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对Database的访问来加速web应用程序。它是一个基于内存的“键值对”存储,用于存储数据库调用、API调用或页面引用结果的直接数据,如字符串、对象等。


memcached是以LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件。现在

已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。


Memcached是一款开发工具,它既不是一个代码加速器,也不是数据库中间件。其设计哲学思想主要反映在如下方面:


1. 简单key/value存储:服务器不关心数据本身的意义及结构,只要是可序列化数据即可。存储项由“键、过期时间、可选的标志及数据”四个部分组成;

2. 功能的实现一半依赖于客户端,一半基于服务器端:客户负责发送存储项至服务器端、从服务端获取数据以及无法连接至服务器时采用相应的动作;服务端负责接收、存储数据,并负责数据项的超时过期;

3. 各服务器间彼此无视:不在服务器间进行数据同步;

4. O(1)的执行效率

5. 清理超期数据:默认情况下,Memcached是一个LRU缓存,同时,它按事先预订的时长清理超期数据;但事实上,memcached不会删除任何已缓存数据,只是在其过期之后不再为客户所见;而且,memcached也不会真正按期限清理缓存,而仅是当get命令到达时检查其时长;


memcached可以使用取余算法和一致性散列算法来确定缓存到那台服务器上。

取余算法效率高,但服务器节点变动时,会使得整个缓存系统崩溃,从而重新构建缓存,在大规模部署中很少使用;

一致性散列算法

假如在一个圆环上分布着2^32个点位,把服务器的主机名或IP地址进行hash计算后除以2^32取余,就得到服务器在圆环上的位置;

数据是如何处理的?如果对mysql发起的请求为key、那么结果为value;对mysql的key做hash运算,除以2^32取余,得到在圆环上的点位,如果此点位上没有memcached服务器,它就会缓存到沿圆环的顺时针方向找到的第一个memcached服务器上。

一致性散列算法对数据的存储时不均匀的,但可以最大限度地减少缓存的失效量。如果服务器A宕机,那么受影响的会是从D到A这一段,其余不受影响,大规模部署时,一定要使用一致性散列算法。

memcached的缓存对象不能大于1M,是由于其内存分配的机制决定的。它会事先对内存进行切片,按照增长因子进行划分;

memcached使用基于libevent库中epoll算法实现并行处理请求;


配置部分:

memcached官方站点:memcached.org

在RHEL 6.4 的光盘中有rpm包,为了一致性,下面采用源码安装;

libevent至少是1.4以后的版本才会支持memcached;

memcached依赖于libevent API,因此要事先安装之,项目主页:http://libevent.org/,读者可自行选择需要的版本下载。本文采用的是目前最新版本的源码包libevent-2.0.21-stable.tar.gz。安装过程:


# tar xf libevent-2.0.21-stable.tar.gz

# cd libevent-2.0.21

# ./configure --prefix=/usr/local/libevent

# make && make install

# ln -sv /usr/local/libevent/includ /usr/includ/libevent

# echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf


# 安装memcached

# tar xf memcached-1.4.15.tar.gz

# cd memcached-1.4.15

# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent

# make && make install


# ldconfig -v | grep memcached

# cd /usr/local/memcached


# memcached的常用选项说明


-l :指定进程监听的地址;

-d: 以服务模式运行;

-u :以指定的用户身份运行memcached进程;

-m :用于缓存数据的最大内存空间,单位为MB,默认为64MB;

-c :最大支持的并发连接数,默认为1024;

-p : 指定监听的TCP端口,默认为11211;

-U :指定监听的UDP端口,默认为11211,0表示关闭UDP端口;

-t :用于处理入站请求的最大线程数,仅在memcached编译时开启了支持线程才有效;

-f :设定Slab Allocator定义预先分配内存空间大小固定的块时使用的增长因子;

-M:当内存空间不够使用时返回错误信息,而不是按LRU算法利用空间;

-n: 指定最小的slab chunk大小;单位是字节;

-S: 启用sasl进行用户认证;


# 启动memcached并查看内存分配情况

/usr/local/memcached/bin/memcached -u daemon  -f 1.1 -vvv ;按照指定的增长因子,显示分配内存情况

-f 1.25  ; 增长因子

-u daemon;以daemon身份运行

-v ;显示内存分配过程

-d ;后台运行;


# memcached SysV的startup脚本代码如下所示,将其建立为/etc/init.d/memcached文件:


#!/bin/bash

#

# Init file for memcached

#

# chkconfig: - 86 14

# description: Distributed memory caching daemon

#

# processname: memcached

# config: /etc/sysconfig/memcached


. /etc/rc.d/init.d/functions


## Default variables

PORT="11211"

USER="nobody"

MAXCONN="1024"

CACHESIZE="64"

OPTIONS=""


RETVAL=0

prog="/usr/local/memcached/bin/memcached"

desc="Distributed memory caching"

lockfile="/var/lock/subsys/memcached"


start() {

       echo -n $"Starting $desc (memcached): "

       daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE

       RETVAL=$?

       [ $RETVAL -eq 0 ] && success && touch $lockfile || failure

       echo

       return $RETVAL

}


stop() {

       echo -n $"Shutting down $desc (memcached): "

       killproc $prog

       RETVAL=$?

       [ $RETVAL -eq 0 ] && success && rm -f $lockfile || failure

       echo

       return $RETVAL

}


restart() {

       stop

       start

}


reload() {

       echo -n $"Reloading $desc ($prog): "

       killproc $prog -HUP

       RETVAL=$?

       [ $RETVAL -eq 0 ] && success || failure

       echo

       return $RETVAL

}


case "$1" in

 start)

       start

       ;;

 stop)

       stop

       ;;

 restart)

       restart

       ;;

 condrestart)

       [ -e $lockfile ] && restart

       RETVAL=$?

       ;;      

 reload)

       reload

       ;;

 status)

       status $prog

       RETVAL=$?

       ;;

  *)

       echo $"Usage: $0 {start|stop|restart|condrestart|status}"

       RETVAL=1

esac


exit $RETVAL



使用如下命令配置memcached成为系统服务:

# chmod +x /etc/init.d/memcached

# chkconfig --add memcached

# service memcached start


安装Memcache的PHP扩展


# tar xf memcache-2.2.5.tgz

# cd memcache-2.2.5

/usr/local/php/bin/phpize

# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache

# make && make install


上述安装完后会有类似以下的提示:


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


编辑/etc/php.ini,在“动态模块”相关的位置添加如下一行来载入memcache扩展:

extension=/path/to/memcache.so  ; /path/to/就是上面安装完成后的提示路径


编译安装LAMP_第6张图片

而后对memcached功能进行测试,在网站目录中建立测试页面test.php,添加如下内容:

$mem = new Memcache;

$mem->connect("127.0.0.1", 11211)  or die("Could not connect");


$version = $mem->getVersion();

echo "Server's version: ".$version."
\n";


$mem->set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");

echo "Store data in the cache (data will expire in 600 seconds)
\n";


$get_result = $mem->get('hellokey');

echo "$get_result is from memcached server.";        

?>



如果有输出“Hello World is from memcached.”等信息,则表明memcache已经能够正常工作。

编译安装LAMP_第7张图片


数据库安装:

本例采用二进制格式的源码:

mysql-5.5.33-linux2.6-x86_64.tar.gz


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


# ln -sv mysql-5.5.33-linux2.6-x86_64 mysql


# cd mysql

# chown root.mysql *


本例中数据目录在/data;

# chown mysql.mysql /data

生成配置文件

# cp support-files/my-large.cnf /etc/my.cnf


设置PATH变量

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

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

使配置生效

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


导出头文件和库文件

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

vi /etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib


初始化数据库:

# scripts/mysql_install_db --user=mysql --datadir=/data


# 制作启动脚本

# cp support-files/mysql.server /etc/init.d/mysqld


修改配置文件中pid文件位置

vi /etc/init.d/mysqld

mysqld_pid_file_path=/var/run/mysql/mysql.pid


添加到启动列表,并设置开机启动

chkconfig --add mysqld

chkconfig mysqld on

service mysqld start


修改PHP服务器上phpMyadmin的配置文件:vi /www/magedu/com/config.inc.php

修改phpMyadmin登录MySQL的密码:

$cfg['Servers'][$i]['host'] = '172.16.1.3';


/* User used to manipulate with storage */

$cfg['Servers'][$i]['controlhost'] = '172.16.1.3';

$cfg['Servers'][$i]['controluser'] = 'root';

$cfg['Servers'][$i]['controlpass'] = 'redhat';

编译安装LAMP_第8张图片

到此为止,整个架构已经完成,由于作者水平所限,不足之处在所难免,敬请指正。