CentOS 7 安装 PHP 7

新买了台服务器,随便倒腾,记录一下。

1. 安装 epel-release

1. 什么是epel

如果既想获得 RHEL 的高质量、高性能、高可靠性,又需要方便易用(关键是免费)的软件包更新功能,那么 Fedora Project 推出的 EPEL(Extra Packages for Enterprise Linux)正好适合你。

EPEL 是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。

2. 使用心得

  1. 不用去换原来yum源,安装后会产生新repo
  2. epel会有很多源地址,如果一个下不到,会去另外一个下

3. 安装方法

1. 搜索EPEL相关的软件包

# yum search epel
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.cloud.aliyuncs.com
 * epel: mirrors.cloud.aliyuncs.com
 * extras: mirrors.cloud.aliyuncs.com
 * updates: mirrors.cloud.aliyuncs.com
 * webtatic: us-east.repo.webtatic.com
================================= N/S matched: epel =================================
epel-release.noarch : Extra Packages for Enterprise Linux repository configuration
epel-rpm-macros.noarch : Extra Packages for Enterprise Linux RPM macros
python3-pkgversion-macros.noarch : Convenience macros for Fedora/EPEL Python 3
                                 : packages building

  Name and summary matches only, use "search all" for everything.

2. 安装EPEL软件包

通过yum搜索的结果中epel-release.noarch就是我们需要的软件包。

安装可以不用包含软件包名称中点“.”后面的部分,当然包含也没有问题。点“.”后面的部分只是提供软件包适应系统架构。noarch为通用型,还有x86_64为64位的系统,i686为32位系统使用。

通过下面命令进行安装。

# yum install epel-release
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.cloud.aliyuncs.com
 * epel: mirrors.cloud.aliyuncs.com
 * extras: mirrors.cloud.aliyuncs.com
 * updates: mirrors.cloud.aliyuncs.com
 * webtatic: uk.repo.webtatic.com
Package epel-release-7-9.noarch already installed and latest version
Nothing to do

想知道epel-release这个包包含哪些内容,就使用rpm命令查询列出。-q 表示查询,-l 标识列出软件包的所包含的文件。

# rpm -ql epel-release
/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
/etc/yum.repos.d/epel-testing.repo
/etc/yum.repos.d/epel.repo
/usr/lib/systemd/system-preset/90-epel.preset
/usr/share/doc/epel-release-7
/usr/share/doc/epel-release-7/GPL

可以看出最主要的就是/etc/yum.repos.d/epel.repo这个文件了,默认已经启用。不确定的话可以用vi等命令查看编辑。

enabled=1说明已经启用

启用之后就可安装EPEL源中提供的软件了。
通过使用yum install XXXXX命令安装,会自动搜索所有可用的软件源,并提示可用的软件包。

2. 安装PHP7的rpm源

# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Retrieving https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Preparing...                          ################################# [100%]
    package webtatic-release-7-3.noarch is already installed

3. 安装PHP7

# yum install php71w

4. 编译安装

1. 下载PHP源码包

这里获取所需版本的PHP的下载地址。

# wget -O php7.tar.gz url/get/from/last/step

2.解压php7

# tar -xvf php7.tar.gz

3. 进入php目录

# cd php-7.x.x

4. 安装依赖包

# 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

5. 编译配置(如果出现错误,基本都是上一步的依赖文件没有安装所致)

嫌麻烦的可以从这一步起参考PHP官方安装说明

# ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm \
--with-fpm-user=nginx  \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared  \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir  \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets  \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache

这里可能会报很多错误,一个个解决即可。这里有大部分的解决方法。

列出几个常见的

checking for png_write_image in -lpng… yes If configure fails try –with-xpm-dir=

configure: error: freetype.h not found.
Fix: Reconfigure your PHP with the following option. --with-xpm-dir=/usr

checking for png_write_image in -lpng… yes configure: error: libXpm.(a|so) not found.

Fix: yum install libXpm-devel

checking for bind_textdomain_codeset in -lc… yes checking for GNU MP support… yes configure: error: Unable to locate gmp.h

Fix: yum install gmp-devel

6. 正式安装

# make && make install

没有报错就表示安装完成。

4. 配置PHP

1. 配置环境变量

# vi /etc/profile

在末尾追加

PATH=$PATH:/usr/local/php/bin
export PATH

执行命令使得改动立即生效

# source /etc/profile

2. 配置php-fpm

把所需的几个配置文件放到需要的位置

# cp php.ini-production /etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm

3. 启动php-fpm

# /etc/init.d/php-fpm start

4. 查看是否已正确安装

# php -v
PHP 7.1.4 (cli) (built: Apr 30 2017 11:20:19) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
# which php
/usr/local/php/bin/php

5. 坑

1. Nginx Error! The page you are looking for is temporarily unavailable. Please try again later.

connect() to unix:/var/run/php-fpm/php-fpm.sock failed (2: No such file or directory) while connecting to upstream

参考这里的解决方案

# cp /etc/php-fpm.d/www.conf.rpmsave /etc/php-fpm.d/www.conf
# vi /etc/php-fpm.d/www.conf
listen 改为
listen = 127.0.0.1:9000;

nginx 配置文件也改
# vi /etc/nginx/conf.d/yoursite.com.conf

fastcgi_pass 127.0.0.1:9000;

重启php-fpm 和 nginx

# sudo /etc/init.d/php-fpm restart
# sudo systemctl restart nginx

6. 参考资料

  1. How To Install Wget on CentOS
  2. CentOS怎样强制卸载PHP以及自定义安装PHP
  3. CentOS7 安装 PHP7最新版
  4. PHP编译过程中常见错误信息的解决方法
  5. PHP编译安装时常见错误解决办法,php编译常见错误
  6. PHP-FPM connect() to unix:/tmp/php5-fpm.sock failed (2: No such file or directory)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容

  • 1.LAMP介绍  LAM(M)P:L: linuxA: apache (httpd)M: mysql, mar...
    尛尛大尹阅读 1,033评论 0 1
  • PHP安装redis扩展 以下操作需要在下载的 phpredis 目录中完成: 如果你是 PHP7 版本,则需要下...
    手表大大阅读 1,458评论 0 1
  • 进入夏季,很多家长反映说宝宝经常稍不注意就出现拉肚子的问题。高温多雨的天气为很多的病毒细菌等致病微生物提供了良好的...
    菜菜凯儿得乐阅读 305评论 0 0
  • 进电梯回家,看见哥哥鞋带开了,弟弟便蹲下给哥哥系鞋带 哥哥小伙伴家长给哥哥一块芝麻煎饼,哥哥给弟弟分。我说给弟弟大...
    英才居阅读 197评论 0 0
  • [不忘初心]20170902学习力六期践行D110 今天玩酷间谍游戏,从手指谣中找间谍,首先找到两只小鸡的4/3,...
    夏苏的花园阅读 191评论 0 0