首页 > 系统相关 >brew 安装 nginx 配合PHP工作

brew 安装 nginx 配合PHP工作

时间:2023-02-09 10:35:51浏览次数:51  
标签:index php nginx location brew PHP hello

1.前言

环境介绍: mac book pro m1 2020

本文记录使用 brew 安装 nginx 配合PHP工作

2.安装PHP

# 查看有哪些PHP版本可以安装
brew search php

# 安装php7.2
brew install [email protected]

# 切换 PHP 版本
brew-php-switcher 7.2


3.nginx的安装及基本配置

brew install nginx

# 一、location /: 因为所有的请求都是以/开头的,所以下面的配置相当于匹配任意的URL

location / {
	root html;
	index index.html index.htm;
}

root: 站点根目录, 相当于Apahce的 DocumentRoot

DocumentRoot "/Users/liang/Sites"

index: 默认访问的文件, 相当于Apahce的 DirectoryIndex

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

# 二、location ~ \.php$: 匹配以.php结尾的文件

# fastcgi_param: 将值中的 /scripts 改为 $document_root

# fastcgi_pass: 如果请求时php文件,那么nginx会把请求转发到 127.0.0.1:9000, 其中 9000 是php-fpm的端口

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

# 查看 php-fpm.d 目录下的 配置文件 www.conf


cat /opt/homebrew/etc/php/7.2/php-fpm.d/www.conf

进入 vim 模式,搜索关键词 9000, 就会发现确实可以找到 listen = 127.0.0.1:9000

image

4.nginx的URL重写

以TP6.0举例,访问 index控制器的 hello 方法,用 /index/hello 访问提示 404

因为nginx 默认是不支持pathinfo方式访问的,如果要访问可以通过 s=/index/hello 访问

// 访问提示404
http://127.0.0.1:8081/index.php/index/hello
// 访问正常
http://127.0.0.1:8081/index.php?s=/index/hello
但是可以通过修改配置文件使其支持pathinfo方式访问,将以下代码放入 location / 中即可

if (!-e $request_filename) {
	rewrite ^/index.php(.*)$ /index.php?s=$1 last;
	rewrite ^(.*)$ /index.php?s=$1 last;
	break;
}

rewrite ^/index.php(.*)$ /index.php?s=$1 last;

/index.php/index/hello 重写为 /index.php?s=/index/hello

rewrite ^(.*)$ /index.php?s=$1 last;

/index/hello 重写为 /index.php?s=/index/hello

修改后的配置文件示例:

image

5.更高效的管理nginx配置文件(虚拟主机)

nginx 要友好的支持PHP项目,只需要去关注server 配置块即可

后续 nginx 上需要绑定多个项目,这是如何做配置呢

方案一: 在 nginx.conf 可以使用多个 server 配置块管理不同的项目,此时不方便管理,因为所有项目的配置都在一个文件中

方案二: 将方案一中的 server块 抽离出来,放到相应的目录下面,而 nginx 也提供了这样一种能力

在 nginx.conf 配置文件的最下面有这样一个配置,就是定义这个目录的路径

include servers/*;
将项目的 server 配置块抽离出来, 放到 servers 目录下,一个项目占用一个配置文件

image

6.配置web访问以及查看目录文件

nginx 默认不支持像 ftp 那样显示文件列表,即使 localhost 指向的目录下面有文件和目录,访问时也会提示 403 Forbidden

image

可以通过给 location / 配置段添加额外参数使其支持显示目录文件,将以下代码放入 location / 中即可

autoindex on; # 开启目录文件列表
autoindex_localtime on; # 显示的文件时间为文件的服务器时间
autoindex_exact_size on; # 显示出文件的确切大小,单位是bytes,但我试的时候没看到效果
charset utf-8,gbk; # 避免中文乱码,使中文文件名可以正常显示
配置示例

location / {
  root /Users/liang/Sites;
  index index.html index.htm index.php;
  autoindex on;
  autoindex_localtime on;
  autoindex_exact_size on;
  charset utf-8,gbk;
}

配置成功

image

原文链接:https://blog.csdn.net/linyichao123/article/details/128451133

标签:index,php,nginx,location,brew,PHP,hello
From: https://www.cnblogs.com/hefeng2014/p/17104340.html

相关文章

  • 使用iis设置网站php版本为7.3
    内容:使用iis设置网站php版本为7.3这张图是多少人的噩梦 早期的宝塔版本没办法在线升级,php版本只能到7.1 默认就没有7.2以上版本 怎么办?可以在iis设置第一步: ......
  • brew安装Nginx
    一、brew安装Nginx安装brewinstallnginx查看安装信息brewinfonginx二、Nginx启动和停止1.启动服务:sudonginx2.停止服务:sudonginx-sstop三、其它常......
  • vue请求nginx获取文件相关问题总结
    需求:点击下载后直接下载附件,不需要预览(如pdf,txt,jpg等浏览器会默认预览该文件)。下载的文件名称需要更改为指定名称。 现有:带有协议://ip:端口/rsk/***/***.txt的ngi......
  • 7、install_mysql_httpd_php_wordpress
    #!/bin/bash##********************************************************************#Author: zikang#QQ: [email protected]#Date: 2021-03-03......
  • centos安装nginx并配置访问
    安装nginxyuminstall-ynginx安装完直接启动#启动nginxsystemctlstartnginx#查看nginx服务状态systemctlstatusnginx编辑配置文件编辑nginx配置文件:vi......
  • PHP , Navicat安装
    1、PHPStudy小皮面板:https://public.xp.cn/upgrades/phpStudy_64.zip    解压缩后       浏览器输入127.0.0.1:80  通过路径查看站点 ......
  • PHP、Navicat安装
    PHPStudy小皮面板:https://public.xp.cn/upgrades/phpStudy_64.zip下载完成后解压后双击点击立即安装 安装完成启动MySQL,Nginx    浏览器输网址127.0.0.1:......
  • PHP图片压缩
    //直接调用就可以了<?phpnamespacethink\admin\storage;/***图片压缩*ClassCompress*@packageEasyAdmin\upload\driver*/classCompress{private$src;......
  • 前端页面分页算法 js+php
    实现效果: 实现思路:通过当前选中页码数值和总页码数量,计算返回结果,以数组的形式返回。遍历数组内容,完成页面渲染。 php算法:/***getNavigatePage**@......
  • php8.2.1编译fileinfo扩展时提示 make: *** [libmagic/softmagic.lo] Error 1
     安装:cd/opt/php-8.2.1/ext/fileinfo/usr/local/php82/bin/phpize./configure--with-php-config=/usr/local/php82/bin/php-config#make&&makeinstall错误如......