PHP安装(CentOS)

简介:

在CentOS上安装PHP7

安装环境及版本:

系统环境:CentOS Linux release 7.5.1804 (Core)
为了便于测试验证,提前安装好apache,并将发布目录定位/var/www/html,具体参考《Apache安装》

一、PHP7 Yum安装

1.1 准备

如果是安装系统自带的默认版本,如下命令即可

yum install php
yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt

1.2 设置PHP的搜索位置

安装PHP7,先设置搜索位置
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

[root@localhost ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Retrieving https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
warning: /var/tmp/rpm-tmp.hwRsM0: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:epel-release-7-12                ################################# [100%]
[root@localhost ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Retrieving https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
warning: /var/tmp/rpm-tmp.0YQXse: Header V4 RSA/SHA1 Signature, key ID 62e74ca5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:webtatic-release-7-3             ################################# [100%]

1.3 查看现有PHP7版本

命令:yum search php7

安装:yum install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-fpm php71w-gd php71w-mbstring php71w-mysqlnd php71w-opcache php71w-pdo php71w-xml

[root@localhost ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-fpm php71w-gd php71w-mbstring php71w-mysqlnd php71w-opcache php71w-pdo php71w-xml
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * epel: ftp.riken.jp
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.bfsu.edu.cn
 * webtatic: uk.repo.webtatic.com
......                                                                                                                                                           18/18 

Installed:
  mod_php71w.x86_64 0:7.1.33-1.w7     php71w-cli.x86_64 0:7.1.33-1.w7          php71w-common.x86_64 0:7.1.33-1.w7      php71w-devel.x86_64 0:7.1.33-1.w7       php71w-embedded.x86_64 0:7.1.33-1.w7     php71w-fpm.x86_64 0:7.1.33-1.w7    
  php71w-gd.x86_64 0:7.1.33-1.w7      php71w-mbstring.x86_64 0:7.1.33-1.w7     php71w-mysqlnd.x86_64 0:7.1.33-1.w7     php71w-opcache.x86_64 0:7.1.33-1.w7     php71w-pdo.x86_64 0:7.1.33-1.w7          php71w-xml.x86_64 0:7.1.33-1.w7    

Dependency Installed:
  libX11.x86_64 0:1.6.7-2.el7          libX11-common.noarch 0:1.6.7-2.el7          libXau.x86_64 0:1.0.8-2.1.el7          libXpm.x86_64 0:3.5.12-1.el7          libpng.x86_64 2:1.5.13-7.el7_2          libxcb.x86_64 0:1.13-1.el7         

Complete!
[root@localhost ~]# 

安装插件:yum install php71w-posix

[root@localhost ~]# yum install php71w-posix
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * epel: d2lzkl7pfhq30w.cloudfront.net
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.bfsu.edu.cn
 * webtatic: us-east.repo.webtatic.com
...
Installed:
  php71w-process.x86_64 0:7.1.33-1.w7                                                                                                                                                                                                       

Complete!
[root@localhost ~]# 

1.4 修改PHP配置文件

命令:vi /etc/php.ini

异常日志路径:568 ;error_log = /var/www/logs/php_errors.log

上传文件大小:799 upload_max_filesize = 20M

时区设置:877 ;date.timezone = Asia/Shanghai

1.5 Apache支持PHP

修改apache配置文件:vi /etc/httpd/conf/httpd.conf

添加PHP首页文件:

   163	<IfModule dir_module>
   164	    DirectoryIndex index.html index.php
   165	</IfModule>

添加对PHP的支持:

   283	    AddType application/x-compress .Z
   284	    AddType application/x-gzip .gz .tgz
后面添加如下内容
AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .php

二、PHP7 make安装

2.1 安装依赖包

命令:yum install -y epel-release

命令:yum install -y libmcrypt-devel

命令:yum -y install php-mcrypt libmcrypt libmcrypt-devel libxml2-devel openssl-devel libcurl-devel libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 libjpeg-turbo-devel libmcrypt-devel mysql-devel

2.2 安装PHP

进入目录:cd /usr/local/src
下载安装文件:wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/php/php-7.0.12.tar.gz

[root@localhost src]# wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/php/php-7.0.12.tar.gz
--2020-07-02 21:24:50--  http://zy-res.oss-cn-hangzhou.aliyuncs.com/php/php-7.0.12.tar.gz
Resolving zy-res.oss-cn-hangzhou.aliyuncs.com (zy-res.oss-cn-hangzhou.aliyuncs.com)... 118.31.219.222
Connecting to zy-res.oss-cn-hangzhou.aliyuncs.com (zy-res.oss-cn-hangzhou.aliyuncs.com)|118.31.219.222|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 19439276 (19M) [application/x-gzip]
Saving to: ‘php-7.0.12.tar.gz’

100%[====================================================================================================================================================================================================>] 19,439,276  1.99MB/s   in 8.6s   

2020-07-02 21:24:59 (2.16 MB/s) - ‘php-7.0.12.tar.gz’ saved [19439276/19439276]
[root@localhost src]# ll
total 669764
drwxr-xr-x. 28 mysql mysql      4096 Jul  2 19:28 apr-1.5.0
-rw-r--r--.  1 root  root    1016391 Dec 26  2013 apr-1.5.0.tar.gz
drwxr-xr-x. 20 mysql mysql      4096 Jul  2 19:35 apr-util-1.5.3
-rw-r--r--.  1 root  root     874462 Dec 26  2013 apr-util-1.5.3.tar.gz
drwxr-xr-x. 12   501 games      4096 Jul  2 19:45 httpd-2.4.23
-rw-r--r--.  1 root  root    8406575 Sep 18  2016 httpd-2.4.23.tar.gz
drwxr-xr-x.  2 root  root          6 Jul  2 20:46 mysql-5.7.17-linux-glibc2.5-x86_64
-rw-r--r--.  1 root  root  654007697 Feb 22  2017 mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
drwxr-xr-x.  9  1169  1169     12288 Jul  2 19:39 pcre-8.38
-rw-r--r--.  1 root  root    2053336 Dec 12  2016 pcre-8.38.tar.gz
-rw-r--r--.  1 root  root   19439276 Nov  2  2016 php-7.0.12.tar.gz

解压缩安装文件:tar zxvf php-7.0.12.tar.gz

[root@localhost src]# tar zxvf php-7.0.12.tar.gz
............
php-7.0.12/main/internal_functions.c.in
php-7.0.12/main/spprintf.c
php-7.0.12/config.guess
php-7.0.12/CODING_STANDARDS
php-7.0.12/README.TESTING2
php-7.0.12/README.RELEASE_PROCESS
php-7.0.12/makedist
php-7.0.12/run-tests.php

进入解压缩目录:cd php-7.0.12

[root@localhost src]# cd php-7.0.12
[root@localhost php-7.0.12]# ll
total 4204
-rw-r--r--.  1 mysql mysql   82329 Oct 13  2016 acinclude.m4
-rw-r--r--.  1 mysql mysql  312706 Oct 13  2016 aclocal.m4
drwxr-xr-x.  2 mysql mysql     222 Oct 13  2016 build
..................
drwxr-xr-x.  3 mysql mysql    4096 Oct 13  2016 win32
drwxr-xr-x.  4 mysql mysql    8192 Oct 13  2016 Zend

检查环境:./configure –prefix=/usr/local/php –with-mysql=mysqlnd –with-openssl –with-mysqli=mysqlnd –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 –enable-maintainer-zts –disable-fileinfo

[root@localhost php-7.0.12]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xm 
l --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-maintainer-zts --disable-fileinfo
configure: WARNING: unrecognized options: --with-mysql
...................
Thank you for using PHP.

config.status: creating php7.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
configure: WARNING: unrecognized options: --with-mysql

编译和安装:make && make install

[root@localhost src]# make && make install
...............
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-7.0.12/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/

2.3 配置文件:/etc/php.ini

命令:cp php.ini-production /etc/php.ini
命令:vi /etc/php.ini

[root@localhost php-7.0.12]# cp php.ini-production /etc/php.ini
cp: overwrite ‘/etc/php.ini’? yes

异常日志路径:568 ;error_log = /var/www/logs/php_errors.log

上传文件大小:799 upload_max_filesize = 20M

时区设置:877 ;date.timezone = Asia/Shanghai

2.3.1 Apache支持PHP,

修改apache配置文件:vi /etc/httpd/conf/httpd.conf

添加PHP首页文件:

   163	<IfModule dir_module>
   164	    DirectoryIndex index.html index.php
   165	</IfModule>

添加对PHP的支持:

   283	    AddType application/x-compress .Z
   284	    AddType application/x-gzip .gz .tgz
后面添加如下内容
AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .php

2.3.2 重启apache服务

命令:systemctl restart httpd

2.4 Pthread安装

为了PHP支持多线程,需要安装Pthread

进入/usr/local/src目录:cd /usr/local/src/

[root@localhost pcre-8.38]# cd /usr/local/src/

下载文件:wget https://github.com/krakjoe/pthreads/archive/v3.1.6.tar.gz

[root@localhost src]# wget https://github.com/krakjoe/pthreads/archive/v3.1.6.tar.gz
--2020-07-02 21:48:33--  https://github.com/krakjoe/pthreads/archive/v3.1.6.tar.gz
Resolving github.com (github.com)... 13.229.188.59
Connecting to github.com (github.com)|13.229.188.59|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/krakjoe/pthreads/tar.gz/v3.1.6 [following]
--2020-07-02 21:48:34--  https://codeload.github.com/krakjoe/pthreads/tar.gz/v3.1.6
Resolving codeload.github.com (codeload.github.com)... 13.250.162.133
Connecting to codeload.github.com (codeload.github.com)|13.250.162.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/x-gzip]
Saving to: ‘v3.1.6.tar.gz’

    [  <=>                                                                                                                                                                                                ] 79,704       199KB/s   in 0.4s   

2020-07-02 21:48:36 (199 KB/s) - ‘v3.1.6.tar.gz’ saved [79704]

解压缩下载文件:tar zxvf v3.1.6.tar.gz
并进入文件目录:cd pthreads-3.1.6

[root@localhost src]# tar zxvf v3.1.6.tar.gz 
........
pthreads-3.1.6/tests/workers-no-start.phpt
pthreads-3.1.6/tests/workers-no-unstack.phpt
pthreads-3.1.6/tests/workers-ok-unstack.phpt 
[root@localhost src]# cd pthreads-3.1.6/
[root@localhost pthreads-3.1.6]# ll
total 76
-rw-rw-r--. 1 root root  2176 Feb 13  2016 appveyor.yml
drwxrwxr-x. 2 root root    91 Feb 13  2016 classes
-rw-rw-r--. 1 root root  1369 Feb 13  2016 config.m4
-rw-rw-r--. 1 root root  1074 Feb 13  2016 config.w32
drwxrwxr-x. 2 root root  4096 Feb 13  2016 examples
-rw-rw-r--. 1 root root  3175 Feb 13  2016 LICENSE
-rw-rw-r--. 1 root root  8855 Feb 13  2016 package.xml
-rw-rw-r--. 1 root root 13295 Feb 13  2016 php_pthreads.c
-rw-rw-r--. 1 root root  1959 Feb 13  2016 php_pthreads.h
-rw-rw-r--. 1 root root  8745 Feb 13  2016 README.md
drwxrwxr-x. 2 root root  4096 Feb 13  2016 src
drwxrwxr-x. 2 root root  4096 Feb 13  2016 tests

生成configure:/usr/local/php/bin/phpize

[root@localhost pthreads-3.1.6]# /usr/local/php/bin/phpize 
Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012

检查安装环境:./configure –with-php-config=/usr/local/php/bin/php-config

[root@localhost pthreads-3.1.6]# ./configure --with-php-config=/usr/local/php/bin/php-config
.....................
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h

编译及安装:make && make install

[root@localhost pthreads-3.1.6]# make && make install
...........
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

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

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20151012/

2.4.1 php支持

注意这里面有个坑,不要在php.ini下配置pthreads扩展,不然php-fpm无法启动,因为pthreads v3版本只能运行在cli下。

需要先将php.ini目录下复制一份,命名为php-cli.ini,命令:cp /etc/php.ini /etc/php-cli.ini

[root@localhost extensions]# ll cp /etc/php.ini /etc/php-cli.ini
[root@localhost extensions]# ll /etc/
total 1224
..............
-rw-r--r--.  1 root root    69823 Jul  2 22:03 php-cli.ini
-rw-r--r--.  1 root root    69724 Jul  2 21:40 php.ini

编辑php-cli.ini文件:vi /etc/php-cli.ini

851 extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20151012/"
852 extension=pthreads.so

重启apache服务:systemctl restart httpd

查看pthread扩展是否装上:/usr/local/php/bin/php -m | grep thread

[root@localhost bin]# /usr/local/php/bin/php -m | grep pthread
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-zts-20151012/curl.so' - /usr/local/php/lib/php/extensions/no-debug-zts-20151012/curl.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-zts-20151012/fileinfo.so' - /usr/local/php/lib/php/extensions/no-debug-zts-20151012/fileinfo.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-zts-20151012/json.so' - /usr/local/php/lib/php/extensions/no-debug-zts-20151012/json.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-zts-20151012/mcrypt.so' - /usr/local/php/lib/php/extensions/no-debug-zts-20151012/mcrypt.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-zts-20151012/phar.so' - /usr/local/php/lib/php/extensions/no-debug-zts-20151012/phar.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-zts-20151012/zip.so' - /usr/local/php/lib/php/extensions/no-debug-zts-20151012/zip.so: cannot open shared object file: No such file or directory in Unknown on line 0
pthreads

三、CENTOS8安装PHP8

dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf -y install yum-utils
dnf module reset php
dnf module install php:remi-8.0 -y
dnf install php -y
yum install php-mysql	
[root@VM-4-8-centos ~]# php -v
PHP 8.0.10 (cli) (built: Aug 24 2021 15:40:40) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.10, Copyright (c), by Zend Technologies

四、验证

新建httpd.conf中指定的发布目录/var/www/html:mkdir -p /var/www/html

在发布目录下,建httpd.conf指定的首页index.php文件

[root@localhost html]# vi index.php
[root@localhost html]# cat index.php 
<?php
phpinfo();
?>

使用http://ip访问

image-18.png (1027×972)
分类:

发表回复