[php][linux]編譯安裝PHP7及擴展


https://www.cnblogs.com/lalalagq/p/9973716.html

一、編譯安裝PHP

1. 下載源碼包並解壓

源碼包地址:http://php.net/downloads.php

下載源碼包

當前PHP最新本門是7.2.9,下載 php-7.2.9 源碼包


wget http://be2.php.net/get/php-7.2.9.tar.gz/from/this/mirror -O php-7.2.9.tar.gz 

解壓源碼包

解壓縮


tar zxf php-7.2.9.tar.gz 

進入解壓縮后的目錄,查看解壓的文件


[root/usr/local/src/php-7.2.9]# ll 總用量 4.1M -rw-rw-r-- 1 root root 84K 814 14:26 acinclude.m4 -rw-r--r-- 1 root root 309K 814 14:26 aclocal.m4 drwxrwxr-x 2 root root 78 814 14:26 appveyor/ -rw-rw-r-- 1 root root 1.4K 814 14:26 .appveyor.yml drwxrwxr-x 2 root root 4.0K 814 14:26 build/ -rwxrwxr-x 1 root root 772 814 14:26 buildconf* -rw-rw-r-- 1 root root 334 814 14:26 buildconf.bat -rw-rw-r-- 1 root root 12K 814 14:26 CODING_STANDARDS -rw-rw-r-- 1 root root 42K 814 14:26 config.guess -rw-rw-r-- 1 root root 36K 814 14:26 config.sub -rwxr-xr-x 1 root root 2.6M 814 14:26 configure* -rw-rw-r-- 1 root root 46K 814 14:26 configure.ac -rw-rw-r-- 1 root root 3.4K 814 14:26 CONTRIBUTING.md -rw-rw-r-- 1 root root 91 814 14:26 CREDITS -rw-rw-r-- 1 root root 867 814 14:26 .editorconfig drwxrwxr-x 76 root root 4.0K 814 14:26 ext/ -rw-rw-r-- 1 root root 21K 814 14:26 EXTENSIONS -rw-rw-r-- 1 root root 137 814 14:26 footer -rw-rw-r-- 1 root root 13K 814 14:26 .gdbinit -rw-r--r-- 1 root root 1.8K 814 14:26 generated_lists -rwxrwxr-x 1 root root 581 814 14:26 genfiles* -rw-rw-r-- 1 root root 8.7K 814 14:26 .gitattributes -rw-rw-r-- 1 root root 4.3K 814 14:26 .gitignore -rw-rw-r-- 1 root root 1.2K 814 14:26 header -rw-rw-r-- 1 root root 86K 814 14:26 INSTALL -rw-r--r-- 1 root root 0 814 14:26 install-sh -rw-rw-r-- 1 root root 3.2K 814 14:26 LICENSE -rw-rw-r-- 1 root root 196K 814 14:26 ltmain.sh drwxrwxr-x 3 root root 4.0K 814 14:26 main/ -rwxrwxr-x 1 root root 4.1K 814 14:26 makedist* -rw-rw-r-- 1 root root 1.1K 814 14:26 Makefile.frag -rw-rw-r-- 1 root root 2.5K 814 14:26 Makefile.gcov -rw-rw-r-- 1 root root 7.0K 814 14:26 Makefile.global -rw-r--r-- 1 root root 0 814 14:26 missing -rw-r--r-- 1 root root 0 814 14:26 mkinstalldirs -rw-rw-r-- 1 root root 90K 814 14:26 NEWS drwxrwxr-x 2 root root 96 814 14:26 pear/ -rw-rw-r-- 1 root root 1.5K 814 14:26 php7.spec.in -rw-rw-r-- 1 root root 2.5K 814 14:26 php.gif -rw-rw-r-- 1 root root 69K 814 14:26 php.ini-development -rw-rw-r-- 1 root root 69K 814 14:26 php.ini-production -rw-rw-r-- 1 root root 6.9K 814 14:26 README.EXT_SKEL -rw-rw-r-- 1 root root 5.0K 814 14:26 README.GIT-RULES -rw-rw-r-- 1 root root 5.3K 814 14:26 README.input_filter -rw-rw-r-- 1 root root 3.4K 814 14:26 README.MAILINGLIST_RULES -rw-rw-r-- 1 root root 1.6K 814 14:26 README.md -rw-rw-r-- 1 root root 5.2K 814 14:26 README.NEW-OUTPUT-API -rw-rw-r-- 1 root root 7.4K 814 14:26 README.PARAMETER_PARSING_API -rw-rw-r-- 1 root root 20K 814 14:26 README.REDIST.BINS -rw-rw-r-- 1 root root 15K 814 14:26 README.RELEASE_PROCESS -rw-rw-r-- 1 root root 5.0K 814 14:26 README.SELF-CONTAINED-EXTENSIONS -rw-rw-r-- 1 root root 15K 814 14:26 README.STREAMS -rw-rw-r-- 1 root root 8.0K 814 14:26 README.SUBMITTING_PATCH -rw-rw-r-- 1 root root 6.6K 814 14:26 README.TESTING -rw-rw-r-- 1 root root 4.9K 814 14:26 README.TESTING2 -rw-rw-r-- 1 root root 4.2K 814 14:26 README.UNIX-BUILD-SYSTEM -rw-rw-r-- 1 root root 115 814 14:26 README.WIN32-BUILD-SYSTEM -rwxrwxr-x 1 root root 84K 814 14:26 run-tests.php* drwxrwxr-x 10 root root 110 814 14:26 sapi/ drwxrwxr-x 4 root root 99 814 14:26 scripts/ -rwxrwxr-x 1 root root 2.1K 814 14:26 server-tests-config.php* -rwxrwxr-x 1 root root 52K 814 14:26 server-tests.php* -rwxrwxr-x 1 root root 108 814 14:26 snapshot* -rw-rw-r-- 1 root root 10 814 14:26 stamp-h.in drwxrwxr-x 10 root root 133 814 14:26 tests/ drwxrwxr-x 3 root root 33 814 14:26 travis/ -rw-rw-r-- 1 root root 1.9K 814 14:26 .travis.yml drwxrwxr-x 3 root root 4.0K 814 14:26 TSRM/ -rw-rw-r-- 1 root root 15K 814 14:26 UPGRADING -rw-rw-r-- 1 root root 3.3K 814 14:26 UPGRADING.INTERNALS -rwxrwxr-x 1 root root 159 814 14:26 vcsclean* drwxrwxr-x 3 root root 4.0K 814 14:26 win32/ drwxrwxr-x 3 root root 8.0K 814 14:26 Zend/ 

2. 編譯安裝

首先安裝PHP編譯安裝所必須的軟件


[root/usr/local/src/php-7.2.9]# yum -y install gcc gcc++ libxml2-devel libicu-devel 

configure指定安裝目錄,啟用fpm


[root/usr/local/src/php-7.2.9]# ./configure --prefix=/usr/local/php7 --enable-fpm 

編譯安裝


[root/usr/local/src/php-7.2.9]# make && make install ...省略... Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/ Installing PHP CLI binary: /usr/local/php7/bin/ Installing PHP CLI man page: /usr/local/php7/php/man/man1/ Installing PHP FPM binary: /usr/local/php7/sbin/ Installing PHP FPM defconfig: /usr/local/php7/etc/ Installing PHP FPM man page: /usr/local/php7/php/man/man8/ Installing PHP FPM status page: /usr/local/php7/php/php/fpm/ Installing phpdbg binary: /usr/local/php7/bin/ Installing phpdbg man page: /usr/local/php7/php/man/man1/ Installing PHP CGI binary: /usr/local/php7/bin/ Installing PHP CGI man page: /usr/local/php7/php/man/man1/ Installing build environment: /usr/local/php7/lib/php/build/ Installing header files: /usr/local/php7/include/php/ Installing helper programs: /usr/local/php7/bin/ program: phpize program: php-config Installing man pages: /usr/local/php7/php/man/man1/ page: phpize.1 page: php-config.1 Installing PEAR environment: /usr/local/php7/lib/php/ [PEAR] Archive_Tar - installed: 1.4.3 [PEAR] Console_Getopt - installed: 1.4.1 [PEAR] Structures_Graph- installed: 1.1.1 [PEAR] XML_Util - installed: 1.4.2 [PEAR] PEAR - installed: 1.10.5 Warning! a PEAR user config file already exists from a previous PEAR installation at '/root/.pearrc'. You may probably want to remove it. Wrote PEAR system config file at: /usr/local/php7/etc/pear.conf You may want to add: /usr/local/php7/lib/php to your php.ini include_path /usr/local/src/php-7.2.9/build/shtool install -c ext/phar/phar.phar /usr/local/php7/bin ln -s -f phar.phar /usr/local/php7/bin/phar Installing PDO headers: /usr/local/php7/include/php/ext/pdo/ 

3. 為php命令建立軟鏈接,加入到環境變量中


[root/usr/local/src/php-7.2.9]# ln -s /usr/local/php7/bin/php /usr/local/bin/php 

4. 創建配置文件,並將其復制到正確的位置

查看PHP基本信息


[root/usr/local/src/php-7.2.9]# php -ini phpinfo() PHP Version => 7.2.9 System => Linux 10.0.2.15 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 Build Date => Aug 17 2018 09:09:29 Configure Command => './configure' '--prefix=/usr/local/php7' '--enable-fpm' Server API => Command Line Interface Virtual Directory Support => disabled Configuration File (php.ini) Path => /usr/local/php7/lib Loaded Configuration File => (none) Scan this dir for additional .ini files => (none) Additional .ini files parsed => (none) PHP API => 20170718 PHP Extension => 20170718 Zend Extension => 320170718 Zend Extension Build => API320170718,NTS PHP Extension Build => API20170718,NTS ...... 

將php.ini復制到 Configuration File (php.ini) Path


[root/usr/local/src/php-7.2.9]# cp php.ini-development /usr/local/php7/lib/php.ini 

5. 配置php-fpm

為php-fpm命令建立軟鏈接,加入到環境變量中


[root/usr/local/src/php-7.2.9]$ ln -s /usr/local/php7/sbin/php-fpm /usr/local/sbin/php-fpm 

復制php配置文件目錄下的 php-fpm.conf.default,並重命名為 php-fpm.conf


[root/usr/local/src/php-7.2.9]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf 

復制php配置文件目錄下的 php-fpm.d/www.conf.default,並重命名為 php-fpm.d/www.conf


[root/usr/local/src/php-7.2.9]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf 

編輯 php-fpm.d/www.conf,設置 php-fpm 模塊使用 www-data 用戶和 www-data 用戶組的身份運行。


vim /usr/local/php7/etc/php-fpm.d/www.conf user = www-data group = www-data 

需要着重提醒的是,如果文件不存在,則阻止 Nginx 將請求發送到后端的 PHP-FPM 模塊, 以避免遭受惡意腳本注入的攻擊

編輯 php.ini,文件中的配置項 cgi.fix_pathinfo 設置為 0 。


[root/usr/local/src/php-7.2.9]# vim /usr/local/php7/lib/php.ini cgi.fix_pathinfo=0 

啟動php-fpm


[root/usr/local/src/php-7.2.9]# php-fpm [root/usr/local/src/php-7.2.9]# ss -tlnp | grep php-fpm LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=4689,fd=5),("php-fpm",pid=4688,fd=5),("php-fpm",pid=4687,fd=7)) 

6. 配置 Nginx 使其支持 PHP 應用


[root/etc/nginx]# vim conf.d/default.conf server { listen 80; root /vagrant; location / { index index.php; } location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; } } 

創建 /vagrant/index.php,並填入 <?="Hello world!";


[root/etc/nginx]# echo '&lt;?="Hello world!";' &gt; /vagrant/index.php [root/etc/nginx]# cat /vagrant/index.php &lt;?="Hello world!"; 

啟動nginx


[root/etc/nginx]# nginx [root/etc/nginx]# ss -tlnp | grep nginx LISTEN 0 128 *:80 *:* users:(("nginx",pid=4725,fd=6),("nginx",pid=4724,fd=6)) 

7. 訪問 curl localhost,輸出 Hello world!,說明PHP + Nginx安裝成功


[root/etc/nginx]# curl localhost Hello world! 

二、編譯安裝PHP擴展

1. php擴展安裝流程

從 pecl.php.net 查找需要的擴展

選擇擴展的版本(注意查看擴展版本與PHP版本的兼容性)

解壓縮下載的文件

判斷文件的安裝類型


&gt; 直裝:(解壓出來就是 .so 文件),直接復制文件到擴展目錄,在php.ini中開啟相應的擴展即可 &gt; 編譯安裝:需要先進行編譯,再復制文件到擴展目錄,在php.ini中開啟相應的擴展 

2. 編譯安裝步驟

在解壓縮的擴展目錄下執行phpize

若 phpize 沒有加入到環境變量,則需要使用絕對路徑。例如 /usr/local/php7/bin/phpize
phpize是用來擴展php擴展模塊的。
通過phpize可以建立php的外掛模塊。

configure 配置編譯參數

主要是配置php配置文件參數,例如:


./configure --with-php-config=/usr/local/php7/bin/php-config 

若配置編譯參數執行過程中出現錯誤,則需要安裝 autoconf。
autoconf可以自動地配置軟件源代碼。

contos/redhat:yum install autoconf
ubuntu:apt-get install autoconf

編譯安裝


make &amp;&amp; make install 

復制 .so 擴展文件到PHP擴展目錄(若已經自動復制,則忽略)

在php.ini中開啟擴展,配置相應的擴展參數

3. 安裝redis擴展實例

php -m查看已安裝的php擴展


[root/usr/local/src/php-7.2.9]# php -m [PHP Modules] Core ctype date dom fileinfo filter hash iconv json libxml pcre PDO pdo_sqlite Phar posix Reflection session SimpleXML SPL sqlite3 standard tokenizer xml xmlreader xmlwriter [Zend Modules] 

從 pecl.php.net 下載 redis-4.1.1 擴展,並解壓縮


[root/usr/local/src]# wget https://pecl.php.net/get/redis-4.1.1.tgz --2018-08-17 10:00:41-- https://pecl.php.net/get/redis-4.1.1.tgz 正在解析主機 pecl.php.net (pecl.php.net)... 104.236.228.160 正在連接 pecl.php.net (pecl.php.net)|104.236.228.160|:443... 已連接。 已發出 HTTP 請求,正在等待回應... 200 OK 長度:220894 (216K) [application/octet-stream] 正在保存至: “redis-4.1.1.tgz” 100%[=========================================================================================================================================================================&gt;] 220,894 192KB/s 用時 1.1s 2018-08-17 10:00:44 (192 KB/s) - 已保存 “redis-4.1.1.tgz” [220894/220894]) [root/usr/local/src]# tar zxf redis-4.1.1.tgz [root/usr/local/src]# cd redis-4.1.1 

在解壓縮的擴展目錄下執行phpize


[root/usr/local/src/redis-4.1.1]# /usr/local/php7/bin/phpize Configuring for: PHP Api Version: 20170718 Zend Module Api No: 20170718 Zend Extension Api No: 320170718 

configure 配置編譯參數


[root/usr/local/src/redis-4.1.1]# ./configure --with-php-config=/usr/local/php7/bin/php-config 

編譯安裝


[root/usr/local/src/redis-4.1.1]# make &amp;&amp; make install 

查看編譯好的擴展文件 redis.so,已經自動復制到php擴展目錄下


[root/usr/local/src/redis-4.1.1]# ll /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/ 總用量 7.1M -rwxr-xr-x 1 root root 3.5M 817 09:15 opcache.a* -rwxr-xr-x 1 root root 1.9M 817 09:15 opcache.so* -rwxr-xr-x 1 root root 1.8M 817 10:06 redis.so* 

編輯 php.ini,添加 extension=redis


[root/usr/local/src/redis-4.1.1]# vim /usr/local/php7/lib/php.ini extension=redis 

此時再使用php -m查看已安裝的php擴展,redis已經在擴展列表中


[root/usr/local/src/redis-4.1.1]# php -m [PHP Modules] Core ctype date dom fileinfo filter hash iconv json libxml pcre PDO pdo_sqlite Phar posix redis Reflection session SimpleXML SPL sqlite3 standard tokenizer xml xmlreader xmlwriter [Zend Modules] 

3. 安裝xdebug擴展實例

從 pecl.php.net 下載 xdebug-2.6.1 擴展,並解壓縮


[root/usr/local/src]# wget https://pecl.php.net/get/xdebug-2.6.1.tgz [root/usr/local/src]# tar zxf xdebug-2.6.1.tgz [root/usr/local/src]# cd xdebug-2.6.1 

在解壓縮的擴展目錄下執行phpize


[root/usr/local/src/xdebug-2.6.1]# /usr/local/php7/bin/phpize Configuring for: PHP Api Version: 20170718 Zend Module Api No: 20170718 Zend Extension Api No: 320170718 

configure 配置編譯參數


[root/usr/local/src/xdebug-2.6.1]# ./configure --with-php-config=/usr/local/php7/bin/php-config 

編譯安裝


[root/usr/local/src/xdebug-2.6.1]# make &amp;&amp; make install 

查看編譯好的擴展文件 redis.so,已經自動復制到php擴展目錄下


[root/usr/local/src/xdebug-2.6.1]# ll /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/ 總用量 8.4M -rwxr-xr-x 1 root root 3.5M 817 09:15 opcache.a* -rwxr-xr-x 1 root root 1.9M 817 09:15 opcache.so* -rwxr-xr-x 1 root root 1.8M 817 10:06 redis.so* -rwxr-xr-x 1 root root 1.3M 817 11:16 xdebug.so* 

編輯 php.ini,添加 zend_extension=xdebug


[root/usr/local/src/xdebug-2.6.1]# vim /usr/local/php7/lib/php.ini zend_extension=xdebug 

此時再使用php -m查看已安裝的php擴展,xdebug已經在擴展列表中


[root/usr/local/src/xdebug-2.6.1]# php -m [PHP Modules] Core ctype date dom fileinfo filter hash iconv json libxml pcre PDO pdo_sqlite Phar posix redis Reflection session SimpleXML SPL sqlite3 standard tokenizer xdebug xml xmlreader xmlwriter [Zend Modules] Xdebug 

原文地址:https://segmentfault.com/a/1190000016080151

 

安裝配置選項1:

./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-openssl --enable-pdo_mysql --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip
安裝配置選項2:

./configure --prefix=/www/server/php/74 --with-config-file-path=/www/server/php/74/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-zlib --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/usr/local/curl --enable-mbregex --enable-mbstring --enable-intl --enable-ftp --with-openssl=/usr/local/openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-gettext --enable-fileinfo --enable-opcache --with-sodium

 

PHP的擴展模塊安裝

模塊安裝總則:

  1. 進入到ext/目錄下對應的模塊
  2. 執行/usr/local/php/bin/phpize 也就是執行一遍phpize生成編譯文件
  3. ./configure --with-php-config=/opt/php5.6.27/bin/php-config
  4. Make && make install

php自身提供了很多擴展,比如curl,gmp, mbstring等等。我們在編譯安裝php時未必安裝了所有擴展。那么在安裝完php后,如果想單獨安裝某個php自身的擴展怎么辦呢?

php版本5.6.27

單獨安裝gd拓展模塊需要cd 到 php-5.6.27/ext/gd

執行 phpize 生成編譯文件,如果系統沒有引入phpize,這個文件在PHP安裝目錄下的bin中

在這里一定要先執行make clean,這次安裝了8台服務器,沒有先執行make clean的100%沒有安裝成功,都沒有添加到freetype support

./configure --with-php-config=/opt/php5.6.27/bin/php-config --with-jpeg-dir --with-png-dir --with-freetype-dir --with-zlib-dir --with-gd

一般情況,如果你正確的指定了php-config或者你的機器只有一個php版本,so都會被正確安裝到相應php的extension_dir。如果執行make install后,extension_dir目錄下沒有相應的so,可以手動將第5步中生成的so文件cp過去

確認freetype 2 在編譯中已被添加

make && make install

PHP安裝擴展模塊

PHP bcmath擴展(PHP配置參數--enable-bcmath)。

安裝

BC數學函數僅在 PHP 編譯時配置了 --enable-bcmath 時可用。

PHP 的 Windows 版本已內建對此擴展的支持。

PHP 的 Linux 版本,則需要手動安裝bcmath擴展。

在PHP的源碼包中,默認就包含bcmath擴展的安裝文件,只需手動安裝一下即可。

1 cd /soft/php-5.5.38/ext/bcmath// 進入PHP的源碼包目錄中的bcmatch擴展目錄
1 /usr/local/php-5.5.38/bin/phpize   // 執行phpize命令
./configure --with-php-config=/usr/local/php-5.5.38/bin/php-config  // 配置
1 make && make install

// 編譯和安裝

安裝完成后,可以得到bcmath.so文件,將該文件拷貝至php的擴展存放目錄。

然后,修改php的配置文件php.ini,加入如下的代碼:

extension=bcmath.so

最后,重啟php服務即可。

1 systenctl restart php-fpm

 


免責聲明!

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



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