张晨的个人博客

Linux安装PHP7.2及使用Nginx部署PHP项目

张晨的个人博客2018-08-27综合技术 12441 0A+A-

系统环境:

Linux:Centos7.4 + Nginx1.8.0

一、创建data-www账号及data-www用户组用于部署php和nginx

groupadd data-www #创建用户组
useradd -d /home/data-www/ -g data-www data-www #创建www用户并指定工作空间和分组
usermod passwd data-www #设置密码


二、PHP安装:

    1.更新linux的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

    2.如果之前已经安装php则需要先卸载

yum -y remove php*

    3.使用yum命令安装php72w及各种扩展

yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml

    4.配置php-fpm(此处user和group必须与nginx.conf中的user一致,否则会报Primary script unknown

    php-fpm是一个php-fastcgi的管理器,为PHP提供管理服务,即php-Fastcgi Process Manager。

vim /etc/php-fpm.d/www.conf #修改php-fpm配置user和group
; RPM: apache Choosed to be able to access some dir as httpd
user = data-www
; RPM: Keep a group allowed to write in log dir.
group = data-www
#wq保存退出
php-fpm #启动php-fpm

    5.查看是否启动

ps -aux|grep php-fpm

三、配置Nginx

    Nginx本身不能处理PHP页面,它只是个web服务器,当接收到请求后,如果是PHP请求,通过反向代理的方式转发给PHP解释器(上文安装的php-fpm)处理,并把结果返回给客户端。

nginx.conf配置如下:

user  data-www data-www; #注意此处和/etc/php-fpm.d/www.conf中的user和group要一致
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80 ;
        # 域名,本地测试可以使用127.0.0.1或localhost
        server_name  www.zhangc.cn;
        # php项目根目录
        root         /home/data-www/blog;
        location / {
            #  定义首页索引文件的名称
            index index.php index.html index.htm;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        # PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
        # Fastcgi服务器和程序(PHP)沟通的协议.
        location ~ .*\.php$ {
            # 设置监听端口
            fastcgi_pass   127.0.0.1:9000;
            # 设置nginx的默认首页文件
            fastcgi_index  index.php;
            # 设置脚本文件请求的路径
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            # 引入fastcgi的配置文件
            include        fastcgi_params;
        }
    }
}


至此linux+php+nginx项目配置结束了,博主也是通过这个方式把zblogasp迁移到zblogphp的,有问题可以留言讨论~

文章关键词
linux
php
nginx
发表评论