Centos7.6 Nginx PHP 配置

阿里云 Centos7.6 Nginx PHP 配置

在云服务器部署PHP项目 查询了许多的资料,但多数只有片段的描述,有些PHP安装教程需要编译安装 而我选择使用yum安装一了百了
更好的q去理解 以及日后的学习 有必要写成一篇博客来记录一下自己的学习过程
为了通用性、也为了日后可能更改云服务器、减少学习成本出发 选取目前腾讯云/阿里云的通用Centos7.6版本

本文使用阿里云实例

前提环境:

环境配置:

  • 安装Nginx
  • 安装PHP

1 安装Nginx:

//EPEL的全称叫 Extra Packages for Enterprise Linux 。EPEL是由 Fedora 社区打造,
//为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。
//装上了 EPEL之后,就相当于添加了一个第三方源。

//安装 epel 
yum -y install epel-release

//安装 epel就可以使用 yum安装nginx
yum -y install nginx

//启动 nginx
systemctl start nginx

//开机启动
systemctl enable nginx

//在浏览器上 输入 公网IP 测试是否成功

修改nginx配置文件
在这里插入图片描述
在这里插入图片描述

vim /etc/nginx/nginx.conf

//add
location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

1.1 安装Nginx方法2(推荐):

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

//安装 nginx
yum -y install nginx

//修改文件
vim /etc/nginx/conf.d/default.conf

配置文件

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


2 安装PHP(php-fpm)

//更新 rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

//安装php全家桶
yum -y install php72w php72w-cli php72w-fpm php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml

3 配置

修改php-fpm配置文件

vim /etc/php-fpm.d/www.conf

//跳转到 第8行 将apache修改为 nginx
 8 user = nginx
 10 group = nginx

//检查 22 行 
22 listen = 127.0.0.1:9000

// 跳转到 366行 修改为
366 env[HOSTNAME] = $HOSTNAME
367 env[PATH] = /usr/local/bin:/usr/bin:/bin
368 env[TMP] = /tmp
369 env[TMPDIR] = /tmp
370 env[TEMP] = /tmp

// 在nginx 配置文件中里 /usr/share/nginx/html 是nginx运行的root目录
//在此目录下创建 php文件
vim /usr/share/nginx/html/phpinfo.php

demo:
<?php phpinfo(); ?>


//启动php-fpm nginx
systemctl start php-fpm
systemctl restart nginx
//通过浏览器 公网IP/phpinfo.php 测试能否访问

php连接数据库demo
//php连接数据库demo
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
 
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
 
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
 
if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 结果";
}
$conn->close();
?>

欢迎指出文章错误 有空修改/回复 谢谢[Doge][Doge][Doge]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值