httpd與php結合的方式有三種:
1、module:將php編譯成httpd的模塊,當Apache服務器收到客戶端的動態資源請求時,httpd服務自身便可以依靠php模塊來處理動態資源。
2、cgi:此種方式是當Apache服務器,每次收到客戶端發來的動態資源請求時,將調用php生成一個子進程來處理客戶端所請求的動態資源,因此n個請求便會在服務器端啓動2n個進程,是對服務器資源極大程度的浪費,因此,此種結合方式很少用。
3、fpm:此種方式是基於fastcgi協議,php作爲一個服務監聽在某個套接字上,當Apache客戶端解析到客戶端所請求的資源是動態資源是,便將請求發送給php服務,由php服務處理動態內容,並響應給httpd服務,再由httpd服務將結果返回給客戶端。此時的Apache服務器便是一個代理服務器。httpd2.4版本新增了一個mod_fcgi模塊,此模塊便是可讓httpd與php基於fpm方式結合,httpd2.2版本若想基於fpm方式與httpd結合,需自行安裝fcgi模塊。
本機版本及IP地址信息:
[root@localhost ~]# uname -r 2.6.32-504.el6.x86_64 [root@localhost ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@localhost ~]# ip addr show | awk -F'[ /]+' '/inet\>/&&!/127.0.0.1/ {print $3}' 172.16.113.14
安裝次序:
1、由於pgp與httpd若與模塊方式結合,那麼首先得現有httpd才能將php編譯進httpd的內核
2、若編譯php支持mariadb的驅動,首先需要知道Mariadb的安裝位置及其版本,才能編譯相應的驅動。
因此,編譯次序爲 httpd---- > MariaDB --- >php ,mariadb和httpd的安裝次序不分前後。
a、關閉iptables //若對iptables規則熟悉,可忽略此條信息
b、關閉selinux
[root@localhost ~]# sed -i 's/\(SELINUX=\).\+/\1disabled/' /etc/sysconfig/selinux [root@localhost ~]# setenforce 0
c、準備開發環境(需提前配置好yum源)
Development tools
Server Platform Development
A、將php編譯進httpd的模塊,實現LAMP結合,並搭建phpMyadmin,實現數據庫圖形藉口管理
需要的源碼包:
1、httpd-2.4.9.tar.bz2----httpd的源碼包,由Apache官網下載 http://www.apache.org/
2、php-5.6.4.tar.xz -----php的源碼包,有php官網下載 http://www.php.net/
3、mariadb-5.5.43-linux-x86_64.tar.gz --- mariadb的二進制版本包,由mariadb官網下載https://downloads.mariadb.org/
4、apr-1.5.0.tar.bz2 和apr-util-1.5.3.tar.bz2 此爲httpd的運行環境,httpd的安裝需要依賴於apr(Apache portable Run-time libraries,Apache可移植運行庫),實現httpd的跨平臺運行。由於htpd2.4需要較高版本的apr及apr-util,所以需將其升級,可通過epel源升級或者源碼升級。此處選擇源碼升級。
1、展開源碼包到/usr/local/src
[root@localhost ~]# tar xf apr-1.5.0.tar.bz2 -C /usr/local/src/ [root@localhost ~]# tar xf apr-util-1.5.3.tar.bz2 -C /usr/local/src/ [root@localhost ~]# tar xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/src/ [root@localhost ~]# tar xf php-5.6.4.tar.xz -C /usr/local/src/ [root@localhost ~]# unzip phpMyAdmin-4.0.5-all-languages.zip -d /usr/local/src/
2、httpd在url重寫時可能需要用到pcre來支持perl語言的正則表達式,安裝過程中需要依賴pere-devel包,因此,先裝上此包:
[root@localhost ~]# yum install -y pcre-devel //需提前配置好yum源
3、安裝apr及apr-util包
[root@localhost ~]# cd /usr/local/src/apr-1.5.0/ //切換至apr解壓目錄 [root@localhost apr-1.5.0]# ./configure --prefix=/usr/local/apr //指明apr的安裝位置 [root@localhost apr-1.5.0]# make && make install //編譯並安裝 [root@localhost apr-1.5.0]# cd /usr/local/src/apr-util-1.5.3/ //切換至apr-util的解壓目錄 [root@localhost apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr //指定安裝目錄及apr路徑 [root@localhost apr-util-1.5.3]# make && make install //編譯並安裝
4、安裝編譯安裝httpd
[root@localhost ~]# cd /usr/local/src/httpd-2.4.9/ //切換至httpd的解壓目錄 [root@localhost httpd-2.4.9]# ./configure --help //使用此選項查看編譯時所支持的參數,由於條目過多,此處指選出需要用到的參數進行講解 --prefix=/usr/local/apache //指明安裝目錄 --sysconfdir=/etc/httpd24 //指定配置文件的安裝路徑,此處爲了避免與rpm包所裝的httpd衝 突,將路徑名取名爲httpd24 --enable-so //支持httpd的模塊化機制 --enable-ssl //啓用支持ssl功能,實現https --enable-cgi //支持cgi協議 --enable-rewrite //支持url重寫 --with-zlib //支持傳輸壓縮 --with-pcre //支持perl語言的正則表達式 --with-apr=/usr/local/apr //指明編譯升級的apr的路徑 --with-apr-util=/usr/local/apr-util //指明編譯升級的apr-util路徑 --enable-modules=most //指明啓動大多數的模塊 --enable-mpms-shared=all //將所有mpm模塊都編譯進httpd(httpd2.4版本的新特性) --with-mpm=event //默認使用event的MPM工作模型 [root@localhost 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 [root@localhost httpd-2.4.9]# make && make install //編譯並安裝
5、爲編譯安裝的httpd添加環境變量
[root@localhost httpd-2.4.9]# vim /etc/profile.d/httpd24.sh //在/etc/profile.d/目錄下創建一個以.sh結尾的文件,並編輯 export PATH=/usr/local/apache/bin/:$PATH //將此信息添加至改文件,並保存退出 [root@localhost httpd-2.4.9]# source /etc/profile.d/httpd24.sh //通知系統重新讀取此文件 現在便可以直接使用編譯安裝的httpd命令了。
6、編譯配置文件,添加pidfile文件路徑
[root@localhost httpd-2.4.9]# vim /etc/httpd24/httpd.conf Pidfile "/var/run/httpd/httpd.pid" //在配置文件中添加該行,並保存退出
7、爲編譯安裝的httpd提供服務管理腳本,放置於/etc/rc.d/init.d下,名爲httpd.24
# chkconfig: - 85 15 # description: The Apache HTTP Server Management script. . /etc/rc.d/init.d/functions if [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpd fi HTTPD_LANG=${HTTPD_LANG-"C"} INITLOG_ARGS="" 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} 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 ${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 } # See how we were called. case "$1" in 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
8、將httpd24添加至開機自動啓動列表,腳本啓動httpd,進行測試:
[root@localhost ~]# chkconfig --add httpd24 [root@localhost ~]# chkconfig --list httpd24 httpd24 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@localhost httpd-2.4.9]# service httpd24 start Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message //此處報錯可忽略 [ OK ] [root@localhost httpd-2.4.9]# ss -tnl | grep 80 LISTEN 0 128 :::80 :::* //80端口已處於監聽狀態
9、安裝mariadb:此安裝過程可查看mysql的展開目錄中的INSTALL-BINARY 文檔
[root@localhost httpd-2.4.9]# cd /usr/local/src/ //切換至src的目錄 [root@localhost src]# mv mariadb-5.5.43-linux-x86_64 /usr/local/mysql //將mariadb的目錄名改爲mysql [root@localhost src]# groupadd -r -g 306 mysql //添加mysql組爲系統組 [root@localhost src]# useradd -r -u 306 -g 306 mysql //添加mysql用戶爲系統用戶並添加至mysql組 [root@localhost src]# chown -R mysql:mysql mysql //將mysql目錄及其子目錄改爲屬主屬組都爲mysql [root@localhost src]# mkdir /mydata/data -pv //創建mysql的數據庫目錄 mkdir: created directory `/mydata' mkdir: created directory `/mydata/data' [root@localhost src]# cd mysql/ [root@localhost src]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data //以mysql用戶安裝mariaDB並指明數據庫目錄位置 [root@localhost mysql]# chown -R root . //將mysql目錄改爲root屬主 [root@localhost mysql]# mkdir /etc/mysql //創建mysql配置文件目錄 //mysql的配置文件查找順序爲: /etc/my.cnf -- > /etc/mysql/*.cnf ---> --defaults-file(若編譯安裝時指定此項) ---> ~/.my.cnf 啓動mysql的用戶家目錄下,最後一個加載的配置參數覆蓋前面的配置參數 [root@localhost mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf //展開目錄下support-files目錄中提供有幾個配置模板,依照內存需求命令, 可根據需要拷貝爲默認的配置文件 [root@localhost mysql]# vim /etc/mysql/my.cnf //編輯配置文件,在[mysqld]容器中添加如下幾個參數 thread_concurrency = 8 //將此項改爲自己cpu核心數量的兩倍 datadir = /mydata/data //指明數據庫目錄 innodb_file_per_table = yes //開啓innodb數據庫每個表存儲爲一個文件 skip_name_resolve = yes //開啓跳過名稱自解析 [root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld //提供服務管理腳本,此腳本爲軟件所提供 [root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld //將服務腳本賦予可執行權限 [root@localhost mysql]# chkconfig --add mysqld //將mysqld添加至開機自動啓動項 [root@localhost mysql]# chkconfig mysqld on //開啓開機自動啓動 [root@localhost mysql]# chkconfig --list mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@localhost mysql]# /etc/init.d/mysqld start //啓動mysqld服務 [root@localhost mysql]# bin/mysql_secure_installation //運行mariadb的安全初始化,以完成爲root用戶添加密碼以及其他的安全操作 至此,mariadb已完成安裝
10、編譯安裝php,將其安裝爲httpd的模塊
[root@localhost php-5.6.4]# yum install -y libmcrypt-devel.x86_64 bzip2-devel //在epel源中有提供 //解決依賴關係,php可能用到bzip2來進行數據的壓縮 [root@localhost php-5.6.4]# yum install -y libxml2-devel //編譯安裝若啓用對xml文檔的支持,則需安裝此項 [root@localhost mysql]# cd /usr/local/src/php-5.6.4/ //切換至php的展開目錄 此處依舊可使用./configure --help 查看編譯安裝時所支持的參數 --prefix=/usr/local/php //指明安裝路徑 --with-mysql=/usr/local/mysql //指定mysql的位置 --with-openssl //支持openssl --with-mysqli=/usr/local/mysql/bin/mysql_config //對mysql進行交互的另外一種接口 --enable-mbstring //對多字節字符串支持,對中文支持,必須啓用此項 --with-freetype-dir //支持freetype字體格式 --with-jpeg-dir //開啓此功能,可以使用php直接處理jpeg格式的圖片 --with-png-dir // 開啓此功能,可以支持php直接處理png格式的圖片 --with-zlib // 支持zlip壓縮,提高數據傳輸效率 --with-libxml-dir=/usr //支持處理xml文檔 --enable-xml //啓用xml功能 --enable-sockets //表示支持php支持socket進行通信
--with-apxs2=/usr/local/apache/bin/apxs //此項表示將php編譯成模塊
--with-mcrypt //支持加密解密
--with-config-file-path=/etc //指明php配置文件的放置位置
--with-config-file-scan-dir=/etc/php.d //表示檢索此位置下的.ini結尾的文件作爲php的配置文件
--with-bz2 //支持bzip2格式的壓縮
--enable-maintainer-zts //若httpd以線程模式運行(worker或event)模式運行,此項必須!表示將php編譯成zts模塊
[root@localhost php-5.6.4]#./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
[root@localhost php-5.6.4]#make -j 4 && make install //編譯並安裝。可使用-j n 指明使用幾顆cpu核心進行編譯,加速編譯過程
./configure過程中,若出現錯誤,一般都是因爲所依賴的*-devel包未安裝,因此,使用yum install安裝即可。
[root@localhost php-5.6.4]# cp php.ini-production /etc/php.ini
//提供php配置文件,此文件無需更改。
[root@localhost php-5.6.4]# vim /etc/httpd24/httpd.conf
//編輯httpd配置文件,找到php的模塊,確保是否php已安裝成功
添加如下幾項:
AddType application/x-httpd-php .php AddType application/x-httpd-source .phps 將DirectoryIndex index.html 改爲 DirectoryIndex index.php index.html
保存退出。執行httpd -t
[root@localhost php-5.6.4]# httpd -t //檢查配置文件是否有誤
[root@localhost php-5.6.4]# /etc/rc.d/init.d/httpd24 reload //重讀配置文件
[root@localhost php-5.6.4]# httpd -M |grep php //檢查php的模塊是否已加載
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
php5_module (shared)
編輯一個測試頁,測試LAMP是否已結合成功----
[root@localhost php-5.6.4]# vim /usr/local/apache/htdocs/index.php
<?php $conn=mysql_connect('127.0.0.1','root','root'); //此處mysql鏈接賬號密碼爲安裝完成Mariadb後,運行安全初始化時設置的。 if ($conn) echo "OK"; else echo "Faulse"; phpinfo(); ?>
11、在瀏覽器中打開網頁進行查看:出現如下頁面,即表示搭建成功。
12、搭建基於主機名的虛擬主機安裝phpMyadmin,實現mysql(MariaDB)圖形化管理:
[root@localhost php-5.6.4]# vim /etc/httpd24/httpd.conf //編輯httpd配置文件 1)註釋中心主機:#DocumentRoot "/usr/local/apache/htdocs" 2)添加: Include /etc/httpd24/vhosts.conf
13、創建並編輯虛擬主機配置文件:
[root@localhost php-5.6.4]# vim /etc/httpd24/vhosts.conf
<VirtualHost 172.16.113.14:80>
DocumentRoot /web/vhosts/pma
ServerName pma.su.com
CustomLog "/var/log/httpd/pma_access.log" common
ErrorLog "/var/log/httpd/pma_error.log"
</VirtualHost>
<Directory "/web/vhosts/pma"> //httpd2.4版本必須爲網站目錄授權纔可訪問
AllowOverride None
Options None
Require all granted
</Directory>
[root@localhost php-5.6.4]# mkdir -pv /var/log/httpd //創建日誌文件目錄
[root@localhost php-5.6.4]# mkdir -pv /web/vhosts/pma //創建網站根目錄
mkdir: created directory `/web'
mkdir: created directory `/web/vhosts'
mkdir: created directory `/web/vhosts/pma'
[root@localhost php-5.6.4]# httpd -t //檢查配置文件是否有誤
[root@localhost pma]# /etc/rc.d/init.d/httpd24 restart //重啓httpd服務
[root@localhost php-5.6.4]# mv /usr/local/src/phpMyAdmin-4.0.5-all-languages/* /web/vhosts/pma/
//將程序文件移動到網站根目錄下
//配置phpMyadmin配置文件
[root@localhost pma]# cp config.sample.inc.php config.inc.php
//將配置文件模板複製爲配置文件(去掉sample)
[root@localhost pma]# vim config.inc.php //編輯配置文件
$cfg['blowfish_secret'] = 'aadf$#23SDA$%8b7c6d'; //在此項添加一個隨機數
編輯測試主機hosts文件(C:\Windows\System32\drivers\etc),添加如下信息,並保存
172.16.113.14pma.su.com
在瀏覽器輸入pma.su.com訪問即可。
14、爲pma.su.com提供https服務:
a>搭建ca服務器:
IP地址爲172.16.113.101
[root@CentOS5 CA]# ls //保證/etc/pki/CA目錄下有這些文件及目錄,並在serial中的第一行寫入01
[root@CentOS5 CA]# (umask 077;openssl genrsa -out private/cakey.pem -out cacert.pem -days 7300)
//生成CA的私鑰
[root@CentOS5 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 7300
//生成CA的自簽證書
b>在httpd服務器生成私鑰,並生成證書申請請求:
[root@localhost ~]# mkdir /etc/httpd24/ssl
[root@localhost ~]# cd /etc/httpd24/ssl
[root@localhost ssl]# (umask 077;openssl genrsa -out httpd.key 2048)
//證書申請請求需填寫國家等信息,需要注意的是,下面此項必須和網站的FQDN完全一致
Common Name (eg, your name or your server's hostname) []:pma.su.com
[root@localhost ssl]# scp httpd.csr [email protected]:/tmp/ //將證書申請請求發送至CA的tmp目錄下
c>在CA服務器簽發證書,併發送給httpd服務器
[root@CentOS5 CA]# openssl ca -in /tmp/httpd.csr -out certs/pma.su.com.crt -days 365 //CA簽發證書
[root@CentOS5 CA]# scp certs/pma.su.com.crt [email protected]:/etc/httpd24/ssl
d>配置
[root@localhost ssl]# vim /etc/httpd24/extra/httpd-ssl.conf
ServerName pma.su.com
ServerAdmin [email protected]
ErrorLog "/var/logs/pma_error_log"
TransferLog "/var/logs/pma_access_log"
SSLCertificateFile "/etc/httpd24/ssl/pma.su.com.crt"
SSLCertificateKeyFile "/etc/httpd24/ssl/httpd.key"
e>編輯Apache配置文件,將下面幾項註釋去掉
Include /etc/httpd24/extra/httpd-ssl.conf
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so
f>將CA的證書導出至測試機的瀏覽器進行測試即可
15、安裝PHP加速器:
原理:php的執行需要兩步:1、編譯 2、執行 ,其中編譯得過程又分爲三個步驟:1、掃描(查看是否存在語法錯誤);2、分析(分析哪些是php代碼,哪些是數據);3、編譯(編譯成opcode);PHP加速器的原理是將php代碼編譯後的opcode緩存至內存中,並共享給其他請求使用,這樣節省了php代碼的執行步驟,從而實現了加速功能。
測試1:
安裝前,可使用ab命令測試httpd服務器每秒可以相應多少個請求:
模仿20個用戶,1000個併發請求
此時,httpd服務器每秒種可響應33個請求。
安裝:
[root@localhost ~]# cd /usr/local/src/xcache-3.2.0/ //切換至xcache的展開目錄
[root@localhost xcache-3.2.0]# /usr/local/php/bin/phpize //生成configure文件
[root@localhost xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
// 指明編譯所用到的php配置文件的位置
[root@localhost xcache-3.2.0]# make && make install
[root@localhost xcache-3.2.0]# mkdir /etc/php.d //創建php的配置掃描路徑
[root@localhost xcache-3.2.0]# cp xcache.ini /etc/php.d/ //拷貝xcache源碼包中的配置文件到php.d目錄
[root@localhost xcache-3.2.0]# vim /etc/php.d/xcache.ini 將其中的extension項的庫文件指向,指向安裝xcache最後顯示的路徑
例如:/usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so
[root@localhost xcache-3.2.0]# service httpd24 restart //重啓httpd
測試:模仿20個用戶,1000個併發請求。
由此可見,加速效果已達到。
16、再次創建一個機遇域名的虛擬主機,搭建wordpress論壇:
[root@localhost ~]# vim /etc/httpd24/vhosts.conf //編輯配置文件
<VirtualHost 172.16.113.14:80>
DocumentRoot /web/vhosts/wp
ServerName wp.su.com
CustomLog "/var/log/httpd/wp_access.log" common
ErrorLog "/var/log/httpd/wp_error.log"
<Directory "/">
AllowOverride None
Options None
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# mkdir /web/vhosts/wp
[root@localhost ~]# unzip wordpress-4.3.1-zh_CN.zip -d /web/vhosts/wp/ //將WordPress解壓至wp.su.com的網站根目錄中
[root@localhost ~]# cd /web/vhosts/wp
[root@localhost wp]# mv wordpress/* ./ //將文件全部移動出來
[root@localhost wp]# cp wp-config-sample.php wp-config.php //將配置模板複製爲配置文件
[root@localhost wp]# vim wp-config.php //編輯配置文件
[root@localhost wp]# /usr/local/mysql/bin/mysql -uroot -hlocalhost -p //登錄數據庫
MariaDB [(none)]> create database wpdb; //創建數據庫‘wpdb’
在測試主機的hosts文件添加:
172.16.113.14 wp.su.com
在瀏覽器輸入主機名訪問進行配置即可。