一、 先說說啥叫lamp
1. lamp簡介: Linux Apache Mysql PHP(Python, Perl)的簡稱,下面說說他們之間的關系
Linux:系統運行平臺
Apache:httpd網頁服務器,不會和后端數據庫發生直接聯系
Mysql:mysql數據服務器,用來為前端應用程序提供數據,例如php腳本
PHP:動態網頁腳本的解釋器,通常聯系httpd和mysqld,如果httpd遇到php腳本時,會交給php解析。腳本運行所需要的數據將向mysqld獲取。
二、 兩種lamp構架實現方式
1. lamp構架的實現方式之一, 基于php模塊實現。 此時PHP是作為一個httpd的動態模塊存在的,由httpd直接管理調度。
操作過程如下
1) 編譯安裝apache
## 安裝編譯環境 # yum -y groupinstall 'Development tools' # yum -y groupinstall 'Desktop Platform Development' # yum -y install pcre-devel ## 編譯安裝apr # tar -xf apr-1.5.2.tar.gz # cd apr-1.5.2 # ./configure --prefix=/usr/local/apr # make && make install ## 編譯安裝apr-util # tar -xf apr-util-1.5.4.tar.gz # cd apr-util-1.5.4 # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr # make && make install ## 編譯安裝httpd2.4.12 # tar -xf httpd-2.4.12.tar.gz # cd httpd-2.4.12 # ./configure --prefix=/usr/local/httpd24 --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 --enables-modules=most --enable-mpms-shared=all --with-mpm=event # make && make install ## 修改配置文件 ## vim /etc/httpd24/httpd.conf 修改 PidFile "/var/run/httpd.pid" ## 提供服務腳本,可以把系統自帶httpd服務腳本改名后,稍加修改后使用。 主要需改 ## apachectl, httpd, prog, pidfile, lockfile 這幾項 其他幾乎不用動,然后給個權限,存成/etc/rc.d/init.d/httpd24 #!/bin/bash # #!/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/httpd24/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/httpd24/bin/apachectl httpd=${HTTPD-/usr/local/httpd24/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
# chkconfig --add httpd24 ## 至于導入導出頭文件,幫助文件和二進制文件,詳見之前關于網絡服務的博客。此處不詳述。
2) 安裝mysql,這里使用的版本為 mysql-5.5.44的二進制文件
## 安裝mysql-5.5.44 到 /usr/local/下面 # tar -xf mysql-5.5.44-linux2.6-x86_64.tar.gz -C /usr/local/ # ln -sv /usr/local/mysql-5.5.44 /usr/local/mysql ## 創建mysql需要的運行用戶 # groupadd mysql # useradd -g mysql -r -s /sbin/nologin -M -d mysql ## 修改mysql相關文件的屬主屬組 # chown -R root:mysql /usr/local/mysql/* ## 初始化mysql數據庫 ## 先創建/data/sqldata 這里最好使用邏輯卷,方便演示只要創建一個目錄就好了 # mkdir /data/mysql # /usr/local/mysql/mysql_instal_db --user=mysql --datadir=/data/sqldata ## 導出mysql的庫文件,二進制文件,頭文件 # echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf # ldconfig # echo 'export $PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh # vim /etc/man.config 添加如下行 MANPATH /usr/local/mysql/man
3)編譯安裝php-5.6.9
## 解決依賴關系 # yum -y groupinstall 'Development tools' # yum -y groupinstall 'Desktop Platform Development' ## 編譯安裝 # tar -xf php-5.6.9.tar.xz # cd php-5.6.9 # ./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/httpd24/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts ## --with-apxs2=/usr/local/httpd24/bin/apxs 這個選項定義了,php將被當做模塊調用 # make # make test # make install ## 提供php配置文件 # cp php.ini-production /etc/php.ini ## 修改httpd.conf,使其支持php,并且可以識別index.php結尾作為首頁 AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps DirectoryIndex index.php index.html
4)性能測試。 使用phpMyAdmin作為事例。
## 安裝phpMyAdmin # tar -xf phpMyAdmin-4.0.10.10-all-languages.tar.xz -C /var/httpd24/htdocs # cp config.sample.inc.php config.inc.php' ## 此時如果使用本機ip訪問的話,應該可以看到phpMyAdmin首頁 ## 使用ab進行壓力測試 # ab -c 100 -n 500 192.168.98.128/index.php 下面結果我們看到,每秒鐘響應次數99個。 一會我們安裝完xcache后,在次進行嘗試
5) 安裝xcache-3.2.0
## 編譯安裝 # tar xf xcache-3.2.0.tar.gz # cd xcache-3.2.0 # /usr/local/php/bin/phpize # ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config # make && make install ## 配置xcache整合到php上 # mkdir /etc/php.d # cp xcache.ini /etc/php.d # vim /etc/php.d/xcache.ini 修改下面指令 extension = /usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so ## 再次進行測試 # ab -c 100 -n 500 192.168.98.128/index.php 可以看到每秒響應次數變為541個, 性能提升超過五倍
2. lamp構架實現方式之二, php作為一個獨立服務,可以與httpd服務器分開部署
在此種模型中, php以php-fpm服務的形式運行,單獨調度。如果分開部署可以大大減輕httpd服務器的壓力。缺點網絡通信不暢的情況下,帶寬有可能成為瓶頸。下面實驗中,另開一個主機,配置ip 192.168.98.129, 把這個作為php-fpm服務器主機。 mysql 和 httpd全部在之前的主機上,地址為192.168.98.128不用重新安裝。下面只在新的主機上重新編譯php作為一個服務。
1) 編譯php-fpm服務
## 在新主機上配置安裝的環境,此地不詳述。 ## 編譯安裝php # tar -xf php-5.6.9.tar.xz # cd php-5.6.9 ## 如果沒有事先安裝mysql則按照以下編譯 # ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --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 --enable-maintainer-zts ## 如果預先安裝mysql則 # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --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 --enable-maintainer-zts php編譯模塊不同點只是把 --with-apxs2=/usr/local/httpd24/bin/apxs替換為 --enable-fpm而已
2) 配置php-fpm
## 提供服務腳本 # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 編輯配置文件, 大部分都不需要修改。 如果已經提供了,就不要再多加。重復的指令會產生錯誤。 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 listen = 192.168.98.129:9000 ## 啟動服務 # service php-fpm start # ss -tnlp LISTEN 0 128 192.168.98.129:9000 *:* users:(("php-fpm",30927,9),("php-fpm",30943,0),("php-fpm",30944,0),("php-fpm",30946,0))
3) 配置httpd服務器,使其支持反向代理
## 在配置文件中啟用反向代理模塊 LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so LoadModule proxy_module modules/mod_proxy.so ## 配置個虛擬主機 <VirtualHost *:80> DocumentRoot "/var/httpd24/htdocs/phpMyAdmin" ServerName playground.com ProxyRequests off ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.98.129:9000/var/htdocs/phpMyAdmin/$1 <Directory "/var/httpd24/htdocs/phpMyAdmin"> Options none AllowOverride none Require all granted </Directory> </VirtualHost> ## 我們把phpMyAdmin放在 192.168.98.129主機,/var/htdocs/phpMyAdmin下面就可以了,當然沒有的話要創建 現在當訪問192.168.98.128/index.php時,將會通過fcgi協議反向代理到192.168.98.129主機的相應目錄下面。
4) 安裝xcache 前后的壓力測試
安裝xcache之前:即使沒有安裝,也比模塊化略快。每秒鐘128個請求。
安裝以后 每秒526個請求
原創文章,作者:以馬內利,如若轉載,請注明出處:http://www.www58058.com/5187