centos 编译安装php

1. 下载源码包并解压

源码包地址:http://php.net/downloads.php
下载源码包

[root@localhost src]# wget -P /usr/local/src/php https://www.php.net/distributions/php-7.4.6.tar.gz
--2020-06-02 13:09:42--  https://www.php.net/distributions/php-7.4.6.tar.gz
Resolving www.php.net (www.php.net)... 185.85.0.29, 2a02:cb40:200::1ad
Connecting to www.php.net (www.php.net)|185.85.0.29|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16501931 (16M) [application/octet-stream]
Saving to: ‘/usr/local/src/php/php-7.4.6.tar.gz’

78% [==================================================================================================================>                                ] 12,984,052   116KB/s  eta 30s 

解压安装包

[root@localhost php]# pwd
/usr/local/src/php
[root@localhost php]# ll
total 16116
-rw-r--r--. 1 root root 16501931 May 12 16:10 php-7.4.6.tar.gz
[root@localhost php]# tar zxf php-7.4.6.tar.gz 
[root@localhost php]# ll
total 16120
drwxrwxr-x. 16 root root     4096 May 12 16:09 php-7.4.6
-rw-r--r--.  1 root root 16501931 May 12 16:10 php-7.4.6.tar.gz

进入解压包,查看解压后文件

[root@localhost php]# cd php-7.4.6/
[root@localhost php-7.4.6]# ll
total 2916
drwxrwxr-x.  2 root root      82 May 12 16:09 appveyor
drwxrwxr-x.  4 root root     215 May 12 16:09 azure
-rw-rw-r--.  1 root root    3342 May 12 16:09 azure-pipelines.yml
drwxrwxr-x.  2 root root    4096 May 12 16:09 build
-rwxrwxr-x.  1 root root    4012 May 12 16:09 buildconf
-rw-rw-r--.  1 root root     346 May 12 16:09 buildconf.bat
-rw-rw-r--.  1 root root   12737 May 12 16:09 CODING_STANDARDS.md
-rwxr-xr-x.  1 root root 2390062 May 12 16:09 configure
-rw-rw-r--.  1 root root   43799 May 12 16:09 configure.ac
-rw-rw-r--.  1 root root   20278 May 12 16:09 CONTRIBUTING.md
drwxrwxr-x.  2 root root     218 May 12 16:09 docs
drwxrwxr-x. 74 root root    4096 May 12 16:09 ext
-rw-rw-r--.  1 root root   23726 May 12 16:09 EXTENSIONS
-rw-rw-r--.  1 root root    3204 May 12 16:09 LICENSE
drwxrwxr-x.  3 root root    4096 May 12 16:09 main
-rw-rw-r--.  1 root root   73020 May 12 16:09 NEWS
drwxrwxr-x.  2 root root     100 May 12 16:09 pear
-rw-rw-r--.  1 root root   72278 May 12 16:09 php.ini-development
-rw-rw-r--.  1 root root   72582 May 12 16:09 php.ini-production
-rw-rw-r--.  1 root root    4712 May 12 16:09 README.md
-rw-rw-r--.  1 root root   29589 May 12 16:09 README.REDIST.BINS
-rwxrwxr-x.  1 root root  126893 May 12 16:09 run-tests.php
drwxrwxr-x.  9 root root     105 May 12 16:09 sapi
drwxrwxr-x.  4 root root     105 May 12 16:09 scripts
drwxrwxr-x. 10 root root     142 May 12 16:09 tests
drwxrwxr-x.  2 root root      68 May 12 16:09 travis
drwxrwxr-x.  2 root root     140 May 12 16:09 TSRM
-rw-rw-r--.  1 root root   34344 May 12 16:09 UPGRADING
-rw-rw-r--.  1 root root   15789 May 12 16:09 UPGRADING.INTERNALS
drwxrwxr-x.  3 root root    4096 May 12 16:09 win32
drwxrwxr-x.  3 root root    8192 May 12 16:09 Zend

2. 编译安装
首先安装PHP编译安装所必须的软件

[root@localhost php-7.4.6]# yum -y install gcc gcc++ libxml2-devel libicu-devel

configure预编译

安装依赖包

yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel

编译 ./configure

configure预编译安装依赖包

./configure  --prefix=/usr/local/php --with-config-file-path=/usr/local/php/lib/  --disable-ipv6 --enable-mysqlnd --with-mysqli --with-pdo-mysql --enable-fpm --with-fpm-user=daemon --with-fpm-group=daemon --with-gd --with-iconv --with-zlib --enable-xml --enable-mbstring --with-openssl --with-gettext --with-curl --enable-opcache --with-bz2 --without-gdbm --enable-sockets --enable-zip

输出结果

Generating files
configure: patching main/php_config.h.in
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
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/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/phpdbg/phpdbg.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: main/php_config.h is unchanged
config.status: executing default commands

+--------------------------------------------------------------------+
| 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.

编译&安装

make && make install

结果输出

 -lxml2 -lxml2 -lxml2 -lz -lssl -lcrypto -lcrypt  -o sapi/cgi/php-cgi
Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
pharcommand.inc
invertedregexiterator.inc
directorytreeiterator.inc
directorygraphiterator.inc
phar.inc

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

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing PHP FPM defconfig:     /usr/local/php/etc/
Installing PHP FPM man page:      /usr/local/php/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/php/fpm/
Installing phpdbg binary:         /usr/local/php/bin/
Installing phpdbg 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
/usr/local/src/php/php-7.4.6/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/

3. 为php命令建立软链接,加入到环境变量中

[root@localhost php]# pwd
/usr/local/php
[root@localhost php]# ln -s /usr/local/php/bin/php /usr/local/bin/php

4. 创建配置文件,并将其复制到正确的位置

[root@localhost php]# php -i|more
phpinfo()
PHP Version => 7.4.6

System => Linux localhost.localdomain 3.10.0-1127.8.2.el7.x86_64 #1 SMP Tue May 12 16:57:42 UTC 2020 x86_64
Build Date => Jun  2 2020 15:47:19
Configure Command =>  './configure'  '--prefix=/usr/local/php' '--with-config-file-path=/usr/local/php/lib/' '--disable-ipv6' '--enable-mysqlnd' '--with-mysqli' '--with-pdo-mysql' '--enab
le-fpm' '--with-fpm-user=daemon' '--with-fpm-group=daemon' '--with-gd' '--with-iconv' '--with-zlib' '--enable-xml' '--enable-mbstring' '--with-openssl' '--with-gettext' '--with-curl' '--e
nable-opcache' '--with-bz2' '--without-gdbm' '--enable-sockets' '--enable-zip'
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /usr/local/php/lib/   // 配置文件
Loaded Configuration File => /usr/local/php/lib/php.ini
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)
PHP API => 20190902
PHP Extension => 20190902
Zend Extension => 320190902
Zend Extension Build => API320190902,NTS
PHP Extension Build => API20190902,NTS
Debug Build => no
Thread Safety => disabled
Zend Signal Handling => enabled
Zend Memory Manager => enabled
Zend Multibyte Support => provided by mbstring
IPv6 Support => disabled
DTrace Support => disabled

Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar
Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, sslv3, tls, tlsv1.0, tlsv1.1, tlsv1.2
Registered Stream Filters => zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk

This program makes use of the Zend Scripting Language Engine:
Zend Engine v3.4.0, Copyright (c) Zend Technologies


 _______________________________________________________________________


Configuration

bz2

BZip2 Support => Enabled
Stream Wrapper support => compress.bzip2://
Stream Filter support => bzip2.decompress, bzip2.compress
BZip2 Version => 1.0.6, 6-Sept-2010

Core

PHP Version => 7.4.6

.............

将php.ini复制到 Configuration File (php.ini) Path

[root@localhost php-7.4.6]# cp php.ini-development /usr/local/php/lib/php.ini
[root@localhost php-7.4.6]# 
[root@localhost php-7.4.6]# cd /usr/local/php/lib/
[root@localhost lib]# 
[root@localhost lib]# ll
total 72
drwxr-xr-x. 4 root root    37 Jun  2 15:52 php
-rw-r--r--. 1 root root 72278 Jun  2 16:14 php.ini

5. 配置php-fpm

为php-fpm命令建立软链接,加入到环境变量中

[root@localhost php]# ln -s /usr/local/php/sbin/php-fpm /usr/local/bin/php-fpm

复制php配置文件目录下的 php-fpm.conf.default,并重命名为 php-fpm.conf

[root@localhost php]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

复制php配置文件目录下的 php-fpm.d/www.conf.default,并重命名为 php-fpm.d/www.conf

[root@localhost php]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

编辑 php-fpm.d/www.conf,设置 php-fpm 模块使用 daemon用户和 daemon 用户组的身份运行

vim /usr/local/php/etc/php-fpm.d/www.conf

 需要着重提醒的是,如果文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块, 以避免遭受恶意脚本注入的攻击

编辑 php.ini,文件中的配置项 cgi.fix_pathinfo 设置为 0 。

 

 启动php-fpm

php-fpm

查看php-fpm是否启动成功

[root@localhost lib]# ps -ef|grep php-fpm
root     15394     1  0 17:51 ?        00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
daemon   15395 15394  0 17:51 ?        00:00:00 php-fpm: pool www
daemon   15396 15394  0 17:51 ?        00:00:00 php-fpm: pool www
root     15427  6508  0 17:54 pts/1    00:00:00 grep --color=auto php-fpm

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

server {
       listen 80;
       server_name 192.169.56.103;
       root /data1/apache2/htdocs;

       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;
      }
   }

创建测试php文件

/data1/apache2/htdocs
[root@localhost htdocs]# ll
total 4
-rw-r--r--. 1 root root 18 Jun  2 18:04 phpinfo.php

访问 localhost/phpinfo()

输出php配置文件信息,说明配置正确

 

编译安装php扩展

1. php扩展安装流程
从 pecl.php.net 查找需要的扩展
选择扩展的版本(注意查看扩展版本与PHP版本的兼容性)
解压缩下载的文件
判断文件的安装类型

> 直装:(解压出来就是 .so 文件),直接复制文件到扩展目录,在php.ini中开启相应的扩展即可
> 编译安装:需要先进行编译,再复制文件到扩展目录,在php.ini中开启相应的扩展

 

2. 编译安装步骤
在解压缩的扩展目录下执行phpize
若 phpize 没有加入到环境变量,则需要使用绝对路径。例如 /usr/local/php/bin/phpize。
phpize是用来扩展php扩展模块的。
通过phpize可以建立php的外挂模块。

configure 配置编译参数

主要是配置php配置文件参数,例如:

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

若配置编译参数执行过程中出现错误,则需要安装 autoconf。
autoconf可以自动地配置软件源代码。

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

编译安装

make && make install

复制 .so 扩展文件到PHP扩展目录(若已经自动复制,则忽略)
在php.ini中开启扩展,配置相应的扩展参数

3. 安装redis扩展实例

# 查看已安装扩展

[root@localhost conf]# php -m
[PHP Modules]
bz2
Core
ctype
curl
date
dom
fileinfo
filter
gettext
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib


[Zend Modules]

 

从 pecl.php.net 下载 redis-4.1.1 扩展,并解压缩

 

[root@localhost src]# wget -P /usr/local/src/php_ext/ http://pecl.php.net/get/redis-5.2.2.tgz
--2020-06-04 12:48:18--  http://pecl.php.net/get/redis-5.2.2.tgz
Resolving pecl.php.net (pecl.php.net)... 104.236.228.160
Connecting to pecl.php.net (pecl.php.net)|104.236.228.160|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 251629 (246K) [application/octet-stream]
Saving to: ‘/usr/local/src/php_ext/redis-5.2.2.tgz’

100%[=================================================================================================================================================>] 251,629      154KB/s   in 1.6s   

2020-06-04 12:48:20 (154 KB/s) - ‘/usr/local/src/php_ext/redis-5.2.2.tgz’ saved [251629/251629]

进入下载目录解压

# /usr/local/src/php_ext
tar zxf redis-5.2.2.tgz

在解压缩的扩展目录下执行phpize

[root@localhost redis-5.2.2]# /usr/local/php/bin/php
php         php-cgi     php-config  phpdbg      phpize      
[root@localhost redis-5.2.2]# /usr/local/php/bin/phpize 
Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902

configure 配置编译参数

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

编译安装

make && make install

执行结果

 

 查看编译好的扩展文件 redis.so,已经自动复制到php扩展目录下

 

 编辑 php.ini,添加 extension=redis

vim /usr/local/php/lib/php.ini

extension=redis

此时查看php扩展,已经安装成功

[root@localhost log]# php -m
[PHP Modules]
bz2
Core
ctype
curl
date
dom
fileinfo
filter
gettext
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
redis
Reflection
session
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

其它扩展同时,使用上面的步骤即可安装扩展

 

posted @ 2020-06-02 20:15  海之角天之涯  阅读(458)  评论(0)    收藏  举报