本人简介:linux小白,Mac是刚刚购进的,虽为PHP开发人员但却一直都使用现成的包来配置开发环境,爱捣饰自己不会的东西。
最近学着自己动手在mac上安装php开发环境,我安装的环境是:PHP70,nginx1.10.2, mysql,在安装好环境后使用nginx配置虚拟主机用来访问现在的项目,然后问题来了:配置好后访问一直报错502,网上查找了很多资料都是什么与nginx无关,php的资源配置有问题,然而一个星期下来了,问题依然没有解决。
最后找到一份关于配置PHP70与nginx的资料,修改了nginx.conf文件,这才能够访问成功,到现在虽然也没弄明白问题具体出现在什么地方,但自己目前的思考结果是,可能新下载的nginx配置文件有很多的资源没有打开,所以导致访问一直报错502,现在把自己新配置的nginx.conf文件内容放上来,请大家多指教。 (安装目录:/usr/local/cellar 配置目录:/usr/local/etc/nginx (这个不清楚)/usr/local/var)
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log /usr/local/var/log/nginx/error.log warn;
#pid logs/nginx.pid;
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"';
#access_log logs/access.log main;
access_log /usr/local/var/log/nginx/access.log main;
port_in_redirect off;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 0;
#gzip on;
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index 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$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
}
# 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 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
include vhosts/*.conf;
}
配置文件的最后一行是我配置虚拟主机的文件,现把我配置的一个虚拟主机文件再放上来,请多指教。
server {
listen 80;
server_name localhost;
charset utf-8;
root /Users/mac登录用户名/项目路径/项目名称/; # 该项要修改为你准备存放相关网页的路径,注意路径的最后要以/结尾
#access_log logs/host.access.log main;
location / {
index index.html index.htm index.php;
autoindex on;
}
#proxy the php scripts to php-fpm
location ~ \.php$ {
include /usr/local/etc/nginx/fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
}
}
现在这样访问就能成功了。
上述的配置在访问成功后,过了一个周末回来再访问又报错502,很悲催,继续研究,这次比较顺利,只是开启了一下php70-fpm就访问成功了。注意:(1)mac默认安装有php,但是一般自己配置不会用它默认安装的php,反正我是自己重新下载的。(2)nginx对php的解析需要php-fpm,这一点与apache不同(可以参考http://blog.csdn.net/geyuezhen/article/details/50716780),而mac有因为默认已经安装php,如果你再安装一个php版本,就会有两个版本的ph-fpm,要使用哪一个需要你动手去启动,我这里安装的是PHP70所以我的启动命令是(这也可以用来重启你想要开启的对应版本的php-fpm):
(1)打开终端
(2)sudo killall php-fpm 先停掉所有php-fpm的进程
cd /usr/local/Cellar/php/版本号/sbin
不同版本的php,sbin目录里会有不同的php-fpm,比如php7.0的是php70-fpm, php5.6.18的是 php56-fpm,你想启动哪个fpm就
(3)敲 sudo ./php70-fpm start 或php56-fpm start就行
nginx的开启/暂停/重启
路径:cd /usr/local/Cellar/nginx/1.10.2_1/bin
sudo nginx / -s stop / -s reload