#######################################php
LAMP平台相关组件功能简述css
编译安装LAMP平台html
编译安装LAMP之以FastCGI方式与php整合mysql
使用ab命令进行压力测试linux
#######################################LAMP平台相关组件功能简述
web
1、httpdsql
做为一款web服务器自己它仅能处理静态请求和内容,例如:html、css、图片等……apache
2、php编程
PHP是脚本编程语言,php解析器,其内部的Zend引擎将执行分为两段:vim
第一段:词法分析、语法分析
用户第一次访问PHP页面时候动态编译为opcode
第二段:执行opcode。
3、xcache
每一个用户请求php都会生成一个进程用于处理和回应,因为进程之间没法共享opcode,opcode在内存空间中,因此当进程消亡时opcode会随之丢失,在实际生成环境中有大量用户请求目标是彻底相同的,若是每一个请求都须要动态生成opcode这不只是一种巨大的资源浪费也会延长对用户请求的响应时间,所以为了使进程之间能共享opcode,引用缓存器例如:APC、eAccelerator、XCache。
4、php如何与mysql进行交互
php解释器(模块)自己仅具有解释php脚本的功能,真正跟mysql交互的是PHP程序,由此程序驱动访问mysql(php53-mysql)。
5、httpd与php的交互方式
CGI,这种方式把php直接编译成httpd的模块,每一个httpd请求进程对应一个php(cgi)进程,httpd动态建立进程须要时间,而且额外的进程会占用资源开销,进程间通讯的结果返回httpd进程,httpd负责销毁进程。由httpd负责子进程的建立和回收。
PHP_MOD,这种方式php做为http进程内部模块运行,须要时候动态加载模块。
fastcgi(httpd做为客户端想php服务器发起请求,php服务器内部管理和销毁进程)fpm,监听在一个套接字上,Daemon进程。
编译安装LAMP平台
1、准备环境、所需软件包版本、安装顺序。
平台基于redhat5.8
httpd所需软件包:
apr-1.4.6.tar.bz2
apr-util-1.4.1.tar.bz2
httpd-2.4.3.tar.bz2
mysql所需软件包:
mysql-5.5.28-linux2.6-i686.tar.gz(通用二进制格式)
php所需软件包:
libmcrypt-2.5.7-5.el5.i386.rpm
libmcrypt-devel-2.5.7-5.el5.i386.rpm
mhash-0.9.2-6.el5.i386.rpm
mhash-devel-0.9.2-6.el5.i386.rpm
php-5.4.13.tar.bz2
xcache所需软件包:
xcache-2.0.0.tar.bz2
phpMyAdmin所需软件包:
phpMyAdmin-3.5.1-all-languages.tar.bz2
安装顺序 httpd --> mysql --> php
2、编译安装httpd-2.4.4
一、解决依赖关系
##########################安装apr########################## tar xf apr-1.4.6.tar.bz2 ./configure --prefix=/usr/local/apr make && make install ##########################安装apr-util##################### tar xf apr-util-1.4.1.tar.bz2 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr make && make install ##########################安装pcre-devel################### yum install pcre-devel
二、编译安装httpd-2.4.4
tar xf httpd-2.4.4.tar.bz2 ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-rewirte --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util make && make install
三、修改Pid文件的路径以迎合启动脚本
vim /etc/httpd/httpd.conf PidFile "/var/run/httpd.pid" //添加以下行
四、提供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 chkconfig --level 35 httpd on chkconfig --list httpd
六、为命令添加搜索路径
vim /etc/profile.d/httpd.sh export PATH=$PATH:/usr/local/apache/bin //加入本行内容
七、修改mpm_module为prefork、检查语法并重启服务
vim /etc/httpd/httpd.conf #LoadModule mpm_event_module modules/mod_mpm_event.so 注释 LoadModule mpm_prefork_module modules/mod_mpm_prefork.so 添加 httpd -t 检查语法 service httpd restart 重启服务
至此httpd安装完成:
3、安装mysql-5.5.28
一、建立mysql用户和mysql组
groupadd -r -g 306 mysql useradd -g 306 -r -u 306 mysql
二、建立连接文件
tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local cd /usr/local ln -sv mysql-5.5.28-linux2.6-i686 mysql chown -R mysql.mysql /usr/local/mysql/*3
三、为数据目录建立lvm
fdisk /dev/sdb 略过步骤 partprobe /dev/sdb 检测 pvcreate /dev/sdb1 建立物理块 vgcreate myvg /dev/sdb1 建立卷组 lvcreate -n mydata -L 5G myvg 建立逻辑卷 lvs mke2fs -j /dev/myvg/mydata 格式化 mkdir /mydata vim /etc/fstab 添加开机自启动 /dev/myvg/mydata /mydata ext3 defaults 0 0 mount -a 挂载 mount mkdir /mydata/data 建立数据目录 chown -R mysql.mysql /mydata/data/ 修改数据目录属主与属组 chmod o-rx /mydata/data/ 修改其余人对此目录权限
四、初始化mysql
scripts/mysql_install_db --user=mysql --datadir=/mydata/data/ chown -R root /usr/local/mysql/*
五、添加启动脚本、主配置文件、命令路径、库文件、头文件
#########################添加启动脚本######################## cp support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld ########################添加主配置文件####################### cp support-files/my-large.cnf /etc/my.cnf vim /etc/my.cnf datadir = /mydata/data 定义数据目录位置 #########################添加命令############################ vim /etc/profile.d/mysql.sh export PATH=$PATH:/usr/local/mysql/bin //添加本行内容 ########################添加库文件########################### echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf ########################添加头文件########################### ln -sv /usr/local/mysql/include /usr/include/mysql
至此mysql安装完成:
3、安装php-5.4.13(php做为httpd模块运行)
一、解决依赖
rpm -ivh libmcrypt-2.5.7-5.el5.i386.rpm libmcrypt-devel-2.5.7-5.el5.i386.rpm mhash-0.9.2-6.el5.i386.rpm mhash-devel-0.9.2-6.el5.i386.rpm
二、编译安装php-5.4.13
tar xf php-5.4.13 ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-opensll --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 #--enable-maintainer-zts(event worker基于线程工做类型的时候须要此选项) make && make install cp php.ini-production /etc/php.ini 建立配置文件
三、结合php和httpd
vim /etc/httpd/httpd.conf DirectoryIndex index.php index.html //添加index.php AddType application/x-httpd-php .php //添加本行内容 AddType application/x-httpd-php-source .phps //添加本行内容
四、测试php是否正常工做
vim /usr/local/apache/htdocs/index.php <?php phpinfo(); ?>
五、测试php是否能正常访问mysql
<?php $conn=mysql_connect('localhost','root',''); if($conn) echo "Success...."; else echo "Failure...."; ?>
4、编译安装xcache-2.0.0
一、编译安装xcache
tar xf xcache-2.0.0.bz2 cd xcacge /usr/local/php/bin/phpize 准备好php扩展编译以备编译,让扩展自己识别php ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config make && make install
2、整合php和xcache
mkdir /etc/php.d cp xcache.ini /etc/php.d vim /etc/php.d/xcache.ini zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so 复制安装完成的内容后加xcache.so
重启httpd,至此xcache安装完成:
5、httpd2.4.4虚拟主机配置
1、中心主机和虚拟主机不能共存
注释中心主机 #DocumentRoot "/usr/local/apache/htdocs" 开启虚拟主机选项 # Virtual hosts Include /etc/httpd/extra/httpd-vhosts.conf
2、建立虚拟主机 vim /etc/httpd/extra/httpd-vhosts.conf
<VirtualHost *:80> ServerName www.soulboy.com DocumentRoot "/www/soulboy.com" <Directory "/www/soulboy.com"> 如不声明目录权限则默认拒绝 Options none AllowOverride none Require all granted </Directory> ErrorLog "/var/log/httpd/soulboy.com-error_log" CustomLog "/var/log/httpd/soulgirl.com-error_log" combined </VirtualHost> <VirtualHost *:80> ServerName www.soulgirl.com DocumentRoot "/www/soulgirl.com" <Directory "/www/soulgirl.com"> Options none AllowOverride none Require all granted </Directory> ErrorLog "/var/log/httpd/soulgirl.com-error_log" CustomLog "/var/log/httpd/soulgirl.com-error_log" common </VirtualHost>
3、测试虚拟主机是否生效
5、部署phpMyAdmin-3.5.1
1、加压到虚拟主机目录并从新修改目录名,并建立和修改配置文件
tar xf phpMyAdmin-3.5.1-all-languages.tar.bz2/ -C /www/soulgirl.com cd /www/soulgirl.com mv phpMyAdmin-3.5.1-all-languages / pma 修改目录名方便访问 cd pma cp config.sample.inc.php config.inc.php 建立配置文件 penssl rand -base64 10 生成10位的随机数 vim config.inc.php 把生成的随机数复制到配置文件中 $cfg['blowfish_secret'] = 'QkMTw3ZlpJlBKA'
2、修改mysql用户密码
mysqladmin -uroot password 'redhat'
3、测试phpMyAdmin部署是否正常
至此说明php和httpd工做正常:
至此说明mysql工做正常:
编译安装LAMP之以FastCGI方式与php整合
1、编译安装httpd-2.4.4
安装步骤同上
2、mysql-5.6.10
安装步骤同上
3、编译安装php-5.4.13
1、解决依赖关系
yum -y groupinstall "X Software Development" rpm -ivh 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
2、编译安装php-5.4.13
tar xf php-5.4.13.tar.bz2 cd php-5.4.13 ./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 intall cp php.ini-production /etc/php.ini 为php提供配置文件:
3、配置php-fpm
###################为php-fpm添加脚本####################### 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 = 5 最小空闲进程数 pm.max_spare_servers = 8 最大空闲进程数 pid = /usr/local/php/var/run/php-fpm.pid PidFile路径 #################启动php-fpm############################## service php-fpm start ps aux | grep php-fpm netstat -tnlp | grep php-fpm 默认状况下,fpm监听在127.0.0.1的9000端口,也可使用以下命令验正其是否已经监听在相应的套接字。
4、配置httpd-2.4.4
1、配置php-fpm
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so 在Apache httpd 2.4之后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它实际上是做为mod_proxy.so模块的扩充,所以,这两个模块都要加载2
2、配置httpd识别php页面
vim /etc/httpd/httpd.conf 一、添加以下二行 AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps 二、定位至DirectoryIndex index.html 修改成: DirectoryIndex index.php index.html 三、注释中心主机 #DocumentRoot "/usr/local/apache/htdocs" 四、删除注释,开启支持虚拟主机功能 # Virtual hosts Include /etc/httpd/extra/httpd-vhosts.conf
3、配置虚拟主机支持使用fcgi
ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1
4、配置虚拟主机支持fastcgi
<VirtualHost *:80> DocumentRoot "/www/soulboy" ServerName www.soulboy.com ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/soulboy/$1 <Directory "/www/soulboy"> Options none AllowOverride none Require all granted </Directory> ErrorLog "logs/dummy-host.example.com-error_log" CustomLog "logs/dummy-host.example.com-access_log" common </VirtualHost>
5、测试以上配置是否正常
<h1>www.soulboy.com</h1> <?php $conn=mysql_connect('localhost','root','redhat'); if($conn) echo "Success...."; else echo "Failure...."; phpinfo(); ?>
至此说明一切正常:
5、编译安装xcache-3.0.1
tar xf xcache-3.0.1.tar.bz2 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 mkdir /etc/php.d/ cp xcache.ini /etc/php.d/ service php-fpm restart
使用ab命令进行压力测试
1、安装xcache以前
ab -c 100 -n 1000 http://www.soulboy.com/pma/index.php
2、安装xcache以后(以一样的并发量测试)
ab -c 100 -n 1000 http://www.soulboy.com/pma/index.php
总结:xcache做用尤其明显,大大节约了资源的浪费。