一 LAMMP 是什么php
L linux ,A Apache,M mysql,M Memcachedhtml
LAMMP 架构图mysql
Apache 相应回复用户html请求linux
FastCGI 把PHP程序执行的结果响应给Apacheweb
Memcache 根据用户请求的动态网页,由程序决定是否须要把数据缓存至Memcache服务器中,Memcache是把数据缓存在内存中sql
Mysql 响应用户查询写入数据数据库
准备环境apache
每一个服务器采用系统centos6.4 x86_64最小化安装,启动网卡,修改主机名,关闭SElinux ,关闭防火墙vim
安装编译环境centos
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.conf PidFile # 定义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 而且使用选项start RETVAL=$? #------定义执行状态返回值 echo [ $RETVAL = 0 ] && touch ${lockfile} #-----成功时建立锁文件 $RETVAL } stop() { echo -n $ killproc -p ${pidfile} -d 10 $httpd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} } reload() { echo -n $ ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/; then RETVAL=$? echo $failure $killproc -p ${pidfile} $httpd -HUP RETVAL=$? fi echo } # See how we were called. start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart) [ -f ${pidfile} ] ; then stop start fi ;; reload) reload ;; graceful|help|configtest|fullstatus) $apachectl RETVAL=$? ;; *) echo $ exit 1 esac exit $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/bin source /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/bin source /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/lib ldconfig
建立登录数据库用户和密码
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就不须要;若是使用的是event make && 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/bin source /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.so service 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############ #开启一个虚拟主机便可 <VirtualHost *:80> DocumentRoot #Apache服务器存放网页的目录 ServerName <Directory > AllowOverride None Options None Require all granted </Directory> ProxyRequests Off #关闭代理请求 ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.200.202:9000/web/docs/$1 #把接收客户端来着php的请求,转到FastCGI服务器上面去执行,/web/docs是指 #FastCGI服务存放php网页的目录 </VirtualHost> ##################################################################### mkdir -pv /web/docs mkdir: created directory `/web
在PHP 服务器上创建于apache 相同架构的网站文件 .php 文件放在PHP服务器上相应目录便可
PHP 服务器的设置
mkdir -p /web/docs cd /web/docs vim test.php <?php infophp() ?>
测试 http://192.168.200.201/test.php
2 PHP-FPM 链接memcached
FastCGI安装了链接memcached的软件包memcache且把memcache.so模块装载到php的配置文件中了,这样就实现了PH-FPM(FastCGI)链接Memcached。
创建测试文件
<?php $mem = new Memcache; $mem->connect("192.168.200.203", 11211) or die("Could not connect"); $version = $mem->getVersion(); echo "Server's version: ".$version."<br/>\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)<br/>\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
创建测试文件
<?php $link=mysql_connect('192.168.200.204','root','password'); if($link) echo "mysql test success!!"; else echo "mysql test failed!!"; mysql_close(); ?>