nginx中配置php项目,php项目下nginx的配置

一、目录及phpmac中使用brew安装的软件包都存放在/usr/local/cellar目录下

软件的配置文件存放在/usr/local/etc目录下,需要在该目录下进行配置文件的修改

项目服务器的存放目录:/usr/local/var,在该文件夹下存放项目文件以及日志文件

启动php:方式一:直接在/usr/local/cellar/php版本/sbin目录下使用./php-fpm启动

方式二:在/usr/local/cellar/php版本/sbin目录下使用nohup ./php-fpm > a.log & 启动,这种启动方式启动后在后台运行,并且会打印日志到a.log文件

二、nginx配置:

(一)、单项目配置单项目配置直接在nginx.conf中配置即可在http下放开log\_format该项

修改access\_log、error\_log目录为/usr/local/var/log/nginx下对应的log文件路径

修改location对应的root目录为项目下的public目录,两处

放开fastcgi\_pass、fastcgi\_index、fastcgi\_param和include四项

在server中的location下添加如下配置if (!-e $request_filename) {

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

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

break;

}注意:if后边必须有一个空格,否则不生效

(二)、多项目配置在项目只有一两个的时候可以直接在nginx.conf中配置,比较方便。当项目很多的情况下,如果所有的项目中的nginx配置都放在nginx.conf文件中会不利于管理和维护,所以项目多的情况下需要对不同的项目配置文件进行拆分在nginx.conf文件最后有一个include servers/*; 这一行的意思是nginx可以从servers/*下读取配置文件,所以不同的项目配置文件应该放在该文件夹下,文件夹名任意,不必须是servers,关闭nginx.conf文件,在同级目录下创建该文件夹,默认nginx没有该文件夹。

进入文件夹创建项目的配置文件,文件名均以.conf结尾

拷贝nginx.conf中server部分的内容到新建的配置文件中,修改端口号、error\_log、access\_log路径,保存退出

修改nginx.conf中的两处root路径,保留到项目所在路径的上一层即可,保存退出。

重启nginx,查看是否生效。##### 不同项目的配置文件内容

server {

listen 8082;

server_name localhost;

#charset koi8-r;

access_log /usr/local/var/log/nginx/food.mall.access.log main;

error_log /usr/local/var/log/nginx/food.mall.error.log;

## 注意:error的级别不是main

location / {

root /Users/apple/workplace/food_mall_server/public;

index index.html index.htm;

if (!-e $request_filename) {

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

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

break;

}

}

#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 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 /Users/apple/workplace/food_mall_server/public;

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;

#}

}

(三)、nginx命令及配置中存在的问题nginx服务命令:启动:nginx

关闭:nginx -s stop

重启:nginx -s reload

nginx配置启动后存在的问题:找不到log文件:这种问题很明显是log文件忘记配置或者路径配置的不正确,需要对error\_log和access\_log修改配置,log文件存放在/usr/local/var/log/nginx目录下

使用localhost:8080/a.php访问a.php文件时显示找不到该文件,这个错误可能由三种原因造成:原因一:没有启动php服务,需要开启php,参照上边的两种启动方式

原因二:如果php服务已经开启,依然显示找不到文件,则需要修改nginx配置,在nginx.conf文件中修改fastcgi_param SCRIPT_FILENAME/scripts$fastcgi_script_name;替换成 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;原因三:没有访问权限,使用chmod 777 项目文件目录设置文件夹的访问权限

重启nginx查看是否可以正常访问

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值