搜索
首页后端开发php教程苹果系统安装php环境的方法详解

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

Sep 27, 2020 pm 05:40 PM
php环境苹果系统

苹果系统安装php环境的方法:首先安装iTerm2以及PhpStorm;然后安装Xcode;接着通过命令“brew install php”安装PHP7.4;最后安装mysql并启动服务即可。

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

推荐:《PHP视频教程

2019 年 10 月 8 日,苹果公司正式发布了新一代 macOS,版本为 Catalina (11.15)。

macOS Catalina 预装了 Ruby(2.6.3)、PHP(7.3.9)、Perl(5.18.4)、Python(2.7.16) 等常用的脚本语言,以及 Apache(2.4.41) Web 服务器。

需要注意的是,在新版本中,zsh 已取代 bash 成为新版操作系统中的默认 shell。

以下是我的 MNMP(macOS-nginx-MySQL-PHP)的安装过程。

本教程用使用了三处代替:

  • 使用 iTerm2 代替了系统自带的命令行终端
  • 使用 nginx 代替了系统自带的 Apache
  • 使用 自行安装的 PHP7.4 代替了系统自带的 PHP7.3.9

安装 iTerm2

推荐 iTerm2,iTerm2 功能强大,可以替代系统默认的命令行终端。下载解压后,将 iTerm2 直接拖入"应用程序"目录。

安装 PhpStorm

推荐 JetBrains PhpStorm 作为集成开发工具。

安装 Xcode

Xcode 是苹果出品的包含一系列工具及库的开发套件。通过 AppStore 安装最新版本的 Xcode(9.0)。我们一般不会用 Xcode 来开发后端项目。但这一步也是必须的,因为 Xcode 会附带安装一些如 Git 等必要的软件。

安装 Command Line Tools for Xcode

这一步会帮你安装许多常见的基于 Unix 的工具。Xcode 命令行工具作为 Xcode 的一部分,包含了 GCC 编译器。在命令行中执行以下命令即可安装:

xcode-select --install # 安装 Xcode Command Line Tools

当 Xcode 和 Xcode Command Line Tools 安装完成后,你需要启动 Xcode,并点击同意接受许可协议,然后关闭 Xcode 就可以了。这一步骤也是必须的,否则 Xcode 包含的一系列开发工具都将不可用。

安装 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.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

写入内容:

<?php
    phpinfo();

重启 nginx:

brew services restart nginx

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

安装 Composer

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

以上是苹果系统安装php环境的方法详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
高流量网站的PHP性能调整高流量网站的PHP性能调整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具