注意:不要去删除系统自带的Apache 和php 我开始就删除了系统自带的Apache,导致后面php一直无法安装成功,重装了Apache后才弄好,所以建议不要轻易删除系统自带的Apache和php,高手另说。
安装 homebrew
homebrew是mac系统下特别好用的一个软件包工具,而且它的安装也是极为简单。
网上有很多人直接给出了homebrew的安装命令,但是大部分的地址已经失效。在homebrew网站上 brew.sh/ ,正中间就是homebrew的安装命令,直接复制到你的终端里执行就可以了。
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
homebrew安装完成了,我们再用brew来安装Nginx,有了brew后,安装就变得很简单了,不需要自己去make之类的。
安装 MySql
brew install mysql
也是一句命令搞定,等执行完后,mysql也安装完毕,接下来就是对mysql的一些配置
1. 先cd到mysql的目录中
cd /usr/local/opt/mysql/
2. 加入launchctl启动控制
mkdir -p ~/Library/LaunchAgents/
cp /usr/local/opt/mysql/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
#取消启动的命令
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plis
3. 执行安全设置脚本,设置root账号密码,如果不执行这一步,是无法用mysql -u root -p这个命令登录mysql的
./bin/mysql_secure_installation
执行上面的命令后,会进入mysql的配置,可以用 mysql -u root -p 来登录mysql
*注意* 在navicat premium 12 & Mysql 8.0.11下连接报错
使用Navicat 12远程连接Mysql 8.0.11时会提示如下报错信息,从错误信息可知caching_sha2_password不能加载。
2059 - Authentication plugin 'caching_sha2_password' cannot be loaded
以上报错是由于目前已有的客户端连接软件还不支持Mysql8新增加的加密方式caching_sha2_password,所以我们需要修改用户的加密方式,将其改为老的加密验证方式。
- 1) 在安装Mysql数据库的主机上登录Mysql对应的用户,上面连接时用的用户为root,所以我们登录root用户。
- 2)执行命令,查看当前用户的加密方式
use mysql;
select user,plugin from user where user='root';
# 可以看到当前用户的加密方式为caching_sha2_password
- 3)执行命令,将用户的加密方式改为mysql_native_password
alter user 'root'@'localhost' identified with mysql_native_password by 'password';
- 4)执行命令
flush privileges
使权限配置项立即生效。
安装nginx
brew install nginx
配置nginx
1. 给nginx 设置管理员权限:如果不设置管理员权限,80端口是不能监听的
#这里的目录根据你实际安装的目录来填写,默认是这个目录,不同的只是nginx的版本号而已
sudo chown root:wheel /usr/local/Cellar/nginx/1.10.1/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.10.1/bin/nginx
2. 加入launchctl启动控制
mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/nginx/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
3. 到这里,nginx基本上是完工了。运行nginx :
sudo nginx #打开 nginx
nginx -s reload|reopen|stop|quit #重新加载配置|重启|停止|退出 nginx
nginx -t #测试配置是否有语法错误
OK, nginx就安装好了,可以在浏览器访问了,默认端口为8080, 在浏览器输入 http://localhost:8080/ 就能看到nginx在本计算机搭建的服务器
8080是nginx自带的默认网站设置的端口, 现在我们自己来创建一个网站,设置端口和映射路径
自定义网站端口
1. 执行vim /usr/local/etc/nginx/nginx.conf 修改nginx 的配置文件,在最后一行加入:
include conf.d/*.conf;
2. 新建相关文件:
# 新建文件夹
mkdir /usr/local/etc/nginx/conf.d
# 新建文件
touch /usr/local/etc/nginx/conf.d/test.conf
vim /usr/local/etc/nginx/conf.d/test.conf
3. .conf文件示例
server {
listen 80;
server_name www.test.com;
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
root "/Users/caishiyin/Sites/huiyu/test";
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root "/Users/caishiyin/Sites/huiyu/test";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index indx.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
4. 绑定自定义域名sudo vim /etc/hosts
,在文件最后加入一行127.0.0.1 nginx中配置的域名
安装php
1.用homebrew安装php
php 的安装相对nginx和mysql来说,要复杂点,因为brew 默认没有php的包
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
# 这里执行brew tap homebrew/dupes会报错,可执行:
git -C "$(brew --repo homebrew/core)" fetch --unshallow
执行完上面命令后,这个时候才可以安装php,不过php有很多的版本,你可以用:brew search php
来查看具体的版本。这里安装7.1版本
brew install php71 --with-imap --with-tidy --with-debug --with-mysql --with-fpm
2. php的配置
安装成功后,就是对php的配置了,因为mac默认是自带php的,所以我们要把我们安装的php加到环境变量里,而不是继续使用mac自带的php
sudo vim ~/.bash_profile
# 在这个文件最后添加下列语句:
export PATH="$(brew --prefix php56)/bin:$PATH"
# 保存文件后,source下这个文件,使刚刚添加的环境变量生效
source ~/.bash_profile
这个时候,你在命令行里执行 php -v 看到的不再是系统自带的php了,而是我们刚刚安装的php
3. 加入launchctl启动控制
bashmkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php@7.1/homebrew.mxcl.php@7.1.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php@7.1.plist
以上,php安装完毕
4. php配置文件的存放路径
/usr/local/etc/php/7.1/php.ini
/usr/local/etc/php/7.1/php-fpm.conf
# 修改php-fpm端口:
vim /usr/local/etc/php/7.1/php-fpm.d/www.conf
安装composer
1. 全局安装 运行命令行
curl -sS https://getcomposer.org/installer | php
2. 移动安装包到/usr/local/bin
mv composer.phar /usr/local/bin/composer
over.