mac上php+nginx配置

brew的安装:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)”
php安装和配置
brew search php55
brew info php55—查看安装时是否需要带上参数--with-fpm
brew install php55 —with-fpm
brew install php-mcrypt(加密用的,不一定要安装的)
安装之后使用php -v查看版本,如果不是你安装的版本需要更改path
执行下列命令更改:
cd
vim .profile
export PATH=/usr/local/bin:$PATH
以下是对php-fpm.conf配置文件的相关修改
1.用户名修改:
user = zhaoye--用户
group = staff—用户组
2.端口
listen = 127.0.0.1:9000=>listen = /opt/run/php.socket(这里的socket是要给以后nginx配置用的)
3.php.ini配置文件修改
date.timezone = Asia/Shanghai
4.找到homebrew-php.josegonzalez.php55.plist (通过brew info php55查找)
拷贝到~/Library/LaunchAgents,因为是端口大于1024的都不是root启动
mv homebrew-php.josegonzalez.php55.plist php-fpm.plist(修改文件名)
修改php-fpm.plist文件
如下可以作为参考
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<false/>(这里不能为true,否则一直保持)
<key>Label</key>
<string>php-fpm</string>(启动服务的命令名)
<key>ProgramArguments</key>
<array>
<string>/usr/local/sbin/php-fpm</string>
<string>--fpm-config</string>
<string>/usr/local/etc/php/5.5/php-fpm.conf</string>(读取配置文件的路径)
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>mac</string>
<key>WorkingDirectory</key>
<string>/usr/local/var</string>
<key>StandardErrorPath</key>
<string>/var/log/php/php-fpm.log</string>
</dict>
</plist>

最后通过
launchctl load -w ~/Library/LaunchAgents/php-fpm.plist
launchctl start php-fpm
启动
到这里php就已经OK了

调试:
可以安装brew install php-xdebug+google_xdebug用来调试
cd /etc/php/5.5/conf.d
给ext-xdebug.ini添加xdebug.remote_enable = On开启调试模式


nginx安装和配置
1.brew install nginx
2.修改nginx的启动plist文件
根据brew info nginx可以看到/usr/local/opt/nginx/homebrew.mxcl.nginx.plist
cp /usr/local/opt/nginx/homebrew.mxcl.nginx.plist /Library/LaunchDaemons/nginx.plist
(nginx是80端口是需要root启动的,所以拷贝到根目录下的Library/LaunchDaemons)
修改nginx.plist文件
参考如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>nginx</string>(启动服务名)
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>(被杀掉之后自己能启动)
<key>UserName</key>
<string>root</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/nginx/bin/nginx</string>(nginx配置路径)
<string>-g</string>
<string>daemon off;</string>
</array>
<key>WorkingDirectory</key>
<string>/usr/local</string>
</dict>
</plist>
3.修改nginx配置文件

cd /usr/local/etc/nginx回到nginx配置文件目录
这里是新建了2个目录site-enabled,site-available用于配置文件(为了升级时减少修改量)
site-available里面存在真正的配置文件,site-enabled里面建链接指向site-available里面的配置文件
具体操作如下:
mkdir sites-enabled
mkdir sites-available
在site-available里面添加配置文件:localhost,test-seekyun.com
修改nginx.conf
参考如下:
user zhaoye admin;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

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

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

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


# HTTPS server
#
#server {
# listen 443;
# server_name localhost;

# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

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

}


里面的include /usr/local/etc/nginx/sites-enabled/*;标识了
其实际配置是在site-enabled里面,site-enabled里面的
test-seekyun.com -> ../sites-available/test-seekyun.com
是一个软链接指向../sites-available/test-seekyun.com
test-seekyun.com配置文件内容如下:
server {
listen 80;
server_name jk.myseekyun.com;(这里的服务名等下是要在/etc/hosts加上的)

#charset koi8-r;

#access_log logs/host.access.log main;

root /Users/zhaoye/workspace/jkweb;(php代码工程所在的目录)

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

#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_pass unix:/opt/run/php.socket;(这个是和上面php-fpm.conf里面的socket)
include fastcgi_params;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}

location /favicon.ico {
error_log /dev/null crit;
return 404;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}

localhost配置文件如下:
server {
listen 80;

#charset koi8-r;

#access_log logs/host.access.log main;

root /Users/zhaoye/workspace/;(php代码所在的目录)

location / {
index index.html index.htm index.php;
autoindex on;
}

#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_pass unix:/opt/run/php.socket;(这个是和php-fpm.conf名称配置一致)
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;
#}
}
cd site-enabled
ln -s site-available/localhost
ln -s site-available/test-seekyun.com
到这里所有的nginx配置都可以了

4.sudo -s(需要root启动)
launchctl load -w /Library/LaunchDaemons/nginx.plist
launchctl start nginx最后通过launchctl启动nginx服务
launchctl stop nginx关闭nginx服务

原文地址:https://www.cnblogs.com/xiaoleiel/p/8295308.html