Home  >  Article  >  Backend Development  >  Detailed explanation of how to install PHP environment on Apple system

Detailed explanation of how to install PHP environment on Apple system

藏色散人
藏色散人Original
2020-09-27 17:40:245202browse

How to install the PHP environment on the Apple system: first install iTerm2 and PhpStorm; then install Xcode; then install PHP7.4 through the command "brew install php"; finally install mysql and start the service.

Detailed explanation of how to install PHP environment on Apple system

                                                       

Recommended: "PHP Video Tutorial"

On October 8, 2019, Apple officially released a new generation of macOS, The version is Catalina (11.15).

macOS Catalina comes pre-installed with common scripting languages ​​such as Ruby (2.6.3), PHP (7.3.9), Perl (5.18.4), Python (2.7.16), and Apache (2.4.41 ) web server.

It should be noted that in the new version, zsh has replaced bash as the default shell in the new operating system.

The following is the installation process of my MNMP (macOS-nginx-MySQL-PHP).

This tutorial uses three substitutions:

  • Use iTerm2 instead of the system’s own command line terminal
  • Use nginx instead of the system’s own The Apache
  • that comes with it uses the self-installed PHP7.4 instead of the PHP7.3.9 that comes with the system

InstallationiTerm2

Recommend iTerm2. iTerm2 is powerful and can replace the system's default command line terminal. After downloading and unzipping, drag iTerm2 directly into the "Applications" directory.

Install PhpStorm

Recommended JetBrains PhpStorm as an integrated development tool.

Installing Xcode

Xcode is a development kit produced by Apple that contains a series of tools and libraries. Install the latest version of Xcode (9.0) via the AppStore. We generally don't use Xcode to develop back-end projects. But this step is also must, because Xcode will install some necessary software such as Git.

Install Command Line Tools for Xcode

This step will help you install many common Unix-based tools. The Xcode command line tools, as part of Xcode, include the GCC compiler. Execute the following command in the command line to install:

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

When Xcode and Xcode Command Line Tools are installed, you need to start Xcode, click to agree to accept the license agreement, and then close Xcode. This step is also must, otherwise a series of development tools included in Xcode will not be available.

Install Homebrew

Homebrew As an indispensable package manager for macOS, it is used to install, upgrade and uninstall commonly used software. Execute the following command on the command line to install:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" # 使用系统自带的 ruby 安装 Homebrew

After installation, you can modify the Homebrew source. Foreign sources have not been very powerful. Here we will change Homebrew's git remote warehouse to University of Science and Technology of China Open Source Software image :

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

Install PHP 7.4

Install PHP7.4.* to replace the PHP7.3 that comes with the system:

brew install php

Start the php service:

brew services start php

Replace the php-fpm that comes with the system:

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

View version information:

php -v
php-fpm -v

Install MySQL

Recommend MySQL 8.0 as the database server:

brew install mysql

Of course, you can also choose to install PostgreSQL or MariaDB.

After the installation is completed, start MySQL:

brew services start mysql

Enter the MySQL server:

mysql -u root -p

Set the root password, security level and other parameters:

mysql_secure_installation

Follow the step-by-step prompts Just do it step by step.

Install Redis

Install redis server:

brew install redis

After the installation is complete, start Redis:

brew services start redis

Use redis client:

redis-cli

Install nginx

Here we choose nginx instead of the Apache that comes with the system as our Web server:

brew install nginx

Start the nginx service:

brew services start nginx

View the installed brew services :

brew services list

Configure the nginx.conf file

You can view the location of the nginx.conf file through the following command:

nginx -h

Output:

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

Open the configuration file :

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

You can see at the end of the file:

include servers/*;

It includes all the files in the servers directory in the same directory. From this, we can create development projects in the servers file Configuration information:

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

Write the following configuration information into the test.conf file:

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;
    }
}

In the above directory of /home/www/php-project Next, we create an index.php file:

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

Write the content:

Restart 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

The above is the detailed content of Detailed explanation of how to install PHP environment on Apple system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn