MNMP (Mac + Nginx + MySQL + PHP) 開發環境搭建

環境搭建:php

安裝homebrew

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

安裝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服務

mysql.server startnginx

第二步:執行mysql_secure_installation

mysql_secure_installation # 執行後按照提示信息進行設置,慢慢看下英文,都能看懂的 啓動mysql:git

  • brew services start mysql
  • mysql.server start

安裝php

先添加php擴展github

brew update # 安裝軟件前都要習慣的更新下brew源
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
複製代碼

而後開始安裝:sql

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文件中,若是這個文件沒有,你本身建一個就行。shell

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,vim

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 重啓下
相關文章
相關標籤/搜索