Mac OS X Mavericks or Yosemite 安裝Nginx、PHP、Mysql、phpMyAdmin

翻譯:http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/
最近Ubuntu用着很不爽,首先是輸入法很難用,所說搜狗發佈了Ubuntu14.04的輸入法,但是遠遠沒有Win下的輸入法好用。其次是沒有qq,在公司喝同事交流很困難,雖說網頁qq也可以聊天,但是傳個文件就不行了。缺少很多應用,用Web版的用很難用。總之Ubuntu就是不爽。於是把家裏塵封的Mac Mini搬到公司爽爽的寫程序。

首先我把Mac升級到Mac10.10.1 OS X Yosemite(在App Store裏可以免費升級)。然後Xcode也要升級到最新版Version6.1,最後安裝(或更新) Xcode Command Line Tools.

安裝Xcode Command Line Tools

打開終端,輸入以下命令,回車,會彈出一個框,點擊安裝(或Install)繼續。

xcode-select --install

安裝完成後,打開Xcode,進入Preferences->Locations,查看Xcode Command Line Tools是否是最新版。
我的是這樣的
Bildschirmfoto-2014-10-20-um-11-27-56

確認你用的是Xcode 6.1!然後安裝homebrew

Homebrew

Mac下的Homebrew相當於Linux下的apt-get、yum,可以獲得最新版的各種安裝包。

首先,你要Xquartz

curl http://xquartz-dl.macosforge.org/SL/XQuartz-2.7.7.dmg -o /tmp/XQuartz.dmg
open /tmp/XQuartz.dmg

然後用以下命令安裝homebrew

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

安裝完成後,運行以下命令檢查是否安裝成功

brew doctor

然後更新、升級下brew源

brew update && brew upgrade

PHP-FPM

因爲brew默認不包含php-fpm,所以要先添加一個

brew tap homebrew/dupes
brew tap homebrew/php

然後運行以下命令安裝php、php-fpm,可能會花較長時間。

brew install --without-apache --with-fpm --with-mysql php55

設置PHP CLI

如果你想在命令行下運行php,你需要更改下bash shell下的環境變量
# If you use Bash

echo 'export PATH="$(brew --prefix homebrew/php/php55)/sbin:$PATH"' >> ~/.bash_profile && . ~/.bash_profile

# If you use ZSH

echo 'export PATH="$(brew --prefix homebrew/php/php55)/sbin:$PATH"' >> ~/.zshrc && . ~/.zshrc

讓php自動開啓

mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php55/homebrew.mxcl.php55.plist ~/Library/LaunchAgents/

運行php-fpm

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

確認php-fpm監聽9000端口

lsof -Pni4 | grep LISTEN | grep php

輸出如下
php-fpm 69659 frdmn 6u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 69660 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 69661 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 69662 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)

Mysql

運行以下命令安裝Mysql

brew install mysql

設置自動重啓

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

開啓數據庫服務

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

安全設置

運行以下命令刪除匿名用戶,並且禁止root遠程登錄。

mysql_secure_installation

> Enter current password for root (enter for none):

如果沒有設置root密碼,直接回車。

> Change the root password? [Y/n]

回車,輸入你的root密碼。

> Remove anonymous users? [Y/n]

直接回車。

> Disallow root login remotely? [Y/n]

直接回車。

> Remove test database and access to it? [Y/n]

直接回車。

> Reload privilege tables now? [Y/n]

直接回車,刷新權限。

測試連接數據庫

mysql -u root -p

輸入root密碼:

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

退出

/q
Bye

phpMyAdmin

首先需要安裝autoconf

brew install autoconf

設置$PHP_AUTOCONF

# If you use Bash
echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.bash_profile && . ~/.bash_profile

# If you use ZSH
echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.zshrc && . ~/.zshrc

安裝phpMyAdmin

brew install phpmyadmin

Nginx

安裝Nginx

brew install nginx

設置自啓

sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

測試Web服務器

啓動nginx

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

現在默認監聽8080端口,運行以下命令測試

curl -IL http://127.0.0.1:8080

輸出:

HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 19 Oct 2014 19:07:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 19 Oct 2014 19:01:32 GMT
Connection: keep-alive
ETag: “5444dea7-264”
Accept-Ranges: bytes

停止Nginx服務

sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

配置

創建nginx文件夾及配置文件

mkdir -p /usr/local/etc/nginx/logs
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl
sudo mkdir -p /var/www

sudo chown :staff /var/www
sudo chmod 775 /var/www

Load PHP-Fpm

curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm

Create default virtual hosts

curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_phpmyadmin -o /usr/local/etc/nginx/sites-available/phpmyadmin

設置 SSL

創建文件夾存放ssl證書和ssl key

mkdir -p /usr/local/etc/nginx/ssl

生成4096bit key和證書

openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=localhost" -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt

Enable virtual hosts

ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default
ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl
ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin

開啓Nginx

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

Test

http://localhost → "Nginx works" page
http://localhost/info → phpinfo()
http://localhost/nope → " Not Found" page
https://localhost:443 → "Nginx works" page (SSL)
https://localhost:443/info → phpinfo() (SSL)
https://localhost:443/nope → "Not Found" page (SSL)
https://localhost:306 → phpMyAdmin (SSL)

配置命令文件

curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases
cat /tmp/.bash_aliases >> ~/.bash_aliases
# If you use Bash
echo "source ~/.bash_aliases" >> ~/.bash_profile
# If you use ZSH
echo "source ~/.bash_aliases" >> ~/.zshrc

Commands

Nginx

nginx.start
nginx.stop
nginx.restart

快速tail日誌文件

nginx.logs.access
nginx.logs.default.access
nginx.logs.phpmyadmin.access
nginx.logs.default-ssl.access
nginx.logs.error
nginx.logs.phpmyadmin.error

查看Nginx配置

sudo nginx -t

PHP-FPM

php-fpm.start
php-fpm.stop
php-fpm.restart

查看PHP—FPM配置文件

php-fpm -t

Mysql

mysql.start
mysql.stop
mysql.restart

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章