Mac搭建PHP开发环境(PHP+Nginx+MySQL)

一、Homebrew安装

homebrew是mac下非常好用的包管理器,会自动安装相关的依赖包,将你从繁琐的软件依赖安装中解放出来。

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

homebrew的常用命令:

brew update #更新可安装包的最新信息,建议每次安装前都运行下
brew search pkg_name #搜索相关的包信息
brew install pkg_name #安装包
复制代码

二、PHP安装

Mac OSX 10.9以后的系统自带了PHP、php-fpm,省去了安装php-fpm的麻烦。

sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
vim /private/etc/php-fpm.conf
复制代码

修改php-fpm.conf文件中的error_log项,默认该项被注释掉,这里需要去注释并且修改为error_log = /usr/local/var/log/php-fpm.log。如果不修改该值,运行php-fpm的时候会提示log文件输出路径不存在的错误。

修改之后在终端输入:
sudo php-fpm
# 干掉php进程()/pkill强制删除php
sudo pkill php-fpm
sudo php-fpm 
复制代码

三、Nginx安装

brew search nginx
brew install nginx
复制代码

nginx相关命令

sudo nginx #打开 nginx
nginx -s reload|reopen|stop|quit  #重新加载配置|重启|停止|退出 nginx
nginx -t   #测试配置是否有语法错误
复制代码

nginx配置

cd /usr/local/etc/nginx/
mkdir conf.d
vim nginx.conf
vim ./conf.d/default.conf
复制代码

nginx.conf

worker_processes 1; 
 
error_log    /usr/local/var/log/nginx/error.log;
 
pid    /usr/local/var/run/nginx.pid;
 
events {
  worker_connections 1024;
}
 
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"';
 
  access_log   /usr/local/var/log/nginx/access.log main;
  port_in_redirect off;
  sendfile    on; 
  keepalive_timeout 65; 
 
  include /usr/local/etc/nginx/conf.d/*.conf;
}
复制代码

修改nginx配置文件,再重启时可能报出如下错误:

nginx: [error] invalid PID number “” in “/usr/local/var/run/nginx/nginx.pid”
复制代码

解决办法:

sudo nginx -c /usr/local/etc/nginx/nginx.conf
sudo nginx -s reload
复制代码

default.conf

server {
    listen       80;
    server_name  www.myweb.com;
    root         项目地址;

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

    location / {
        index  index.html index.htm index.php;
        autoindex   on;
    try_files $uri /app_dev.php$is_args$args;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }

    location = /info {
        allow   127.0.0.1;
        deny    all;
        rewrite (.*) /.info.php;
    }
    error_page  404     /404.html;
    error_page  403     /403.html;
}

复制代码

php-fpm

#proxy the php scripts to 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;
}
复制代码

四、MySQL安装

brew search mysql
brew install mysql@5.7
复制代码

其他命令

# 获取 service 列表
brew services list
# 重启 mysql 服务
brew services restart mysql
# 停止 mysql 服务
brew services stop mysql
复制代码

配置到环境中

echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile 
复制代码
PS:如果出现下面这个错误:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
复制代码

就执行下面命令

mysql.server start
复制代码

设置MySQL开机自启动

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
复制代码

开启MySQL安全机制

mysql_secure_installation
复制代码
jianghehedeMacBook-Pro:~ comet
$ mysql_secure_installation
 
Securing the MySQL server deployment.
 
Connecting to MySQL using a blank password.
 
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
 
Press y|Y for Yes, any other key for No: N   // 这个选yes的话密码长度就必须要设置为8位以上,但我只想要6位的
Please set the password for root here.
 
New password:            // 设置密码
 
Re-enter new password:     // 再一次确认密码
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
 
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y    // 移除不用密码的那个账户
Success.
 
 
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
 
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
 
 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
 
 
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.
 
 - Removing privileges on test database...
Success.
 
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
 
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
 
All done!
复制代码

五、安装完成。。。。

六、打赏

觉得可以请小弟喝瓶矿泉水吧。嘻嘻嘻

           

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值