相信技术的力量

Centos7搭建PHP环境

安装PHP

编译安装

下载PHP包

PHP官网下载链接:

https://secure.php.net/downloads.php

选择最新的tar.gz包,通过FTP上传至linux服务器,解压PHP压缩包

tar zxvf php-7.2.2.tar.gz

编译

进入php7文件夹

cd php-7.2.2

输入命令:

./configure --enable-fpm --enable-mbstring

自己在安装的过程中出现了如下异常:

configure: error: xml2-config not found. Please check your libxml2 installation.

提示找不到xml2-config文件,让检查libxml2的安装情况。

检测libxml2是否安装,显示已安装

[root@nginx php-7.2.2]# rpm -qa|grep libxml2
libxml2-2.9.1-6.el7_2.3.x86_64

重复安装无效

[root@nginx php-7.2.2]# yum install libxml2
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Package libxml2-2.9.1-6.el7_2.3.x86_64 already installed and latest version
Nothing to do

但查找xml2-config文件,还是提示没有

[root@nginx php-7.2.2]# find/-name "xml2-config"
-bash: find/-name: No such file or directory

通过搜索引擎找到解决方案:检测libxml2-devel的安装情况,通过rpm -qa |grep libxml2-devel命令发现没有安装,于是开始安装。

[root@nginx php-7.2.2]# yum install libxml2-devel -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package libxml2-devel.x86_64 0:2.9.1-6.el7_2.3 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================
Package                                Arch                            Version                                    Repository                    Size
========================================================================================================================================================
Installing:
libxml2-devel                          x86_64                          2.9.1-6.el7_2.3                            base                          1.0 M

Transaction Summary
========================================================================================================================================================
Install  1 Package

Total download size: 1.0 M
Installed size: 8.8 M
Downloading packages:
libxml2-devel-2.9.1-6.el7_2.3.x86_64.rpm                                                                                        | 1.0 MB  00:00:01    
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libxml2-devel-2.9.1-6.el7_2.3.x86_64                                                                                                1/1 
  Verifying  : libxml2-devel-2.9.1-6.el7_2.3.x86_64                                                                                                1/1 

Installed:
  libxml2-devel.x86_64 0:2.9.1-6.el7_2.3                                                                                                                

Complete!

此时再次检测libxml2安装情况和xml2-config文件,发现都没问题了:

[root@nginx php-7.2.2]# rpm -qa |grep  libxml2
libxml2-2.9.1-6.el7_2.3.x86_64
libxml2-devel-2.9.1-6.el7_2.3.x86_64
[root@nginx php-7.2.2]# find / -name "xml2-config"
/usr/bin/xml2-config

于是重新

./configure --enable-fpm --enable-mbstring

成功:

Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| 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

测试安装结果:

[root@nginx web]# php -v
PHP 7.2.2 (cli) (built: Feb 24 2018 15:17:15) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

yum安装

配置yum源

# 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

安装

# yum install php72w php72w-cli php72w-mbstring php72w-mysql php72w-fpm php72w-pdo

详细可参考:

https://blog.csdn.net/zhezhebie/article/details/73325663

配置FPM

编译安装

如果是PHP编译安装 , 那么还需要配置PHP-FPM,并启动FPM服务,让FPM和Nginx可以正常通信。

按照PHP官网的配置说明,创建配置文件

cp php.ini-development /usr/local/php/php.ini
cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
cp sapi/fpm/php-fpm /usr/local/bin

然后启动 php-fpm 服务:

/usr/local/bin/php-fpm

报如下异常:

ERROR: Unable to globalize '/usr/local/NONE/etc/php-fpm.d/*.conf' (ret=2) from /usr/local/etc/php-fpm.conf

编辑php-fpm.conf配置文件

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

将最后一行include=NONE/etc/php-fpm.d/*.conf修改为include=etc/php.fpm.d/*.conf

include=NONE/etc/php-fpm.d/*.conf #修改前
include=etc/php.fpm.d/*.conf    #修改后

继续启动php-fpm 服务:

/usr/local/bin/php-fpm

报如下异常:

[root@nginx php]# /usr/local/bin/php-fpm
[24-Feb-2018 16:04:17] WARNING: Nothing matches the include pattern '/usr/local/etc/php-fpm.d/*.conf' from /usr/local/etc/php-fpm.conf at line 125.
[24-Feb-2018 16:04:17] ERROR: No pool defined. at least one pool section must be specified in config file
[24-Feb-2018 16:04:17] ERROR: failed to post process the configuration
[24-Feb-2018 16:04:17] ERROR: FPM initialization failed

进入目录进入目录/usr/local/etc/php-fpm.d,执行

cp www.conf.default www.conf

再起启动php-fpm服务,通过netstat -tunlp |grep 9000命令,发现启动成功了:

[root@nginx sbin]# netstat -tunlp |grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*              LISTEN      118470/php-fpm: mas 

yum安装

安装命令,此处指定与PHP对应的fpm版本号

# yum install php70w-fpm

启动,停止,重启命令:

# service php-fpm start
# service php-fpm stop
# service php-fpm reload

其他各种扩展可参考

http://blog.csdn.net/river131/article/details/53838640

配置Nginx

这里直接记录已经正常运行phpMyAdmin的Nginx配置,此时phpMyAdmin放在了Nginx的html目录下。

server {

       listen 8081;
       server_name localhost;

       location /phpmyadmin {
            root html;
            index index.php index.html;
       }    

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

}

配置完成以后,记得:

service nginx restart

输入:http://yourIpAddress:8081/phpmyadmin可直接访问phpMyAdmin登录页面。

Nginx常见异常

403 Forbidden

检查 html目录的访问权限ls -l

File not found

index.php添加到location节点的index处。

location / {
    root html;
    index index.php index.html;
}     

另外,如果报出File not found异常,记得

安装Mysqli扩展

# yum install php-mysqli
或
# yum install php70w-mysqli

重启PHP-FPM,查看安装结果

# service php-fpm restart

(完)

参考链接:

http://blog.csdn.net/etwo123/article/details/54582088 (重点感谢)

http://php.net/manual/zh/install.unix.nginx.php

http://blog.csdn.net/u010647316/article/details/56489795

http://blog.csdn.net/dabao1989/article/details/22898857

http://blog.csdn.net/xiao_zhui/article/details/72556781

⬆️