lamp組合一種是基於linux平臺的web應用部署:
L: Linux, A: apache (httpd), M: MySQL (MariaDB), P (php, python, perl, ruby)php
工做流程:html
web進程接受客戶端請求,客戶端請求的資源有動態的也有靜態,靜態資源請求web進程直接從磁盤讀取,動態資源請求須要服務器提供一個PHP語言的環境進行運行,運行過程可能須要讀取數據(由數據庫提供數據提升效率),將動態資源運行的結果給web進程,由web進程響應給用戶。python
爲了提升響應速度,可將web進程,php環境,數據庫分別部署在不一樣的服務器上。(這就是fcgi模式)mysql
早期有客戶端自行處理動態資源,得到運行結果(如瀏覽器的插件,flash等)linux
關於動態資源請求的工做圖web
CGI協議:Common Gateway Interface,通用網關接口
簡化版的http協議,用於web服務器與應用程序進程通訊;(在lamp體系中httpd進程調動php語言等其餘的進程)httpd須要CGI模塊才能成爲cgi協議的客戶端和其餘應用程序進程交流redis
httpd進程還將URL轉換成本地文件路徑發給編程語言環境進程,由該進程本身調用文件
PHP語言有一套本身完整的解釋器,無需依賴linux操做系統的特別命令sql
Database:mongodb
PHP應用程序直接讀取磁盤上的數據(文件形式存儲的數據),影響應用進程自己運行,將功能下放交給數據庫讀取磁盤上的數據,數據庫進程讀取並組織成結構完後再傳遞給PHP應用程序提升應用程序計算運行的效率。數據庫
數據庫爲PHP等應用程序提供數據庫接口(API),在應用程序的代碼中調用此接口。
a與p的結合方式有三種:
Module:將應用程序做爲第三方模塊,加載在httpd的進程中
cgi:web服務器根據客戶端的動態請求開啓一個子進程,該子進程就是PHP負責運算,運算完成後由httpd進程銷燬
fcgi:應用程序本身起一個進程,監聽着與httpd進程的請求(基於套接字能夠跨主機),工做模式相似httpd的prework。此種方法能夠徹底把應用程序獨立成服務器。
請求流程:
Client --> (http) --> httpd --> (somefile.php, cgi) --> application server --> (mysql) --> mysql server
php程序與MySQL如何交互:
解釋器無須與MySQL對接, 須要處理數據實際上是解釋器上運行的程序;
存儲系統有多種:
NoSQL: redis, mongodb等等
SQL: MariaDB, MySQL, Oracle等
NewSQL:
分佈式存儲:
程序與存儲系統交互須要經過專用的接口(驅動)進行
CentOS 6.6 AMP(apache,mysql,php)
須要安裝基本程序包:
httpd, php, php-mysql, mysql-server
rpm包安裝的PHP爲模塊可能被httpd進程所加載,PHP能夠嵌入html代碼中 web服務器只會將PHP封裝的那部分代碼提交給PHP解釋器
PHP代碼鏈接mysql的擴展(稱之爲API接口或驅動),在centos中被作成一個RPM包(php-mysql),安裝完成須要從新加載!
部署(Module方式)步驟:
1、在1.11上編譯安裝apache2.4.9
1.前期準備
httpd-2.4.9須要較新版本的apr和apr-util,所以須要事先對其進行升級。升級方式有兩種,一種是經過源代碼編譯安裝,一種是直接升級rpm包。這裏選擇使用編譯源代碼的方式進行。想服務器上傳三個源代碼包apr-1.5.0.tar.bz2 apr-util-1.5.3.tar.bz2 httpd-2.4.9.tar.bz2
2.安裝apache細節
(1)編譯安裝apr
[root@student1 mnt]# tar xf apr-1.5.0.tar.bz2 [root@student1 mnt]# cd apr-1.5.0 [root@student1 apr-1.5.0]# ./configure --prefix=/usr/local/apr checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... x86_64-unknown-linux-gnu [root@student1 apr-1.5.0]# make && make install
(2)編譯安裝apr-until
[root@student1 mnt]# tar xf apr-util-1.5.3.tar.bz2 [root@student1 mnt]# cd apr-util-1.5.3 [root@student1 apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [root@student1 apr-util-1.5.3]# make && make install
(3)編譯安裝apache2.4.9
httpd-2.4.9編譯過程也要依賴於pcre-devel軟件包,須要事先安裝。此軟件包系統光盤自帶,使用yum本地源安裝。
[root@student1 Packages]# yum install pcre-devel-7.8-6.el6.x86_64.rpm以後開始安裝apapche,執行以下配置
./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
遇到configure: WARNING: OpenSSL version is too old no
checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures
解決方法:使用yum安裝新的openssl-devel就能解決,安裝完畢後再次執行配置,順利經過
[root@student1 httpd-2.4.9]# make && make install順利安裝完畢
三、修改httpd的主配置文件,設置其Pid文件的路徑
編輯/etc/httpd24/httpd.conf,添加以下行便可:
PidFile "/var/run/httpd.pid"(本次本身編寫的)四、提供SysV服務腳本/etc/rc.d/init.d/httpd,內容以下:
#!/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/functionsif [ -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=0start() {
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
esacexit $RETVAL
然後爲此腳本賦予執行權限:
# chmod +x /etc/rc.d/init.d/httpd加入服務列表:
# chkconfig --add httpd
2、在3.12安裝mysql-5.5.33
一、準備數據存放的文件目錄
建立/var/mydata/data目錄作爲mysql數據的存放目錄。
二、新建用戶以安全方式運行進程:
# groupadd -r mysql # useradd -g mysql -r -s /sbin/nologin -M -d /var/mydata/data mysql # chown -R mysql:mysql /var/mydata/data
三、安裝並初始化mysql-5.5.33
# tar xf mysql-5.5.33-linux2.6-i686.tar.gz -C /usr/local # cd /usr/local/ # ln -sv mysql-5.5.33-linux2.6-i686 mysql # cd mysql # chown -R mysql:mysql . # scripts/mysql_install_db --user=mysql --datadir=/var/mydata/data # chown -R root .
四、爲mysql提供主配置文件:
# cd /usr/local/mysql # cp support-files/my-large.cnf /etc/my.cnf
並修改此文件中thread_concurrency的值爲你的CPU個數乘以2,好比這裏使用以下行:
thread_concurrency = 2另外還須要添加以下行指定mysql數據文件的存放位置:
datadir = /var/mydata/data
五、爲mysql提供sysv服務腳本:# cd /usr/local/mysql # cp support-files/mysql.server /etc/rc.d/init.d/mysqld # chmod +x /etc/rc.d/init.d/mysqld
添加至服務列表:
# chkconfig --add mysqld # chkconfig mysqld on
然後就能夠啓動服務測試使用了。
爲了使用mysql的安裝符合系統使用規範,並將其開發組件導出給系統使用,這裏還須要進行以下步驟:
六、輸出mysql的man手冊至man命令的查找路徑:編輯/etc/man.config,添加以下行便可:
MANPATH /usr/local/mysql/man七、輸出mysql的頭文件至系統頭文件路徑/usr/include:
這能夠經過簡單的建立連接實現:
# ln -sv /usr/local/mysql/include /usr/include/mysql
八、輸出mysql的庫文件給系統庫查找路徑:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
然後讓系統從新載入系統庫:
# ldconfig九、修改PATH環境變量,讓系統能夠直接使用mysql的相關命令。
鏈接數據庫併爲數據庫建立用戶以供後面PHP測試
[root@MariaDB ~]# mysql -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 13 Server version: 5.5.36-MariaDB-log MariaDB Server Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> create user 'test'@'192.168.%.%' identified by 'ewq321'; Query OK, 0 rows affected (0.00 sec)
3、在1.11上編譯安裝php-5.4.26
一、解決依賴關係:
配置好yum源爲阿里雲上的源(系統安裝源及epel源)後執行以下命令:
# yum -y groupinstall "Desktop Platform Development"(本機安裝系統時選擇桌面版了,桌面開發環境完整) # yum -y install bzip2-devel libmcrypt-devel #
二、編譯安裝php-5.4.26
# tar xf php-5.4.26.tar.bz2 # cd php-5.4.26 # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
說明:
一、這裏爲了支持apache的worker或event這兩個MPM,編譯時使用了--enable-maintainer-zts選項。
報錯1:configure: error: xml2-config not found. Please check your libxml2 installation.
解決方法:
在屢次安裝libxml2-2.7.6-14.el6.x86_64時,出現以下提醒
Examining libxml2-2.7.6-14.el6.x86_64.rpm: libxml2-2.7.6-14.el6.x86_64
libxml2-2.7.6-14.el6.x86_64.rpm: does not update installed package.
Error: Nothing to do
結合報錯斷定多是libxm12的擴展包未安裝,yum -y install libxm12-devel報錯1解決
報錯2:configure: error: Cannot find libmysqlclient_r under/usr/local/mysql. Note that the MySQL client library is not bundledanymore!
解決方法:
若是使用PHP5.3以上版本,爲了連接MySQL數據庫,能夠指定PHP鏈接mysql的相關參數設置指定爲mysqlnd函數庫(它是專門爲PHP而寫的一個庫,用了PHP的內在管理函數以及一些網絡流的函數。),這樣在本機就不須要先安裝MySQL或MySQL開發包了。mysqlnd從php 5.3開始可用,能夠編譯時綁定到它(而不用和具體的MySQL客戶端庫綁定造成依賴),但從PHP 5.4開始它就是默認設置了。
指定鏈接php新的函數庫mysqlnd方法:./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd# make # make test # make intall
爲php提供配置文件:
# cp php.ini-production /etc/php.ini三、 編輯apache配置文件httpd.conf,以apache支持php
# vim /etc/httpd/httpd.conf
一、添加以下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps二、定位至DirectoryIndex index.html
修改成:
DirectoryIndex index.php index.html然後從新啓動httpd,或讓其從新載入配置文件便可測試php是否已經能夠正常使用。
測試頁面index.php示例以下:
<?php
$link = mysql_connect('192.168.3.12,'test',‘ewq321');
if ($link)
echo "Success...";
else
echo "Failure...";mysql_close();
?>報錯3:/tmp/php-5.4.26/ext/standard/info.c:878: undefined reference to `executor_globals_id'
collect2: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1
解決方法:形成此錯誤的緣由是編譯PHP時漏加了一個參數設置發現後從新編譯,但沒有事先進行make clean清理,應先make clean然後從新編譯./configure再make&&make install發現打開index。php頁面一直報"Failure...",後在web服務器上安裝mysql客戶端,鏈接測試報錯以下
[root@student1 Packages]# mysql -h 192.168.3.12 -u root -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.3.12' (113)
[root@student1 Packages]# mysql -h 192.168.3.12 -u root –p
經檢查發現數據庫服務器防火牆沒有關閉,關閉後正常鏈接。