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

7,473 阅读2分钟

一、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!

五、安装完成。。。。