这两天在Mac上安装LNMP开发环境,现在整理一下安装的大概步骤及需要注意的点
1. 安装Homebrew
Homebrew是一款Mac系统下的软件包管理工具,brew命令类似ubuntu上的apt-get,能十分方便的在Mac上安装或卸载软件
一句话安装命令
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
参考资料:Homebrew官网
2. 安装php
Mac系统上默认安装了php,但是版本为5.5,版本稍微有点低并且有些扩展是没有安装。目前PHP7已经面世,据官方的说法是现在PHP7要比PHP5.6快上一倍,所以我选择重新安装php,并把Mac系统的默认php版本指定为PHP7
// 更新brew
$ brew update
// 添加三方仓库
$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php
// 如果没有该目录,则创建
$ sudo mkdir /usr/local/var
$ sudo chmod 777 /usr/local/var
$ sudo mkdir /usr/local/sbin/
$ sudo chown -R <username>:<group> /usr/local/sbin/
// 安装php7前,先关闭老版本的PHP55,并解除/usr/local/bin/php软链接
$ brew unlink php55
// 我们可以通过brew options命令来查看安装选项
$ brew options php70
// 开始安装PHP7
$ brew install php70 (--with-apache 假如需要生成apache的libphp7.so,则添加该安装选项)
// 查看php版本
$ php -v
PHP 7.0.13 (cli) (built: Dec 5 2016 14:49:24) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
3. 配置php及php-fpm
可执行文件及配置文件路径
php, phpize, php-config /usr/local/opt/php70/bin
php-fpm /usr/local/opt/php70/sbin/php-fpm
php.ini /usr/local/etc/php/7.0/php.ini
php-fpm.conf /usr/local/etc/php/7.0/php-fpm.conf
修改php-fpm配置
$ sudo vim /usr/local/etc/php/7.0/php-fpm.conf
// 为了防止访问无权限,所以修改user及group
user = wanghuiyu
group = staff
// 为了区别不同版本的php-fpm,修改php70-fpm的端口
listen = 127.0.0.1:9001
添加php-fpm为开机启动项
$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/opt/php70/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
查看php-fpm是否启动成功
$ ps aux|grep php-fpm
wanghuiyu 973 0.0 0.1 2484880 4636 ?? S 10:58上午 0:00.12 /usr/local/opt/php70/sbin/php-fpm --fpm-config /usr/local/etc/php/7.0/php-fpm.conf
wanghuiyu 436 0.0 0.1 2486928 6288 ?? S 10:08上午 0:00.23 /usr/local/opt/php70/sbin/php-fpm --fpm-config /usr/local/etc/php/7.0/php-fpm.conf
wanghuiyu 435 0.0 0.0 2486928 2628 ?? S 10:08上午 0:00.34 /usr/local/opt/php70/sbin/php-fpm --fpm-config /usr/local/etc/php/7.0/php-fpm.conf
wanghuiyu 434 0.0 0.0 2479792 308 ?? Ss 10:08上午 0:00.45 /usr/local/opt/php70/sbin/php-fpm --fpm-config /usr/local/etc/php/7.0/php-fpm.conf
wanghuiyu 3483 0.0 0.0 2424612 436 s000 R+ 2:46下午 0:00.00 grep php-fpm
4. 安装Nginx
$ brew install nginx
添加nginx为开机启动项
$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/nginx/1.10.2_1/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
设置权限
// 为了监听1024以下端口,需修改nginx所属者
$ sudo chown root:wheel /usr/local/Cellar/nginx/1.10.2_1/bin/nginx
$ sudo chmod u+s /usr/local/Cellar/nginx/1.10.2_1/bin/nginx
nginx的操作命令
// 启动nginx
sudo nginx
// 重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit
// 测试配置是否有语法错误
nginx -t
5. 配置nginx
$ sudo vim /usr/local/etc/nginx/nginx.conf
配置nginx虚拟主机
server {
listen 80;
server_name localhost;
# 设定网站根目录
root /Users/wanghuiyu/PhpstormProjects/stock/public;
# 网站默认首页
index index.php index.html index.htm;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# 修改为 Laravel 转发规则,否则PHP无法获取$_GET信息,提示404错误
try_files $uri $uri/ /index.php?$query_string;
}
#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;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /Users/wanghuiyu/PhpstormProjects/stock/public$fastcgi_script_name;
include /usr/local/etc/nginx/fastcgi_params;
}
}
重启nginx
$ nginx -s quit
$ sudo nginx
6. 安装mysql
MySQL官网下载dmg安装包,手动安装
至此,我们的LNMP环境就安装完成了。