编译安装LAMMP架构

一、LAMMP简介

LAMP(Linux+Apache+Mysql+PHP)一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,常被放在一起使用,拥有超高的兼容度,共同组成了一个强大的Web应用程序平台。与Java/J2EE架构相比,LAMP具有Web资源丰富、轻量、快速特点;与微软的.NET架构相比,LAMP具有通用、跨平台、高性能、超稳定的优势。并且该软件开发的项目在软件方面的投资成本较低,因此受到IT界广泛的关注。在LAMMP架构的基础上我们增加了Memcache缓冲服务器,解决了应用程序与数据库之间的瓶颈问题,降低了数据库的访问压力,有效提高了访问速度,这就形成了LAMMP架构。

二、配置环境及架构介绍

系统环境:RHEL6.4-i386

Apachehttpd-2.4.6

aprapr-1.4.6

apr-utilapr-util-1.5.2

Mysqlmysql-5.5.33-linux2.6-i686

PHPphp-5.4.19

Memcachedmemcached-1.4.15

Xcachexcache-3.0.3

libeventlibevent-2.0.21

拓扑图

注释:1客户端向Apache服务器发出请求

              2Apache服务器交给后台php-fpm处理

              3php-fpm向memcache服务器请求数据

              4memcache若缓存的数据就返回给php-fpm,否侧就告诉php-fpm没有其想要的数据,php-fpm若请求到数据,就返回给客户端

               5php-fpm在没有请求到需要的数据时就向数据库请求数据

               6数据库将数据回应给php-fpm

               7php-fpm根据需要决定是否将数据缓存到memcache

               8php-fpm将数据送给Apache服务器

               9Apache服务器将数据回送到客户端

三、编译安装Apache

进程:httpd软件包httpd-2.4.6

注意:httpd-2.4.6依赖于较新版本的apr apr-util

1.编译安装apr

# tar xf apr-1.4.6.tar.bz2
# cd apr-1.4.6
# ./configure --prefix=/usr/local/apr//指定安装路径
# make && make install

2.编译安装apr-until

# 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

     3.解决依赖关系之后安装Apache

   <1>解压源码包,加载模块。

# tar xf httpd-2.4.6.tar.bz2
# cd httpd-2.4.6
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --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=most --with-mpm=event
# make && make install

   <2>修改主配置文件,指定pid文件路径

Vim /etc/httpd/httpd.conf,
添加如下行:
PidFile  "/var/run/httpd.pid"

<3>Apche提供SysV服务脚本

#!/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/httpd/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/usr/local/httpd/apache/logs/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

       <4>赋予脚本执行权限,加入服务列表

#chmod +x /etc/rc.d/init.d/httpd
#chkconfig �Cadd htppd//加入服务列表
#chkconfig httpd on//开机自动启动服务
#chkconfig �Clist httpd //默认2、3、4、5级别下开启

     <5>启动服务,在客户端测试

# Service httpd start

四、通用二进制包安装mysql

    1.准备安装环境

<1>首先创建逻辑卷(逻辑卷创建过程不做演示)

创建目录/mydata/data作为mysql的数据目录

mkdir �Cp /mydata/data

将逻辑卷挂载至/mydata/data

vim /etc/fstab (实现开机自动挂载,不做详细解释)

<2>创建系统用户mysql

useradd �Cr mysql

修改/mydata/data属主、属组均为mysql

chown �CR mysql:mysql /mydata/data

<3>下载mysql通用二进制包,解压至/usr/local,安装mysql

#tar xf mysql-5.5.33-linux2.6-i686.tar.gz  �CC /usr/local
#cd /usr/local
#ln �Csv mysql-5.5.33-linux2.6-i686 mysql
#cd mysq
#chown �CR root:mysql .
#cd support-files//主配置文件样例
# cp my-large.cnf /etc/my.cnf//根据内存选定主配文件512M选择my-large
# vim /etc/my.cnf  修改如下两项
thread_concurrency = 2//处理器核心数
datedir = /mydata/data//数据目录
# cp mysql.server /etc/rc.d/init.d/mysqld  //移动服务脚本
# chkconfig --add mysqld
#chkconfig mysqld on
#scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
#vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
# . /etc/profile.d/mysql.sh

<4>启动服务,并测试

service mysqld start

五、编译安装PHP(PHP-fpm)

    1.解决依赖关系

#yum �Cy groupinstall “X Soltware Development”

#yum �Cy install libxml2-devel bzip2-devel libmcrypt-devel mhash-devel

2.安装PHP

# tar xf php-5.4.19.tar.bz2
# cd php-5.4.19
# ./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
#make && make install
为PHP提供配置文件
# cp php.ini-production /etc/php.ini

 3.配置PHP-fpm

      <1>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

     <2>php-fpm提供配置文件,并修改配置文件。

#cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
修改配置文件
# vim /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//启用PID文件

    <3>启动服务,并验证

# service php-fpm start
# ps aux | grep php-fpm
# netstat -tnlp | grep php-fpm//验证其是否监听在相应的套接字上

六、安装xcache ,为php加速

 1.安装xcache

# tar xf xcache-3.0.1.tar.gz
# cd xcache-3.0.1
# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install
注:安装结束,出现如下行
Installingsharedextensions:   /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

2.编辑php.ini文件,整合phpxcache

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

# mkdir /etc/php.d
# cp xcache.ini /etc/php.d
编辑/etc/php.d/xcache.ini,找到以zend_extension开头的行
vim /etc/php.d/xcache.ini
zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
注:如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位

七、安装memcache插件

#安装php连接memcached缓存服务器插件memcache
# cd /root/memcache-2.2.7
# /usr/local/php/bin/phpize
# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
# make && make install
#在安装结束时会提示如下代码:
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
#在php.ini文件中加入如下代码,装载memcache模块
# vim /etc/httpd24/pam.d/p.ini
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so

八、安装libeventMemcached

memcache依赖于libenent 因此安装mencahce要先安装libevent

1.安装libevent

# tar xf libevent-2.0.21-stable.tar.gz
# cd libevent-2.0.21-stable
# ./configure --prefix=/usr/local/libevent
# make && make install
# ln -sv /usr/local/libevent/include/ /usr/include/libevent
`/usr/include/libevent' -> `/usr/local/libevent/include/'
#echo"/usr/local/libevent/lib/">/etc/ld.so.conf.d/libevent.conf
# ldconf//重新读取库文件

 2.安装mencache

# tar xf memcached-1.4.15.tar.gz
# cd memcached-1.4.15
#./configure--enable-memcache --prefix=/usr/local/memcached --with-php-config=/usr/local/php/bin/php-config
# make && make install

 3.memcached提供SysV服务脚本

#!/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"
IPADDR="172.16.15.15"
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 -l "$IPADDR"
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

 4.为脚本添加执行权限,加入到系统服务并设置为开机自动启动

#chmod +x /etc/rc.d/init.d/mencached
#chkconfig --add mencached
#chkconfig mencached on
#chkconfig --list memcached

 5.启动mencache服务

#service memcached start


你可能感兴趣的:(web架构,lammp)