1.安装php

yum install php

#检查版本
php -v

2.安装nginx

yum install nginx

#检查版本
nginx -v

3.更改nginx默认端口

vi /etc/nginx/nginx.conf
#更改端口88

4.开放nginx端口

# 重启防火墙,以保证防火墙一定是开启的
systemctl restart firewalld

# 在防火墙添加端口88且设置永久开启
firewall-cmd --zone=public --add-port=88/tcp --permanent

# 重新加载防火墙,使上一步操作生效
firewall-cmd --reload

# 查看88端口是否开放
firewall-cmd --zone=public --query-port=88/tcp

5.开启HTTP服务

# 重启防火墙,以保证防火墙一定是开启的
systemctl restart firewalld

# 在防火墙添加服务http且设置永久开启
firewall-cmd --zone=public --add-service=http --permanent

# 重新加载防火墙,使上一步操作生效
firewall-cmd --reload

# 查看http服务是否开放
firewall-cmd --query-service http

6.启动Nginx服务并访问Nginx网页

# 重启Nginx服务,不用start用restart是为了避免读者之前启动过Nginx服务且没有关闭
systemctl restart nginx

# 检查Nginx服务的运行状态,有running字样说明启动成功
systemctl status nginx

# 设置Nginx服务开机自启动,此步为防止以后重启了Linux后使用Nginx前忘记启动服务,读者视自身情况选择是否执行本命令
systemctl enable nginx

# 在浏览器输入IP:88访问Nginx网页

7.安装PHP-FPM并启动该服务

yum install php-fpm.x86_64

# 重启PHP-FPM服务,不用start用restart是为了避免读者之前启动过PHP-FPM服务且没有关闭
systemctl restart php-fpm

# 检查PHP-FPM服务的运行状态,有running字样说明启动成功
systemctl status php-fpm

# 设置PHP-FPM服务开机自启动,此步为防止以后重启了Linux后使用PHP-FPM前忘记启动服务,读者视自身情况选择是否执行本命令
systemctl enable php-fpm

8.修改Nginx配置文件使其能运行PHP文件

vi /etc/nginx/nginx.conf

#修改
    server {
    listen       88 default_server;
    listen       [::]:88 default_server;
    # 这里改动了,也可以写你的域名
    server_name  xx.xxx.xxx;
    # 默认网站根目录(www目录)
    root         /var/www/;
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    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,Python)沟通的协议.
    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;
    }
}

# 查看修改后的Nginx配置文件是否有误
nginx –t

# 重启Nginx服务
systemctl restart nginx

9.测试访问PHP文件

# 新建PHP文件test.php
vi /usr/share/nginx/html/test.php 

# 按下i键进入编辑模式,输入以下内容以显示PHP配置信息:
<?php
phpinfo();  
?>
# 按下esc键退出编辑模式

# 保存并退出文件
:wq

# 在浏览器输入IP:88/test.php访问PHP网页

更多推荐

nginx+php配置