Mac 下安装与配置 nginx php7.1

Mac 下安装与配置 nginx php7.1

apache和nginx解析PHP的原理

对于apache而言,php是挂在apache下的一个模块,当http请求一个php文件时,apache会调用php模块进行解析,然后返回html给apache。#而nginx必须通过一个FastCGI的进程管理器来启动php解析,也就是php-fpm,当nginx接受php请求时,它会向它监听的fpm发送一个请求,当fpm接收到请求以后,再创建一个子进程来调用php进行解析。当解析完成后,回收线程,返回html给nginx。

查看Apache和PHP的版本号

MacBookPro:~ shuzehttpd?vServerversion:Apache/2.4.25(Unix)Serverbuilt:Feb6201720:02:10MacBookPro:shuze php -v

PHP 5.6.30 (cli) (built: Feb 7 2017 16:18:37)

Copyright (c) 1997-2016 The PHP Group

Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

处理系统自带的apache

关闭系统自带的apache

sudo apachectl stop

如果apache已经加入了launchctl,这样关闭

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

可以选择直接删除相关的文件

?

sudo rm /usr/sbin/apachectl sudo rm /usr/sbin/httpd sudo rm -r /etc/apache2/

还是

sudo rm /usr/sbin/httpd

sudo rm-rf/etc/apache2/

?

安装xcode命令行工具

Xcode-select —install

检查当前环境是否最新符合brew运行

MacBookPro:~ shuze$ brew doctor

Your system is ready to brew.

Homebrew安装

ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

Homebrew 会将软件包安装到独立目录,并将其文件软链接至 /usr/local 。

Homebrew 不会将文件安装到它本身目录之外,你可将 Homebrew 安装到任意位置。

nginx的安装与配置

安装nginx

MacBookPro:~ shuze$ brew install nginx

如果需要安装nginx的其他版本,可以使用brew edit nginx来修改其内容

sudo nginx #打开

nginx nginx -s reload|reopen|stop|quit #重新加载配置|重启|停止|退出 nginx

nginx -t #测试配置是否有语法错误

实际上,nginx服务的启动可以用-c nginx的配置文件参数制定其配置文件,默认加载/usr/local/etc/nginx/nginx.conf文件

启动nginx也可以: sudo brew services start nginx

停止nginx也可以: sudo brew services stop nginx

Check if Nginx is running on default port

curl -IL http://127.0.0.1:8080

curl -IL http://localhost

设置权限

普通用户登陆时并且监听的端口在1024以下的,例如把默认的8080端口改为了80端口

要 nginx 开机启动的话,需要给予它管理员权限

sudo chown root:wheel /usr/local/opt/nginx/bin/nginx

sudo chmod u+s /usr/local/opt/nginx/bin/nginx

还是???

sudo chown root:wheel /usr/local/Cellar/nginx/1.10.0/bin/nginx

sudo chmod u+s /usr/local/Cellar/nginx/1.10.0/bin/nginx

开机自启动nginx服务设置(加入launchctl启动控制):

mkdir -p ~/Library/LaunchAgents

cp /usr/local/Cellar/nginx/1.10.0/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

打开 nginx 后,默认的访问端口 8080,如果要改为 80 端口,则要修改 “/usr/local/etc/nginx/nginx.conf” 下监听(listen)端口值。

默认的文件访问目录(root)是 “/usr/local/Cellar/nginx/1.2.6/html”(这里的1.2.6是安装的nginx的版本)。

?

安装之后更改下nginx.conf 的user nginx 的运行用户( #user nobody;),为之后的运行脚本提供读写权限,以登录用户作为运行用户。

?

用法详解 nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

-?,-h : 打开帮助信息

-v : 显示版本信息并退出

-V : 显示版本和配置选项信息,然后退出

-t : 检测配置文件是否有语法错误,然后退出

-q : 在检测配置文件期间屏蔽非错误信息

-s signal : 给一个 nginx 主进程发送信号:stop(停止), quit(退出), reopen(重启), reload(重新加载配置文件)

-p prefix : 设置前缀路径(默认是:/usr/local/Cellar/nginx/1.2.6/)

-c filename : 设置配置文件(默认是:/usr/local/etc/nginx/nginx.conf)

-g directives : 设置配置文件外的全局指令

php的安装和配置

brew 默认没有 php 安装包,引入第三方的php库:

brew tap homebrew/dupes

brew tap josegonzalez/homebrew-php

(

brew tap homebrew/dupes && \ brew tap homebrew/php && \ brew install –without-apache –with-fpm –with-mysql php70

)

brew search php7 查看一下有什么php7版本可以安装

安装php7.1:

brew install php71 –without-apache –with-fpm

(brew install php56 –with-imap –with-tidy –with-debug –with-pgsql –with-mysql –with-fpm)

启动服务

brew services start php71

安装过程给出的重要提示

The php.ini file can be found in:

/usr/local/etc/php/7.1/php.ini

If you are having issues with custom extension compiling, ensure that you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:

PATH=”/usr/local/bin:PATH”IfyouwishtoswapthePHPyouuseonthecommandline,youshouldaddthefollowingto/.bashrc,/.zshrc,/.profileoryourshell′sequivalentconfigurationfile:exportPATH=”(brew --prefix homebrew/php/php71)/bin:$PATH”

由于Mac系统预装了php以及php-fpm,为了启动的时候能直接启动安装的最新的php,要把php安装的二进制文件所在的路径加入到系统路径中。

如果我们设置了开机的时候自动启动,我们不会发现有什么问题,可是当重新配置了扩展,需要重新启动php-fpm的时候,我们如果直接

使用php-fpm -c /usr/local/etc/php/7.1/php.ini -y /usr/local/ext/php/5.6/php-fpm.conf的时候,我们发现

我们的扩展安装没有起作用,原因是我们使用了/usr/sbin/php-fpm的命令,可以通过which查看。

使用brew安装的php-fpm是在 /usr/local/opt/php71/sbin目录下。

需要修改path,指定 php 的解析路径。

在~/.bashrc(没有则创建)最后加入一行:export PATH=”(brew??prefixphp71)/bin:PATH”

使之生效 source ~/.bashrc

(

export PATH=”brew??prefixphp71)/bin:PATH”

export PATH=/usr/local/opt/php71/bin:PATH将php路径加入PATHsudovim/.bashprofileexportPATH=”(brew --prefix php71)/bin:$PATH”

source ~/.bash_profile

)

到此,php以及PHP-fpm已经安装成功

设置php-fpm开机启动(加入launchctl启动控制)

MacBookPro:~ shuzemkdir?p/Library/LaunchAgentsMacBookPro:shuze cp /usr/local/opt/php71/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/

MacBookPro:~ shuze$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist

(

cp /usr/local/Cellar/php71/7.1.8_20/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/

)

配置路径

/usr/local/etc/php/7.1/php-fpm.d/www.conf

user = _www

group = _www

改为

user = YOUR_USERNAME

group = YOUR_GROUP || staff

启动php sudo brew services start php71

查看进程 lsof -Pni4 | grep LISTEN | grep php

安装过程很有用的命令:

杀死一个进程的命令:sudo kill -9 pid #pid替换为你的进程号

比如你要杀死php-fpm进程,也可以使用如下命令:sudo killall php-fpm

如何查看进程号:ps -ef | grep php-fpm

nginx后续配置

nginx的配置文件在 /usr/local/etc/nginx/文件中vim nginx.conf然后 根据 自己的需求去配置就行

include servers/*; 引入了servers目录的配置,在servers 里添加server 配置文件

vim phptest.conf #在servers目录添加一个vhost文件, 内容在后面

配置完毕,重载配置文件 sudo nginx -s reload

一切准备妥当,nginx 80 localhost 已经可以显示页面,php-fpm 9000端口已经运行。

下一步 添加host sudo vim /etc/hosts

添加一行 127.0.0.1 phptest.dev

测试一下 ping phptest.dev

配置 Nginx 支持 PHP-FPM

sudo vim /usr/local/etc/nginx/nginx.conf

添加默认首页 php

index index.php index.html index.htm;

取消以下内容的注释,并做修改

location ~ .php{

fastcgi_intercept_errorson;

fastcgi_pass127.0.0.1:9000;

fastcgi_indexindex.php;

fastcgi_paramSCRIPT_FILENAME/usr/local/Cellar/nginx/1.6.0_1/htmlfastcgi_script_name;

include /usr/local/etc/nginx/fastcgi_params;

}

在访问目录下(默认是/usr/local/Cellar/nginx/1.6.0_1/html)执行 php 文件了。在index.php测试一下 “phpinfo()” 吧

Mac下nginx的配置文件的默认存放路径--/usr/local/etc/nginx/,改文件下包括如下

1. sites-enabled(默认情况下没有sites-enabled这个文件夹,为了方便管理配置的server节点,创建sudo mkdir sites-enabled)

2. fastcgi.conf(提供参数的两个文件是 fastcgi.conf 和 fastcgi_params)

3. fastcgi_params(先有fastcgi_params,后有fastcgi.conf,除了SCRIPT_FILENAME这个参数之外,其他一模一样)

4. nginx.conf

nginx.conf

在server中修改本地服务访问的文件和浏览器访问的域名

server {

listen 80;

server_name localhost; //访问的域名,修改server_name 不能相同

root /Users/LIAN/Desktop/DIY/trunk/; //本地文件地址,不能出现中文

index index.html index.htm; //访问的文件

}

一个完整的nginx.conf例子

worker_processes 1;

error_log /Library/Logs/nginx/error.log debug;

events {

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 /Library/Logs/nginx/access.log main;

sendfile on;

keepalive_timeout 65;

idex index.html index.php;

include /usr/local/etc/nginx/sites-enabled/*;

}

修改PHP配置文件

/usr/local/etc/php/7.1/php.ini

/usr/local/etc/php/7.1/php-fpm.conf

include=/usr/local/etc/php/7.1/php-fpm.d/*.conf

/usr/local/etc/php/7.1/php-fpm.d/www.conf

listen = 127.0.0.1:9000

每次修改了php.ini文件都要重启php-fpm

sudo killall php-fpm #先停掉所有php-fpm的进程

cd /usr/local/Cellar/php71/7.1.8_20/sbin/

sudo ./php71-fpm start #启动php-fpm

这是一份相对简单的nginx配置

worker_processes 4;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

log_format main ‘remoteaddr?remote_user [timelocal]"request” ’

‘statusbody_bytes_sent “httpreferer”′‘”http_user_agent" "http_x_forwarded_for”’;

access_log/usr/local/var/log/nginx/access.logmain;

sendfileon;

keepalive_timeout65;

server{

listen80;

server_namelocalhost;

charsetutf-8;

access_log/usr/local/var/log/nginx/localhost.access.logmain;

root/var/www;#你的网站根目录

location/{

indexindex.htmlindex.htmindex.php;

try_filesuri /ruiindex.php?args;

}

error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

location ~ .php{

fastcgi_pass127.0.0.1:9000;

fastcgi_indexindex.php;

fastcgi_paramSCRIPT_FILENAME/var/wwwfastcgi_script_name;

include fastcgi_params;

}

location ~ /.ht {

deny all;

}

}

include sites-enabled/nginx-*.conf;

}

下面给出一份sites-enabled文件夹下的nginx的配置。需要说明的是,上面的配置中的server节点中的内容,包括server,

可以单独拿出来,放在一份单独的配置中,由最后一句的include的命令引入。

phptest.conf

server {

listen 80;

server_name phptest.dev 或 test.com之类;

charset koi8-r;

charset utf-8;

access_log /usr/local/var/log/nginx/phptest.dev.access.log main;

error_log /usr/local/var/log/nginx/phptest.dev.error.log;

root /var/www/phptest/web根据实际目录;

location / {

try_files uriuri/ /index.php?$args; ?????

index index.php index.html index.htm;

}

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{

fastcgi_pass127.0.0.1:9000;

fastcgi_indexindex.php;

fastcgi_paramSCRIPT_FILENAME/var/www/phptest/web(即document_root直接接后面的)$fastcgi_script_name;

include fastcgi_params;

try_files $uri =404;

}

deny access to .htaccess files, if Apache’s document root

concurs with nginx’s one

#location ~ /.ht {

deny all;

}

如上,打开浏览器输入http://localhost/访问第一个server节点配置的服务,输入http://test-local.com/访问第二个server节点。

请修改hosts的host配置,打开/etc/hosts文件,加入一行: 127.0.0.1 test-local.com

将TCP改成socket方式的配置方法:

修改php-fpm.conf

vim /usr/local/etc/php/7.1/php-fpm.conf

vim /usr/local/etc/php/7.1/php-fpm.d/www.conf (更改这个文件)

;listen = 127.0.0.1:9000

listen = /var/run/php7.1-fpm.sock

修改nginx配置文件server段(fastcgi_pass 127.0.0.1:9000; to fastcgi_pass unix:/tmp/php5-fpm.sock)

location ~ [^/].php(/|$) {

#fastcgi_pass 127.0.0.1:9000;

fastcgi_pass unix:/dev/shm/php-cgi.sock;

}

重启php-fpm与nginx

我们要在这个php-fpm文件里面设置nginx的用户主,才不会显示502

listen = /tmp/www.sock

user =

group =

listen.owner = nobody //定义属主

listen.group = nobody //定义属组

版本高于5.4(含5.4) 默认监听的socket文件权限是所有者只读,属组和其他用户没有任何权限。所以,nginx的启动用户(咱们配置的是nobody)就没有办法去读这个socket文件,最终导致502,这个问题可以在nginx的错误日志中发现。解决办法很简单,上面给出的配置文件中就有避免这个问题的配置。

listen.owner = nobody //定义属主

listen.group = nobody //定义属组

这两个配置就是定义socket的属主和属组是谁。除了这个还有一种方法

listen.mode = 777 这样nobody也可以有读取权限了。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值