LNMP环境配置
安装准备
首先扩展 homebrew
brew tap homebrew/dupes
brew tap homebrew/php
安装 Redis
安装服务
brew install redis
常用服务控制命令
# 设置开机启动
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
# 启动
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
# 关闭
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
安装 MySQL
安装服务
brew install mysql
常用服务控制命令
# 设置开机启动
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
# 启动
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
# 关闭
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
# 初始化MySQL
mysql_secure_installation
安装 PHP \ phpmyadmin 及扩展包
# 查看安装参数
brew info php56
# 开始安装
brew install php56 --with-fpm --with-gmp --with-imap --with-tidy --with-debug --with-mysql --with-libmysql --without-apache
brew install php56-apcu\
php56-gearman\
php56-geoip\
php56-gmagick\
php56-imagick\
php56-intl\
php56-mcrypt\
php56-memcache\
php56-memcached\
php56-mongo\
php56-pdo-pgsql\
php56-phalcon\
php56-swoole\
php56-redis\
php56-sphinx\
php56-uuid\
php56-xdebug
# 安装 phpmyadmin
brew install phpmyadmin
请按实际需要进行选择,需要说明的是:
- gearman - 任务分发服务库,可选
- geoip - IP反查地理信息库
- gmagick & imagick - 图像处理库
- phalcon - C-extension MVC模式的全栈框架
- swoole - C写成的高性能异步网络通信框架,与nodejs所用方法类似
- xdebug - 调试工具
PHP编译过程中如果遇到 configure: error: Cannot find OpenSSL's 错误,执行 xcode-select --install 重新安装一下Xcode Command Line Tools
替换系统的PHP
# If you use Bash
echo 'export PATH="$(brew --prefix php56)/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="$(brew --prefix php56)/sbin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
# If you use ZSH
echo 'export PATH="$(brew --prefix php56)/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="$(brew --prefix php56)/sbin:$PATH"' >> ~/.zshrc
source ~/.zshrc
启用 /usr/local/etc/php/5.6/php-fpm.conf 中 pid 配置
pid = run/php-fpm.pid
常用服务控制命令
# 设置开机启动
ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
# 测试php-fpm配置
php-fpm -t
# or
php-fpm -c /usr/local/etc/php/5.6/php.ini -y /usr/local/etc/php/5.6/php-fpm.conf -t
# 启动php-fpm
php-fpm -D
# or
php-fpm -c /usr/local/etc/php/5.6/php.ini -y /usr/local/etc/php/5.6/php-fpm.conf -D
# or
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
# 关闭php-fpm
kill -INT `cat /usr/local/var/run/php-fpm.pid`
# or
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
# 重启php-fpm
kill -USR2 `cat /usr/local/var/run/php-fpm.pid`
# or
brew services restart php56
安装 Nginx
开始安装(附带 geoip 模块)
brew install nginx --with-http_geoip_module
常用服务控制命令
# 测试配置
nginx -t
# 开机启动
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
# 启动
nginx
# or
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
# 重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|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
虚拟站点配置样例 sites-available 目录下,有效站点需要链接到 sites-enabled 目录下。
server {
listen 8080;
server_name localhost;
root ~/www;
access_log log/xxx.access.log ;
error_log log/xxx.error.log ;
# SSL configure
# ssl on;
# ssl_certificate ssl/xxx.crt;
# ssl_certificate_key ssl/xxx.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
location / {
index index.php index.html;
}
location ~* \.(docx|swf|mp4|pdf|png|jpg|jpeg|gif|html|css|js|svg|ttf|woff)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location ~ \.php($|/) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# rewrite for CI
# if (!-e $request_filename) {
# rewrite ^/(.*)$ /index.php/$1 last;
# break;
# }
location ~ /\.ht {
deny all;
}
error_page 404 /404.html;
error_page 403 /403.html;
}
如需测试用SSL证书,请用如下命令生成,位于 /usr/local/etc/nginx/ssl/ 目录下,请修改文件名 xxx 为有效名称(建议站点名)。
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/xxx.key -out /usr/local/etc/nginx/ssl/xxx.crt
服务控制快捷命令
将下列快捷命令写入 ~/.bash_aliases
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'
将 ~/.bash_aliases 加入 ~/.bash_profile
echo "[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases" >> ~/.bash_profile
安装脚本
#!/bin/bash
BREW="/usr/local/bin/brew"
BREWCACHE="/Library/Caches/Homebrew"
BASEDIR="$(dirname $0)/.."
LAUNCHDIR="${HOME}/Library/LaunchAgents"
if [ ! -d "${LAUNCHDIR}" ]; then
mkdir $LAUNCHDIR
fi
if [ ! -f "$BREW" ]; then
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
cd /usr/local
git remote set-url origin git://mirrors.ustc.edu.cn/homebrew.git
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile
fi
$BREW tap homebrew/dupes
$BREW tap homebrew/php
if [ ! -d "${BREWCACHE}" ]; then
mkdir $BREWCACHE
fi
cp -f $BASEDIR/software/pkgs/* $BREWCACHE
$BREW install redis
ln -sfv /usr/local/opt/redis/*.plist $LAUNCHDIR
launchctl load "${LAUNCHDIR}/homebrew.mxcl.redis.plist"
$BREW install mysql
ln -sfv /usr/local/opt/mysql/*.plist $LAUNCHDIR
echo -e "[mysqld]\nsql_mode=NO_ENGINE_SUBSTITUTION\n" > /usr/local/etc/my.cnf
launchctl load "${LAUNCHDIR}/homebrew.mxcl.mysql.plist"
$BREW install php56 --with-fpm --with-mysql --without-apache
$BREW install php56-mcrypt php56-redis
ln -sfv /usr/local/opt/php56/*.plist $LAUNCHDIR
launchctl load "${LAUNCHDIR}/homebrew.mxcl.php56.plist"
$BREW install phpmyadmin
$BREW install nginx
ln -sfv /usr/local/opt/nginx/*.plist $LAUNCHDIR
launchctl load "${LAUNCHDIR}/homebrew.mxcl.nginx.plist"
cat << EOF >> ~/.bash_aliases
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'
EOF
echo "[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases" >> ~/.bash_profile
echo "[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases" >> ~/.zshrc
exit 0