Centos7 php 5.6.19編譯安裝


0x01  前言

在php官網下載php-5.6.19.tar.gz源代碼(php7雖然說性能提升很大,但是小菜菜還是先用着這個先吧),解壓后根目錄有個INSTALL文件,里面有安裝教程了,目錄如下:

Installing PHP
__________________________________________________________________

* General Installation Considerations
* Installation on Unix systems
+ Apache 1.3.x on Unix systems
+ Apache 2.x on Unix systems
+ Lighttpd 1.4 on Unix systems
+ Sun, iPlanet and Netscape servers on Sun Solaris
+ CGI and command line setups
+ HP-UX specific installation notes
+ OpenBSD installation notes
+ Solaris specific installation tips
+ Debian GNU/Linux installation notes
* Installation on Mac OS X
+ Using Packages
+ Using the bundled PHP
+ Compiling PHP on Mac OS X
* Installation of PECL extensions
+ Introduction to PECL Installations
+ Downloading PECL extensions
+ Installing a PHP extension on Windows
+ Compiling shared PECL extensions with the pecl command
+ Compiling shared PECL extensions with phpize
+ php-config
+ Compiling PECL extensions statically into PHP
* Problems?
+ Read the FAQ
+ Other problems
+ Bug reports
* Runtime Configuration
+ The configuration file
+ .user.ini files
+ Where a configuration setting may be set
+ How to change configuration settings
* Installation
__________________________________________________________________

對應你當前的系統,查看相應的內容,當前我的環境應該參考Apache 2.x on Unix systems這部分

Apache 2.x on Unix systems

This section contains notes and hints specific to Apache 2.x installs
of PHP on Unix systems.
Warning

We do not recommend using a threaded MPM in production with Apache 2.
Use the prefork MPM, which is the default MPM with Apache 2.0 and 2.2.
For information on why, read the related FAQ entry on using Apache2
with a threaded MPM

The » Apache Documentation is the most authoritative source of
information on the Apache 2.x server. More information about
installation options for Apache may be found there.

The most recent version of Apache HTTP Server may be obtained from
» Apache download site, and a fitting PHP version from the above
mentioned places. This quick guide covers only the basics to get
started with Apache 2.x and PHP. For more information read the » Apache
Documentation. The version numbers have been omitted here, to ensure
the instructions are not incorrect. In the examples below, 'NN' should
be replaced with the specific version of Apache being used.

There are currently two versions of Apache 2.x - there's 2.0 and 2.2.
While there are various reasons for choosing each, 2.2 is the current
latest version, and the one that is recommended, if that option is
available to you. However, the instructions here will work for either
2.0 or 2.2.
1. Obtain the Apache HTTP server from the location listed above, and
unpack it:
gzip -d httpd-2_x_NN.tar.gz
tar -xf httpd-2_x_NN.tar

2. Likewise, obtain and unpack the PHP source:
gunzip php-NN.tar.gz
tar -xf php-NN.tar

3. Build and install Apache. Consult the Apache install documentation
for more details on building Apache.
cd httpd-2_x_NN
./configure --enable-so
make
make install

4. Now you have Apache 2.x.NN available under /usr/local/apache2,
configured with loadable module support and the standard MPM
prefork. To test the installation use your normal procedure for
starting the Apache server, e.g.:
/usr/local/apache2/bin/apachectl start

and stop the server to go on with the configuration for PHP:
/usr/local/apache2/bin/apachectl stop

5. Now, configure and build PHP. This is where you customize PHP with       這里開始就是php的安裝,上面說的都是apache
various options, like which extensions will be enabled. Run
./configure --help for a list of available options. In our example
we'll do a simple configure with Apache 2 and MySQL support.
If you built Apache from source, as described above, the below
example will match your path for apxs, but if you installed Apache
some other way, you'll need to adjust the path to apxs accordingly.
Note that some distros may rename apxs to apxs2.
cd ../php-NN
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql
make
make install

If you decide to change your configure options after installation,
you'll need to re-run the configure, make, and make install steps.         如果需要改變設置就重新安裝
You only need to restart apache for the new module to take effect.
A recompile of Apache is not needed.
Note that unless told otherwise, 'make install' will also install
PEAR, various PHP tools such as phpize, install the PHP CLI, and
more.
6. Setup your php.ini
cp php.ini-development /usr/local/lib/php.ini

You may edit your .ini file to set PHP options. If you prefer
having php.ini in another location, use
--with-config-file-path=/some/path in step 5.                                        自定義配置文件路徑
If you instead choose php.ini-production, be certain to read the
list of changes within, as they affect how PHP behaves.
7. Edit your httpd.conf to load the PHP module. The path on the right
hand side of the LoadModule statement must point to the path of the
PHP module on your system. The make install from above may have
already added this for you, but be sure to check.
LoadModule php5_module modules/libphp5.so                               在httpd配置文件中添加語句加載php模塊
8. Tell Apache to parse certain extensions as PHP. For example, let's
have Apache parse .php files as PHP. Instead of only using the
Apache AddType directive, we want to avoid potentially dangerous   只使用AddType指令具有潛在危險
uploads and created files such as exploit.php.jpg from being
executed as PHP. Using this example, you could have any
extension(s) parse as PHP by simply adding them. We'll add .php to
demonstrate.
<FilesMatch \.php$>                                 正則表達式,匹配所有“.php”結尾的文件名
SetHandler application/x-httpd-php
</FilesMatch>
Or, if we wanted to allow .php, .php2, .php3, .php4, .php5, .php6,
and .phtml files to be executed as PHP, but nothing else, we'd use
this:
<FilesMatch "\.ph(p[2-6]?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
And to allow .phps files to be handled by the php source filter,
and displayed as syntax-highlighted source code, use this:
<FilesMatch "\.phps$">

SetHandler application/x-httpd-php-source
</FilesMatch>
mod_rewrite may be used To allow any arbitrary .php file to be
displayed as syntax-highlighted source code, without having to
rename or copy it to a .phps file:
RewriteEngine On
RewriteRule (.*\.php)s$ $1 [H=application/x-httpd-php-source]
The php source filter should not be enabled on production systems,
where it may expose confidential or otherwise sensitive information
embedded in source code.
9. Use your normal procedure for starting the Apache server, e.g.:
/usr/local/apache2/bin/apachectl start

OR
service httpd restart

Following the steps above you will have a running Apache2 web server
with support for PHP as a SAPI module. Of course there are many more
configuration options available Apache and PHP. For more information
type ./configure --help in the corresponding source tree.

Apache may be built multithreaded by selecting the worker MPM, rather
than the standard prefork MPM, when Apache is built. This is done by
adding the following option to the argument passed to ./configure, in
step 3 above:
--with-mpm=worker

This should not be undertaken without being aware of the consequences
of this decision, and having at least a fair understanding of the
implications. The Apache documentation regarding » MPM-Modules
discusses MPMs in a great deal more detail.

Note:

The Apache MultiViews FAQ discusses using multiviews with PHP.

Note:

To build a multithreaded version of Apache, the target system must
support threads. In this case, PHP should also be built with
experimental Zend Thread Safety (ZTS). Under this configuration, not
all extensions will be available. The recommended setup is to build
Apache with the default prefork MPM-Module.
__________________________________________________________________

0x02  php編譯選項

具體參數含義可以用./configure --help來查看。源自 http://blog.csdn.net/niluchen/article/details/41513217,還有一個官方的文檔參考http://php.net/manual/zh/configure.about.php

列表如下(部分參數未得到解釋):

# 指定 php 安裝目錄

--prefix=/usr/local/php 

# 指定php.ini位置

--with-config-file-path=/usr/local/php/etc 

# mysql安裝目錄,對mysql的支持

--with-mysql=/usr/local/mysql

mysqli擴展技術不僅可以調用MySQL的存儲過程、處理MySQL事務,而且還可以使訪問數據庫工作變得更加穩定。

--with-mysqli=/usr/local/mysql/bin/mysql_config   

整合 apache,apxs功能是使用mod_so中的LoadModule指令,加載指定模塊到 apache,要求 apache 要打開SO模塊

--with-apxs2=/usr/local/apache/bin/apxs 

# 選項指令 --with-iconv-dir 用於 PHP 編譯時指定 iconv 在系統里的路徑,否則會掃描默認路徑。

--with-iconv-dir=/usr/local 

--with-freetype-dir   打開對freetype字體庫的支持 

--with-jpeg-dir   打開對jpeg圖片的支持 

--with-png-dir   打開對png圖片的支持 

--with-zlib-dir   打開zlib庫的支持,用於http壓縮傳輸

--with-libxml-dir   打開libxml2庫的支持

--disable-rpath    關閉額外的運行庫文件 

--enable-bcmath    打開圖片大小調整,用到zabbix監控的時候用到了這個模塊

--enable-shmop --enable-sysvsem  這樣就使得你的PHP系統可以處理相關的IPC函數了。

--enable-inline-optimization  優化線程

--with-curl    打開curl瀏覽工具的支持 

--with-curlwrappers    運用curl工具打開url流 

--enable-mbregex

--enable-fpm 打上PHP-fpm 補丁后才有這個參數,CGI方式安裝的啟動程序

--enable-mbstring    多字節,字符串的支持 

--with-mcrypt                    mcrypt算法擴展

--with-mhash                     mhash算法擴展

--with-gd    打開gd庫的支持 

--enable-gd-native-ttf   支持TrueType字符串函數庫

--with-openssl      openssl的支持,加密傳輸https時用到的

--enable-pcntl   freeTDS需要用到的,可能是鏈接mssql 才用到

--enable-sockets     打開 sockets 支持

--with-xmlrpc    打開xml-rpc的c語言 

--enable-zip   打開對zip的支持 

--enable-ftp   打開ftp的支持 

--with-bz2    打開對bz2文件的支持        

--without-iconv   關閉iconv函數,字符集間的轉換 

--with-ttf     打開freetype1.*的支持,可以不加了 

--with-xsl     打開XSLT 文件支持,擴展了libXML2庫 ,需要libxslt軟件 

--with-gettext     打開gnu 的gettext 支持,編碼庫用到 

--with-pear    打開pear命令的支持,PHP擴展用的 

--enable-calendar    打開日歷擴展功能

--enable-exif    圖片的元數據支持 

--enable-magic-quotes    魔術引用的支持 

--disable-debug    關閉調試模式 

--with-mime-magic=/usr/share/file/magic.mime      魔術頭文件位置

 

CGI方式安裝才用的參數

--enable-fastCGI            支持fastcgi方式啟動PHP

--enable-force-CGI-redirect        重定向方式啟動PHP

--with-ncurses         支持ncurses 屏幕繪制以及基於文本終端的圖形互動功能的動態庫

--with-gmp  應該是支持一種規范

--enable-dbase                     建立DBA 作為共享模塊

--with-pcre-dir=/usr/local/bin/pcre-config      perl的正則庫案安裝位置

--disable-dmalloc

--with-gdbm                     dba的gdbm支持

--enable-sigchild

--enable-sysvshm

--enable-zend-multibyte         支持zend的多字節

--enable-wddx

--enable-soap

0x03  安裝過程

[root@localhost php-5.6.19]# ./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 --enable-xml --enable-sockets --with-apxs2=/usr/local/apache2/bin/apxs --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts

1、按照上面選項編譯報錯

checking libxml2 install dir... yes
checking for xml2-config path... 
configure: error: xml2-config not found. Please check your libxml2 installation.

顯示內容是xml2-config找不到,發現libxml2已經安裝但是安裝文件沒有xml2-config,安裝libxml2-devel后

[root@localhost php-5.6.19]# rpm -ql libxml2-devel
/usr/bin/xml2-config
……

2、繼續編譯,報錯如下

checking for BZip2 support... yes
checking for BZip2 in default path... not found
configure: error: Please reinstall the BZip2 distribution

安裝bzip2-devel

[root@localhost php-5.6.19]# yum install bzip2-devel

3、繼續編譯,報錯如下

checking for specified location of the MySQL UNIX socket... no
configure: error: Cannot find libmysqlclient_r under /usr/local/mysql/.
Note that the MySQL client library is not bundled anymore!

對於這個問題有個比較久遠的帖子 http://blog.chinaunix.net/uid-10697776-id-2935536.html有提及,這個方法我沒有測試,另一種說就是因為64位的linux的lib路徑有問題加上--with-libdir=lib的編譯選項,這個試過加上也是同樣的編譯報錯。

最終找到一個可行的辦法就是,編譯之前,先處理一下mysql的庫,默認查找libmysqlclient_r.so,可是mysql默認為libmysqlclient.so,內容完全一樣,做個鏈接即可
# cd /usr/local/mysql/lib/mysql/ 
# ln -s libmysqlclient.so.15.0.0 libmysqlclient_r.so

最后成功解決,可以make了,視個人系統環境不同,可能編譯出錯也不同,其時就是少什么庫,就安裝什么,對於編譯錯誤找到一個總結比較好的帖子http://www.cnblogs.com/alexqdh/archive/2012/11/20/2776017.html

Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

make 完成之后,信息提示如下:

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
directorytreeiterator.inc
invertedregexiterator.inc
directorygraphiterator.inc
pharcommand.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

最終make install 安裝完成

[root@localhost php-5.6.19]# make install
Installing PHP SAPI module: apache2handler
/usr/local/apache2/build/instdso.sh SH_LIBTOOL='/usr/local/apr/build-1/libtool' libphp5.la /usr/local/apache2/modules
/usr/local/apr/build-1/libtool --mode=install install libphp5.la /usr/local/apache2/modules/
libtool: install: install .libs/libphp5.so /usr/local/apache2/modules/libphp5.so
libtool: install: install .libs/libphp5.lai /usr/local/apache2/modules/libphp5.la
libtool: install: warning: remember to run `libtool --finish /usr/local/src/php-5.6.19/libs'
chmod 755 /usr/local/apache2/modules/libphp5.so
[activating module `php5' in /etc/httpd/httpd.conf]
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20131226/
Installing PHP CLI binary: /usr/local/php/bin/
Installing PHP CLI man page: /usr/local/php/php/man/man1/
Installing PHP CGI binary: /usr/local/php/bin/
Installing PHP CGI man page: /usr/local/php/php/man/man1/
Installing build environment: /usr/local/php/lib/php/build/
Installing header files: /usr/local/php/include/php/
Installing helper programs: /usr/local/php/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /usr/local/php/lib/php/
[PEAR] Archive_Tar - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.3.0
[PEAR] PEAR - installed: 1.10.1
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/usr/local/src/php-5.6.19/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/

0x03 后續工作

查看apache配置文件,發現已經加載了php模塊

LoadModule alias_module modules/mod_alias.so
#LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php5_module modules/libphp5.so

在apache安裝目錄下modules里面有php安裝后新增的libphp5.so

[root@localhost php-5.6.19]# ll /usr/local/apache2/modules/libphp5.so
-rwxr-xr-x. 1 root root 34524225 3月 7 00:12 /usr/local/apache2/modules/libphp5.so

以上即可說明php已經正常安裝

1、在apache配置文件添加以下語句,實現php文件調用php模塊解析

 AddType application/x-httpd-php .php

2、復制php源碼根目錄下的配置文件到/etc

cp php.ini-production /etc/php.ini

至此php已經可以正常工作了

0x04 測試

因為apache編譯安裝的網頁文件目錄為/usr/local/apache2/htdocs

在其中新增php文件測試能否正常解析,新增經典測試語句

[root@localhost php-5.6.19]# cat /usr/local/apache2/htdocs/phpinfo.php
<?php
phpinfo();
?>

測試正常


免責聲明!

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



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