Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

PHP 7.4 Development Stack on macOS Catalina with Nginx, MySQL, and Redis (including phpredis)

Tech 1

macOS Catalina (10.15) ships with Apache and several scripting languages preinstalled, but the following setup uses Nginx, Homebrew PHP 7.4, MySQL 8, and Redis. The default shell is zsh; commands assume ~/.zshrc for shell configuration.

Terminal and IDE

  • Install iTerm2 by dragging the app into /Applications.
  • Install PhpStorm if an IDE is desired.

Xcode and Command Line Tools

Install Aple’s toolchain and except licenses:

xcode-select --install
# Then open Xcode once and accept the license

Homebrew

Install Homebrew and initialize your shell:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Optional: switch to a mirror (example uses USTC). Adjust if needed:

cd "$(brew --repo)" && git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" && \
  git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

PHP 7.4 (Homebrew)

Install and run PHP-FPM. Prefer a versioned formula to avoid system PHP:

brew install php@7.4
brew services start php@7.4

Ensure the brewed PHP precedes the system PHP in PATH:

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

Verify:

php -v
php-fpm -v

MySQL

Install, start, and secure the server:

brew install mysql
brew services start mysql
mysql -u root
# In another step, run the secure setup
mysql_secure_installation

Redis

Install and start Redis:

brew install redis
brew services start redis
redis-cli ping   # Expect PONG

Nginx

Install and start Nginx:

brew install nginx
brew services start nginx

Inspect default paths (note the -V output includes the nginx.conf location):

nginx -V

Open the main config to confirm it includes a servers directory include:

vi /usr/local/etc/nginx/nginx.conf
# Look for: include servers/*;

Create a virtual host in the included directory:

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

Example server block integrating PHP-FPM and a typical front controller:

upstream local_phpfpm {
    server 127.0.0.1:9000;  # Homebrew php-fpm default
}

server {
    listen 8088;
    server_name localhost;
    root /Users/$USER/Sites/demo/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_index index.php;
        fastcgi_pass local_phpfpm;
    }

    location ~ /\. {
        deny all;
    }
}

Create a simple PHP entry point:

mkdir -p ~/Sites/demo/public
cat > ~/Sites/demo/public/index.php <<'PHP'
<?php
header('Content-Type: text/plain');
echo "Nginx + PHP-FPM (Catalina)\n";
phpinfo();
PHP

Test and reload Nginx:

nginx -t
brew services restart nginx

Open http://localhost:8088 in a browser.

Composer

Install Composer and configure an optional mirror:

php -r "copy('https://getcomposer.org/installer','composer-setup.php');"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
rm composer-setup.php

# Optional China mirror
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

PHP Redis Extension (phpredis)

Two common approaches are shown below.

Install via PECL

Make sure pecl resolves to the brewed PHP toolchain, then install:

pecl install redis

Enable the extension in php.ini and restart PHP-FPM:

INI_FILE=$(php --ini | awk -F': ' '/Loaded Configuration File/ {print $2}')
[ -n "$INI_FILE" ] && echo "extension=redis.so" | sudo tee -a "$INI_FILE"
brew services restart php@7.4
php -m | grep -i redis

Build from Source

Compile a specific release manually:

VER=5.1.0
curl -LO https://pecl.php.net/get/redis-${VER}.tgz
tar -xzf redis-${VER}.tgz
cd redis-${VER}
phpize
./configure --with-php-config="$(command -v php-config)"
make -j4
sudo make install

INI_FILE=$(php --ini | awk -F': ' '/Loaded Configuration File/ {print $2}')
[ -n "$INI_FILE" ] && echo "extension=redis.so" | sudo tee -a "$INI_FILE"
brew services restart php@7.4
php -m | grep -i redis

Service Management and Inspection

  • List managed daemons: brew services list
  • Restart individual services: brew services restart php@7.4, brew services restart nginx, brew services restart mysql, brew services restart redis
  • Validate Nginx config anytime: nginx -t

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.