preface

In the first article of 2021, I recently wanted to do a project with PHP, but I never thought of setting up the environment.

After surfing the web for a while, you’ll see that there are a lot of different ways to build PHP (which is not always a good thing). The most common ways to build PHP are Homestead (based on virtual machines), XAMPP, MAMP, Laradock (based on Docker), and so on, but these are heavy weights. Nginx + php-fpm: Nginx + phP-fpm: Nginx + phP-fpm: Nginx + phP-fpm: Nginx + phP-fpm: Nginx + phP-fpm And not too heavy.

Note: This article is based on the Mac environment.

The installation process

  • 1. Check whether PHP is installed
  • 2. Check whether the Composer is installed
  • 3. Check whether laravel is installed
  • 4. Check whether phP-fpm is installed
  • 5. Check whether nginx is installed
  • 6. Configure the coexistence of multiple versions
  • 7. Create a Laravel project and test it

Check whether PHP is installed

Newer Mac systems come with PHP and phP-FPM as follows:

#version
$ php -vPHP 7.3.11 (cli) (built: Jun 5 2020 23:50:40) (NTS) Copyright (c) 1997-2018 The PHP Group Zend Engine V3.3.11, Copyright (c) 1998-2018 Zend Technologies
#The execution path
$ which php
/usr/bin/php

#The configuration file
$ ls /etc/php-fpm.conf
/etc/php-fpm.conf

#The actual configuration file, since /etc/php-fpm.conf uses the include keyword
$ ls /private/etc/php-fpm.d
www.conf         www.conf.default
Copy the code

Check that Composer is installed

Composer is a package manager for PHP, similar to PIP, which does not come with the Mac system.

#The installation
$ brew install composer

$ composer --version
Composer version 1.9.0 2019-08-02 20:55:32
Copy the code

Check whether laravel is installed

The laravel project will be created later to test whether the PHP environment is set up successfully. It is recommended to install it, and you will use it later.

#The installation
$ composer global require laravel/installer

#test
$ laravel --version
Laravel Installer 4.1.1
Copy the code

Check whether nginx is installed

Since I chose nginx + php-fpm to set up the PHP environment, nginx must be installed here.

#The installation
$ brew install nginx

#test
$ nginx -vNginx version: nginx / 1.19.5Copy the code

Coexistence of multiple versions

For historical reasons, many projects don’t have a standard PHP version, and projects can jump from PHP5.6, PHP7.2, or even PHP8.0, which can lead to syntax inconsistencies and strange problems, so it’s important to have multiple versions coexist.

This time through Homebrew to achieve the coexistence of multiple versions, implementation is relatively simple, first of all, we abandon the Mac built-in PHP, all use BREW installation.

Install php5.6

#The installation
$The brew install [email protected]

#Configure environment variables (available at brew info [email protected])Echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/. ZSHRC echo 'export PATH="/usr/local/opt/[email protected]/sbin:$PATH"'  >> ~/.zshrcCopy the code

Install php7.2

#The installation
$The brew install [email protected]

#Configure environment variables (available at brew info [email protected])Echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/. ZSHRC echo 'export PATH="/usr/local/opt/[email protected]/sbin:$PATH"'  >> ~/.zshrcCopy the code

Php-fpm configures different ports

The core of multi-version coexistence is to start different versions of PHP-FPM, so avoid port hogging.

Why phP-FPM? This is where I was confused at first, because PHP, unlike Javascript, can be parsed directly by the browser, so when a user requests a PHP program, Nginx first passes the request through php-fpm to the PHP interpreter for processing, see here

#The configuration file path is available at brew info [email protected] or brew info [email protected]

#Php5.6 listens on port 9001
$ vim /usr/localThe/etc/PHP / 5.6 / PHP - FPM. ConfListen = 127.0.0.1:9001
#Php7.2 listens on port 9002
$ vim /usr/localThe/etc/PHP / 7.2 / PHP - FPM. D/www.confListen = 127.0.0.1:9002Copy the code

Start the service

#Start the php5.6The brew services start shivammathur/PHP/[email protected]
#Start the php7.2The brew services start at [email protected]
#To view
$ ps -ef | grep php-fpm501 12981 1 0 11:35 am?? 0:00.15 /usr/local/opt/[email protected]/sbin/php-fpm --nodaemonize 501 12988 12981 0 11:35 am?? 0:00.74 /usr/local/opt/[email protected]/sbin/php-fpm --nodaemonize 501 12989 12981 0 11:35 am?? 00.00 /usr/local/opt/[email protected]/sbin/php-fpm --nodaemonize 501 13141 1 0 11:35 am?? 0:00.14 /usr/local/opt/[email protected]/sbin/php-fpm --nodaemonize 501 13148 13141 0 11:35 am?? 0:00.17 /usr/local/opt/[email protected]/sbin/php-fpm --nodaemonize 501 13149 13141 0 11:35 am?? 0:00. 00 / usr/local/opt/[email protected] / sbin/PHP - FPM - nodaemonizeCopy the code

At this point, multiple versions of PHP have been built, and the subsequent test can be used to check whether the building is successful.

Project deployment and testing

This time we will test the correctness of the above multiple versions by deploying two versions of the Laravel project, and let you know how to deploy the PHP project.

1. Create projects

#Create a project
#Laravel 5.2 requires a minimum PHP version of 5.5.9Composer create-project laravel/laravel=5.2.* php56 --prefer-dist
#Laravel 5.6 requires a minimum PHP version of 7.1.3Composer create-project laravel/laravel=5.6.* php72 --prefer-distCopy the code

2. Create the blog56 configuration

Vim/usr/local/etc/nginx/servers/php56. Conf server {# listening port, that is, we access the port 6001 default_server listen; Server_name 127.0.0.1; # the default website the root directory the root directory (WWW)/Users/XXX/PhpstormProjects php56 / public; access_log /Users/xxx/PhpstormProjects/php56/access.log; error_log /Users/xxx/PhpstormProjects/php56/error.log; location / { try_files $uri /index.php? $query_string; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; Location = /50x.html {} # PHP script requests all forward to FastCGI processing. Location ~ \.php${# this is the port we set up above, and the 9001 will be sent to PHP5.6. fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }}Copy the code

3. Create a configuration for Blog72

Vim/usr/local/etc/nginx/servers/php72. Conf server {# listening port, that is, we access the port 6002 default_server listen; Server_name 127.0.0.1; # the default website the root directory the root directory (WWW)/Users/XXX/PhpstormProjects php72 / public; access_log /Users/xxx/PhpstormProjects/php72/access.log; error_log /Users/xxx/PhpstormProjects/php72/error.log; location / { try_files $uri /index.php? $query_string; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; Location = /50x.html {} # PHP script requests all forward to FastCGI processing. Location ~ \.php${# this is the port we set up above, and the 9002 will be sent to PHP7.2. fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }}Copy the code

4. Access test: visit http://localhost:6001/ and http://localhost:6002/ to see the welcome screen

Summary on pit

Nginx configuration must be access_log and error_log, so that the error can be located, because there are many cases of Nginx configuration error!

2. Pay attention to the browser proxy problem, many people including me used to test in the browser (GET request), but sometimes due to proxy problems, the request will fail, then try to execute on the command line, or other browsers (personal experience).

PHP/nginx/nginx/nginx/nginx/nginx/nginx/nginx/nginx/nginx/nginx/nginx

#The author's configuration (can't say he has a problem with configuration, can only say that everyone's environment is different)location / { try_files $uri $uri/ /index.php? $query_string; }
#Actual configurationlocation / { try_files $uri /index.php? $query_string; }Copy the code

Solution reference: stackoverflow.com/questions/1…

Warning: here is a warning, many people due to incorrect configuration (permissions or path problems), so remember to add access_log and error_log, to know what error!! In addition, sometimes direct testing in the browser may not be successful, possibly due to proxy issues.

reference

2. Use BREW to realize the coexistence and arbitrary switching of multiple versions of PHP in MAC 3