大佬教程收集整理的这篇文章主要介绍了在 macOS High Sierra 10.13 搭建 PHP 开发环境,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
2017 年 9 月 26 日,苹果公司正式发布了新一代 macOS,版本为 High Sierra (11.13)。
macOS High Sierra 预装了 Ruby(2.3.3)、php(7.1.7)、Perl(5.18.2)、Python(2.7.10) 等常用的脚本语言,以及 Apache(2.4.27) Web 服务器。
以下是我的 MNMP(macOS-Nginx-MysqL-php)的安装过程。
推荐 iTerm2,iTerm2 功能强大,可以替代系统默认的命令行终端。下载解压后,将 iTerm2 直接拖入"应用程序"目录。
推荐 jetBrains phpStorm 作为集成开发工具。
Xcode 是苹果出品的包含一系列工具及库的开发套件。通过 AppStore 安装最新版本的 Xcode(9.0)。我们一般不会用 Xcode 来开发后端项目。但这一步也是必须的,因为 Xcode 会附带安装一些如 Git 等必要的软件。
这一步会帮你安装许多常见的基于 Unix 的工具。Xcode 命令行工具作为 Xcode 的一部分,包含了 GCC 编译器。在命令行中执行以下命令即可安装:
# 安装 Xcode Command Line Tools
xcode-SELEct --install
当 Xcode 和 Xcode Command Line Tools 安装完成后,你需要启动 Xcode,并点击同意接受许可协议,然后关闭 Xcode 就可以了。这一步骤也是必须的,否则 Xcode 包含的一系列开发工具都将不可用。
Homebrew 作为 macOS 不可或缺的套件管理器,用来安装、升级以及卸载常用的软件。在命令行中执行以下命令即可安装:
# 使用系统自带的 ruby 安装 Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安装后可以修改 Homebrew 源,国外源一直不是很给力,这里我们将 Homebrew 的 git 远程仓库改为中国科学技术大学开源软件镜像:
cd "$(brew --repo)"
git remote set-url origin git://mirrorS.Ustc.edu.cn/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
echo 'export HOMEBREW_BottLE_DOMAIN=https://mirrorS.Ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
brew install wget
brew install autoconf
brew install openssl
这里我们选择 Nginx 代替系统自带的 Apache,作为我们的 Web 服务器:
brew install Nginx
安装完成后,Nginx 的一些常用命令:
sudo Nginx # 启动 Nginx 服务
Nginx -h # Nginx 帮助信息
sudo Nginx -s stop|quit|reopen|reload # 停止|退出|重启|重载 Nginx 服务
推荐 MysqL 作为数据库服务器:
brew install MysqL
当然,你也可以选择安装 POSTGResql 或者 MariaDB。
安装完成后,启动 MysqL:
@H_346_70@mysql.server start
进入 MysqL 服务器:
@H_346_70@mysqL -u root -p
推荐 redis 作为 nosql 数据库服务器:
brew install redis
安装完成后,启动 redis:
redis-server
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
brew install php72
echo export PATH="$(brew --prefix homebrew/php/php72)/bin:$PATH" >> ~/.bash_profile # 代替系统自带的 php
source ~/.bash_profile
php -v
php-fpm -v
启动 php-fpm:
sudo killall php-fpm && sudo php-fpm
@R_491_6749@可以查看 Nginx.conf 文件的位置:
Nginx -h
输出:
Nginx version: Nginx/1.12.1
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.12.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 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 文件:
vi /home/www/php_project/index.php
写入内容:
<?php
phpinfo();
重启 Nginx:
sudo Nginx -s stop && sudo Nginx
打开浏览器,访问localhost:8099
。可以看到关于 php 配置的信息。
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://packagist.phpcomposer.com # 改为国内源
不推荐用 pecl 的方式安装 php 扩展。以 php-redis 扩展为例,下载源码包来进行安装:
wget https://pecl.php.net/get/redis-3.1.3.tgz # 下载源码包
tar -zxvf redis-3.1.3.tgz # 解压
cd redis-3.1.3
phpize # 生成编译配置
./configure # 编译配置检测
make # 编译
make install # 安装
扩展安装完成后,我们还需最后一步,修改php.ini
文件,并重启 php-FPM:
# 追加 extension=redis.so
vi /usr/local/etc/php/7.2/php.ini
# 重启 php-fpm
sudo killall php-fpm && sudo php-fpm -D
# 查看是否安装成功
php -R_280_11845@ |grep redis
以上是大佬教程为你收集整理的在 macOS High Sierra 10.13 搭建 PHP 开发环境全部内容,希望文章能够帮你解决在 macOS High Sierra 10.13 搭建 PHP 开发环境所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。