CentOS 7 安装 PHP、配置 Nginx

安装过程参考官方文档:PHP: Unix 系统下的 Nginx 1.4.x - Manual

PHP 安装

1. 下载源码

官方网站:PHP: Downloads

这里以 php-7.4.16 为例,我选择下载 tar.gz 格式的。

2. 解压、配置、编译

tar -xvf php-7.4.16.tar.gz
cd /opt/php-7.4.16
./configure --enable-fpm
make && make install

如果遇到错误【configure: error: no acceptable C compiler found in $PATH】,可以通过安装 C 语言编译器解决:

yum -y install gcc

【configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met】libxml2 libxml2-devel

【configure: error: Package requirements (sqlite3 > 3.7.4) were not met】yum -y install sqlite-devel

补充:

1. 官方网站上使用了 ./configure --enable-fpm --with-mysql,但是 php-7.4.16 似乎已经不再支持此选项,你会得到这样的警告

【configure: WARNING: unrecognized options: --with-mysql】

2. PHP在 5.3.3 之后已经把 php-fpm 并入到 php 的核心代码中了。 所以 php-fpm 不需要单独的下载安装。

要想 php 支持 php-fpm,只需要在编译php源码的时候带上 --enable-fpm 就可以了。

3. 准备工作

将默认的配置文件拷贝一份,并重命名使 PHP 能够识别

cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
cp /usr/local/etc/php-fpm.d/www.conf.default  /usr/local/etc/php-fpm.d/www.conf

将源码中的启动脚本拷贝到 /etc/init.d/,并赋予执行权限

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

修改 php-fpm.conf 文件,将文件末尾的 

include=NONE/etc/php-fpm.d/*.conf

改为

include=etc/php-fpm.d/*.conf

4. 启动 php-fpm

/etc/init.d/php-fpm start

Nginx 配置

Nginx 安装参考:https://blog.csdn.net/qq_39291919/article/details/107095991

nginx.conf 增加以下配置。其实这个类似配置在 nginx.conf 已经存在(被注释),可以参考。

location ~ \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi.conf;
}

1. 有关 fastcgi_index 是否真正生效的解释:nginx中的fastcgi_index用于什么? - 程序园

2. 官方文档给出的配置关于 fastcgi_params 配置过时,可以参考:Linux下nginx.conf中 fastcgi_params和fastcgi.conf的区别_Cinjosy的博客-CSDN博客_fastcgi.conf

根据个人习惯,我会在 conf 文件夹下面创建 vhost 文件夹,然后再 nginx.conf 最末端使用 include 导入配置。这些配置的名称往往都是

<域名>.conf,比如 cannedbread.com.conf

http {
    ...
    include vhost/*.conf
}

配置 vhost/cannedbread.com.conf

server {
    listen 80;
    server_name cannedbread.com;  # 域名
    root /opt/www;                     # 网站的根目录

    location / {
        index index.html index.htm index.php; # 域名下默认访问文件
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi.conf;
    }
}

测试能够访问

刚才,在 cannedbread.com.conf(根据自己的配置)中配置的 root /opt/www 文件夹下创建 index.php,键入内容

<?php
phpinfo();

接着访问查看是否能够成功

注意需要配置 hosts 域名映射

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罐装面包

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值