Apache升級PHP教程(以5.3.3升級到5.6.30為例)


最簡單的LAMP環境搭建當然是通過yum來安裝,但由於鏡像倉庫中的軟件版本更新較慢,經常會遇到版本過舊的問題,尤其是安裝一些新版本的CMS時的PHP。

這時我們需要手動編譯PHP,Linux編譯安裝經常是./configure--make--make install三大步,但是實踐中發現簡單地這樣安裝會發現通過php -v呈現的版本號已經改變但是通過phpinfo()函數查看到的httpd使用的php版本還是沒有改變。

回顧apache引入php支持的配置過程,其中最主要的是在httpd.conf中配置了LoadModule php5_module modules/libphp5.so,結合系統php已更新而apache中的php卻未更新的現像,可以推測apache調用php並不是直接調用系統的php而就是調用libphp5.so;新編譯安裝php未成功替換掉apache中的php的原因就是沒編譯生成新的libphp5.so去替換舊的libphp5.so。

而最終的測試可以驗證這種推測是正確的。以下是Apache升級PHP的步驟,這里有些操作比較粗操比如提示未知的編譯選項--with-exec-dir等並未理會,生產環境按此升級需謹慎。

 

1.下載php

下載鏈接:http://php.net/downloads.php

選擇需要版本下載,5.x版本和7.x版本還是有點區別的,建議原先是5.x的選擇5.x版本下載7.x的版本選擇7.x下載

 

2.安裝apxs

apxs是apache擴展的編譯程序,要確保apxs已安裝

which apxs

如果不存在該命令則通過安裝httpd-devel來安裝:

裝完后再次使用which命令定位apxs安裝路徑:

 

3.查看當前使用php編譯參數

在apache網站根目錄下寫個phpinfo文件,然后在瀏覽器訪問

cat > phpinfo.php << EOF
<?php
    phpinfo();
?>
EOF

Configure Command右側即當前PHP編譯命令,我們使用一樣的命令編譯新的PHP即可(注意--with-apxs2的值是否上步安裝的apxs所在的路徑)

 

4.編譯安裝PHP

將壓下載的PHP進入解壓出的目錄,使用./configure--make--make install三大步進行編譯安裝

./configure就直接復制上邊查到的,單引號不影響的,有可能有些包沒安裝安裝即可,有可能有些選項新版不支持這里我沒管。如我這里使用:

'./configure' '--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu' '--target=x86_64-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/var/lib' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib64' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--with-xpm-dir=/usr' '--enable-gd-native-ttf' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--with-kerberos' '--enable-ucd-snmp-hack' '--enable-shmop' '--enable-calendar' '--without-sqlite' '--with-libxml-dir=/usr' '--enable-xml' '--with-system-tzdata' '--with-apxs2=/usr/sbin/apxs' '--without-mysql' '--without-gd' '--disable-dom' '--disable-dba' '--without-unixODBC' '--disable-pdo' '--disable-xmlreader' '--disable-xmlwriter' '--without-sqlite3' '--disable-phar' '--disable-fileinfo' '--disable-json' '--without-pspell' '--disable-wddx' '--without-curl' '--disable-posix' '--disable-sysvmsg' '--disable-sysvshm' '--disable-sysvsem'

make

make install

configure報錯:checking for BZip2 in default path... not found

解決辦法:yum install -y gzip2-devel

configure報錯:configure: error: Unable to locate gmp.h

解決辦法:yum install -y gmp-devel

make報錯:ext/pcre/.libs/php_pcre.o: In function `zm_info_pcre': /usr/myapp/php-5.6.30/ext/pcre/php_pcre.c:133: undefined reference to `php_pcre_version'....

解決辦法:看意思是結構體沒定義,可能是php_pcre.c沒正確include但這可能性不大,也可能是安裝的pcre版本太舊;不太清楚,最后我直接把./configure中的--with-pcre-regex=/usr去掉了,慎用。

 

5.再次查看phpinfo.php頁面確認升級成功

先重啟apache再刷新頁面

service httpd restart

 

參考:

https://segmentfault.com/q/1010000005115503/a-1020000005116821

http://blog.csdn.net/work888study/article/details/8610467

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM