原文路径:http://youyusan.github.io/2016/01/30/php-nginx-in-mac/
测试php-fpm
php-fpm -v
附:测试Mac自带的php-fpm命令:/usr/bin/php-fpm -v
运行php-fpm
sudo php-fpm -D
关闭php-fpm
sudo killall php-fpm
查看php-fpm运行状态
lsof -Pni4 | grep LISTEN | grep php
php-fpm开机启动
ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
可能出现的问题一
ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory
ERROR: failed to load configuration file '/private/etc/php-fpm.conf'
ERROR: FPM initialization failed
解决方法
sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
可能出现的问题二
ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
ERROR: failed to post process the configuration
ERROR: FPM initialization failed
解决方法
sudo vi /private/etc/php-fpm.conf
#找到`error_log`项,在下面添加:
error_log = /usr/local/var/log/php-fpm.log
pid = /usr/local/var/run/php-fpm.pid
安装nginx
基础安装
安装命令:
brew install nginx
启动关闭命令:
#测试配置是否有语法错误
nginx -t
#打开 nginx
sudo nginx
#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit
开机启动:
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
nginx监听80端口:
sudo chown root:wheel /usr/local/Cellar/nginx/1.6.0_1/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.6.0_1/bin/nginx
配置文件
按ubuntu文件格式来储存配置文件,方便配置多个域名:
mkdir -p /usr/local/var/logs/nginx
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl
编辑Nginx全局配置
vim /usr/local/etc/nginx/nginx.conf
#添加以下内容
worker_processes 1;
error_log /usr/local/var/logs/nginx/error.log debug;
pid /usr/local/var/run/nginx.pid;
events {
worker_connections 256;
}
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" $host $request_time $upstream_response_time $scheme '
'$cookie_evalogin';
access_log /usr/local/var/logs/access.log main;
sendfile on;
keepalive_timeout 65;
port_in_redirect off;
include /usr/local/etc/nginx/sites-enabled/*;
}
设置nginx php-fpm配置文件
vim /usr/local/etc/nginx/conf.d/php-fpm
#输入以下配置
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
include /usr/local/etc/nginx/fastcgi.conf;
}
在/usr/local/etc/nginx/sites-enabled
目录下,一个文件对应一个域名的配置,我们设置web服务器目录是/var/www
sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod 775 /var/www
vi /var/www/info.php
vi /var/www/index.html
vi /var/www/403.html
vi /var/www/404.html
创建默认虚拟主机default
vim /usr/local/etc/nginx/sites-available/default
#输入以下配置
server {
listen 80;
server_name localhost;
root /var/www/;
access_log /usr/local/var/logs/nginx/default.access.log main;
location / {
index index.html index.htm index.php;
autoindex on;
include /usr/local/etc/nginx/conf.d/php-fpm;
}
location = /info {
allow 127.0.0.1;
deny all;
rewrite (.*) /.info.php;
}
error_page 404 /404.html;
error_page 403 /403.html;
}