一 LAMMP 是什么

L linux ,A Apache,M mysql,M Memcached

LAMMP 架构图

211841557.png

Apache 相应回复用户html请求

FastCGI 把PHP程序执行的结果响应给Apache

Memcache 根据用户请求的动态网页,由程序决定是否需要把数据缓存至Memcache服务器中,Memcache是把数据缓存在内存中

Mysql 响应用户查询写入数据

准备环境

每个服务器采用系统centos6.4 x86_64最小化安装,启动网卡,修改主机名,关闭SElinux ,关闭防火墙

安装编译环境

yum install vim
yum install man
yum install ntp
yum groupinstall "development tools" "server platform development" "desktop platform development"

apache 主机名 www.daphne.com ip 192.168.200.201

php 主机名 php.daphne.com ip 192.168.200.202

mem 主机名 mem.daphne.com ip 192.168.200.203

mysql 主机名 sql.daphne.com ip 192.168.200.204

二 LAMMP 实现

1 apache安装

依赖的软件包 apr-1.4.6.tar.bz2 apr-util-1.5.2.tar.bz2 pcre-devel httpd-2.4.4.tar.bz2

安装apr

tar xf apr-1.4.6.tar.bz2
cd apr-1.4.6
./configure --prefix=/usr/local/apr 
make && make install

安装apr-util

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

yum install pcre-devel

 

安装httpd

 

tar xf httpd-2.4.4.tar.bz2
cd httpd-2.4.4
./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl

--enable-cgi --enable-cgid --enable-modules=most --enable-mods-shared=most --enable-rewrite

--with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util

--enable-mpms-shared=all --with-mpm=--sysconfdir=/etc/httpd  :指定配置文件安装位置
--enable-so              :支持动态共享模块如果没有这个模块PHP将无法与apache结合工作
--enable-ssl             :启用支持ssl
--enable-cgi             :支持cgi
--enable-rewrite         :支持URL重写--with-zlib             :压缩库,在互联网上传播时可节约带宽
--with-apr=/usr/local/apr :指定apr路径
--with-apr-util=/usr/local/apr-util :指定apr-util路径
--enable-mpms-shared=all  :支持多道处理模块
--with-mpm=         :设定默认的模块


更改配置文件

cd /etc/httpd/
cp httpd.conf httpd.conf.bak
vim httpd.confPidFile     # 定义pid 文件的路径

提供服务脚本

Vim  /etc/init.d/httpd
#!/bin/bash
#
# chkconfig: - 85 15
# description: Apache  a World Wide Web server.  It  used to serve \
. /etc/rc.d/init.d/functions      -----读取函数 [ -f /etc/sysconfig/httpd ]; then. /etc/sysconfig/httpd
fi
# Start httpd  the C locale by .
HTTPD_LANG=${HTTPD_LANG-}
# This will prevent initlog from swallowing up a pass-phrase prompt # mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=# Set HTTPD=/usr/sbin/httpd.worker  /etc/sysconfig/httpd to use a server
# with the thread-based  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 -form  messages.
apachectl=/usr/local/apache/bin/apachectl   # -----指定apachectl程序位置
httpd=${HTTPD-/usr/local/apache/bin/httpd} #-------httpd程序位置
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}  #----如果文件存在就使用存在文件路径,如果不存在就使用/var/rum/httpd.pid
lockfile=${LOCKFILE-/var//subsys/httpd} # --------创建的锁文件
RETVAL=0
start() {echo -n $LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS #----以$pidfile文件执行httpd 并且使用选项startRETVAL=$?  #------定义执行状态返回值echo[ $RETVAL = 0 ] && touch ${lockfile} #-----成功时创建锁文件 $RETVAL
}
stop() {echo -n $   killproc -p ${pidfile} -d 10 $httpdRETVAL=$?echo[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}reload() {echo -n $ ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/; thenRETVAL=$?echo $failure $killproc -p ${pidfile} $httpd -HUPRETVAL=$?fiecho
}# See how we were called.  start)start;;stop)stop;;status)status -p ${pidfile} $httpdRETVAL=$?;;restart)stopstart;;condrestart)        [ -f ${pidfile} ] ; thenstopstartfi;;reload)reload;;graceful|help|configtest|fullstatus)$apachectl        RETVAL=$?;;*)echo $   exit 1
esacexit $RETVAL

 

chmod u+x /etc/init.d/httpd

 

设置开机自动启动

chkconfig --add httpd
chkconfig httpd on
chkconfig httpd --list
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

启动apache 测试

service httpd start
Starting httpd: AH00558: httpd: Could not reliably determine the serverServerName' directive globally to suppress  message[  OK  ]
启动成功 有警告vim  /etc/httpd/httpd.conf
ServerName  localhost :80
service httpd restart

用浏览器访问一下

显示 It Works!

为httpd服务添加环境变量

Vim /etc/profile.d/httpd.sh
export PATH=$PATH:/usr/local/apache/binsource /etc/profile.d/httpd.sh
httpd -t
Syntax OK

二 安装mysql数据库

解压通用二进制软件包

tar xf mysql-5.5.37-linux2.6-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -sv  mysql-5.5.37-linux2.6-x86_64 msql

建立数据库文件 生产环境数据库存放在独立LVM硬盘上面

mkdir -pv /mydata/data   
useradd -r mysql
chown -R mysql:mysql /mydata/data

建立mysql配置文件

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

建立服务脚本

cp mysql.server /etc/rc.d/init.d/mysqld

修改配置文件

Vim /etc/my.cnf
thread-concurrency = 4  #cpu物理核心的1-2 倍
datadir= /mydata/data

 

初始化mysql 脚本

 

yum install libaio
cd /usr/local/mysql
scripts/mysql_install_db --user=mysql --datadir=/mydata/data

 

开机自动启动mysql

 

chkconfig --add mysqld
chkconfig  mysqld on

修改PATH环境变量

cd /etc/profile.d/
vim mysqld.sh
export PATH=$PATH:/usr/local/mysql/binsource /etc/profile.d/mysqld.sh

 

链接头文件

 

ln -sv /usr/local/mysql/include  /usr/include/mysqld.conf

链接库文件

cd /etc/ld.so.conf.d/
vim mysqld.conf
/usr/local/mysql/libldconfig

 

创建登陆数据库用户和密码

 

mysql
mysql>drop user root@;
mysql>drop user root@;
mysql>create user root@ identified by ;
mysql>flush privileges;

 

 三 安装PHP

如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。

安装图片资源软件

yum -y install gd gd-devel  libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel

 

安装libxml 库

 

yum -y  install libxml2 libxml2-devel

 

安装bzip2压缩库

 

yum install -y bzip2 bzip2-devel

 

安装mcrypt加密库

 

默认yum没有此安装包 需要添加epel

yum install libmcrypt libmcrypt-devel

 

 编译PHP

注 编译参数--enable-fpm,支持FastCGI PHP模块,此参数决定是否能把PHP安装成#FastCGI服务器

--with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd 本机不需要先安装

mysql 并可以链接其他mysql服务器

tar xf php-5.4.19.tar.gz
cd php-5.4.19
./configure --prefix=/usr/local/php  --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  --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd--with-openssl :让其能够支持openssl功能
--enable-mbstring :多字节string,支持中文或者是非一个字节能够表示的语言
--with-gd : 支持gd库
--with-freetpye-dir:支持freetype功能,freetype:自由的可移植的字体库,可以实现去引用特定字体的
--with-jpeg-dir:支持jpeg图片
--with-png-dir:支持png图片
--with-zlib:互联网上常用的,通用格式的压缩库,让数据文件先压缩再传送给客户端
--with-libxml-dir:xml(扩展标记语言),现在的很多系统在实现数据交互的时候,都要基于xml来实现,所以要php支持xml,并且让其知道其库文件所在位置
--enable-sockets:让php支持基于套接字的通信
--with-mcrypt:支持加密功能的,额外的加密库
--with-config-file-path :php配置文件的路径放在了什么地方 主配置文件是php.ini
--with-config-file-scan :主配置文件的片段,也是配置文件,这个路径下以.ini结尾的都是配置文件片段
--with-bz2 :压缩库
--enable-maintainer-zts :这一项的使用取决于apache是什么类型的,apache使用的是prefork就不需要;如果使用的是eventmake && make install

  

提供PHP 配置文件

cd php-5.4.19
cp php.ini-production /etc/php.ini

 

修改PATH环境变量

 

cd /etc/profile.d/
vim php-fpm.sh
export PATH=$PATH:/usr/local/php/binsource /etc/profile.d/php-fpm.sh

建立php-fpm服务的配置文件

cd  /usr/local/php/etc 
mv php-fpm.conf. php-fpm.conf
vim php-fpm.conf
listen 192.168.200.202:9000

 

为php-fpm建立服务脚本

 

cd
cd php-5.4.19/sapi/fpm
cp 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

 

安装FastCGI 与 memcached服务链接的软件

 

tar  xf memcache-2.7.7.tgz
cd memcache-2.7.7
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/loca/php/bin/php-config --enable-memcahe

 

加载memcache.so模块

 

mkdir -pv /etc/php.d
vim memcache.ini
extension=memcache.soservice php-fpm restart

  

安装FastCGI 加速opcode代码的软件

tar xf xcache-3.0.3
/uar/local/php/bin/phpize
./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config

 

建立xcache配置文件,加载xcache.so模块

 

cp xcache.ini /etc/php.d/
service php-fpm restart

  

四 memcached服务器的安装

在此采用 yum 安装的方式

yum install memcached
service memcached start

到此为止每个服务器独立安装完成,为使其协同工作,配置如下

1 apache 与 FastCGI

apache服务的设置

vim /etc/httpd/httpd.conf
#DocumentRoot   #注释这行LoadModule proxy_module modules/mod_proxy.so  #开启代理的模块LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so#开启连接fastcgi的模块Include conf/extra/httpd-vhosts.conf #开启让主配置文件载入虚拟主机的配置文件############################################################[root@jie1 ~]# vim /usr/local/apache/conf/extra/httpd-vhosts.conf#####vim  /usr/local/apache/conf/extra/httpd-vhosts.conf#############开启一个虚拟主机即可DocumentRoot   #Apache服务器存放网页的目录ServerName    AllowOverride NoneOptions NoneRequire all grantedProxyRequests Off  #关闭代理请求ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.200.202:9000/web/docs/$1#把接收客户端来着php的请求,转到FastCGI服务器上面去执行,/web/docs是指
#FastCGI服务存放php网页的目录#####################################################################mkdir -pv /web/docs
mkdir: created directory `/web

在PHP 服务器上建立于apache 相同架构的网站文件 .php 文件放在PHP服务器上相应目录即可

 

PHP 服务器的设置

mkdir -p /web/docs
cd /web/docs
vim test.php
infophp()
?>

  

测试 http://192.168.200.201/test.php

2 PHP-FPM 连接memcached

FastCGI安装了连接memcached的软件包memcache且把memcache.so模块装载到php的配置文件中了,这样就实现了PH-FPM(FastCGI)连接Memcached。

建立测试文件

$mem = new Memcache;
$mem->connect("192.168.200.203", 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.";
?>

 3 PHP-FPM 连接mysql

PHP-FPM连接mysql,在编译的时候可以加这三个参数,然后PHP-FPM服务器上也可以不用安装mysql也可以连接mysqld服务器。

--with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd

建立测试文件

$link=mysql_connect('192.168.200.204','root','password');
if($link) echo "mysql test success!!";
else echo "mysql test failed!!";
mysql_close();
?>