mac m2芯片 安装nginx + php + mysql

2023-12-13 14:33:31

1.安装homebrew:

系统本身就有(命令brew -v查看下),如果没有安装一下

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

2.安装nginx

brew install nginx

3.安装php

brew tap shivammathur/php
brew search php
brew install shivammathur/php/php@7.4

如果安装多个php版本,就修改下PHP端口配置:

我的路径是:/opt/homebrew/etc/php/7.4/php-fpm.d/www.conf

listen = 127.0.0.1:9000
改为
listen = 127.0.0.1:9074

4.修改nginx配置

sudo vim?/opt/homebrew/etc/nginx/nginx.conf

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

5.在?/opt/homebrew/etc/nginx下新建文件夹servers,在servers下新建新建test1.conf和test2.conf

mkdir servers
cd servers    
vim test1.conf

test1.conf配置内容

server {
    listen       80;
    server_name www.test1.cn;
    # 配置项目路径
    root  /Users/liuxiaoyun/www/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$ {
    	# 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;
    }
}

test2.conf的配置

server {
    listen       80;
    server_name www.test2.cn;
    # 配置项目路径
    root  /Users/liuxiaoyun/www/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;
    }
}

6.检查配置

nginx -t
如果报文件权限问题:sudo chmod -R 777 /var/logs

7.设置php-fpm开机自启

sudo cp /opt/homebrew/opt/php@7.4/homebrew.php@7.4.service /Library/LaunchAgents

8.启动php-fpm

brew services start php@7.4

9.验证是否启动成功

lsof -i :9074

10.终端切换php版本

解除之前版本链接:brew unlink php
增加新版本链接:
brew link --overwrite php@7.4

11.php加入环境变量

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

12.安装composer

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

13.验证composer

composer74 -v

14.mysql加入环境变量

 echo 'export PATH="/opt/homebrew/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc


For compilers to find mysql@5.7 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/mysql@5.7/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/mysql@5.7/include"

15.启动数据库

 brew services start mysql@5.7

16.brew安装mysql5.7总是出问题,我的解决办法是,更新homebrew,安装的mysql8.0

17.php项目运行:

(1).nginx执行重新加载:nginx -s reload ;报错:

[error] invalid PID number "" in "/opt/homebrew/var/run/nginx.pid"

解决:

重新加载配置文件 nginx.conf,然后再执行 reload:

nginx -c /opt/homebrew/etc/nginx/nginx.conf
nginx -s reload 

(2). 修改hosts文件,/etc/hosts。添加自定义本地域名

#上边配置的test1.conf和test2.conf文件里自定义的域名
127.0.0.1。www.test1.cn
127.0.0.1。www.test2.cn

(3).在~/www/test1里添加index.php。访问域名www.test1.cn就完成啦

文章来源:https://blog.csdn.net/xiaoyun888_/article/details/134946807
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。