分离php和mysql搭建LAMP平台
前言:
原先的lamp我们在练习的时候通常在一台计算机上完成的,那么可否将apache、php和mysql分离,如何做到
实验准备:
准备两台机子:
192.168.10.251: 安装mysql
192.168.10.252: 安装apache+php
关闭防火墙,selinux
安装mysql
卸载系统存在的rpm软件包,并建立用户,不允许登录系统
for mysql in `rpm -qa |grep mysql`; do rpm -e --nodeps $mysql;done &> /dev/null
if (id mysql); then userdel -r mysql;useradd -s /sbin/nologin -M mysql;else useradd -s /sbin/nologin ;fi &> /dev/null
解压缩包:
tar xf mysql-5.1.55.tar.gz -C /usr/src/
cd /usr/src/mysql-5.1.55
三把斧:
./configure --prefix=/usr/local/mysql \
--with-charset=utf8 \
--with-extra-charsets=gbk,gb2312 \
--with-collation=utf8_general_ci
make&& make install
拷贝启动模版文件和服务启动进程并给予脚本执行权限
\cp -f support-files/my-medium.cnf /etc/my.cnf
\cp -f support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod a+x /etc/rc.d/init.d/mysqld
初始化数据库:
cd /usr/local/mysql/bin
./mysql_install_db --user=mysql
数据库路径优化:
chown -R root:mysql /usr/local/mysql
chown -R mysql /usr/local/mysql/var/
ln -s /usr/local/mysql/bin/* /usr/local/bin/
ln -s /usr/local/mysql/lib/mysql/* /usr/lib/
ln -s /usr/local/mysql/include/mysql/* /usr/include/
添加开机启动并启动服务:
chkconfig --add mysqld
service mysqld start
进入mysql对apache所在ip给予用户的权限
mysql> grant all on *.* to test@192.168.10.252 identified by ‘123‘;
mysql> flush privileges;
安装apache+php
apache
卸载系统存在的rpm软件包,并建立用户,不允许登录系统
for httpd in `rpm -qa |grep httpd`; do rpm -e $httpd --nodeps; done
解压缩包:
tar xf httpd-2.2.17.tar.gz -C /usr/src/
cd /usr/src/httpd-2.2.17/
三把斧:
./configure --prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset \
--enable-cgi
make && make install
拷贝启动模版文件和服务启动进程并给予脚本执行权限
cd /usr/local/httpd/conf
cp -p httpd.conf httpd.conf.bak
\cp -f /usr/local/httpd/bin/apachectl /etc/rc.d/init.d/httpd
Apache路径优化:
ln -s /usr/local/httpd/bin/* /usr/local/bin/
ln -s /usr/local/httpd/lib/* /usr/local/lib
ln -s /usr/local/httpd/include/* /usr/local/include/
修改启动脚本
sed -i ‘/#!/a \# chkconfig:35 85 25\n# description:a apache server script‘ /etc/init.d/httpd
sed -i ‘/^#ServerName/a \ServerName domain‘ /usr/local/httpd/conf/httpd.conf
添加开机启动并启动服务:
chkconfig --add httpd
service httpd start
php
export LD_LIBRARY_PATH=/usr/local/lib:$ LD_LIBRARY_PATH
cd ~
#源代码安装libmcrypt
tar xf libmcrypt-2.5.8.tar.gz -C /usr/src/
cd /usr/src/libmcrypt-2.5.8/
./configure &&make && make install
ln -s /usr/local/lib/libmcrypt.* /usr/lib/
cd ~
#源代码安装mhash
tar xf mhash-0.9.9.9.tar.gz -C /usr/src/
cd /usr/src/mhash-0.9.9.9/
./configure && make && make install
ln -s /usr/local/lib/libmhash* /usr/lib/
cd ~
#源代码安装mcrypt
tar xf mcrypt-2.6.8.tar.gz -C /usr/src/
cd /usr/src/mcrypt-2.6.8/
./configure&&make && make install
cd ~
mount /dev/sr0 /mnt/
yum install -y libtool
#源代码安装PHP
tar xf php-5.3.6.tar.gz -C /usr/src/
cd /usr/src/php-5.3.6/
./configure --prefix=/usr/local/php5 --with-mcrypt --with-apxs2=/usr/local/httpd/bin/apxs --with-config-file-path=/usr/local/php5 --enable-mbstring
# --with-mysql=/usr/local/mysql 注意此行去掉
make && make install
#复制样例文件为php的主配置文件,并修改配置打开短标签
\cp -f /usr/src/php-5.3.6/php.ini-development /usr/local/php5/php.ini
sed -i ‘/^;default_charset/a \
default_charset="utf8"‘ /usr/local/php5/php.ini
sed -i ‘/^short_open_tag/ s/Off/On/‘ /usr/local/php5/php.ini
#添加ZendGuardLoader模块(优化页面加载速度)
cd ~
tar xf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz -C /usr/src/
cd /usr/src/ZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x/
\cp -f ZendGuardLoader.so /usr/local/php5/lib/php/
cat <<EOF >>/usr/local/php5/php.ini
zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
zend_loader.enable=1
EOF
#调整Apache配置,添加.php支持及目录索引
sed -i ‘/php5_module/a \AddType application/x-httpd-php .php .phml‘ /usr/local/httpd/conf/httpd.conf
sed -i ‘/^[[:space:]]*DirectoryIndex/ s/DirectoryIndex/& index.php/‘ /usr/local/httpd/conf/httpd.conf
#功能测试
cat <<END > /usr/local/httpd/htdocs/test.php
<?php
phpinfo();
?>
END
cat <<END > /usr/local/httpd/htdocs/mysql.php
<?php
\$link=mysql_connect(‘192.168.10.251:/tmp/mysql.sock‘,‘test‘,‘123‘);
if(\$link) echo "mysql Connect Successfully!";
else echo "Connect Faild!";
mysql_close();
?>
END
service httpd restart
当然测试的时候php结合成功了,但是mysql连接失败:因为php还没能连接mysql
注意是通过socket的方式连接
test.php
mysql.php
实现PHP连接MYSQL
安装mysql-devel
yum -y install mysql-devel
利用php的扩展命令phpize对mysql进行支持
cd /usr/src/php-5.3.6/ext/mysql
/usr/local/php5/bin/phpize
利用三把斧生成扩展模块mysql.so并在php.ini配置文件内指定此模块路径
./configure --with-mysql \
--with-php-config=/usr/local/php5/bin/php-config
checking for the location of libz... no
configure: error: Cannot find libmysqlclient under /usr.
Note that the MySQL client library is not bundled anymore!
这个错误时因为在64位的linux系统中,libmysqlclient 默认安装到了 /usr/lib64/mysql/ 目录下,但是php编译时,要去/usr/lib目录下查找
解决办法:在/usr/lib目录下创建一个软连接
ln -s /usr/lib64/mysql/libmysqlclient.so.16 /usr/lib/libmysqlclient.so
make && make install
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don‘t forget to run ‘make test‘.
Installing shared extensions: /usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626/
vim /usr/local/php5/php.ini
Extension=/usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626/mysql.so
重启服务并测试访问
本文出自 “St1sk” 博客,请务必保留此出处http://st1sk.blog.51cto.com/9837690/1691034
原文地址:http://st1sk.blog.51cto.com/9837690/1691034