首页 > 代码库 > LAMMP源码环境搭建
LAMMP源码环境搭建
一、LAMMP环境简介
LAMMP是linux、Apache、Mysql、Memcahed、PHP的首字母缩写
LAMMP网络拓扑图(待补)
工作模式:
apache响应回复用户html请求并转发php程序给FastCGI
FastCGI把php程序执行结果响应给apache
mysql响应用户的数据的写入和查询
memcached根据用户请求的程序决定是否需要memcached服务器将数据缓存至内存中
系统环境:
LAMMP分别搭建在4台CentOS6.4.x86_64服务器上并且安装好编译环境
地址规划:
memcached 192.168.1.105
apache 192.168.1.106
php 192.168.1.107
mysql 192.168.1.108
二、Apache源码安装
安装apr
[root@www ~]# tar xf apr-1.4.6.tar.bz2 [root@www apr-1.4.6]# ./configure --prefix=/usr/local/apr [root@www apr-1.4.6]# make && make install
安装apr-util
[root@www ~]# tar xf apr-util-1.5.2.tar.bz2 [root@www apr-util-1.5.2]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [root@www apr-util-1.5.2]# make && make install
yum方式安装pcre-devel
[root@www ~]# yum install pcre-devel
安装httpd
[root@www ~]# tar xf httpd-2.4.6.tar.bz2 [root@www httpd-2.4.6]# ./configure --prefix=/usr/local/apache24 --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 [root@www httpd-2.4.6]# make && make install
为httpd提供服务脚本
[root@www ~]# vim /etc/rc.d/init.d/httpd24
#!/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/apache24/bin/apachectl httpd=${HTTPD-/usr/local/apache24/bin/httpd} prog=httpd pidfile=${PIDFILE-/usr/local/apache24/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
为脚本赋予执行权限
[root@www ~]# chmod +x /etc/rc.d/init.d/httpd24
添加环境变量
[root@www ~]# vim /etc/profile.d/httpd24.sh export PATH=/usr/local/apache24/bin:$PATH #添加此行
设置开机启动
[root@www ~]# chkconfig --add httpd24 [root@www ~]# chkconfig httpd24 on
修改主机名为www.xiaoya.net
启动服务
[root@www ~]# service httpd24 start
浏览器访问测试一下
显示 It works!
httpd服务成功启动
三、通用二进制格式安装mysql
生产环境中通常将数据库文件存放在逻辑卷(LVM)上
新建逻辑卷并挂载在/mydata目录上(创建过程省略...)
新建用户mysql并让其对/mydata/data有属主属组权限
[root@mysql ~]# mkdir /mydata/data [root@mysql ~]# groupadd -r mysql [root@mysql ~]# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql [root@mysql ~]# chown -R mysql:mysql /mydata/data
查看当前系统是否在监听3306端口,有则关闭
将二进制包解压至/usr/local下
[root@mysql ~]# tar xf mysql-5.5.38-linux2.6-x86_64.tar.gz -C /usr/local/ [root@mysql local]# ln -sv mysql-5.5.38-linux2.6-x86_64 mysql #创建链接 [root@mysql local]# chown -R root:mysql /usr/local/mysql/* #修改mysql下所有文件属组为mysql
建立并修改配置文件
[root@mysql support-files]# cp my-huge.cnf /etc/my.cnf [root@mysql support-files]# vim /etc/my.cnf thread_concurrency = 4 #修改为物理核心数的2倍 datadir = /mydata/data #添加此行
建立服务脚本
[root@mysql support-files]# cp mysql.server /etc/rc.d/init.d/mysqld
启动服务之前先初始化
[root@mysql mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data #指定用户和数据目录
现在可以启动服务了
[root@mysql ~]# service mysqld start [root@mysql ~]# chkconfig --add mysqld [root@mysql ~]# chkconfig mysqld on
导出man文档
[root@mysql ~]# vim /etc/man.config MANPATH /usr/local/mysql/man #添加此行
导出头文件
[root@mysql ~]# ln -sv /usr/local/mysql/include /usr/include/mysql
导出库文件
[root@mysql ~]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf [root@mysql ~]# ldconfig
添加mysql环境变量
[root@mysql ~]# vim /etc/profile.d/mysql.sh [root@mysql ~]# . /etc/profile.d/mysql.sh export PATH=/usr/local/mysql/bin:$PATH
成功登录mysql
[root@mysql ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.38-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql>
四、PHP源码安装
安装libxml
[root@php ~]# yum -y install libxml2-devel
安装bzip2
[root@php ~]# yum -y install bzip2-devel
安装mcrypt(yum默认源没有此安装包,需要额外源)
我这里是直接下载到本机用rpm安装
[root@php ~]# rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm [root@php ~]# rpm -ivh libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
解压安装php
[root@php ~]# tar xf php-5.4.30.tar.gz [root@php php-5.4.30]# ./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-fpm --enable-sockets --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
使用PHP.5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了 (使用--with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd在本地服务器可以不用安装mysql)
[root@php php-5.4.30]# make && make install
建立php配置文件
[root@php php-5.4.30]# cp php.ini-production /etc/php.ini
建立php-fpm服务脚本
[root@php fpm]# cp init.d.php-fpm /etc/rc.d/init.d/php-fpm [root@php ~]# chmod +x /etc/rc.d/init.d/php-fpm #赋予执行权限
建立并修改php-fpm配置文件
[root@php etc]# cp php-fpm.conf.default php-fpm.conf [root@php etc]# vim php-fpm.conf listen = 192.168.1.107:9000 #修改
添加开机启动
[root@php ~]# chkconfig --add php-fpm [root@php ~]# chkconfig php-fpm on
启动php-fpm
[root@php ~]# service php-fpm start
安装xcache
[root@php ~]# tar xf xcache-3.0.3.tar.bz2 [root@php ~]# cd xcache-3.0.3 [root@php xcache-3.0.3]# /usr/local/php/bin/phpize [root@php xcache-3.0.3]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-xcache [root@php xcache-3.0.3]# make && make install
安装结束后出现如下行
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
整合xcache和php
将xcache提供的样例配置导入php.ini
[root@php xcache-3.0.3]# mkdir /etc/php.d [root@php xcache-3.0.3]# cp xcache.ini /etc/php.d/
修改xcache.ini
[root@php ~]# vim /etc/php.d/xcache.ini extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so
五、整合Apache和PHP
1.httpd配置
[root@www ~]# vim /etc/httpd24/httpd.conf 注释掉DocumentRoot "/usr/local/apache24/htdocs" 启用mod_proxy.so和mod_proxy_fcgi.so模块 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
载入虚拟主机配置文件
Include /etc/httpd24/extra/httpd-vhosts.conf
让apache能识别php格式的页面,并支持php格式的主页
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
设置index.php为默认主页
DirectoryIndex index.php index.html
配置虚拟主机
[root@www ~]# vim /etc/httpd24/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/web/htdocs" ServerName www.xiaoya.net ProxyRequests Off #关闭正向代理 ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.1.107:9000/web/htdocs/$1(fastcgi服务存放php文件的目录,需要在php服务器上建立) <Directory "/web/htdocs"> Options none AllowOverride none Require all granted </Directory> </virtualHost>
#把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://192.168.1.107:9000后指明了这两个参数,其它的参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。
2.PHP设置
[root@php ~]# mkdir -pv /web/htdocs
建立测试网页文件index.php
[root@php ~]# vim /web/htdocs/index.php <?php phpinfo(); ?>
本地配置好hosts在浏览器输入www.xiaoya.net 测试
上图显示PHP工作正常并且已成功整合xcache
测试PHP与mysql连接
[root@php ~]# vim /web/htdocs/sqltest.php <?php $link=mysql_connect(‘192.168.1.108‘,‘root‘,‘‘ ); if ($link) echo "Success..."; else echo "Failure..." ?>
浏览器输入http://192.168.1.106/sqltest.php
成功连接mysql
六、源码安装memcached
先安装libevent
[root@mem ~]# tar xf libevent-2.0.21-stable.tar.gz [root@mem libevent-2.0.21-stable]# ./configure --prefix=/usr/local/libevent [root@mem libevent-2.0.21-stable]# make && make install
导出库文件
root@mem ~]# vim /etc/ld.so.conf.d/libevent.conf /usr/local/libevent/lib #添加此行 [root@mem ~]# ldconfig
安装memcached
[root@mem ~]# tar xf memcached-1.4.15.tar.gz [root@mem ~]# cd memcached-1.4.15 [root@mem memcached-1.4.15]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent [root@mem memcached-1.4.15]# make && make install
提供服务脚本
root@mem bin]# vim /etc/rc.d/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
[root@mem bin]# chmod +x /etc/rc.d/init.d/memcached [root@mem bin]# service memcached start
七、整合Memcached和PHP
安装memcache
[root@php ~]# tar xf memcache-2.2.7.tgz [root@php ~]# cd memcache-2.2.7 [root@php memcache-2.2.7]# /usr/local/php/bin/phpize [root@php memcache-2.2.7]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache [root@php memcache-2.2.7]# make && make install
建立memcache.ini文件
[root@php memcache-2.2.7]# vim /etc/php.d/memcache.ini extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
重启php-fpm服务
[root@php memcache-2.2.7]# service php-fpm restar
测试Memcached和PHP连接,建立测试文件test.php
[root@php ~]# vim /web/htdocs/test.php <?php $mem = new Memcache; $mem->connect("192.168.1.105", 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."; ?>
浏览器输入http://192.168.1.106/test.php 测试
memcached服务器上查看
[root@mem bin]# telnet 192.168.1.105 11211 Trying 192.168.1.105... Connected to 192.168.1.105. Escape character is ‘^]‘. get hellokey VALUE hellokey 0 11 Hello World END
以上信息表明emcached工作正常
-----------------------------LAMMP环境全部搭建完毕。