標籤:mariadb 獨立 lamp php mysql
實驗環境:
VM1:192.168.1.132,用於apache伺服器
VM2:192.168.1.134,用於php伺服器
VM3:192.168.1.137,使用者mariadb伺服器
軟體版本:httpd-2.4.9,php-5.4.26,mariadb-5.5.39
一、編譯安裝httpd-2.4.9
1、解決依賴關係
# yum -y install pcre-devel
# yum -y install mod_ssl
# yum groupinstall -y "Development tools"
# yum groupinstall -y "Server Platform Development"
編譯安裝apr-1.5.0.tar.bz2
# tar xf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr
# make && make install
編譯安裝apr-until-1.5.3.tar.bz2
# tar xf apr-util-1.5.3.tar.bz2
# cd apr-util-1.5.3
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install
2、編譯安裝httpd-2.4.9
# tar xf httpd-2.4.9.tar.bz2
# cd httpd-2.4.9
# ./configure --prefix=/usr/local/apache --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
# make && make install
3、修改httpd設定檔
啟用fcgi模組
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/46/31/wKioL1PuGtvhXD7rAAEYzLEf40E632.jpg" title="2014-08-15 22_30_16-root@httpd__mnt - Xshell 4.jpg" alt="wKioL1PuGtvhXD7rAAEYzLEf40E632.jpg" />
在設定檔中添加如下語句讓apache支援php格式的頁面,並支援php格式的首頁
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/46/2F/wKiom1PuGhjwqUa7AABulSjX1MI739.jpg" title="2014-08-15 22_31_55-root@httpd__mnt - Xshell 4.jpg" alt="wKiom1PuGhjwqUa7AABulSjX1MI739.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/46/2F/wKiom1PuHQ6jThCmAAJdxjUiydA874.jpg" title="2014-08-15 22_44_30-root@httpd__mnt - Xshell 4.jpg" alt="wKiom1PuHQ6jThCmAAJdxjUiydA874.jpg" />
關閉正向 Proxy,啟用反向 Proxy,添加語句,fcgi:後面的IP地址是php應用伺服器的IP地址,/var/phpscript是php伺服器中用於存放php分頁檔的目錄位置,使用者在向前端的apache伺服器請求php頁面時,httpd進程會將使用者請求反向 Proxy至php伺服器對應的目錄下的分頁檔,經過php解譯器處理後返回給前端apache伺服器html頁面格式的檔案。
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.1.134:9000/var/phpscript/$1
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/46/2F/wKiom1PuGruTcQJIAABx3VkjLzA665.jpg" title="2014-08-15 22_34_30-root@httpd__mnt - Xshell 4.jpg" alt="wKiom1PuGruTcQJIAABx3VkjLzA665.jpg" />
設定Pid檔案路徑,這個條目是為了使用apache服務指令碼而設定的。
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/46/2F/wKiom1PuHZ3RNyCDAAAkfZHjDds028.jpg" title="2014-08-15 22_46_49-root@httpd__mnt - Xshell 4.jpg" alt="wKiom1PuHZ3RNyCDAAAkfZHjDds028.jpg" />
4、提供SysV服務指令碼,在/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/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 [email protected]
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/httpd24
添加服務列表:
# chkconfig --add httpd24
# 啟動服務:
# service httpd24 start
apache伺服器準備完畢。
二、編譯安裝php-5.4.26
1、解決依賴關係:
# yum groupinstall -y "Desktop Platform Development"
# yum -y install bzip2-devel libmcrypt-devel
2、編譯安裝php-5.4.26
# tar xf php-5.4.26.tar.bz2
# cd php-5.4.26
# ./configure --prefix=/usr/local/php5 --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --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
資料庫伺服器是另一台單獨主機,所以編譯php時指定mysql的安裝位置時,需要設定為mysqlnd,同時顯式指出使用fpm模組
# make && make install
為php提供設定檔:
# cp php.ini-production /etc/php.ini
3、配置php-fpm
為php-fpm提供SysV服務指令碼,並將其添加至服務列表
# 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/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
修改php-fpm的設定檔:
# vim /usr/local/php5/etc/php-fpm.conf
啟用pid檔案,該選項的啟用位置在全域配置段下方,並修改php-fpm監聽的地址,原監聽地址是127.0.0.1,需要改為可以與外部通訊的地址,設定:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/46/30/wKiom1PuImLh_oBZAADpt3J1d3E799.jpg" title="2014-08-15 23_07_11-root@php__usr_local_php5_etc - Xshell 4.jpg" alt="wKiom1PuImLh_oBZAADpt3J1d3E799.jpg" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/46/32/wKioL1PuJAfSXFOFAAGTa4plD8A207.jpg" title="2014-08-15 23_09_06-root@php__usr_local_php5_etc - Xshell 4.jpg" alt="wKioL1PuJAfSXFOFAAGTa4plD8A207.jpg" />
啟動服務:
# service php-fpm start
查看服務監聽的連接埠:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/46/32/wKioL1PuJIvQYeiJAACPMukSCa8343.jpg" title="2014-08-15 23_11_44-root@php__usr_local_php5_etc - Xshell 4.jpg" alt="wKioL1PuJIvQYeiJAACPMukSCa8343.jpg" />
可以看到php-fpm工作在9000連接埠,監聽的地址是192.168.1.134。
建立php頁面儲存目錄,存放php分頁檔。
# mkdir /var/phpscript
# vim /var/phpscript/index.php;編輯php分頁檔,內容如:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/46/32/wKioL1PuLjTyBe9mAADRws0kAc0042.jpg" title="2014-08-15 23_52_55-root@php__var_phpscript - Xshell 4.jpg" alt="wKioL1PuLjTyBe9mAADRws0kAc0042.jpg" />
php應用程式伺服器準備完畢。
三、安裝mariadb-5.5.39
1、準備資料存放的檔案系統,建立邏輯卷,用於資料庫存放資料。
建立一個磁碟分割,分區格式為LVM:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/46/30/wKiom1PuJaHjdgAjAAESRSEN8xk174.jpg" title="2014-08-15 23_21_03-root@database__shared_nfs - Xshell 4.jpg" alt="wKiom1PuJaHjdgAjAAESRSEN8xk174.jpg" />
# kpartx -af /dev/sda;通知核心重讀磁碟分割表
# partx -a /dev/sda;通知核心重讀磁碟分割表
# cat /proc/partitions;查看核心識別分區情況
# pvcreate /dev/sda3
# vgcreate myvg /dev/sda3
# lvcreate -L 10G -n mylv myvg
# lvs;查看lv建立結果
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/46/30/wKiom1PuJnKiazTDAAEDgb6-EmI820.jpg" title="2014-08-15 23_24_30-root@database__shared_nfs - Xshell 4.jpg" alt="wKiom1PuJnKiazTDAAEDgb6-EmI820.jpg" />
# mke2fs -t ext4 /dev/myvg/mylv
# mkdir /mydata/data;建立用於儲存資料庫資料的目錄
# vim /etc/fstab;編輯fstab檔案,將/dev/myvg/mylv掛載至/mydata/data
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/46/32/wKioL1PuKFqCsdr0AAHB1sb5OEU907.jpg" title="2014-08-15 23_27_56-root@database__shared_nfs - Xshell 4.jpg" alt="wKioL1PuKFqCsdr0AAHB1sb5OEU907.jpg" />
# mount -a
2、建立用於以安全方式運行進程
# groupadd -r mysql
# useradd -g mysql -s /sbin/nologin -r mysql
# chown -R mysql:mysql /mydata/data
3、安裝初始化MariaDB-5.5.39
# tar xf mariadb-5.5.39-linux-x86_64.tar.gz
# cd /usr/local
# ln -sv mariadb-5.5.39-linux-x86_64 mysql
# cd mysql
# chown -R mysql:mysql .
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# chown -R root .
4、為mariadb提供主設定檔
# cd /usr/local/mysql
# cp support-files/my-large.cnf /etc/my.cnf
# vim /etc/my.cnf;修改資料庫主設定檔,添加資料存放的路徑:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/46/32/wKioL1PuKdPg2bvOAAGKlYYDr84534.jpg" title="2014-08-15 23_34_18-root@database_~ - Xshell 4.jpg" alt="wKioL1PuKdPg2bvOAAGKlYYDr84534.jpg" />
5、為MariaDB提供服務指令碼
# cd /usr/local/mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chown +x /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
6、mariadb安裝完成後續的配置工作:
輸出mariadb的man手冊,新增內容
# vim /etc/man.config
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/46/32/wKioL1PuKo2gNTfZAACU-UTQLwQ854.jpg" title="2014-08-15 23_37_26-root@database_~ - Xshell 4.jpg" alt="wKioL1PuKo2gNTfZAACU-UTQLwQ854.jpg" />
輸出mariadb標頭檔
# ln -sv /usr/local/mysql/include /usr/include/mysql
輸出mariadb的庫檔案:
# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf
# ldconfig;系統重新裝載庫檔案
修改PATH環境變數,使系統可以直接使用mariadb相關命令
# vim /etc/profile.d/mysql.sh
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/46/32/wKioL1PuK9aQ9_ykAACHXMGDtM8273.jpg" title="2014-08-15 23_42_55-root@database_~ - Xshell 4.jpg" alt="wKioL1PuK9aQ9_ykAACHXMGDtM8273.jpg" />
串連mysql,建立遠端連線使用者,用於測試資料庫連接:
# mysql
# CREATE USER [email protected]‘192.168.1.%‘ IDENTIFIED BY ‘tpass‘;
現在三台主機準備完畢,在apache伺服器上添加靜態頁面內容,在php伺服器上添加php分頁檔用於測試:
在apache伺服器的網站根目錄下建立一個名稱為test.html的分頁檔,編輯的內容如:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/46/30/wKiom1PuK-SQ_um0AABaWvHmaSg589.jpg" title="2014-08-15 23_46_29-root@httpd__usr_local_apache_htdocs - Xshell 4.jpg" alt="wKiom1PuK-SQ_um0AABaWvHmaSg589.jpg" />
在瀏覽器內測試效果如下:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/46/32/wKioL1PuLUPQlMQCAAE-Lf9wEBY641.jpg" title="2014-08-15 23_47_43-http___192.168.1.132_test.html.jpg" alt="wKioL1PuLUPQlMQCAAE-Lf9wEBY641.jpg" />
測試php頁面訪問及資料庫的串連情況:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/46/32/wKioL1PuL4fhQhhpAAFSBsO6FsY975.jpg" title="2014-08-15 23_58_34-http___192.168.1.132_index.php.jpg" alt="wKioL1PuL4fhQhhpAAFSBsO6FsY975.jpg" />