Build a PHP development environment on macOS (brew installs nginx, mysql and multiple versions of php, and configures an environment for multiple php to run simultaneously)

Building a php development environment on macOS

1.Install brew

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

2. Install Nginx

brew install nginx

3.Install mysql

brew install mysql

4.Install php

Since there are no versions before PHP7.2 in the homebrew main library, and versions 7.2 and 7.3 are also marked as outdated versions; therefore, you need to hang the third-party extension first. The specific operations are as follows:

brew tap shivammathur/php
brew search php

php5.6

brew install shivammathur/php/[email protected]

php7.3

brew install shivammathur/php/[email protected]

php7.4

brew install shivammathur/php/[email protected]

php8.2

By default, new versions 8 and above can be installed directly.

brew install php

5. Modify php settings

sudo vim Under /usr/local/etc/php/5.6/php-fpm.conf:

Note: The configuration file path of version 5.6 is different from other versions.
listen = 127.0.0.1:9000
is changed to
listen = 127.0.0.1:9056

sudo vim Under /usr/local/etc/php/7.3/php-fpm.d/www.conf:
listen = 127.0.0.1:9000
is changed to
listen = 127.0.0.1:9073

sudo vim Under /usr/local/etc/php/7.4/php-fpm.d/www.conf:
listen = 127.0.0.1:9000
is changed to
listen = 127.0.0.1:9074

sudo vim Under /usr/local/etc/php/8.2/php-fpm.d/www.conf:
listen = 127.0.0.1:9000
is changed to
listen = 127.0.0.1:9082

6. Modify nginx configuration

sudo vim /usr/local/etc/nginx/nginx.conf

Change to the following:

#user  nobody;
worker_processes  1;

error_log  /var/logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    include servers/*;
}

Configure site test1:

Createcd /usr/local/etc/nginx/servers  a new site configuration file like this:
sudo vim test1.conf
The content is as follows:

server {
    listen       80;
    server_name  localhost;
    # 配置项目路径
    root   /Users/xxx/xxx/php/2023/test1; 

    #access_log  logs/host.access.log  main;

    location / {
        index  index.html index.htm index.php;
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=/$1  last;
            break;
        }
    }

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
    	# 9056上面设置的监听端口,加载php5.6
        fastcgi_pass   127.0.0.1:9056;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Configure site test2:

Createcd /usr/local/etc/nginx/servers  a new site configuration file like this:
sudo vim test2.conf
The content is as follows:

server {
    listen       80;
    server_name  localhost;
    # 配置项目路径
    root   /Users/kbq/workspace/php/2023/test2; 

    #access_log  logs/host.access.log  main;

    location / {
        index  index.html index.htm index.php;
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=/$1  last;
            break;
        }
    }

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
    	# 9074上面设置的监听端口,加载php7.4
        fastcgi_pass   127.0.0.1:9074;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Check configuration

nginx -t
If file permission issues are reported: sudo chmod -R 777 /var/logs

Set php-fpm to start automatically at boot

Not verified successfully

Change [email protected] under /usr/local/opt/[email protected] to [email protected]  under /usr/local/opt/[email protected]

Change [email protected] under /usr/local/opt/[email protected] and [email protected] under /usr/local/opt/[email protected] under /usr/local/opt/php . homebrew.php.service _

copy to

/Library/LaunchAgents

Finally restart the system 

Start php-fpm manually

 brew services start [email protected]

 brew services start [email protected]

 brew services start [email protected]

 brew services start [email protected]

Verify whether startup is successful

lsof -i :9056

lsof -i :9073

lsof -i :9074

lsof -i :9082

Terminal switch php version

Unlink the previous version: brew unlink php

 Add new version link:

brew link --overwrite [email protected]

brew link --overwrite [email protected]

brew link --overwrite [email protected]

brew link --overwrite [email protected]

 Add php to environment variables

Add the following to  /Users/xxx/.bash_profile

export PATH=${PATH}:/usr/local/opt/[email protected]/bin
export PATH=${PATH}:/usr/local/opt/[email protected]/sbin
alias php56="/usr/local/opt/[email protected]/bin/php"

export PATH=${PATH}:/usr/local/opt/[email protected]/bin
export PATH=${PATH}:/usr/local/opt/[email protected]/sbin
alias php73="/usr/local/opt/[email protected]/bin/php"

export PATH=${PATH}:/usr/local/opt/[email protected]/bin
export PATH=${PATH}:/usr/local/opt/[email protected]/sbin
alias php74="/usr/local/opt/[email protected]/bin/php"

export PATH=${PATH}:/usr/local/opt/[email protected]/bin
export PATH=${PATH}:/usr/local/opt/[email protected]/sbin
alias php82="/usr/local/opt/[email protected]/bin/php"

Make the configuration effective  source .bash_profile

Verify whether the configuration is successful, terminal input

php56 -v

php73 -v

php74 -v

php82 -v

If content similar to the following is displayed, it is considered successful.

PHP 8.2.12 (cli) (built: Nov  6 2023 02:54:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.12, Copyright (c), by Zend Technologies

Install composer

php56 -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php56 -r "if (hash_file('sha384', 'composer-setup.php') === 'e21205b207c3ff031906575712edab6f13eb0b361f2085f1f1237b7126d785e826a450292b6cfd1d64d92e6563bbde02') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php56 composer-setup.php
php56 -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer56

Check if successful 

~/ composer74
Failed loading /usr/local/Cellar/php/7.3.3/lib/php/pecl/20180731/xdebug.so:  dlopen(/usr/local/Cellar/php/7.3.3/lib/php/pecl/20180731/xdebug.so, 0x0009): tried: '/usr/local/Cellar/php/7.3.3/lib/php/pecl/20180731/xdebug.so' (no such file)
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.6.5 2023-10-06 10:11:52

To install other versions, change php56 to php73, php74, php82, and change composer56 to composer73, composer74, composer82

おすすめ

転載: blog.csdn.net/u010926168/article/details/134289062