网站首页 » 网站运营 » Linux » Centos 7 php 环境安装及配置
上一篇:
下一篇:

Centos 7 php 环境安装及配置

前言

在这篇文章之前,我已经分享过三篇有关于在 centos 7下配置 web服务器环境的文章了,如果你有什么不懂可以前往阅读在这里为您开个传送门,速度有点快,请系好您的安全带《Centos 7 FTP(vsftp)服务安装及配置》、《Centos 7 Apache 服务安装及配置》、《Centos 7 MariaDB 数据库服务安装及配置》。加上这一篇文章就刚好凑够 WEB 服务器环境配置的四大天王了。php 的安装及配置相对于前面三个服务的配置来说算是简单的了,好,废话不多说,我们来安装 php。

php 安装

在网上你可以看到好几种不同的安装方法,在我逛遍天涯海角后,给大家弄了一套简单实用的安装方法,下面就给大家献上!

安装记录

[root@niaoyun53098 ~]# yum install php
Loaded plugins: fastestmirror
base | 3.6 kB 00:00
extras | 3.4 kB 00:00
rpmforge | 1.9 kB 00:00
updates | 3.4 kB 00:00
Loading mirror speeds from cached hostfile
* base: mirrors.yun-idc.com
* extras: mirrors.tuna.tsinghua.edu.cn
* rpmforge: mirrors.neusoft.edu.cn
* updates: mirror.bit.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
--> Processing Dependency: php-common(x86-64) = 5.4.16-36.1.el7_2.1 for package: php-5.4.16-36.1.el7_2.1.x86_64
--> Processing Dependency: php-cli(x86-64) = 5.4.16-36.1.el7_2.1 for package: ph p-5.4.16-36.1.el7_2.1.x86_64
--> Running transaction check
---> Package php-cli.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
---> Package php-common.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
--> Processing Dependency: libzip.so.2()(64bit) for package: php-common-5.4.16-3 6.1.el7_2.1.x86_64
--> Running transaction check
---> Package libzip.x86_64 0:0.10.1-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
php x86_64 5.4.16-36.1.el7_2.1 updates 1.4 M
Installing for dependencies:
libzip x86_64 0.10.1-8.el7 base 48 k
php-cli x86_64 5.4.16-36.1.el7_2.1 updates 2.7 M
php-common x86_64 5.4.16-36.1.el7_2.1 updates 563 k

Transaction Summary
================================================================================
Install 1 Package (+3 Dependent packages)

Total download size: 4.7 M
Installed size: 17 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): libzip-0.10.1-8.el7.x86_64.rpm | 48 kB 00:00
(2/4): php-5.4.16-36.1.el7_2.1.x86_64.rpm | 1.4 MB 00:00
(3/4): php-common-5.4.16-36.1.el7_2.1.x86_64.rpm | 563 kB 00:00
(4/4): php-cli-5.4.16-36.1.el7_2.1.x86_64.rpm | 2.7 MB 00:00
--------------------------------------------------------------------------------
Total 4.5 MB/s | 4.7 MB 00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libzip-0.10.1-8.el7.x86_64 1/4
Installing : php-common-5.4.16-36.1.el7_2.1.x86_64 2/4
Installing : php-cli-5.4.16-36.1.el7_2.1.x86_64 3/4
Installing : php-5.4.16-36.1.el7_2.1.x86_64 4/4
Verifying : php-common-5.4.16-36.1.el7_2.1.x86_64 1/4
Verifying : php-cli-5.4.16-36.1.el7_2.1.x86_64 2/4
Verifying : libzip-0.10.1-8.el7.x86_64 3/4
Verifying : php-5.4.16-36.1.el7_2.1.x86_64 4/4

Installed:
php.x86_64 0:5.4.16-36.1.el7_2.1

Dependency Installed:
libzip.x86_64 0:0.10.1-8.el7 php-cli.x86_64 0:5.4.16-36.1.el7_2.1
php-common.x86_64 0:5.4.16-36.1.el7_2.1

Complete!
[root@niaoyun53098 ~]# yum install php-mysql php-gd libjpeg* php-ldap php-odbc p hp-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.yun-idc.com
* extras: mirrors.tuna.tsinghua.edu.cn
* rpmforge: mirrors.neusoft.edu.cn
* updates: mirror.bit.edu.cn
Package libjpeg-turbo-1.2.90-5.el7.x86_64 already installed and latest version
Package php-common-5.4.16-36.1.el7_2.1.x86_64 already installed and latest versi on
Resolving Dependencies
--> Running transaction check
---> Package libjpeg-turbo-devel.x86_64 0:1.2.90-5.el7 will be installed
---> Package libjpeg-turbo-static.x86_64 0:1.2.90-5.el7 will be installed
---> Package libjpeg-turbo-utils.x86_64 0:1.2.90-5.el7 will be installed
---> Package php-bcmath.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
---> Package php-gd.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
--> Processing Dependency: libt1.so.5()(64bit) for package: php-gd-5.4.16-36.1.e l7_2.1.x86_64
---> Package php-ldap.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
---> Package php-mbstring.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
---> Package php-mysql.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
--> Processing Dependency: php-pdo(x86-64) = 5.4.16-36.1.el7_2.1 for package: ph p-mysql-5.4.16-36.1.el7_2.1.x86_64
---> Package php-odbc.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
--> Processing Dependency: libodbc.so.2()(64bit) for package: php-odbc-5.4.16-36 .1.el7_2.1.x86_64
---> Package php-pear.noarch 1:1.9.4-21.el7 will be installed
--> Processing Dependency: php-posix for package: 1:php-pear-1.9.4-21.el7.noarch
---> Package php-xml.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.24)(64bit) for package: php- xml-5.4.16-36.1.el7_2.1.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.22)(64bit) for package: php- xml-5.4.16-36.1.el7_2.1.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: php- xml-5.4.16-36.1.el7_2.1.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.13)(64bit) for package: php- xml-5.4.16-36.1.el7_2.1.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: php- xml-5.4.16-36.1.el7_2.1.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: php-xml-5.4.16-36. 1.el7_2.1.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: php-xml-5.4.16-36 .1.el7_2.1.x86_64
---> Package php-xmlrpc.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
--> Running transaction check
---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed
---> Package php-pdo.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
---> Package php-process.x86_64 0:5.4.16-36.1.el7_2.1 will be installed
---> Package t1lib.x86_64 0:5.1.2-14.el7 will be installed
---> Package unixODBC.x86_64 0:2.3.1-11.el7 will be installed
--> Processing Dependency: libltdl.so.7()(64bit) for package: unixODBC-2.3.1-11. el7.x86_64
--> Running transaction check
---> Package libtool-ltdl.x86_64 0:2.4.2-21.el7_2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
libjpeg-turbo-devel x86_64 1.2.90-5.el7 base 98 k
libjpeg-turbo-static x86_64 1.2.90-5.el7 base 120 k
libjpeg-turbo-utils x86_64 1.2.90-5.el7 base 76 k
php-bcmath x86_64 5.4.16-36.1.el7_2.1 updates 56 k
php-gd x86_64 5.4.16-36.1.el7_2.1 updates 126 k
php-ldap x86_64 5.4.16-36.1.el7_2.1 updates 51 k
php-mbstring x86_64 5.4.16-36.1.el7_2.1 updates 503 k
php-mysql x86_64 5.4.16-36.1.el7_2.1 updates 99 k
php-odbc x86_64 5.4.16-36.1.el7_2.1 updates 64 k
php-pear noarch 1:1.9.4-21.el7 base 357 k
php-xml x86_64 5.4.16-36.1.el7_2.1 updates 124 k
php-xmlrpc x86_64 5.4.16-36.1.el7_2.1 updates 66 k
Installing for dependencies:
libtool-ltdl x86_64 2.4.2-21.el7_2 updates 49 k
libxslt x86_64 1.1.28-5.el7 base 242 k
php-pdo x86_64 5.4.16-36.1.el7_2.1 updates 97 k
php-process x86_64 5.4.16-36.1.el7_2.1 updates 54 k
t1lib x86_64 5.1.2-14.el7 base 166 k
unixODBC x86_64 2.3.1-11.el7 base 413 k

Transaction Summary
================================================================================
Install 12 Packages (+6 Dependent packages)

Total download size: 2.7 M
Installed size: 7.9 M
Is this ok [y/d/N]: y
Downloading packages:
(1/18): libjpeg-turbo-devel-1.2.90-5.el7.x86_64.rpm | 98 kB 00:00
(2/18): libjpeg-turbo-static-1.2.90-5.el7.x86_64.rpm | 120 kB 00:00
(3/18): libjpeg-turbo-utils-1.2.90-5.el7.x86_64.rpm | 76 kB 00:00
(4/18): libtool-ltdl-2.4.2-21.el7_2.x86_64.rpm | 49 kB 00:00
(5/18): php-gd-5.4.16-36.1.el7_2.1.x86_64.rpm | 126 kB 00:00
(6/18): php-ldap-5.4.16-36.1.el7_2.1.x86_64.rpm | 51 kB 00:00
(7/18): php-bcmath-5.4.16-36.1.el7_2.1.x86_64.rpm | 56 kB 00:00
(8/18): libxslt-1.1.28-5.el7.x86_64.rpm | 242 kB 00:00
(9/18): php-mysql-5.4.16-36.1.el7_2.1.x86_64.rpm | 99 kB 00:00
(10/18): php-mbstring-5.4.16-36.1.el7_2.1.x86_64.rpm | 503 kB 00:00
(11/18): php-odbc-5.4.16-36.1.el7_2.1.x86_64.rpm | 64 kB 00:00
(12/18): php-process-5.4.16-36.1.el7_2.1.x86_64.rpm | 54 kB 00:00
(13/18): php-pear-1.9.4-21.el7.noarch.rpm | 357 kB 00:00
(14/18): php-pdo-5.4.16-36.1.el7_2.1.x86_64.rpm | 97 kB 00:00
(15/18): php-xml-5.4.16-36.1.el7_2.1.x86_64.rpm | 124 kB 00:00
(16/18): t1lib-5.1.2-14.el7.x86_64.rpm | 166 kB 00:00
(17/18): php-xmlrpc-5.4.16-36.1.el7_2.1.x86_64.rpm | 66 kB 00:00
(18/18): unixODBC-2.3.1-11.el7.x86_64.rpm | 413 kB 00:00
--------------------------------------------------------------------------------
Total 1.7 MB/s | 2.7 MB 00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : php-pdo-5.4.16-36.1.el7_2.1.x86_64 1/18
Installing : php-process-5.4.16-36.1.el7_2.1.x86_64 2/18
Installing : libtool-ltdl-2.4.2-21.el7_2.x86_64 3/18
Installing : unixODBC-2.3.1-11.el7.x86_64 4/18
Installing : libxslt-1.1.28-5.el7.x86_64 5/18
Installing : php-xml-5.4.16-36.1.el7_2.1.x86_64 6/18
Installing : t1lib-5.1.2-14.el7.x86_64 7/18
Installing : libjpeg-turbo-devel-1.2.90-5.el7.x86_64 8/18
Installing : libjpeg-turbo-static-1.2.90-5.el7.x86_64 9/18
Installing : php-gd-5.4.16-36.1.el7_2.1.x86_64 10/18
Installing : 1:php-pear-1.9.4-21.el7.noarch 11/18
Installing : php-odbc-5.4.16-36.1.el7_2.1.x86_64 12/18
Installing : php-mysql-5.4.16-36.1.el7_2.1.x86_64 13/18
Installing : php-ldap-5.4.16-36.1.el7_2.1.x86_64 14/18
Installing : php-mbstring-5.4.16-36.1.el7_2.1.x86_64 15/18
Installing : php-xmlrpc-5.4.16-36.1.el7_2.1.x86_64 16/18
Installing : php-bcmath-5.4.16-36.1.el7_2.1.x86_64 17/18
Installing : libjpeg-turbo-utils-1.2.90-5.el7.x86_64 18/18
Verifying : libjpeg-turbo-static-1.2.90-5.el7.x86_64 1/18
Verifying : libjpeg-turbo-utils-1.2.90-5.el7.x86_64 2/18
Verifying : php-bcmath-5.4.16-36.1.el7_2.1.x86_64 3/18
Verifying : libjpeg-turbo-devel-1.2.90-5.el7.x86_64 4/18
Verifying : t1lib-5.1.2-14.el7.x86_64 5/18
Verifying : php-xmlrpc-5.4.16-36.1.el7_2.1.x86_64 6/18
Verifying : libxslt-1.1.28-5.el7.x86_64 7/18
Verifying : php-mysql-5.4.16-36.1.el7_2.1.x86_64 8/18
Verifying : php-mbstring-5.4.16-36.1.el7_2.1.x86_64 9/18
Verifying : php-gd-5.4.16-36.1.el7_2.1.x86_64 10/18
Verifying : libtool-ltdl-2.4.2-21.el7_2.x86_64 11/18
Verifying : php-process-5.4.16-36.1.el7_2.1.x86_64 12/18
Verifying : php-pdo-5.4.16-36.1.el7_2.1.x86_64 13/18
Verifying : php-odbc-5.4.16-36.1.el7_2.1.x86_64 14/18
Verifying : 1:php-pear-1.9.4-21.el7.noarch 15/18
Verifying : unixODBC-2.3.1-11.el7.x86_64 16/18
Verifying : php-xml-5.4.16-36.1.el7_2.1.x86_64 17/18
Verifying : php-ldap-5.4.16-36.1.el7_2.1.x86_64 18/18

Installed:
libjpeg-turbo-devel.x86_64 0:1.2.90-5.el7
libjpeg-turbo-static.x86_64 0:1.2.90-5.el7
libjpeg-turbo-utils.x86_64 0:1.2.90-5.el7
php-bcmath.x86_64 0:5.4.16-36.1.el7_2.1
php-gd.x86_64 0:5.4.16-36.1.el7_2.1
php-ldap.x86_64 0:5.4.16-36.1.el7_2.1
php-mbstring.x86_64 0:5.4.16-36.1.el7_2.1
php-mysql.x86_64 0:5.4.16-36.1.el7_2.1
php-odbc.x86_64 0:5.4.16-36.1.el7_2.1
php-pear.noarch 1:1.9.4-21.el7
php-xml.x86_64 0:5.4.16-36.1.el7_2.1
php-xmlrpc.x86_64 0:5.4.16-36.1.el7_2.1

Dependency Installed:
libtool-ltdl.x86_64 0:2.4.2-21.el7_2 libxslt.x86_64 0:1.1.28-5.el7
php-pdo.x86_64 0:5.4.16-36.1.el7_2.1 php-process.x86_64 0:5.4.16-36.1.el7_2.1
t1lib.x86_64 0:5.1.2-14.el7 unixODBC.x86_64 0:2.3.1-11.el7

Complete!
[root@niaoyun53098 ~]# echo "<?php phpinfo() ?>" >> /home/wwwroot/yunkus.com/inf o.php
[root@niaoyun53098 ~]# systemctl restart httpd.service

上面就是安装php的全程记录,如果你觉得东西太多,找不到方向,看不到重点,想看看还有没有更清晰明了的安装过程,我只能弱弱的告诉你,还真有,下面我就把php安装过程当中使用到的命令行代码精炼出来,让你开开心心地在一分钟内把 php 环境搞定。

安装详情

1.终端输入如下命令行,进行php的安装

# yum install php

2.回车后,不多久就会有如下提示,直接输入 y 就好,到此你就可以坐等 php 安装完成并给你送来“Complete!”贺电!

Is this ok [y/d/N]:

配置详情

安装完后,我们还得为 php 安装一些常用的扩展如:对数据库的支持扩展,方法也极其地简单,终端输入如下命令

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

回车稍微等待一下下你会看到同样的问候,你懂的直接回 y 就好。

Is this ok [y/d/N]:

再等那么一下下,来自centos 7 的贺电就送上了内容依然是:“Complete!”。

好了之后,我们肯定得把安装好的 php 环境放到实战中感爱感受那份成就感的重量。但老天就是那么容易跟我们开玩笑,访问网站新建的 info.php 时浏览器显示的竟然是空白页,这又是为什么呢?原因非常地简单,你忘记了一件不起眼,重至关重要的一步重启下 Apache 服务,代码如下。

# systemctl restart httpd.service

如果你服务器里安装的是nginx 执行下面的命令行即可

# systemctl restart nginx.service

好了,现在你刷新下页面,php 的专属页面是不是出现了,成就感就是来得那么突然。祝您好运!

因为各种原因,安装上面的php安装步骤完装配置完后,访问时竟然失败了。这又是为什么呢?原来我现安装这个 php 版本不一样(PHP 5.4.16),之前装的是什么版本我就不知道了。但装完这个新版本后,php 文件却无法访问。也就是说,环境配置不成功。要想php环境可用,你还需要安装一个东西 php-fpm ,运行如下命令行

yum install php php-fpm

安装完之后,启动下php-fpm

systemctl start php-fpm.service

停止 php 服务

systemctl stop php-fpm.service

重启 php 服务

systemctl restart php-fpm.service

查看 php 运行状态

systemctl status php-fpm.service

设置开机启动

systemctl enable php-fpm.service

取消开机启动

systemctl disable php-fpm.service

设置开机启动时会提示你系统会创建一个配置文件,并且需要重启服务你只需要输入两次密码(如果不是roote用户)进行确认就可以了:

大致过程

==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ===

Authentication is required to manage system service or unit files.

Authenticating as: Password:

==== AUTHENTICATION COMPLETE ===

Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===

Authentication is required to reload the systemd state.

Authenticating as:

Password:

==== AUTHENTICATION COMPLETE ===

这样就完成了,centos 7 的 php 环境配置。nginx 服务开机启动跟 php 的很相似,可以自己试试。

如果你确定了站点配置其它服务已经配置好并已经启动,访问网站时出现“nginx 502 Bad Gateway” 错误的话,你不妨可以查看下 php-fpm 服务的状态,看看是否没启动此服务。

  • 微信扫一扫,赏我

  • 支付宝扫一扫,赏我

声明

原创文章,不经本站同意,不得以任何形式转载,如有不便,请多多包涵!

本文永久链接:http://yunkus.com/centos7-php-service-install-config/

Leave a Reply

Your email address will not be published. Required fields are marked *

评论 END