Mac安装php开发环境

作为一名前端,了解后端开发和掌握至少一门后端语言是非常必须的。
之前一直用的是 nodejs , 这段时间想着再上手PHP。
便以此贴作为入门的开端吧。

安装Homebrew

这个就不需要介绍了, 有安装的就跳过,没安装的或还不了解的,相信官网的介绍会比我几句话的描述还详细
官网 Homebrew

安装nginx

在装了brew 后再来安装软件就很方便了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

$ brew search nginx
==> Formulae
nginx ✔

$ brew install nginx

// 卸载
$ brew uninstall nginx

// 检查下安装是否完成
$ nginx -v
nginx version: nginx/1.17.1

// 启动nginx
$ nginx

// 关闭/退出/重启/重载配置nginx
$ nginx -s [stop, quit, reopen, reload]

//

nginx 启动后试着访问 localhost:8080

welcome-to-nginx.png

访问成功✌️

安装 php

我安装PHP时候也是直接 brew install

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

$ brew instal php


// 安装成功

Finally, check DirectoryIndex includes index.php
DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
/usr/local/etc/php/7.3/

To have launchd start php now and restart at login:
brew services start php
Or, if you don't want/need a background service you can just run:
php-fpm

启动php $ sudo php-fpm

报了个错, 提示没有找到相关的配置文件

1
2
3
4
5
$ php-fpm
[12-Jul-2019 17:07:07] WARNING: Nothing matches the include pattern '/private/etc/php-fpm.d/*.conf' from /private/etc/php-fpm.conf at line 125.
[12-Jul-2019 17:07:07] ERROR: failed to open error_log (/usr/local/var/log/php-fpm.log): Permission denied (13)
[12-Jul-2019 17:07:07] ERROR: failed to post process the configuration
[12-Jul-2019 17:07:07] ERROR: FPM initialization failed

前往 /private/etc/php-fpm.d 目录下后发现有一个 www.conf.default 文件,于是把www.conf.default 复制一份改个名

1
$ sudo cp www.conf.default www.conf

重新启动就可以了。

nginx 配置

nginx 的配置文件在 /usr/local/etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

#user nobody;
worker_processes 1;
# nginx 的错误日志文件路径, 默认是 /usr/local/var/log/nginx/ 下
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

# gzip 压缩开关
#gzip on;

# nginx 最主要的就是这个虚拟主机配置了
server {
listen 8080;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}


# 开启这一段配置 便可以通过 FastCGI 来解析php 代码了
# 至于 FastCGI 是什么,我暂时还不是很了解。后面再了解下。
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#

# 这下面的是 server 配置的例子
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

# 导入在servers 文件夹下的 配置文件, 我们要新建网站也是在这个文件夹内添加
include servers/*;
}

去 servers 文件夹下新建一个 虚拟主机配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ cd servers/
$ touch local.test.com.conf
$ vi local.test.com


server{
listen 8022;
server_name local.test.com
root /Users/yeungx/www/Project;

location / {
index index.php index.html index.htm;
}

location ~ \.php$ {
root /Users/yeungx/www/Project;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

然后在用户目录下新建 www/Project 项目文件夹, 新建 index.php

1
2
<?php
phpinfo();

访问 local.test.com:8033, 能看到 PHP 的详细配置信息 。
到这里完成了nginx 和 php 的安装和配置了 。

凌晨1点半了。晚安!