mac安装nginx+php

87 篇文章 4 订阅

nginx

安装命令

brew install nginx

默认服务路径,项目根目录

/usr/local/var/www

配置文件路径

nginx安装好后,只有对应的默认配置文件。

/usr/local/etc/nginx/nginx.conf.default

需要手动拷贝一份为nginx.conf。

执行命令

sudo cp /usr/local/etc/nginx/nginx.conf.default  /usr/local/etc/nginx/nginx.conf

这样就有默认配置文件了。

/usr/local/etc/nginx/nginx.conf 

!!!服务启动命令!!!

brew services start nginx
sudo nginx    #启动nginx服务
sudo nginx -s reload    #重新载入配置文件
sudo nginx -s stop    #停止nginx服务

关于php-fpm

Mac上默认安装了php和php-fpm,所以暂没使用brew进行php安装。但需要注意的几个点:

一、php-fpm对应的配置文件只有默认的。

/private/etc/php-fpm.conf.default

二、拷贝配置文件

sudo /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf

三、修改配置

修改error_log 错误日志路径,否则php-fpm会启动失败。 重要

error_log = /usr/local/var/log/php-fpm.log

修改nginx配置

当nginx和php-fpm都按照上述配置完毕后,再进行nginx的配置:

一、找到server中的下面代码,添加index.php

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

二、在找到开启FastCGI server

# 下面是针对Apache服务
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}
# 这里是针对nginx服务
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
   #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   include        fastcgi_params;
}

!!!注意注意注意!!!

需要修改上面的配置

需要把1修改为2
1 fastcgi_param SCRIPT_FILENAME/scripts$fastcgi_script_name;
2 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

三、启动nginx和php-fpm

sudo nginx -s reload
sudo php-fpm

四、验证php服务

/usr/local/var/www下添加index.php文件,文件里面随意写些合法的验证代码即可。

说一个自己的坑  ,因为本来自己的机子上有php 版本是7.1,为了使用其他功能 花了两个小时升级到7.4(网速太慢)

在 配置 php-fpm 时 ,照搬上面的 发现NGINX 就是不支持php ,花了一天时间 想明白了  自己的php 不是在这个目录下。

搞明白以后 长舒了一口气。

#user  nobody;
#user  root;
worker_processes  1;
error_log  /usr/local/var/log/nginx/error.log;
pid        /usr/local/var/run/nginx.pid;
#pid        logs/nginx.pid;


events {
   #use epoll; 事件驱动模型 
    worker_connections  1024; # 最大连接数
}


http {
    include       mime.types; #文件扩展名与文件类型映射表
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /usr/local/var/log/nginx/access.log;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65; #链接超时时间

    #gzip  on;

    

    
    server {
        listen       80;#端口
        server_name  site1.com;#你的站点域名/ip
        root         /data/site1/public; #你的站点目录,绝对路径即可
        index index.php index.html index.htm;

        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        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;
        }
    }


    server {
        listen       80;
        server_name  lepai.larance.com;

        root  /Users/larance/PhpstormProjects/ms-lepai-article;
        index  index.html index.htm index.php;

        #error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        :wqlocation / {
            try_files $uri $uri/ /index.php?$query_string;
        }
        location = /50x.html {
            root   html;
        }

        #
        location ~ \.php$ {
            root   /Users/larance/PhpstormProjects/ms-lepai-article;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;

        }
    }


 include servers/*;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Mac安装和配置NginxPHP可以按照以下步骤进行: 1. 安装Homebrew:Homebrew是Mac上的包管理器,可以简化安装过程。打开终端,并执行以下命令安装Homebrew: ``` /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` 2. 安装Nginx:使用Homebrew安装Nginx,运行以下命令: ``` brew install nginx ``` 3. 配置Nginx:默认情况下,Nginx的配置文件位于`/usr/local/etc/nginx/nginx.conf`。您可以根据需要编辑该文件,配置服务器块等。 4. 启动Nginx:执行以下命令启动Nginx: ``` sudo nginx ``` 5. 安装PHP:使用Homebrew安装PHP,运行以下命令: ``` brew install php ``` 6. 配置PHPPHP的配置文件位于`/usr/local/etc/php/{版本号}/php.ini`。您可以根据需要编辑该文件,配置PHP的相关设置。 7. 启动PHP-FPM:PHP需要通过FastCGI Process Manager(PHP-FPM)与Nginx进行通信。执行以下命令启动PHP-FPM: ``` sudo php-fpm ``` 8. 配置Nginx以使用PHP:编辑Nginx的配置文件`/usr/local/etc/nginx/nginx.conf`,找到`location`块,并将其修改为: ``` 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; } ``` 9. 重启Nginx:执行以下命令重启Nginx,使配置生效: ``` sudo nginx -s reload ``` 现在,您应该已经成功安装和配置了NginxPHP。您可以在浏览器中访问`http://localhost`来测试是否正常工作。如果您在其他目录下放置了PHP文件,请确保在Nginx的配置文件中正确指定了根目录。 希望这些步骤能帮助到您!如果您有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

larance

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

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

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

打赏作者

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

抵扣说明:

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

余额充值