OSX配置Nginx和PHP开发环境(整理)

安装前准备

  • 更新brew brew update
  • 安装xcode工具 xcode-select --install

安装Nginx

安装

brew install nginx

配置

cd /usr/local/etc/nginx/
vim nginx.conf
在http{} 字段里添加,位置在27行左右,sendfile on;之前
client_max_body_size 1024M;
(vim显示行号的方法,在vim命令行模式输入set nu)

开机启动Nginx

sudo cp /usr/local/opt/nginx/*.plist  /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

安装Mysql

安装

brew install mysql

启动Mysql

sudo mysql.server start

开机启动Mysql

mkdir -p ~/Library/LaunchAgents

cp /usr/local/Cellar/mysql/5.6.16/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

常见错误

ERROR! The server quit without updating PID file (/usr/local/var/mysql/Qzhou.local.pid)

解决

查看具体错误,可以发现一般是权限问题

vim /usr/local/var/mysql/xxxx.err

确认_mysql用户组和用户是否存在

dscl . list /groups |grep _mysql
dscl . list /users |grep _mysql
修改mysql目录用户组
sudo chown -R _mysql:_mysql /usr/local/var/mysql

再次启动

sudo mysql.server start

安装php55和php-fpm

安装

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
(如果tap错了,可以brew untap xxx删除)
brew install php55 --with-debug --with-fpm --with-pear --with-imap --with-intl --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd

配置php

vim /usr/local/etc/php/5.5/php.ini
修改以下配置(vim搜索方法:向下搜索命令行模式输入/post_max,向上搜索?post_max,下一项n,上一项N)
post_max_size = 1024M
memory_limit = 1024M
upload_max_filesize = 1024M

配置php-fpm

vim /usr/local/etc/php/5.5/php-fpm.conf
查找: listen = 127.0.0.1:9000  替换为: listen = /var/run/php5-fpm.sock
找到以下三句,删除前面的分号
;listen.owner = _www
;listen.group = _www
;listen.mode = 0666

创建软连接启动php-fpm

ln -s /usr/local/opt/php55/sbin/php55-fpm /usr/local/bin/

启动

sudo php55-fpm start

重启

sudo php55-fpm restart

新建php工作目录

cd ~
mkdir htdocs

再次配置Nginx,启用php

vim /usr/local/etc/nginx/nginx.conf
修改 server 的listen字段:
listen 80;
root   /Users/Qzhou/htdocs; #改成自己的
修改 location / 为如下:
location / {
    root html # 移动到location外面了,对应上面的root
    index  index.php;
    autoindex on; #本地开发开启目录浏览,服务器勿开启
    autoindex_exact_size off;
    autoindex_localtime on;
}
修改 location ~ \.php$为如下:
location ~ \.php$ {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

重启Nginx要两步

sudo pkill nginx
sudo nginx

重新配置Nginx

sudo nginx -s reload

配置虚拟域名

创建虚拟域名

sudo vi /etc/hosts
输入 127.0.0.1       zlzkj.io // 虚拟域名

配置虚拟目录

/usr/local/etc/nginx/servers
touch zlzkj.io.conf // 创建虚拟主机配置
并修改nginx.conf文件
include servers/*.conf;

参考配置

zlzkj.io.conf的配置文件

server {
    listen 80;

    server_name zlzkj.io;

    root /Volumes/Mac/www/zlzkj/web; #改成自己的

    #access_log /var/log/nginx/zlzkj.io.access.log;
    error_log /var/log/nginx/zlzkj.io.error.log;

    location / {
        index index.php;
        try_files $uri @rewriteapp;
        add_header Access-Control-Allow-Origin *;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /index.php/$1 last;
    }

    location ~ ^/(index|index_dev)\.php(/|$) {
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS              off;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 8 128k;
    }

    location ~* \.(jpg|jpeg|gif|png|ico|swf)$ {
        expires 3y;
        access_log off;
        gzip off;
    }

    location ~* \.(css|js)$ {
        access_log off;
        expires 3y;
    }


    # 以下配置允许运行.php的程序,方便于其他第三方系统的集成。
    location ~ \.php$ {
        # [改] 请根据实际php-fpm运行的方式修改
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS              off;
    }
}