清风徐来
Michael's Blog
macOS安装Ngnix php MySQL
Mac OS下安装php的执行环境,不依赖第三方包

php.png

Nginx

虽然Apache本身包含在macOS中,但我们建议安装Nginx,特别是轻量级且易于配置。

Installation

安装和启动Nginx,我们使用

brew install nginx
sudo brew services start nginx

虽然我们没有在brew安装中使用sudo,但如果我们想使用默认端口80,则必须使用它来启动Nginx

Configuration

编辑配置文件:

vi /usr/local/etc/nginx/nginx.conf

首先,我们必须向Nginx授予访问我们文件的权限,并避免令人讨厌的403 Forbidden错误。为此,我们更改第一行,其中<user>是您的用户名:

user <user> staff;

然后,我们将在http指令中添加一个新的部分:

server {
  listen 80;
  server_name localhost;
  root /Users/<user>/Documents/path/to/your/website;
  index index.html index.htm;
}

然后我们重新启动Nginx

sudo brew services restart nginx

PHP

为了将PHP与Nginx一起使用,我们将使用PHP-FPM。在这里,我们将使用PHP 7.2,但您可以轻松选择任何其他版本:

brew install php72

接着编辑配置文件:

vi /usr/local/etc/nginx/nginx.conf

修改以index开头的行:

index index.php index.html;

最后,在部分服务器中添加以下行,以运行PHP:

location ~ \.php {
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
}

为了解决File not found.我们还需要为PHP提供正确的权限。在以下文件中:

vi /usr/local/etc/php/7.2/php-fpm.d/www.conf

修改以下参数:

user = <user>
group = staff

最后,我们重新启动Nginx以激活更改,不要忘记启动PHP,以避免502 Bad Gateway:

sudo brew services restart nginx
sudo brew services start php72

MySQL

安装并启动:

brew install mariadb
brew services start mariadb

还要修改MySQL的密码:

mysql_secure_installation

update: 最新版本,安装后启动,使用本机账号登陆后设置密码

$ mariadb -u michael
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 10.4.13-MariaDB Homebrew

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD("root");
Query OK, 0 rows affected (0.004 sec)

MariaDB [mysql]>

自己完成完美的MAMP安装:)


最后修改于 2020-06-19