MacOS12安装PHP7.4、composer、Nginx

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(); ?>

在成功浏览器访问:

-

gin
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 1

配置 nginx.conf没有成功,能帮忙解决么

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!