mac os x安裝ngigx+php fastcgi+mysql+memcache詳細流程

Part 1: MacPortsphp

Mac上裝軟件經常使用的是MacPorts和homebrew,這個軟件會很方便地提供軟件的安裝。裝這些前先得裝Xcode,Xcode在appstore上有,一個多G,下載安裝,完成後在launchpad上回有Xcode圖標,要點擊纔是正式安裝,裝完後你在終端輸入gcc -v,發現無此命令,沒這個命令可無法裝軟件哦,應該是Xcode默認安裝沒有裝完整,打開Xcode,點擊左上方Xcode-》Open develop tools-》more develop tools,進入Xcode的網頁搜索UNIX,安裝command_line_tools_for_xcode_.dmg,完成後再進入終端,輸入gcc -v,ok有了,哈哈哈。html

我使用的是macports安裝,macports到http://www.macports.org/install.php下載dmg文件安裝就能夠了,安裝成功後,在終端輸入node

port list #能夠看全部軟件列表mysql

port search xxx #能夠搜索是否有該軟件nginx

port install xxx #安裝git

port uninstall xxx #卸載github

port clean xxx #刪除安裝臨時文件web

Ps:homebrew的安裝sql

 

首先:
sudo chown -R `whoami` /usr/local
xcode

而後能夠正式開始安裝,我推薦的安裝方式是先用 git-osx-installer 裝上 git,而後用 git 安裝:

cd /usr/local

git init

git remote add origin git://github.com/mxcl/homebrew.git

git pull origin master

裝完後命令與port命令差很少 好比 brew search;brew install

Part 2: Nginx

 

在終端上輸入

sudo port install nginx spawn-fcgi

安裝完成後啓動nginx會說沒有nginx.conf文件,到/opt/local/etc/nginx目錄下看到如下幾個文件:

fastcgi_params.example koi-utf koi-win mime.types.example nginx.conf.example win-utf

直接複製example文件:

sudo mv mime.types.example mime.types

sudo mv nginx.conf.example nginx.conf

啓動nginx:

sudo nginx

訪問http://127.0.0.1/,就能夠看到NginxWelcome頁面。

sudo nginx -t 檢測配置文件是否有效

sudo nginx -s reload 重啓

sudo nginx -h # 幫助

重點來了

一、讓nginx開機自動啓動

vi /Library/LaunchDaemons/org.macports.nginx.plist

內容爲

 

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"

"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >

<plist version='1.0'>

<dict>

<key>Label</key><string>org.macports.nginx</string>

<key>ProgramArguments</key>

<array>

        <string>/opt/local/bin/daemondo</string>

        <string>--label=nginx</string>

        <string>--start-cmd</string>

        <string>/opt/local/sbin/nginx</string>

        <string>;</string>

        <string>--pid=fileauto</string>

        <string>--pidfile</string>

        <string>/opt/local/var/run/nginx/nginx.pid</string>

</array>

<key>Debug</key><false/>

<key>Disabled</key><true/>

<key>KeepAlive</key><true/>

</dict>

</plist>

若是文件已經有內容直接退出vi

執行

sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist

這樣nginx開機就回自動啓動

二、修改nginx.conf內容

vi /opt/local/etc/nginx/nginx.conf

下面是個人內容

 

#user  nobody;

user  www www;

worker_processes  1;

error_log  /Users/apple/logs/nginx_errors.log;

events {

    worker_connections  4098;

}

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  /Users/apple/logs/access.log  main;

    sendfile        on;

    tcp_nopush     on;

    tcp_nodelay         on;

                fastcgi_connect_timeout 300;

                fastcgi_send_timeout 300;

                fastcgi_read_timeout 300;

                fastcgi_buffer_size 64k;

                fastcgi_buffers 4 64k;

                fastcgi_busy_buffers_size 128k;

                fastcgi_temp_file_write_size 256k;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    gzip  on;

    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  include /opt/local/etc/nginx/conf.d/*.conf;

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

}

而後創建目錄

sudo mkdir /opt/local/etc/nginx/sites-enabled

進入cd /opt/local/etc/nginx/sites-enabled

編輯一個站點的conf文件

我建了一個default,內容以下

 

server {

    listen   80; ## listen for ipv4

    listen   [::]:80 default ipv6only=on; ## listen for ipv6

    server_name  localhost;

    access_log  /Users/apple/logs/localhost.access.log;

    location / {

        root  /Users/apple/phpdocs;

        index index.php index.html index.htm;

    }

 

    location ~ \.php$ {

        fastcgi_pass   127.0.0.1:9000;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  /Users/apple/phpdocs$fastcgi_script_name;

        include fastcgi_params;

    }

}

保存後在相應的路徑上創建log文件和html文件

 

這個時候先不要放index.php,會出現bad gatway錯誤,我們還沒裝php和fastcgi呢

 

Part 3: PHP

 

在終端上輸入

 

安裝php5,擴展模塊根據本身的須要調整

 port install php5 +fastcgi fcgi php5-gd php5-mysql php5-sqlite php5-eaccelerator php5-curl php5-iconv php5-mcrypt

安裝完成後,到/opt/local/etc/php5下,cp php.ini-recommended php.ini
而後修改幾個項:
error_reporting = E_ALL & ~E_NOTICE
display_errors = On
error_log = /Users/jonathan/logs/php5/error.log
date.timezone = Asia/Shanghai
手動建立/Users/jonathan/logs/php5/error.log日誌

         重點來了

咱們要再寫一個plist文件跑fastcgi

       vi /Library/LaunchDaemons/org.macports.phpfcgi.plist

       內容以下

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

  <key>Label</key><string>org.macports.phpfcgi</string>

  <key>Debug</key><false />

  <key>OnDemand</key><false />

  <key>RunAtLoad</key><false />

  <key>EnvironmentVariables</key>

  <dict>

    <key>PHP_FCGI_CHILDREN</key><string>2</string>

    <key>PHP_FCGI_MAX_REQUESTS</key><string>5000</string>

  </dict>

  <key>LaunchOnlyOnce</key><true />

  <key>ProgramArguments</key>

  <array>

    <string>/opt/local/bin/spawn-fcgi</string>

    <string>-C 2</string>

    <string>-p 9000</string>

    <string>-f /opt/local/bin/php-cgi</string>

  </array>

</dict>

</plist>

保存後輸入

sudo launchctl load -w /Library/LaunchDaemons/org.macports. phpfcgi.plist

這樣就自動啓動php fastcgi

如今去網站目錄放個index.php,看下phpinfo()吧

Part4: 安裝MySQL

安裝mysql很方便,去mysql網站下個dmg吧,有三個文件要安裝,一個是mysql安裝文件,一個是mysql自動啓動,一個是系統設置裏的添加項控制mysql的開關。

裝完之後下載phpmyadmin到網站目錄,

 

phpMyAdmin目錄的config.sample.inc.php命名爲config.inc.php

打開config.inc.php,作以下修改:

$cfg['blowfish_secret'] = '';//用於Cookie加密,隨意的長字符串

$cfg['Servers'][$i]['host'] = '127.0.0.1';//MySQL守護程序作了IP綁定

$cfg['Servers'][$i]['AllowNoPassword'] = false;//能夠訪問無密碼的MySQL

web訪問phpMyAdmin,並修改MySQL密碼不爲空。

Part4: 安裝memcache

裝這個原本覺得能夠用port install php5-memcache,結果port的編譯包可能有問題,不能building memcache,只有本身編譯了

先裝libevent

port install libevent

安裝完後去http://pecl.php.net/package/memcache下載最新的版本,下載完成後解壓,進入到文件夾裏,依次執行如下命令:

phpize
./configure
make
make install
 編譯完成後 memcache.so會放到 /opt/local/lib/php/extensions/debug-non-zts-xxxxxxx目錄下
寫一個php加載memcache的配置文件
vi  /opt/local/var/db/php5/ memcache.ini
內容爲: extension=memcache.so
保存退出
而後寫一個memcache的啓動腳本
vi  /etc/init.d/memcached
內容爲:

#!/bin/bash

DAEMON=/opt/local/bin/memcached

NAME=memcached

DESC=memcached

USER=nobody

HOST=0.0.0.0

PORT=11211

MEM=32

LOG=/Users/apple/logs/memcached/info.log

 

case "$1" in

  start)

        echo -n "Startring $DESC: "

        $DAEMON -m $MEM -p $PORT -l $HOST -u $USER -d -vv >> $LOG 2>&1

        echo "$NAME."

        ;;

  stop)

        echo -n "Stopping $DESC: "

        killall $NAME

        echo "$NAME."

        ;;

  restart)

        echo -n "Restarting $DESC: "

        killall $NAME

        sleep 1

        $DAEMON -m $MEM -p $PORT -l $HOST -u $USER -d -vv >> $LOG 2>&1

        echo "$NAME."

        ;;

  *)

        echo "Usage: $NAME {start|stop|restart}" >&2

        exit 1

        ;;

esac

 

exit 0

保存後記得賦予可執行權限:chmod +x /etc/init.d/memcached
      最後不要忘記建立日誌文件:/Users/apple/logs/memcached/info.log

而後讓memcached開機啓動

寫一個plist文件

vi /Library/LaunchDaemons/org.memcache.plist

內容爲

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

  <key>Label</key><string>org.macports.memcache</string>

  <key>OnDemand</key><true/>

  <key>Username</key>

    <string>nobody</string>

  <key>ProgramArguments</key>

  <array>

    <string>/etc/init.d/memcached</string>

    <string>start</string>

  </array>

  <key>Debug</key><false/>

  <key>Disabled</key><true/>

  <key>RunAtLoad</key><true/>

  <key>KeepAlive</key><true/>

</dict>

</plist>

一樣保存完後運行

sudo launchctl load -w /Library/LaunchDaemons/org.memcache.plist

相關文章
相關標籤/搜索