PHP 环境搭建

php 安装与配置

安装

brew install php56 --without-snmp --without-apache --with-debug --with-fpm --with-intl --with-homebrew-curl --with-homebrew-libxslt --with-homebrew-openssl --with-mysql

   brew install php56-xdebug

配置

由于Mac本身自带有PHP,因此需要添加系统环境变量PATH来替代自带PHP版本。

export PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin"

修改PHP配置文件

vi /usr/local/etc/php/5.6/php.ini
vi /usr/local/etc/php/5.6/php-fpm.conf
vi /usr/local/etc/php/5.6/conf.d/ext-xdebug.ini

在 php.ini 文件中,找到 date.timezone ,去掉注释,并配置如下:

date.timezone = Asia/Hong_Kong

在ext-xdebug.ini文件中加入以下配置:

xdebug.idekey="PHPSTORM"
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
;xdebug.remote_host=localhost
xdebug.remote_port=9010
xdebug.remote_autostart = on

修改php-fpm配置文件,vim /usr/local/etc/php/5.6/php-fpm.conf,找到pid相关大概在25行,去掉注释 pid = run/php-fpm.pid, 那么php-fpm的pid文件就会自动产生在/usr/local/var/run/php-fpm.pid。

测试

php -i | less

测试配置文件

php-fpm -t

启动与关闭

#启动php-fpm
php-fpm -D

#关闭
kill -INT `cat /usr/local/var/run/php-fpm.pid`

重启
kill -USR2 `cat /usr/local/var/run/php-fpm.pid`

启动之后,确保php运行在9000端口

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

安装PHP7

//brew tap josegonzalez/php

brew install php70 --without-snmp --without-apache --with-debug --with-fpm --with-intl --with-homebrew-curl --with-homebrew-libxslt --with-homebrew-openssl --with-mysql

   brew install php70-xdebug

   //如果上面不成功,则用下面命令
   brew install --build-from-source php70-xdebug

安装多版本PHP

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
brew install php-version
brew untap josegonzalez/homebrew-php

vi $HOME/.zshrc
增加:source $(brew --prefix php-version)/php-version.sh && php-version 5

//安装php
brew install php56
brew unlink php56

附CentOS下PHP安装

yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof
yum -y install php-xml

安装 composer

brew install composer

设置composer仓库地址

composer config -g repositories.packagist composer http://packagist.phpcomposer.com

nginx 安装与配置

安装

brew install nginx

配置

brew默认安装nginx在 /usr/local/Cellar/nginx/xxx-version/下,因此默认的配置文件指向本目录下面的 html 目录。

创建相关文件

mkdir /usr/local/var/logs/nginx
mkdir /usr/local/etc/nginx/conf.d
mkdir 

修改 /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"';

     access_log  /usr/local/var/logs/access.log  main;

     sendfile        on;
     keepalive_timeout  65;
     port_in_redirect off;

     include /usr/local/etc/nginx/conf.d/*.conf;
}

设置nginx php-fpm配置文件

vim /usr/local/etc/nginx/conf.d/php-fpm
#proxy the php scripts to 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;
}

创建默认虚拟主机default

vim /usr/local/etc/nginx/conf.d/default.conf
server {
    resolver 8.8.8.8;
    listen 80 default_server;
    server_name  _;
    location / {
        proxy_pass  $scheme://$host$request_uri;
        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-for $remote_addr;
    }

    location  /tagboard/1.0.14/js/index.js {
        root   "/local/replace/server";
    }

}

server {
    listen       80;
    server_name  localhost;

    charset urf8;

    root html;

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

    #error_page  404              /404.html;

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

    location ~ \.php$ {
        include         conf.d/php-fpm;
    }

}

测试

测试配置文件是否有错误

nginx -t

nginx 默认监听8080端口,为了改成80端口,则:

sudo chown root:wheel /usr/local/Cellar/nginx/xxx-version/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/xxx-version/bin/nginx

启动与关闭

#启动
nginx

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

如果需要nginx开机启动,则:

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents

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

常见问题

1. nginx 403 forbidden

引起nginx 403 forbidden有二种原因,一是缺少索引文件,二权限问题。

如果是权限问题,则:

vi /usr/local/etc/nginx/nginx.conf

user user [group]

# 默认group等于user,为了确保正确,执行 ls -la