MNMP (Mac + Nginx + MySQL + PHP) 开发环境搭建

2,370 阅读2分钟

环境搭建:

安装homebrew

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

安装Mysql

先查找下mysql:brew search mysql 看一下mysql的版本信息: brew info mysql brew install mysql 设置密码: 安装时的消息有这么一句话We've installed your MySQL database without a root password. To secure it run:mysql_secure_installation,那就来设置下root的密码

第一步:打开mysql服务

mysql.server start

第二步:执行mysql_secure_installation

mysql_secure_installation # 执行后按照提示信息进行设置,慢慢看下英文,都能看懂的 启动mysql:

  • brew services start mysql
  • mysql.server start

安装php

先添加php扩展

brew update # 安装软件前都要习惯的更新下brew源
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

然后开始安装:

brew install php70 --with-debug --with-gmp --with-homebrew-curl --with-homebrew-libressl --with-homebrew-libxml2 --with-homebrew-libxslt --with-imap --with-libmysql --with-mssql

由于Mac自带了php和php-fpm,因此需要添加系统环境变量PATH来替代自带PHP版本,我们用的是zsh,所以放进.zshrc中,如果你用的shell是bash,那么可以把下面的信息写入到~/.bash_profile文件中,如果这个文件没有,你自己建一个就行。

echo 'export PATH="$(brew --prefix php70)/bin:$PATH"' >> ~/.zshrc  #for php
echo 'export PATH="$(brew --prefix php70)/sbin:$PATH"' >> ~/.zshrc  #for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbib:$PATH"' >> ~/.zshrc #for other brew install soft
source ~/.zshrc

下面先来看下php-fpm的配置文件,路径在/usr/local/etc/php/7.0/php-fpm.conf,

13 [global]
 14 ; Pid file
 15 ; Note: the default prefix is /usr/local/var
 16 ; Default Value: none
 17 ;pid = run/php-fpm.pid
 18 
 19 ; Error log file
 20 ; If it's set to "syslog", log is sent to syslogd instead of being written
 21 ; in a local file.
 22 ; Note: the default prefix is /usr/local/var
 23 ; Default Value: log/php-fpm.log
 24 ;error_log = log/php-fpm.log

自己看下上面的信息,去掉17行和24行前面的分号,使用php-fpm -t测试下配置是否正确,按提示信息是不管它也可以,默认就是在/usr/local/var路径下的,不过还是设置下吧;

php-fpm -t
[24-Oct-2016 11:20:31] NOTICE: configuration file /usr/local/etc/php/7.0/php-fpm.conf test is successful

php-fpm的一些管理:

#测试php-fpm配置
php-fpm -t

#启动php-fpm
php-fpm -D

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

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

#也可以用上文提到的brew命令来管理php-fpm
brew services start|stop|restart php70

#还可以用这个命令来管理php-fpm
php70-fpm start|stop|restart

安装Nginx

重启:sudo nginx -s reload 和前面一样先brew search nginx查找nginx, 看下信息brew info nginx 然后安装brew install nginx

  • brew install nginx
  • #测试配置是否有语法错误 nginx -t
  • #打开 nginx sudo nginx
  • #重新加载配置|重启|停止|退出 nginx nginx -s reload|reopen|stop|quit
  • sudo nginx

配置nginx,让它监听php-fpm的进程,这样当用户打开浏览器访问的时候,身为反向代理的nignx就能把东西让php去执行了。

我们要配置nginx.conf文件,创建一个php-fpm文件(监听php-fpm), 还要约定下将nginx.pid文件,log日志,以及以后我们要配置的站点.conf的路径,我们的路径约定还是按照brew默认的目录来设置,如下:

# nginx.conf,已经被创建好了,我们一会要更改下
/usr/local/etc/nginx/nginx.conf

# php-fpm,这个我们就放在和nginx.conf一样的路径下吧,这个要我们自己创建
/usr/local/etc/nginx/php-fpm

# 日志文件放在/usr/local/var/log/nginx中,默认已经有了access.log和error.log文件了
/usr/local/var/log/nginx/

# nginx.pid文件,放在/usr/local/var/run/下面,和php-fpm.pid放一堆
/usr/local/var/run/

# 以后要配置的站点.conf, 我们就放在/usr/local/etc/nginx/servers/下面,这个servers文件夹本身就存在的
/usr/local/etc/nginx/servers/

# 站点的根目录,也就用brew给我们设置的吧
/usr/local/var/www/

  • vim /usr/local/etc/nginx/nginx.conf
worker_processes  1;

error_log   /usr/local/var/log/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/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/servers/*;
}

  • vim /usr/local/etc/nginx/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;
    }
  • /usr/local/etc/nginx/servers/default.conf
server {
    listen       80;
    server_name  www.ya.com;
    root         /usr/local/var/www/local_yaspace;

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

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        index  index.html index.htm index.php;
        autoindex   on;
        include     /usr/local/etc/nginx/php-fpm;
    }

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            try_files $uri = 404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    error_page  404     /404.html;
    error_page  403     /403.html;
}
  • servers sudo nginx -t 测试下配置文件
  • sudo nginx # 已经开启的用sudo nginx -s reload 重启下