Mac 下 Nginx、PHP、MySQL 和 PHP-fpm 的安裝和配置

說明: 原文:Mac 下 Nginx、PHP、MySQL 和 PHP-fpm 的安裝和配置我的博客永久地址。
博客中作了更新,可參考後面的更新記錄。

雜七雜八的雜

Mac下搭建MNPM環境是每一個使用者mac的phper必備的技能。通常都是找到新工做入職的第一天作的事情--配置環境。php

如上,今天入職的,配置開發環境。公司配備的硬件設備很屌,15年產macbook pro筆記本一臺(13寸),配置了8GB DDR3內存,2.7GHz core i5處理器,120GB的閃存。固然了,沒我本身買的配置高,哈哈~~html

安裝 Mac 的包管理器 - homebrew

home-brew是什麼?先這樣說吧,home-brew與OS X就像nodes與npm,java與maven(或者gradle),php與composer,apt-get與Ubutun,yum與centos,還有其餘等等吧,都是宿主的開發工具或包的依賴管理。java

安裝Homebrew以前,先安裝xcode命令行工具,安裝成功後,打開xcode的應用,查看Xcode是否爲最新,若是不是最新的請在App Store中升級Xcode。node

安裝xcode命令行工具的命令:mysql

xcode-select --install
Note【 NEW】:若是是Mac 10.x的版本,請下載Mac命令行工具安裝包。Mac新版本不支持使用 xcode-select --install安裝。下載 傳送門,選擇適合本身操做系統版本的CommondLineTools下載且安裝。

安裝完後,請使用brew doctor命令檢查當前環境是否最新符合brew運行,若是xcode的版本過低,則會有相應的提示信息。nginx

home-brew的安裝很容易,只要你的客戶終端安裝了ruby便可,其實,你一點都不用擔憂此事,OS X系統已經預裝了ruby。git

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

網上不少關於安裝homebrew的文章相較時間早,因此不少文章依然提供下面這種安裝,其實測試發現改地址已經返回404,並不能提供資源服務。github

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"web

關於Homebrew的更多知識,請參見下面幾個站點的連接。redis

安裝 Nginx 服務器

安裝好了Homebrew以後,即可以使用brew命令來安裝相應的包了。接下來,安裝nginx服務器。

brew install nginx

若是須要安裝nginx的其餘版本,可使用brew edit nginx來修改其內容:

class Nginx < Formula   desc "HTTP(S) server and reverse proxy, and
 IMAP/POP3 proxy server"   homepage "http://nginx.org/"   url
 "http://nginx.org/download/nginx-1.10.0.tar.gz"   sha256
 "8ed647c3dd65bc4ced03b0e0f6bf9e633eff6b01bac772bcf97077d58bc2be4d"
 head "http://hg.nginx.org/nginx/", :using => :hg

 bottle do
 sha256 "69839647f12306f8756eb7934eed946e55ffb47c1a2813f126523d824cd53a9d" =>
 :el_capitan
 sha256 "af4b2cad55c8414c2c29db340c94da9270ec66044f8a52f1d0e0efe1f11adb9b" =>
 :yosemite
 sha256 "8bc5364108c213b062427a98b361d3caf91e8f5a8ef518f23954bdb41e10b9df" =>
 :mavericks   end

 #Before submitting more options to this formula please check they
 aren\'t   # already in Homebrew/homebrew-nginx/nginx-full:   #
 https://github.com/Homebrew/homebrew-nginx/blob/master/Formula/nginx-full.rb
 option "with-passenger", "Compile with support for Phusion Passenger module"   option "with-webdav", "Compile with support for WebDAV module"   option "with-debug", "Compile with support for debug log"

brew edit nginx

從上面信息能夠看出nginx的下載地址等信息,能夠根據本身的需求修改。

brew執行完以後,nginx服務器就算安裝好了,運行下面幾條命了測試一下:

# 啓動 nginx服務
sudo nginx

# 從新加載配置|重啓|中止|退出 nginx
nginx -s reload|reopen|stop|quit

#測試配置是否有語法錯誤
nginx -t

nginx啓動後,在瀏覽器中輸入http://localhost:8080/,回車便可看到運行結果,顯示的是/usr/local/Cellar/nginx/1.10.0/html/index.html文件的內容。

相關參數可自行修改,具體怎麼配置nginx,請參看nginx的配置。

實際上,nginx服務的啓動能夠用-c nginx的配置文件參數制定其配置文件,默認加載/usr/local/etc/nginx/nginx.conf文件,固然nginx的操做不止這些命令,還有一些信號操做,關於nginx的信號操做等知識,留在它章詳細的說一下。

# 啓動 nginx
sudo ngixn -c /usr/local/etc/nginx/nginx.conf

#測試配置是否有語法錯誤
nginx -t -c /usr/local/etc/nginx/nginx.conf

補充

開機自啓動nginx服務設置:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/nginx/1.10.0/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

配置非管理員開機nginx自動啓動的權限和分組:

sudo chown root:wheel /usr/local/Cellar/nginx/1.10.0/sbin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.10.0/sbin/nginx
說明:{尊重知識,尊重別人的勞動成果}
參考文章: Mac下Nginx、MySQL、PHP-FPM的安裝配置

安裝和配置 MySQL 服務器

安裝mysql同nginx同樣簡單,執行brew命令:

brew install mysql

執行完brew命令,若是沒有出錯,mysql算是安裝到本機或者服務器了,固然,此過程會看到不少信息打印到shell窗口。

接下來,咱們執行一些簡單的配置命令。

  • a.初始化mysql數據庫:
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql

這裏須要說明一下,網上不少資料顯示須要tmpdir參數,以下:

mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

這個會出錯,具體緣由暫時不知道,在segmentfault上有人提問過了,目前沒有完美的答案。

  • b.查看mysql數據庫是否啓動,若是沒有啓動則使用mysqld命令啓動。
ps -ef | grep mysql

若是沒有啓動,使用安裝的mysql目錄下的mysqld命令啓動mysql:

/usr/local/Cellar/mysql/5.7.12/bin/mysqld

固然,你可直接使用/usr/local/bin/mysqld就能夠啓動mysql服務。

which mysqld

結果發現:

MacBook-Pro:joyven $ which mysql
 /usr/local/bin/mysqld

此時的數據庫沒有密碼,即便是root用戶,也無需密碼就可登陸到數據庫服務。所以,須要設置數據庫密碼:

/usr/local/bin/mysqladmin -u root password 'new-password'

如今訪問 mysql 仍是不用密碼就能夠鏈接,若是要設置一些登錄密碼的安全訪問限制,則需執行下面的 mysql安全安裝指令:

/usr/local//bin/mysql_secure_installation

主要是設置修改root密碼(設置過了能夠不用設置,略過)、刪除匿名訪問、刪除root網絡訪問、刪除test數據庫。指令執行完後,登錄mysql就須要密碼驗證了:

mysql -u root -p

開機啓動 mysql

mkdir -p ~/Library/LaunchAgents/
cp /usr/local/Cellar/mysql/5.7.12/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

若是要中止 mysql 服務則:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

安裝 PHP56 和 PHP-fpm

php的安裝很簡單,php-fpm目前已經集成到php的內核,能夠看成內核來安裝了。好久之前不是哦,請注意,我不記得是何時加入到內核的,若是你樂意查,查了請在評論區告訴我一下。

至於他爲何是內核的緣由,參考《搞不清FastCgi與PHP-FPM之間是個什麼樣的關係》的提問,主要看看下面各位大神@的回答。

這是2018年3月份以前的安裝方式:安裝php以前,請先用brew tap命令引入第三方的php庫,brew倉庫中沒有php的安裝包。

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

Note:【NEW】因爲Homebrew/php自來水在2018年3月底被棄用,並將全部PHP公式轉移到Homebrew/core,舊的brew tap homebrew/dupesbrew tap homebrew/versionsbrew tap homebrew/homebrew-php都會報如下錯誤。

Warning: homebrew/dupes was deprecated. This tap is now empty as all its formulae were migrated.

解決方法:安裝brew是homebrew/core做爲核心已加入到brew倉庫。若是你已經手動執行過brew tap homebrew/dupes或者brew tap josegonzalez/homebrew-php,則須要刪除已經加過的倉庫,對應的命令爲:brew untap homebrew/dupes或者brew untap josegonzalez/homebrew-php

萬事俱備,只欠東風,不,只欠一條命令。

brew install php56 --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm

下面是輸出的信息:

brew install php56 --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm --with-curl=/usr/local/Cellar/curl/
==> Installing php56 from josegonzalez/php
==> Installing dependencies for josegonzalez/php/php56: readline, postgre
==> Installing josegonzalez/php/php56 dependency: readline
==> Downloading https://homebrew.bintray.com/...
####################################################################### 100.0%
==> Pouring readline-6.3.8.el_capitan.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

OS X provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are defaulting this GNU Readline installation to keg-only.

Generally there are no consequences of this for you. If you build your own software and it requires this formula, you'll need to add to your build variables:

LDFLAGS:  -L/usr/local/opt/readline/lib
CPPFLAGS: -I/usr/local/opt/readline/include

==> Summary
🍺 /usr/local/Cellar/readline/6.3.8: 46 files, 2.0M
==> Installing josegonzalez/php/php56 dependency: postgresql
==> Downloading https://homebrew.bintray.com/...
######################################################################## 100.0%
==> Pouring postgresql-9.5.2.el_capitan.bottle.1.tar.gz
==> /usr/local/Cellar/postgresql/9.5.2/bin/initdb /usr/local/var/postgres
==> Caveats
If builds of PostgreSQL 9 are failing and you have version 8.x installed,you may need to remove the previous version first. See:
https://github.com/Homebrew/h...

To migrate existing data from a previous major version (pre-9.0) of PostgreSQL, see:
https://www.postgresql.org/do...

To migrate existing data from a previous minor version (9.0-9.4) of PosgresSQL, see:
https://www.postgresql.org/do...

You will need your previous PostgreSQL installation from brew to perform pg_upgrade.
Do not run brew cleanup postgresql until you have performed the migration.

To have launchd start postgresql now and restart at login:
brew services start postgresql
Or, if you don't want/need a background service you can just run:
postgres -D /usr/local/var/postgres
==> Summary
🍺 /usr/local/Cellar/postgresql/9.5.2: 3,135 files, 34.9M
==> Installing josegonzalez/php/php56 dependency: libpng
==> Downloading https://homebrew.bintray.com/...
######################################################################## 100.0%
==> Pouring libpng-1.6.21.el_capitan.bottle.tar.gz
🍺 /usr/local/Cellar/libpng/1.6.21: 25 files, 1.2M
==> Installing josegonzalez/php/php56 dependency: freetype
==> Downloading https://homebrew.bintray.com/...
######################################################################## 100.0%
==> Pouring freetype-2.6.3.el_capitan.bottle.tar.gz
🍺 /usr/local/Cellar/freetype/2.6.3: 61 files, 2.5M
==> Installing josegonzalez/php/php56 dependency: gettext
==> Downloading https://homebrew.bintray.com/...
######################################################################## 100.0%
==> Pouring gettext-0.19.7.el_capitan.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

OS X provides the BSD gettext library and some software gets confused if both are in the library path.

Generally there are no consequences of this for you. If you build your own software and it requires this formula, you'll need to add to your build variables:

LDFLAGS:  -L/usr/local/opt/gettext/lib
CPPFLAGS: -I/usr/local/opt/gettext/include

==> Summary
🍺 /usr/local/Cellar/gettext/0.19.7: 1,934 files, 16.7M
==> Installing josegonzalez/php/php56 dependency: icu4c
==> Downloading https://homebrew.bintray.com/...
######################################################################## 100.0%
==> Pouring icu4c-57.1.el_capitan.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

OS X provides libicucore.dylib (but nothing else).

Generally there are no consequences of this for you. If you build your own software and it requires this formula, you'll need to add to your build variables:

LDFLAGS:  -L/usr/local/opt/icu4c/lib
CPPFLAGS: -I/usr/local/opt/icu4c/include

==> Summary
🍺 /usr/local/Cellar/icu4c/57.1: 265 files, 65.0M
==> Installing josegonzalez/php/php56 dependency: imap-uw
==> Downloading https://homebrew.bintray.com/...
######################################################################## 100.0%
==> Pouring imap-uw-2007f.el_capitan.bottle.tar.gz
🍺 /usr/local/Cellar/imap-uw/2007f: 151 files, 9.0M
==> Installing josegonzalez/php/php56 dependency: jpeg
==> Downloading https://homebrew.bintray.com/...
######################################################################## 100.0%
==> Pouring jpeg-8d.el_capitan.bottle.2.tar.gz
🍺 /usr/local/Cellar/jpeg/8d: 19 files, 713.7K
==> Installing josegonzalez/php/php56 dependency: libxml2
==> Downloading https://homebrew.bintray.com/...
######################################################################## 100.0%
==> Pouring libxml2-2.9.3.el_capitan.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

OS X already provides this software and installing another version in parallel can cause all kinds of trouble.

Generally there are no consequences of this for you. If you build your own software and it requires this formula, you'll need to add to your build variables:

LDFLAGS:  -L/usr/local/opt/libxml2/lib
CPPFLAGS: -I/usr/local/opt/libxml2/include

==> Summary
🍺 /usr/local/Cellar/libxml2/2.9.3: 276 files, 9.8M
==> Installing josegonzalez/php/php56 dependency: unixodbc
==> Downloading https://homebrew.bintray.com/...
######################################################################## 100.0%
==> Pouring unixodbc-2.3.4.el_capitan.bottle.tar.gz
🍺 /usr/local/Cellar/unixodbc/2.3.4: 39 files, 952.3K
Warning: josegonzalez/php/php56: --with-pgsql was deprecated; using --with-postgresql instead!
==> Installing josegonzalez/php/php56
==> Downloading https://php.net/get/php-5.6.2...
==> Downloading from https://secure.php.net/distri...
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/php56/5.6.21 --localstatedir=/usr/loc
==> make
==> make install
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:

LoadModule php5_module

/usr/local/opt/php56/libexec/apache2/libphp5.so

The php.ini file can be found in:

/usr/local/etc/php/5.6/php.ini

✩✩✩✩ Extensions ✩✩✩✩

If you are having issues with custom extension compiling, ensure that you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:

PATH="/usr/local/bin:$PATH"

PHP56 Extensions will always be compiled against this PHP. Please install them using --without-homebrew-php to enable compiling against system PHP.

✩✩✩✩ PHP CLI ✩✩✩✩

If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc, ~/.zshrc, ~/.profile or your shell's equivalent configuration file:

export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"

✩✩✩✩ FPM ✩✩✩✩

To launch php-fpm on startup:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

The control script is located at /usr/local/opt/php56/sbin/php56-fpm

OS X 10.8 and newer come with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH:

PATH="/usr/local/sbin:$PATH"

You may also need to edit the plist to use the correct "UserName".

Please note that the plist was called 'homebrew-php.josegonzalez.php56.plist' in old versions of this formula.

To have launchd start josegonzalez/php/php56 now and restart at login:
brew services start josegonzalez/php/php56
==> Summary
🍺 /usr/local/Cellar/php56/5.6.21: 331 files, 56.2M, built in 8 minutes 14 seconds

我我什麼要把這些信息貼出來,緣由很簡單,這些信息給我給出了咱們安裝是否成功,安裝在那個路徑下了,接下來咱們還須要手動作些什麼(好比開機啓動等)。

因爲Mac系統預裝了php以及php-fpm,因此,爲了啓動的時候能直接啓動安裝的最新的php,請把php安裝的二進制文件所在的路徑加入到系統路徑中,這個地方須要注意了。若是咱們設置了開機的時候自動啓動,咱們不會發現有什麼問題,但是當我從新配置了擴展,須要從新啓動php-fpm的時候,咱們若是直接使用php-fpm -c /usr/local/etc/php/5.6/php.ini -y /usr/local/ext/php/5.6/php-fpm.conf的時候,咱們發現咱們的擴展安裝沒有起做用,緣由是咱們使用了/usr/sbin/php-fpm的命令,能夠經過which查看。因此須要注意,使用brew安裝的php-fpm是在/usr/local/opt/php56/目錄下面。

export PATH="$(brew --prefix php54)/bin:$PATH"

到此,php以及PHP-fpm已經安裝成功了。那麼咱們仍是設置php-fpm開機啓動,在哪裏找這段代碼呢,安裝過程打印的信息,也就是我爲何要把安裝過程信息完完整整貼一遍的緣由:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

順便說一下或許對你在安裝過程頗有用的命令:

  • 殺死一個進程的命令:
sudo kill -9 pid #pid替換爲你的進程號
  • 好比你要殺死php-fpm進程,也可使用以下命令:
sudo killall php-fpm
# 或者
sudo killall -HUP php-fpm
  • 如何查看進程號:
ps -ef | grep php-fpm

配置 Nginx 服務器

Nginx服務器的配置,這裏只做簡單的配置部分說明,至於想對較爲繁瑣的配置,好比ip_hash,upstream,gzip,反向代理等內容放到它章做解。

假如你的網站根目錄是在/var/www下面,那麼咱們只需配置一個簡單的站點做爲nginx配置的開始。

前面說過了,Mac下nginx的配置文件的默認存放路徑--/usr/local/etc/nginx/,改文件下包括以下幾個文件(夾):

  1. fastcgi.conf
  2. mime.types
  3. servers
  4. fastcgi.conf.default
  5. mime.types.default
  6. sites-enabled
  7. fastcgi_params
  8. nginx.conf
  9. uwsgi_params
  10. fastcgi_params.default
  11. nginx.conf.default
  12. uwsgi_params.default
  13. koi-utf
  14. scgi_params
  15. win-utf
  16. koi-win
  17. scgi_params.default

默認狀況下,沒有sites-enabled這個文件夾,這個時咱們爲了方便管理配置的server節點,建立的一個文件夾。建立命令:sudo mkdir sites-enabled

配置中常常用到一個nginx的參數,提供參數的兩個文件時fastcgi.conf和fastcgi_params,這兩個文件除了SCRIPT_FILENAME這個參數以外,其餘如出一轍,爲何要如出一轍呢,具體自省Google,這裏是說,先有fastcgi_params,後有fastcgi.conf,因此大多數的時候咱們看到配置中不少人仍是喜歡繼續用fastcgi_params。

nginx.conf的基本配置:

#user  nobody;
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        charset utf-8;

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

        root /var/www;
        location / {
            #root   html;
            index  index.html index.htm index.php;
            try_files $uri /$uri index.php?$args;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            #root           /var/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }

    include sites-enabled/nginx-*.conf;

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    #include servers/*;
}

上面的太亂了,咱們簡化一下吧:

worker_processes  4;

events {
    worker_connections  1024;
}

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;


    server {
        listen       80;
        server_name  localhost;

        charset utf-8;

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

        root /var/www; #你的網站根目錄
        location / {
            index  index.html index.htm index.php;
        try_files $uri /$rui index.php?$args;
        }

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

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
            include        fastcgi_params;
        }

        location ~ /\.ht {
            deny  all;
        }
    }

    include sites-enabled/nginx-*.conf;
}

這是一份相對簡單的nginx配置,相對複雜一點的配置會在複雜的場景中使用,通常初步開發這些就能夠知足了。

下面給出一份sites-enabled文件夾下的nginx的配置。須要說明的是,上面的配置中的server節點中的內容,包括server,能夠單獨拿出來,放在一份單獨的配置中,由最後一句的include的命令引入。

nginx-test.conf

server {
    listen 80;
    server_name test-local.com;

    charset utf-8;
    access_log /usr/local/var/log/nginx/test-local.com.access.log main;
    error_log /usr/local/var/log/nginx/test-local.com.error.log;

    root /var/www/test-php/backend/web;

    location / {
        try_files $uri $uri/ /index.php?$args;
        index index.html index.htm index.php;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/test-php/backend/web$fastcgi_script_name;
        include fastcgi_params;
        try_files $uri =404;
    }
}

如上,打開瀏覽器輸入http://localhost/訪問第一個server節點配置的服務,輸入http://test-local.com/訪問第二個server節點。

可是,別太急,請修改hosts的host配置,打開/etc/hosts文件,加入一行:

127.0.0.1 test-local.com

結束以前,擴展幾點

大多數時候,咱們不得不安裝php的擴展,好比 GD,mcrypt,CURL,XML,MEMCACHED 等擴展配置,這些東西在咱們的開發中經常用到,對於幾個相對較難的擴展,作一些記錄吧。

  • curl安裝
  • mcrypt安裝
  • memcached安裝

更新日誌

更新:2016-05-10 23:19

  • 殘片段章
  • 補充php和php-fpm的安裝
  • mac下安裝php擴展:
brew install php56-apcu php56-intl php56-redis php56-uuid php56-zookeeper \
php56-thrift php56-solr php56-ssh2 php56-gmagick php56-kafka php56-libevent \
php56-imagick php56-msgpack php56-geoip php56-mcrypt php56-swoole \
php56-scrypt php56-xdebug php56-yaf php56-yaml php56-xhprof \
php56-memcache php56-memcached php56-gearman

更新:2019-10-06 20:43:50

  • 增長了xcode安裝命令行工具xcode-select --install失敗的狀況,建議下載命令行工具dmg文件安裝
  • 增長了使用brew tap homebrew/dupesbrew tap josegonzalez/homebrew-php在2018年3月份被廢棄後安裝時報錯誤的解決方法。
相關文章
相關標籤/搜索