1. 程式人生 > >mac下安裝lnmp環境

mac下安裝lnmp環境

0.安裝brew

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

1.安裝PHP

brew tap homebrew/homebrew-php
brew install php56 --without-snmp --without-apache --with-debug --with-fpm --with-intl --with-homebrew-curl --with-homebrew-libxslt --with-homebrew-openssl
--with-imap --with-mysql --with-tidy

如有錯誤configure: error: Cannot find libz
xcode-select –install

在~/.bashrc里加入:
export PATH=”/usr/local/bin:/usr/local/sbin:$PATH”

安裝完成後,通過braw search php 查詢相關的php擴充套件,再安裝。不需要每次都重新安裝編譯php

安裝laravel支援,及其他擴充套件

brew install mcrypt php56-mcrypt
brew install php56-mongo
php56-redis

如果擴充套件報錯可以重灌,如

brew reinstall -v php56-redis --build-from-source

啟動和停止php-fpm
php-fpm -D
killall php-fpm

2.安裝nginx

brew install nginx

安裝完畢後可以通過
nginx
nginx -s quit

調整下檔案配置
mkdir -p /usr/local/var/logs/nginx
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

配置檔案
vim /usr/local/etc/nginx/nginx.conf

worker_processes  1;
error_log   /usr/local/var/logs/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" $host $request_time $upstream_response_time $scheme '
    '$cookie_evalogin';

    access_log  /usr/local/var/logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/sites-enabled/*;
}

vim /usr/local/etc/nginx/conf.d/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;
}

vim /usr/local/etc/nginx/sites-enabled/default

server {
    listen       8080;
    server_name  localhost;
    root     /Users/guoyuanjian/lnmp/test;

    location / {
    index  index.html index.htm index.php;
    include     /usr/local/etc/nginx/conf.d/php-fpm;
    }
}

3.安裝MySQL

brew install mysql

啟動
mysql.server start

設定密碼
mysqladmin -uroot password ‘root’

4.Redis

brew install redis
Redis預設配置檔案不允許以Deamon方式執行,因此需要先修改配置檔案

vim /usr/local/etc/redis.conf
將daemonize修改為yes,然後載入配置檔案即可實現後臺程序啟動

redis-server /usr/local/etc/redis.conf

5.安裝mongodb

brew install mongodb
啟動
mkdir -p /data/db
mongod
預設路徑是/data/db,也可以指定路徑
mongod –dbpath

通過配置檔案啟動
cat /data/db/mongo.conf

dbpath = /data/db
logpath = /data/db/mongo.log
fork = true
port = 27017
oplogSize = 4096

mongod -f /data/db/mongo.conf

6.設定別名

對所有服務的啟動停止設定別名方便操作(這一步可不設定)
vim ~/.bash_profile
加入

alias nginx.start='launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.stop='launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.restart='nginx.stop && nginx.start'
alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'
alias mysql.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.restart='mysql.stop && mysql.start'
alias redis.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.restart='redis.stop && redis.start'
alias redis.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongo.plist"
alias redis.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mongo.plist"
alias redis.restart='redis.stop && redis.start'

7.其他

如要重新安裝擴充套件
brew reinstall php56-imagick –build-from-source