centos7上nginx+PHP7.2的安装及配置

Sep 20, 2018

centos7上nginx+PHP7.2的安装和一些配置(php和nginx),以及centos的yum踩坑记录

修改yum源

国内使用centos默认的yum下载源非常慢,将其换为国内的阿里源-部署测试

  • 安装wget

    sudo yum install wget
    
  • 备份原始的更新源

    cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
    
  • 下载并设置更新源为阿里源

    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    
  • 清理yum缓存

    yum clean all
    
  • 创建yum缓存

    yum makecache
    

问题记录

在安装过程中会可能因本机dns解析遇到以下问题

sources.list

解决方案

  • /etc/resolv.conf中增加nameserver 8.8.8.8
  • 重新执行 yum makecache

安装nginx

  • 添加nginx源

    rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    
  • 安装nginx

    yum install nginx
    
  • 启动nginx

    systemctl start nginx
    
  • 访问ip(本地127.0.0.1),查看是否成功

    nginx

ok!

安装PHP

  • 添加PHP源

    yum install epel-release
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    
  • 安装php 和 php-fpm

    yum install php72w php72w-fpm
    

nginx配置

  • nginx的配置文件位于/etc/nginx/conf.d

    修改默认配置文件default.conf内容为

    server {
        listen       80;
        server_name  localhost;
    
        location / {
            root   /usr/share/nginx/html;#项目根目录
            index  index.html index.htm index.php;
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;#根据项目根目录修改
            include        fastcgi_params;
        }
    
    }
    
  • 重启nginx

    systemctl restart nginx
    
  • 启动php-fpm

    service start php-fpm
    
  • nginx解析目录下添加php文件test.php内容如下

    <?php
        echo phpinfo();
    ?>
    
  • 访问ip(本地127.0.0.1)/test.php 页面如下

大功告成!!!