mac安装nginx+php

参考链接

nginx

安装命令

brew install nginx

执行结果:

nginx 默认监听 8080 端口,启动成功之后,在浏览器访问 http://localhost:8080 可以看到 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服务

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

nginx 已经跑起来了,但是目前还不能解析 php,我们需要利用php-fpm解析。php-fpm 和 php 一起安装,我们先去安装 php,再做相关配置。

php

关于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文件,文件里面随意写些合法的验证代码即可。

在浏览器里面运行localhost:8080,如果返回预期,则说明nginx和php-fpm配置OK,可以进行后续开发啦。

添加自己的servers配置

touch jasper.conf

添加servers配置(配置基本核心的即可)

server {
    listen 8081;
    index index.php index.html index.htm;
    server_name jasper.tme.com;
    root /Users/songzeyu/workspace/code/;

    # charset utf - 8;

    access_log /usr/local/etc/nginx/log/localhost.access.log;
    error_log /usr/local/etc/nginx/log/localhost.error.log;

    location / {
       # if (!-f $request_filename) {
         #   rewrite ^ /(.*)$ /index.php last;
        #}
	index index.php;
    }

    error_page 404 /404.html;
    location = /40x.html {
    }

    error_page   500 502 503 504  /50x.html;
    location =
        /50x.html {
    }

    #proxy the php scripts to php-fpm
    location ~ .*\.(php)?$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi.conf;
        proxy_buffering off;
    }

    location ~ /\.ht {
        deny all;
    }
}

添加hosts配置

sodo vi /private/etc/hosts
# 添加一行
127.0.0.1  jasper.tme.com

然后就可以在/Users/songzeyu/workspace/code/目录进行开发啦。

其他常见问题

重启php-fpm失败1

/private/etc/php-fpm.conf

如果不修改error_log值,运行php-fpm的时候会提示log文件输出路径不存在的错误。

修改如下:

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

无php.ini配置

在目录下只有默认配置

/private/etc/php.ini.default

需要手动拷贝php.ini配置。

sudo cp /private/etc/php.ini.default  /private/etc/php.ini

启动nginx失败(端口被占用)

sudo nginx 

结果提示80端口被占用。

返回结果:

nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (48: Address already in use)
nginx: [emerg] still could not bind()

查询到使用nginx的进程,进行kill掉。

netstat -ano|grep 80
ps aux | grep nginx
sudo kill -9  主进程号

启动php-fpm失败

sudo php-fpm

返回结果:

[13-Jul-2019 18:46:39] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48)
[13-Jul-2019 18:46:39] ERROR: FPM initialization failed

查询使用进程

ps aux | grep php-fpm

返回结果:

songzeyu         56929   0.0  0.0  4298044   1584 s005  T     5:56下午   0:00.03 vi /private/etc/php-fpm.conf
nobody           41541   0.0  0.1  4312548   7784   ??  S     6:49下午   0:00.63 php-fpm
nobody           41540   0.0  0.1  4314684   9584   ??  S     6:49下午   0:00.82 php-fpm
nobody           41515   0.0  0.1  4314640   9568   ??  S     6:48下午   0:00.87 php-fpm
root             16452   0.0  0.0  4309868    300   ??  Ss   五01下午   0:01.04 php-fpm
songzeyu         78046   0.0  0.0  4297908    124 s004  T    四03下午   0:00.02 vi /private/etc/php-fpm.conf
songzeyu         77981   0.0  0.0  4298044    124 s004  T    四03下午   0:00.02 vi /private/etc/php-fpm.conf.default
songzeyu         60719   0.0  0.0  4286472    836 s005  S+    6:47下午   0:00.00 grep php-fpm

拿到主进程号S,即上图的41541, 41540,41515,S+代表有子进程,即16452

sudo kill -9  主进程号(41541) // 如果有多个,需要执行多个。
posted @ 2021-02-20 14:45  Lotos  阅读(179)  评论(0编辑  收藏  举报