LAMP:Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一組常用來搭建動態網站或者服務器的開源軟件,組成一個網絡架構。
他們直接的請求過程大致是如下過程:
在開始講解安裝和配置過程之前,對於我各人我自己先總結點我對軟件包安裝的一些梳理。
這些也許你們都懂,但是作爲我自己,我還是想抓住可利用的空間和時間去總結總結你們覺得很小兒科的東西。
首先對於linux中軟件安裝有兩種,一是設置好yum源,直接 yum install 安裝。二是手動編譯源碼安裝。
對於直接yum install 或者 rpm -ivh 安裝的軟件,其安裝生成的文件可以通過 rpm -ql rmp_name 查看。當然有些東西是需要自己稍微總結一下才能記的勞的,你不能每次總是通過rpm -ql來查詢吧,這裏我就我自己爲我自己總結的一點東西陳述一下,當然只是僅僅對應我自己,因爲我是真的很笨的。
直接用軟件包安裝的軟件,其會生成很多文件,其中有主程序,配置文件,庫文件,頭文件,日誌文件,man文檔等等。當然這些文件的存儲位置還是相對很固定的,我說的是相對,不排除有些軟件安裝生成的目錄很奇怪。
好了,說了半天進入總結的話題吧。下面的總結是基於RPM包安裝。我們也不說的那麼詳細了。就粗略點說說。
配置文件一般在/etc下。
安裝程序一般在/usr/sbin /usr/bin
庫文件在/etc/ld.so.conf.d/
頭文件在/usr/include/
日誌在/var/log/
Man文檔 /etc/man.config中
服務腳本的位置:/etd/rc.d/init.d/是爲服務的用service啓動添加腳本支持
當我們手動編譯安裝時,編譯時就指定了安裝的主程序(--prefix)和配置文件(--sysconfdir)的位置,剩下的服務腳本要寫到上面對應的位置,當然這些都需要我們手動去寫。
好了,就寫這麼多,下來我們來手動安裝配置LAMP。
首先安裝apache
Apache編譯時,依賴apr 和 apr-util 兩個軟件包,那首先就需要下載編譯安裝apr 和 apr-util
這種是基於
(1) 編譯安裝apr
# tar xf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr
# make && make install
(2) 編譯安裝apr-util
# 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
(3) httpd-2.4.9編譯過程也要依賴於pcre-devel軟件包,需要事先安裝
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/httpd --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的主配置文件,設置其Pid文件的路徑
編輯/etc/httpd/httpd.conf,添加如下行即可:
PidFile "/var/run/httpd.pid"
4、提供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/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 $@
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/httpd
加入服務列表:
# chkconfig --add httpd
編譯安裝好httpd後可以先測試下有沒有安裝成功和生效。
#DocumentRoot "/usr/local/apache/htdocs"
#DirectoryIndex index.php index.html
這兩項,上面的是修改網頁文件的目錄或者說是所在位置,而下面的是添加網頁的主頁文件。
爲了使用mysql的安裝符合系統使用規範,並將其開發組件導出給系統使用,這裏還需要進行如下步驟:
6、輸出mysql的man手冊至man命令的查找路徑:
編輯/etc/man.config,添加如下行即可:
MANPATH /usr/local/mysql/man
7、輸出mysql的頭文件至系統頭文件路徑/usr/include:
這可以通過簡單的創建鏈接實現:
# ln -sv /usr/local/mysql/include /usr/include/mysql
8、輸出mysql的庫文件給系統庫查找路徑:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而後讓系統重新載入系統庫:
# ldconfig
9、修改PATH環境變量,讓系統可以直接使用mysql的相關命令。
三、編譯安裝php-5.4.26
1、解決依賴關係:
請配置好yum源(系統安裝源及epel源)後執行如下命令:
# yum -y groupinstall "Desktop Platform Development"
# yum -y install bzip2-devel libmcrypt-devel
2、編譯安裝php-5.4.26
首先下載源碼包至本地目錄,下載位置ftp://172.16.0.1/pub/Sources/new_lamp。
# 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
說明:
1、這裏爲了支持apache的worker或event這兩個MPM,編譯時使用了--enable-maintainer-zts選項。
2、如果使用PHP5.3以上版本,爲了鏈接MySQL數據庫,可以指定mysqlnd,這樣在本機就不需要先安裝MySQL或MySQL開發包了。mysqlnd從php 5.3開始可用,可以編譯時綁定到它(而不用和具體的MySQL客戶端庫綁定形成依賴),但從PHP 5.4開始它就是默認設置了。
# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
# make
# make test
# make intall
爲php提供配置文件:
# cp php.ini-production /etc/php.ini
3、 編輯apache配置文件httpd.conf,以apache支持php
# vim /etc/httpd/httpd.conf
1、添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
2、定位至DirectoryIndex index.html
修改爲:
DirectoryIndex index.php index.html
而後重新啓動httpd,或讓其重新載入配置文件即可測試php是否已經可以正常使用。
測試頁面index.php示例如下:
<?php
$link = mysql_connect('127.0.0.1','root','mageedu');
if ($link)
echo "Success...";
else
echo "Failure...";
mysql_close();
?>
四、安裝xcache,爲php加速:
1、安裝
# tar xf xcache-3.0.3.tar.gz
# cd xcache-3.0.3
# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install
安裝結束時,會出現類似如下行:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/
2、編輯php.ini,整合php和xcache:
首先將xcache提供的樣例配置導入php.ini
# mkdir /etc/php.d
# cp xcache.ini /etc/php.d
說明:xcache.ini文件在xcache的源碼目錄中。
接下來編輯/etc/php.d/xcache.ini,找到zend_extension開頭的行,修改爲如下行:
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
注意:如果php.ini文件中有多條zend_extension指令行,要確保此新增的行排在第一位。
五、啓用服務器狀態
mod_status模塊可以讓管理員查看服務器的執行狀態,它通過一個HTML頁面展示了當前服務器的統計數據。這些數據通常包括但不限於:
(1) 處於工作狀態的worker進程數;
(2) 空閒狀態的worker進程數;
(3) 每個worker的狀態,包括此worker已經響應的請求數,及由此worker發送的內容的字節數;
(4) 當前服務器總共發送的字節數;
(5) 服務器自上次啓動或重啓以來至當前的時長;
(6) 平均每秒鐘響應的請求數、平均每秒鐘發送的字節數、平均每個請求所請求內容的字節數;
啓用狀態頁面的方法很簡單,只需要在主配置文件中添加如下內容即可:
<Location /server-status>
SetHandler server-status
Require all granted
</Location>
需要提醒的是,這裏的狀態信息不應該被所有人隨意訪問,因此,應該限制僅允許某些特定地址的客戶端查看。比如使用Require ip 172.16.0.0/16來限制僅允許指定網段的主機查看此頁面。
將配置過程轉化爲個人的語言:
基於fastcgi協議去解析前端httpd服務器上的php頁面時,需要額外的配置
在編譯php時,加入--enable-fpm 表示加入php-fpm
在安裝好php後,(因爲php整合的那些協議功能服務都在自己的安裝包內,可以進去安裝包內找相關的功能對應的配置文件和服務腳本)
至此,就需要進入php安裝包內,將php-fpm對應的配置文件和服務腳本放到對應的位置,來啓動這個php-fpm服務。由這個看來,php-fpm也是依賴於php軟件的,只是它把自己做成了模塊來讓httpd調用。
當然php-fpm這個需要的配置文件和服務腳本,需要自己去php安裝包內尋找。
下來就是加入服務列表中了 chkchonfig --add php-fpm 開啓服務 chkconfig php-fpm on
可以通過 chkconfig --list | grep php-fpm來查看開啓狀態。
當然,還有一個重要的配置,就是php-fpm的配置文件中,要修改其pid的目錄(對應我們手動編譯安裝的軟件都需要手動去指定或者去加入這個pid文件的目錄位置,因爲這個位置是告訴服務腳本去什麼地方找pid文件,來確認程序是不是啓動了(軟件的啓動一般通過pid和鎖文件是否存在對應的/var/run ,/var/lock/subsys來看程序是否啓動)),修改到對應目錄即可。
因爲是httpd去調用php-fpm服務進程去響應php頁面的,所以還需要去httpd的配置文件中欲與附加這樣的功能,配置文件在/etc/httpd/httpd.conf中。打開配置文件,加入支持解析php的命令:
AddType application/x-httpd-php .php
AddType applicaiton/x-httpd-php-source .phps
Httpd默認只能解析html類型的網頁文件,既然我們給他加入瞭解析php的解析,那就需要在其網頁文件的目錄中加入php類型的主頁了,如下:
DiecrotyIndex index.php index.html
其實這樣就可以了,但是我們的環境是啓動了虛擬主機,當我們啓用虛擬主機後,就需要在配置文件中把DocumentRoot “/usr/local/apache/htdocs”註釋掉,因爲我們有很多個虛擬主機(或者叫域名,我們就叫虛擬主機好了),所以我們要在對應的虛擬主機下寫入對應主頁所在的位置。還有,php-fpm做爲了httpd的模塊,那麼我們應該去啓用他,在配置文件中講註釋掉的兩行文件去掉,分別是 mod_proxy_fcgi.so 和 mod_proxy.so兩個模塊,完成配置。那麼現在就可以進入到虛擬主機的配置文件中去配置了虛擬主機來支持php-fpm了。
虛擬主機的配置文件在/etc/httpd/extra/httpd_vhosts.conf,進入配置文件中,在對應的虛擬主機中加入如下兩行:
ProxyRequest off
proxyPassmath ^(./*\.php)$ fcgi://172.0.0.1:9000/www/firsrpage.com/$1
ProxyRequests Off:關閉正向代理
ProxyPassMatch:把以.php結尾的文件請求發送到php-fpm進程,php-fpm至少需要知道運行的目錄和URI,所以這裏直接在fcgi://127.0.0.1:9000後指明瞭這兩個參數,其它的參數的傳遞已經被mod_proxy_fcgi.so進行了封裝,不需要手動指定。
我們還要個給我們的這個虛擬主機對應的主頁文件的目錄給與權限
<Directory "/www/magedu.com">
Options none
AllowOverride none
Require all granted
</Directory>