在 CentOS 7 下安装 PHP 7

关键字

CentOS, PHP, CentOS7, PHP 7, remi , php-fpm, YUM

背景概述

一般,我们选择在 Windows 和 macOS 下开发PHP,而 PHP 在 Windows 和 macOS 的安装相对简单,而在 Linux 服务器环境 CentOS 7 下官方未直接说明。本文旨在帮助读者在 CentOS 7 下顺利安装 PHP 7,本安装过程应已经过笔者反复测试,以确保成功。

本文采用 rpm 方式安装,对 CentOS 7 下 PHP 7 所有版本有效。截止本文发布,CentOS 最小化安装版版本是 1810,PHP 为 7.3.7​ 。

如果按本文安装时遇到任何问题,请留言或通过【桃花岛】官方联系我们。

软件依赖

  • CentOS 7
  • remi:用来安装 php 的 rpm 软件源
  • PHP 7:适用于 7.0,7.1,7.2,7.3

具体步骤

安装 epel 和 remi 软件源

yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm

安装 yum-utils 工具指定更新版本

这是 yum 工具包,用来更方便的管理 yum 软件源:

yum -y install yum-utils

yum-utils 提供 yum-config-manager 命令,它可以设置 remi 软件包作为默认源来安装PHP,比如安装 PHP 7.3 版本的命令如下

yum-config-manager --enable remi-php73

返回如下:

Loaded plugins: fastestmirror
=========================================================================================================== repo: remi-php73 ============================================================================================================
[remi-php73]
async = True
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/7
baseurl = 
cache = 0
cachedir = /var/cache/yum/x86_64/7/remi-php73
check_config_file_age = True
compare_providers_priority = 80
cost = 1000
deltarpm_metadata_percentage = 100
deltarpm_percentage = 
enabled = 1
enablegroups = True
exclude = 
failovermethod = priority
ftp_disable_epsv = False
gpgcadir = /var/lib/yum/repos/x86_64/7/remi-php73/gpgcadir
gpgcakey = 
gpgcheck = True
gpgdir = /var/lib/yum/repos/x86_64/7/remi-php73/gpgdir
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
hdrdir = /var/cache/yum/x86_64/7/remi-php73/headers
http_caching = all
includepkgs = 
ip_resolve = 
keepalive = True
keepcache = False
mddownloadpolicy = sqlite
mdpolicy = group:small
mediaid = 
metadata_expire = 21600
metadata_expire_filter = read-only:present
metalink = 
minrate = 0
mirrorlist = http://cdn.remirepo.net/enterprise/7/php73/mirror
mirrorlist_expire = 86400
name = Remi's PHP 7.3 RPM repository for Enterprise Linux 7 - x86_64
old_base_cache_dir = 
password = 
persistdir = /var/lib/yum/repos/x86_64/7/remi-php73
pkgdir = /var/cache/yum/x86_64/7/remi-php73/packages
proxy = False
proxy_dict = 
proxy_password = 
proxy_username = 
repo_gpgcheck = False
retries = 10
skip_if_unavailable = False
ssl_check_cert_permissions = True
sslcacert = 
sslclientcert = 
sslclientkey = 
sslverify = True
throttle = 0
timeout = 30.0
ui_id = remi-php73
ui_repoid_vars = releasever,
   basearch
username = 

类似的,如果安装 7.1 和 7.2 版本,则命令如下:

yum-config-manager --enable remi-php71
yum-config-manager --enable remi-php72

使用如下命令将 PHP 及常用的扩展安装至系统:

yum -y install php php-common php-fpm
yum -y install php-mysql php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-apc php-cli php-pear php-pdo php-curl

安装验证

运行

php -v

返回如下即表示 PHP 7 已安装成功

PHP 7.3.7 (cli) (built: Jul  3 2019 11:30:22) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies

运行

php -m

返回如下,表示 PHP 7 常用扩展已安装成功

[PHP Modules]
apc
apcu
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
igbinary
json
libxml
mbstring
mcrypt
memcache
memcached
msgpack
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
zlib

[Zend Modules]

问题汇总

暂无

相关文章

暂无

发表评论

电子邮件地址不会被公开。 必填项已用*标注