MacOS12安裝PHP7.4、composer、Nginx

fyonecon發表於2021-07-29

MacOS12 Monterey已經不自帶PHP了,雖然PHP8已經發布,但是為了穩一點,還是選擇PHP7.4。

這裡我們用brew來安裝所有用的到擴充套件。

1. 安裝brew(國內源):

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

順升級brew,這裡主要是確保國內brew源和全球同步:

brew update

2. 安裝PHP7.4:

(也可安裝最新版PHP:brew install php)

brew install php@7.4

控制檯裡會有如下提示,這是告訴如何鏈路PHP到mac OS裡面:

3. 鏈路PHP:

根據控制檯提示,分別執行如下5句命令:

fyonecon@DJhanwudi ~ % echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
fyonecon@DJhanwudi ~ % echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc
fyonecon@DJhanwudi ~ % export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
fyonecon@DJhanwudi ~ % export CPPFLAGS="-I/usr/local/opt/php@7.4/include"
fyonecon@DJhanwudi ~ % brew services start php@7.4

  1. 檢視PHP版本:
php -v

PHP安裝成功。

5. 全域性安裝composer:

fyonecon@DJhanwudi ~ % php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
fyonecon@DJhanwudi ~ % php composer-setup.php
All settings correct for using Composer
Downloading...

Composer (version 2.1.5) successfully installed to: /Users/fyonecon/composer.phar
Use it: php composer.phar

fyonecon@DJhanwudi ~ % php -r "unlink('composer-setup.php');"
fyonecon@DJhanwudi ~ % sudo mv composer.phar /usr/local/bin/composer
Password:
fyonecon@DJhanwudi ~ % composer selfupdate
You are already using the latest available Composer version 2.1.5 (stable channel).
fyonecon@DJhanwudi ~ %

6. 安裝nginx:

brew install nginx

檢視nginx版本:

nginx -v

啟動nginx:

brew services start nginx

成功訪問如下網址代表成功:

127.0.0.1:8080/

其他命令:

停止:brew services stop nginx

重啟:brew services restart nginx

7. 配置nginx.conf:

檔案在:/usr/local/etc/nginx/nginx.conf ,你可以直接把如下程式碼全部替換到nginx.conf:


#user  nobody;
worker_processes  1;

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

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index index.php index.html;
        }

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl http2;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index index.php index.html;
    #    }
    #}
    include servers/*;
}

需要重啟nginx:

brew services restart nginx

8. 驗證PHP檔案執行:

程式碼預設存放目錄:

/usr/local/var/www

設定一個index.php檔案,並填入如下內容:

<?php phpinfo(); ?>

在成功瀏覽器訪問:

-

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章