在OSX上安装php开发环境

开始慢慢体会到 Mac 作为开发机的优势了,记录一下搭建 php 开发环境的过程。

一些准备工作

安装Xcode

首先必须要安装xcode,通过Mac appStore就可以.然后还要注意安装code命令行工具:

    xcode-select --install

安装好了以后记得先手动打开一次xcode,同意一下用户协议.

安装homebrew

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

安装iterm 和 Oh-My-Zsh

iterm 是很好用的命令行工具,这个可以直接去官网 下载安装.

zsh是非常复杂强大的shell,而Oh-My-Zsh又是非常简单易用的zsh.所以…

自动安装

    wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

不过OSX貌似没有自带wget,所以需要 brew去装一个

手动安装

    git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
    cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

安装好了以后需要退出当前的shell,重开你的iterm就能使用强大的Oh-My-Zsh了.

zsh 的配置文件位于 ~/.zshrc ,可以先尝试着去修改一下主题什么的,慢慢摸索.

安装php

因为我们使用的ngnix,所以在安装的时候要注意编译fpm以及禁用apache:

    brew install php56 \
    --without-snmp \
    --without-apache \
    --with-fpm \
    --with-intl \
    --with-homebrew-curl \
    --with-homebrew-libxslt \
    --with-homebrew-openssl \
    --with-imap \
    --with-mysql \
    --with-tidy

安装好了以后注意看说明,brew会提示如何把php加入开机启动:

    ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

修改环境变量

由于系统已经自带了php环境,因此我们需要修改环境变量,优先执行我们安装的php版本,修改~/.zshrc 文件:

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

然后让配置生效:

    source ./zshrc

这时候我们再通过php -vphp-fpm -v 查看,就能发现运行的是我们安装的php版本了.

brew 安装的php有时候会带有一个bug,当发起https请求的时候会报错.这时候需要我们重新安装openssl:

    brew rm curl
    brew update
    brew install —with-openssl curl

再重启一下fpm就可以了.

安装php相关的拓展

    brew install php56-gearman php56-msgpack php56-memcache php56-memcached php56-mongo php56-redis php56-xdebug

由于众所周知的原因brew经常出现下载失败的状况,一般只要重新下载就可以了.但是有时候会报某个包不存在,但是手动 brew install 却提示已经安装的情况.这时候就需要brew uninstall xxx 之后再 brew install xxx 就可以了

安装nginx

    brew install nginx

同样根据提示,加入开机启动:

    ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

nginx 安装后只能监听非80端口,监听80端口候需要给予root权限(注意路径的版本号,有可能不同):

    sudo chown root:wheel /usr/local/Cellar/nginx/1.8.0/bin/nginx
    sudo chmod u+s /usr/local/Cellar/nginx/1.8.0/bin/nginx

nginx.conf的一些配置

我们可以仿照ubuntu下的nginx的文件夹配置:

    mkdir -p /usr/local/etc/nginx/sites-available
    mkdir -p /usr/local/etc/nginx/conf

然后修改/usr/local/etc/nginx/nginx.conf文件:


    #user  nobody;

    worker_processes  1;

    #指定error.log的路径

    error_log  /usr/local/var/log/nginx/error.log debug;

    #pid        logs/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的路径

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

        sendfile        on;

        keepalive_timeout  65;

        #gzip  on;

        port_in_redirect off;

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

    }

然后/usr/local/etc/nginx/site-enabled/目录可以N个配置文件对应N个项目了,比如一个local.conf文件:

    server {

        listen       80;

        server_name  localhost;

        root         /Users/evanszhang/develop;

        index index.php;

        location ~ \.php {

            include fastcgi_params;

            fastcgi_buffers 128 4k;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /home/larablog/public$fastcgi_script_name;

        }

    }

最后用root启动nginx:

    sudo nginx

安装mysql

    brew install mysql

同样最好也是开机启动

    ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

通过以下命令为mysql的root用户设置一个密码:

    mysqladmin -uroot password "mypassword"

安装其他组件:

redis

    brew install redis

设置开机启动:

    ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

修改/usr/local/etc/redis.conf配置文件,将daemonize的值改为yes.允许以守护进程执行.

如果想手动启动redis,可以执行以下命令:

    redis-server /usr/local/etc/redis.conf

memcached

    brew install memcached

设置开机启动:

    ln -sfv /usr/local/opt/memcached/*.plist ~/Library/LaunchAgents
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist

以上安装完成.

Comments

comments powered by Disqus