分離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 [email protected] 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-gd --enable-gd-native-ttf --enable-gd-jis-conv
如遇到以下錯誤和解決方案如下
configure: error: libpng.(a|so) not found.
yum install libpng libpng-devel
ln -s /usr/lib64/libjpeg.so /usr/lib/
ln -s /usr/lib64/libpng.so /usr/lib/
# --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
重啓服務並測試訪問
----------------------------------------------------------------------------------------------------------------------------------
補充說明:因爲在之前我部署網站的時候發現如下圖:(上文中已經改好失誤)
是因爲在配置php的時候沒有加入gd庫的支持,朋友們配置時候可以自行添加如下的三行即可解決
--with-gd \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
並在編譯的時候報錯,上面已經有說明:再次強調下
如遇到以下錯誤和解決方案如下
yum install libpng libpng-devel
configure: error: libpng.(a|so) not found.
[root@www php-5.3.6]# ln -s /usr/lib64/libjpeg.so /usr/lib/
[root@www php-5.3.6]# ln -s /usr/lib64/libpng.so /usr/lib/
完事後自可解決