centos7通过yum安装指定版本php的方法:1、安装源;2、执行【yum install -y php】命令选择对应版本的php进行安装;3、添加php常用扩展;4、执行【systemctl restart】命令启动php。

1、安装源

安装epel-release:

yum -y install epel-release

(推荐教程:php图文教程)

添加remi源:

rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm

安装yum-config-manager实用程序:

yum -y install yum-utils

2、安装PHP

选择对应的版本进行安装

安装PHP5.4:

yum install -y php

安装PHP7.0:

yum-config-manager --enable remi-php70
yum -y install php php-opcache

安装PHP7.1:

yum-config-manager --enable remi-php71
yum -y install php php-opcache

安装前可使用yum search php71搜索可安装的软件包。

完成后还需要添加PHP常用扩展:

yum -y install php-mysql php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-soap curl curl-devel
yum -y install php71-php-fpm.x86_64
systemctl restart php71-php-fpm    #启动php
netstat -tunlp|grep 9000     #查看php启动状态
vim /etc/opt/remi/php71/php-fpm.d/www.conf
user = nginx   #修改用户为nginx
group = nginx   #修改组为nginx

(视频教程推荐:php视频教程)

3、nginx配置

安装nginx

yum install nginx     #安装nginx
vim /etc/nginx/conf.d/test.conf
 
server {
    listen 80;
    #listen [::]:80;
    server_name 39.105.1.170;
    client_max_body_size 50m;
 
    location / {
        charset  utf-8;
        root /var/www;
        index  index.html index.htm;
        }
     location ~ \\.php$ {
        root           /var/www;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
        include        fastcgi_params;
    }
}

启动nginx

nginx -s reload      #启动nginx

在/var/www新建两个文件,一个html文件,一个php文件

test.html的内容为:
 
<h1>Hello World</h1>
 
test.php的内容为:
 
<?php
phpinfo();
?>

浏览器访问:39.105.1.170/test.html 和 39.105.1.170/test.php