macOS Sierra下安装配置nginx, php7与mysql
首先我们需要安装macOS上的包管理器HomeBrew。
访问brew.sh,执行首页上的那行命令即可。
完成后可以把brew源换成国内的镜像源,参考LUG@USTC
完成后brew update
php7
HomeBrew的默认源里不包含php,需要先tap
brew tap homebrew/dupes brew tap homebrew/php brew install --without-apache --with-fpm --with-mysql php70
如果你使用bash,修改.bash_profile
将如下两个目录加入$PATH环境变量。
后半段的作用是配置bash自动补全。不过推荐使用zsh,比bash不知道好用到哪里去了。
## # Homebrew ## export PATH="/usr/local/bin:$PATH" export PATH="/usr/local/sbin:$PATH" ## # Homebrew bash completion ## if [ -f $(brew --prefix)/etc/bash_completion ]; then source $(brew --prefix)/etc/bash_completion fi
启动php7
[code]brew services start php70[/code]
Mysql
brew install mysql
启动mysql
brew services start mysql
为mysql创建账户及设置密码。和ubuntu不同,macOS上的mysql用户名和密码需要在安装完成后手动设置。
mysqladmin -u root password 'yourpassword'
nginx
安装
brew install nginx
配置nginx与php7-fpm
为nginx创建类似ubuntu的目录结构
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
nginx的配置文件位于/usr/local/etc/nginx/
首先移除已经存在的nginx.conf
,并用下面的文件替换
curl -L https://gist.githubusercontent.com/dtomasi/ab76d14338db82ec24a1fc137caff75b/raw/c7c99476e6d8bd5b23e814c5593861adb9b54765/nginx.conf -o /usr/local/etc/nginx/nginx.conf
这个配置文件的access_log
及error_log
所配置的目录并不存在,需要手动创建,或者干脆直接换一个地方。
在conf.d
目录下新建php-fpm
文件,写入如下内容:
location ~ \.php$ { try_files $uri = 404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
在sites-enabled
目录下新建一个配置文件,例如default
server { listen 80; server_name localhost; root /path/to/your/site; access_log /usr/local/etc/nginx/logs/default.access.log main; location / { include /usr/local/etc/nginx/conf.d/php-fpm; try_files $uri $uri/ /index.php?$query_string; #这一行表示将404的url交由index.php处理,laravel和wordpress需要用到。 } location = /info { allow 127.0.0.1; deny all; rewrite (.*) /.info.php; } error_page 404 /404.html; error_page 403 /403.html; }
测试配置文件
sudo nginx -t
测试无误后启动nginx
sudo nginx
或重启nginx
sudo nginx -s reload
不建议使用brew services
命令,我被坑过无数次了。
2 Comments
纛下靥狗
WoW吼吼
凯の秘密基地
没有Mac 打酱油路过~