苹果系统安装php环境的方法详解

PHP技术
455
0
0
2022-04-10
标签   PHP环境

安装 Homebrew

Homebrew 作为 macOS 不可或缺的套件管理器,用来安装、升级以及卸载常用的软件。在命令行中执行以下命令即可安装:

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

使用系统自带的 ruby 安装 Homebrew

安装后可以修改 Homebrew 源,国外源一直不是很给力,这里我们将 Homebrew 的 git 远程仓库改为中国科学技术大学开源软件镜像:

cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

替换brew.git:

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

替换homebrew-core.git:

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc

替换Homebrew Bottles源:

source ~/.zshrc

安装 PHP 7.4

安装 PHP7.4.* 来代替系统自带的 PHP7.3:

brew install php

启动 php 服务:

brew services start php

替换系统自带的 php-fpm:

echo 'export PATH="/usr/local/opt/php/sbin:$PATH"' >> ~/.zshrc
source ~/.zshrc

查看版本信息:

php -v
php-fpm -v

安装 MySQL

推荐 MySQL 8.0 作为数据库服务器:

brew install mysql

当然,你也可以选择安装 PostgreSQL 或者 MariaDB。

安装完成后,启动 MySQL:

brew services start mysql

进入 MySQL 服务器:

mysql -u root -p

设置 root 密码、安全等级等参数:

mysql_secure_installation

按照步骤提示一步一步来即可。

安装 Redis

安装 redis 服务器:

brew install redis

安装完成后,启动 Redis:

brew services start redis

使用 redis 客户端:

redis-cli

安装 nginx

这里我们选择 nginx 代替系统自带的 Apache,作为我们的 Web 服务器:

brew install nginx

启动 nginx 服务:

brew services start nginx

查看已安装的 brew services:

brew services list

查看nginx信息:

brew info nginx

配置 nginx.conf 文件

通过以下命令可以查看 nginx.conf 文件的位置:

nginx -h

输出:

nginx version: nginx/1.17.3
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/Cellar/nginx/1.17.3_1/)
-c filename : set configuration file (default: /usr/local/etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file

打开配置文件:

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

在文件末尾可以看到:

include servers/*;

它将同目录下的servers目录里的文件都包含了进来,由此,我们可以在servers文件里创建开发项目的配置信息:

cd /usr/local/etc/nginx/servers/
vi test.conf

将以下配置信息,写入 test.conf文件中:

server {
listen 8099;
server_name localhost;
root /home/www/php-project;
rewrite . /index.php;
location / {
index index.php index.html index.htm;
autoindex on;
}
#proxy the php scripts to php-fpm
location ~ \.php$ {
include /usr/local/etc/nginx/fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
}
}

在上述的/home/www/php-project的目录下,我们创建一个 index.php 文件:

vim /home/www/php-project/index.php

写入内容:

phpinfo();

重启 nginx:

brew services restart nginx

打开浏览器,访问http://localhost:8099,即可访问到关于 PHP 配置的信息。

解决macos下配置nginx+php-fpm出现 file not found的问题

原配置文件/usr/local/etc/nginx/nignx.config

server {
listen 8081;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 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;
}

————————————————

修改为:

server {
listen 8081;
server_name localhost;
root /Users/fjh1997/upload; #这里要注意#charset koi8-r;
#access_log logs/host.access.log main;
location / {
index index.php;
}
#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;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$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;#}
}

需要注意的是,务必删去 location ~ .php$ {里面的root项,同时修改/scripts f a s t c g i s c r i p t n a m e ; 为 fastcgi_script_name;为 fastcgis​criptn​ame;为document_root$fastcgi_script_name;,并在location外层添加root项。如本例为/Users/fjh1997/upload;

默认项目位置:/usr/local/var/www

Composer 是 PHP 用来管理依赖(dependency)关系的工具。你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的库文件。

安装并替换镜像:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

改为阿里云的国内源

安装 PHP 扩展

以 php-redis 扩展为例,有下载源码包来进行安装或者 pecl install 安装:

wget https://pecl.php.net/get/redis-5.1.0.tgz # 下载源码包
tar -zxvf redis-5.1.0.tgz # 解压
cd redis-5.1.0 # 进入目录
phpize # 生成编译配置
./configure # 编译配置检测
make # 编译
make install # 安装

扩展安装完成后,我们还需最后一步,修改php.ini文件,并重启 PHP 服务:

vi /usr/local/etc/php/7.4/php.ini # 追加 extension=redis.so
brew services restart php

重启 php 服务

php -m |grep redis 

查看是否安装成功

或者使用 pecl 安装:

pecl install redis