基於fcgi的LAMP試驗環境及其原理
基於fcgI編譯安裝LAMP實現層次化
一:二進制源碼安裝Mysql
1.將數據存儲在基於Raid5的邏輯卷中;
創建基於raid5的邏輯卷
1.1將磁盤格式化爲raid卷
# fdisk /dev/sdb
WARNING: DOS-compatible mode isdeprecated. It's strongly recommended to
switchoff the mode (command 'c') and change display units to
sectors (command 'u').
Command (m forhelp): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (5224-15665, default5224):
Using defaultvalue 5224
Last cylinder, +cylinders or +size{K,M,G} (5224-15665, default15665): +10G
Command (m forhelp): n
Command action
e extended
p primary partition (1-4)
e
Selected partition 4
First cylinder (6530-15665, default6530):
Using defaultvalue 6530
Last cylinder, +cylinders or +size{K,M,G} (6530-15665, default15665): +20G
Command (m forhelp): n
First cylinder (6530-9141, default6530):
Using defaultvalue 6530
Last cylinder, +cylinders or +size{K,M,G} (6530-9141, default9141): +10G
Command (m forhelp): n
First cylinder (7836-9141, default7836):
Using defaultvalue 7836
Last cylinder, +cylinders or +size{K,M,G} (7836-9141, default9141): +10G
Command (m forhelp): t
Partition number (1-6): 3
Hex code (type L to list codes): fd
Changed system type of partition 3to fd (Linux raid autodetect)
Command (m forhelp): t
Partition number (1-6): 5
Hex code (type L to list codes): fd
Changed system type of partition 5to fd (Linux raid autodetect)
Command (m forhelp): t
Partition number (1-6): 6
Hex code (type L to list codes): fd
Changed system type of partition 6to fd (Linux raid autodetect)
Command (m forhelp): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed witherror 16: Device or resource busy.
The kernel still uses the old table. The newtable will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
1.2.重讀分區信息(在分區表中沒有所見的分區信息需要重複kpartx和partx)
# kpartx -af /dev/sdb # partx -a /dev/sdb # cat /proc/partitions 驗證
1.3 創建raid5
# mdadm -C /dev/md0 -a yes -l 5-c 32K -n 3/dev/sdb{3,5,6} # mdadm -D /dev/md0 驗證
1.4基於raid5創建邏輯卷,並開機自動掛載
# pvcreate /dev/md0 創建物理卷 Physical volume "/dev/md0"successfully created # vgcreate data /dev/md0 創建卷組 Volume group "data"successfully created # lvcreate -L 10G -n mydata data 創建邏輯卷 Logical volume "mydata"created # mkdir data 創建掛在點 # vim /etc/fstab 添加如下一行 自動掛載 /dev/data/mydata /data ext4 defaults 00 # mount -a # mke2fs -t ext4 -L MYDATA /dev/data/mydata 創建ext4文件系統
2.通用二進制格式安裝Mysql
2.1爲Mysql數據庫創建用戶和組
# groupadd -r mysql # useradd -g mysql -r -s /sbin/nologin mysql # id mysql uid=496(mysql) gid=493(mysql) groups=493(mysql)
2.2下載二進制格式的源碼包至本地,解壓至/usr/local目錄
# tar -xf mysql-5.5.33-linux2.6-i686.tar.gz -C /usr/local/ # cd /usr/local/
2.3 爲mysql-5.5.33-linux2.6-i686創建鏈接方便以後更新
# ln -sv mysql-5.5.33-linux2.6-i686 mysql `mysql' -> `mysql-5.5.33-linux2.6-i686'
2.4改變mysql-
5.5
.
33
-linux2.
6
-i686目錄Mysql數據目錄下所有文件的屬主和屬組
# chown -R root:mysql mysql/*
# cd mysql
# ll
# mkdir /data/data 創建Mysql的數據目錄
chown -R mysql.mysql /data/data
2.5初始化Mysql的系統庫
# scripts/mysql_install_db --datadir=/data/data/ --user=mysql
# chown -R mysql ./* 將該目錄下的所有文件的屬主改變爲mysql
2.6爲Mysql添加啓動腳本
# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
2.7爲Mysql提供配置文件並對其進行如下修改
# cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
# vim /etc/my.cnf
thread_concurrency = 4修改爲cpu核心數的2倍
datadir=/data/data 添加一行爲其指定數據目錄
2.8 Mysql安裝後的後續配置
# ln -sv /usr/local/mysql/include/ /usr/include/mysql
導出頭文件
`/usr/include/mysql/include' -> `/usr/local/mysql/include/'
# echo "/usr/local/mysql/lib/">/etc/ld.so.conf.d/mysql.conf
導出庫文件
# ldconfig
重讀庫文件
# ldconfig -p | grep "mysql"
libtcmalloc_minimal.so.0(libc6) => /usr/local/mysql/lib/libtcmalloc_minimal.so.0
libmysqlclient_r.so.16(libc6) => /usr/lib/mysql/libmysqlclient_r.so.16
libmysqlclient.so.18(libc6) => /usr/local/mysql/lib/libmysqlclient.so.18
libmysqlclient.so.16(libc6) => /usr/lib/mysql/libmysqlclient.so.16
libmysqlclient.so (libc6) => /usr/local/mysql/lib/libmysqlclient.so
# echo '/usr/local/mysql/bin:$PATH'> /etc/profile.d/mysql.sh
導出二進止文件
# echo $PATH
/usr/lib/qt3.3/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# vim /etc/man.config
MANPATH /usr/local/mysql/man 導出man文檔
2.9啓動Mysql服務
# service mysqld start
Starting MySQL [ OK ]
二: 編譯安裝httpd-2.4.9
httpd-2.4.9依賴你較高版本的apr和apr-util,並且還依賴域pcre-devel開發編譯安裝之前首先將apr-1.5.0、apr-util-1.5.3和httpd-2.4.9的源碼包下載至本地
1.解決編譯安裝httpd的依賴關係
# yum install -y prce-devel # yum groupinstall -y \"Development Tools\" # yum groupinstall -y \"Server Platform Development\"
2.編譯安裝apr-1.5.0
# tar xf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr
# make
# make install
3.編譯安裝apr-util-1.5.3
# 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
4.編譯安裝httpd-2.4.9(生產環境根據自己的需要啓用所需要的選項)
# tar xf httpd-2.4.9.tar.bz2 # cd httpd-2.4.9 # ./configure --prefix=/usr/local/apache 指定安裝目錄 --sysconfdir=/etc/httpd 制定配置文件的目錄 --enable-so 讓apache核心裝載DSO,但是不實際編譯任何動態模塊 --enable-ssl 啓用ssl --enable-cgi 啓用通用網關接口協議 --enable-rewrite 重寫Apache重寫功能 --with-zlib zlib庫 --with-pcre 啓用pcre庫 --with-apr=/usr/local/apr 啓用apr --with-apr-util=/usr/local/ apr-util 啓用apr-util --enables-modules=most 啓用httpd常用的模塊 --enable-mpms-shared=most 啓用常用的mpm模塊 --with-mpm=event 指定mpm工作模式爲event # make # make install
5.編譯安裝httpd-2.4.9之後的配置
1提供服務啓動腳本# vim /etc/rc.d/init.d/httpd
# 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/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure. So we just do it the way init scripts
# are expected to behave here.
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} $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=6
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
# Force LSB behaviour from killproc
LSB=1 killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
if [ $RETVAL -eq 7 ]; then
failure $"httpd shutdown"
fi
fi
echo
}
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart|try-restart)
if status -p ${pidfile} $httpd >&/dev/null; then
stop
start
fi
;;
force-reload|reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
RETVAL=2
esac
exit $RETVAL
2.爲腳本添加可執行權限
/etc/rc.d/init.d/httpd
3.將Apache服務腳本添加服務列表
# chkconfig --add httpd
# chkconfig --list httpd
# chkconfig httpd on
4在/etc/httpd/httpd.conf配置文件中添加一行
PidFile "/var/run/httpd/httpd.pid"
5.導出二進制文件
# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
6.啓動服務
# service httpd start
Starting httpd: [ OK ]
三:編譯安裝php-5.4.26
1.解決依賴關係
# yum -y groupinstall "Desktop Platform Development"
# yum -y install libmcrypt-devel
# yum -y install zipb2-devel
2.解壓至當前目錄中,並切換至其目錄下
# tar xf php-5.4.26.tar.bz2
# cd php-5.4.26
3.編安裝php-5.4.26 (生產環境根據自己的需要啓用所需要的選項)
# ./configure --prefix=/usr/local/php 指定安裝目錄 --with-mysql=mysqlnd 指定使用mysql --with-pdo-mysql=mysqlnd 啓用pdo模塊 --with-mysqli=mysqlnd 啓用mysqli,優化支持 --with-openssl 啓用openssl --enable-mbstring 啓用多字節,支持字符串 --with-freetype-dir 啓用對freetype字體庫的支持 --with-jpeg-dir 啓用對jpeg圖片的支持 --with-png-dir 啓用對png圖片的支持 --with-zlib 啓用對壓縮庫的支持 --with-libxml-dir=/usr 啓用libxml2庫的支持 --enable-xml 啓用xml --enable-sockets 啓用對套接字的支持 --enable-fpm 啓用fpm模塊 --with-mcrypt 啓用對加密的支持 --with-config-file-path=/etc 指定編譯安裝時配置文件安裝的路徑 --with-config-file-scan-dir=/etc/php.d 指定php.ini文件的安裝路徑 --with-bz2 啓用b2壓縮 --enable-maintainer-zts 啓用對worker和event的支持 # make 編譯php # make install 安裝php
4.爲php提供配置文件
# cp php.ini-production /etc/php.ini
5.配置php-fpm
爲php-fpm提供SysV init腳本,並將其添加至主服務列表中 # 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 # chkconfig --list php-fpm
6.爲php-fpm提供配置文件
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
7.編輯php-fpm的配置文件
# vim /usr/local/php/etc/php-fpm.conf 配置fpm的相關選項爲你所需要的值 pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 2 pm.max_spare_servers = 8 pid = /usr/local/php/var/run/php-fpm.pid 啓用pid文件 可以啓動php-fpm了: # service php-fpm start 來驗正是否開啓php-fpm # ps aux | grep php-fpm
四:配置httpd-2.4.9使其支持fcgi
1.啓用httpd的相關模塊在Apache httpd 2.4以後已經專門有一個模塊針對FastCGI的實現,此模塊爲:mod_proxy_fcgi.so,它其實是作爲mod_proxy.so模塊的擴充,因此,這兩個模塊都要加載
# vim /etc/httpd/httpd.conf 啓用如下兩個模塊 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
2. 編輯apache配置文件httpd.conf,讓apache能識別php格式的頁面,並支持php格式的主頁
1 2 3 4 5 6 | # vim /etc/httpd/httpd.conf 1 .定位至AddType添加如下兩行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps 2 .定位至DirectoryIndex index.html 修改爲:
DirectoryIndex index.php index.html |
3.配置虛擬主機支持fcgi
註釋掉中心主機:DocumentRoot 啓用虛擬主機,編輯httpd.config將 #Include/etc/httpd/extra/httpd-dav.conf改爲 Include /etc/httpd/extra/httpd-dav.conf 編輯/etc/httpd/extra/httpd-dav.conf啓用虛擬主機支持fcgi # vim /etc/httpd/extra/httpd-dav.conf <virtualhost> ServerAdmin [email protected] DocumentRoot \"/web/www.a.magedu.com/htdocs\" ServerName www.a.magedu.com ServerAlias www.a.magedu.com ErrorLog \"logs/a.magedu.com-error_log\" CustomLog \"logs/a.magedu.com-access_log\" combined <directory>options none Require all granted</directory>ProxyRequests Off ProxyPassMatch ^/(.*\\.php)$ fcgi://172.16.19.2:9000/web/a.magedu.com/htdocs/$1</virtualhost><virtualhost>ServerAdmin [email protected] DocumentRoot \"/web/b.www.magedu.com/htdocs\" ServerName www.b.magedu.com ServerAlias www.b.magedu.com ErrorLog \"logs/b.magedu.com-error_log\" CustomLog \"logs/b.magedu.com-access_log\" combined <directory>options none Require all granted</directory>ProxyRequests Off ProxyPassMatch ^/(.*\\.php)$ fcgi://172.16.19.2:9000/web/b.magedu.com/htdocs/$1</virtualhost>
注意:
ProxyRequests Off:關閉正向代理
ProxyPassMatch:把以.php結尾的文件請求發送到php-fpm進程,php-fpm至少需要知道運
行 的目錄和URI,所以這裏直接在fcgi://172.16.19.2:9000後指明瞭這兩個參數, 其它
的參數的傳遞已經被mod_proxy_fcgi.so進行了封裝,不需要手動指定。
四:測試基於fcgi模式的LAMP環境
1.爲www.a.magedu.com提供鏈接mysql的測試
2. 爲www.b.magedu.com提供測試
4.基於fcgi模式的LAMP環境搭建成功