MAC系统部署NGINX+PHP+MYSQL开发环境

第一步:安装brew

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

如果mac没有git或git版本太老,系统会提示你安装或升级git后,才可以继续安装brew。

提示:git至少需要20GB的磁盘空间。

安装脚本过程记录:

...................................................................................................................................................

--> 脚本开始执行

未发现Git代理(属于正常状态)

              开始  进入brew官方安装脚本  开始

下载官方install.sh文件,当前目录是: /Users/ziyin

Cloning into 'brew-install-ck'...

remote: Enumerating objects: 18, done.

remote: Counting objects: 100% (18/18), done.

remote: Compressing objects: 100% (16/16), done.

remote: Total 18 (delta 0), reused 11 (delta 0), pack-reused 0

Receiving objects: 100% (18/18), 20.54 KiB | 1.14 MiB/s, done.

==> Checking for `sudo` access (which may request your password)...

==> This script will install:

/opt/homebrew/bin/brew

/opt/homebrew/share/doc/homebrew

/opt/homebrew/share/man/man1/brew.1

/opt/homebrew/share/zsh/site-functions/_brew

/opt/homebrew/etc/bash_completion.d/brew

/opt/homebrew

==> The following new directories will be created:

/opt/homebrew/bin

/opt/homebrew/etc

/opt/homebrew/include

/opt/homebrew/lib

/opt/homebrew/sbin

/opt/homebrew/share

/opt/homebrew/var

/opt/homebrew/opt

/opt/homebrew/share/zsh

/opt/homebrew/share/zsh/site-functions

/opt/homebrew/var/homebrew

/opt/homebrew/var/homebrew/linked

/opt/homebrew/Cellar

/opt/homebrew/Caskroom

/opt/homebrew/Frameworks

Press RETURN/ENTER 现在是brew官方安装提示,它需要你按回车键开始 other key to abort:

==> /usr/bin/sudo /usr/bin/install -d -o root -g wheel -m 0755 /opt/homebrew

==> /usr/bin/sudo /bin/mkdir -p /opt/homebrew/bin /opt/homebrew/etc /opt/homebrew/include /opt/homebrew/lib /opt/homebrew/sbin /opt/homebrew/share /opt/homebrew/var /opt/homebrew/opt /opt/homebrew/share/zsh /opt/homebrew/share/zsh/site-functions /opt/homebrew/var/homebrew /opt/homebrew/var/homebrew/linked /opt/homebrew/Cellar /opt/homebrew/Caskroom /opt/homebrew/Frameworks

==> /usr/bin/sudo /bin/chmod ug=rwx /opt/homebrew/bin /opt/homebrew/etc /opt/homebrew/include /opt/homebrew/lib /opt/homebrew/sbin /opt/homebrew/share /opt/homebrew/var /opt/homebrew/opt /opt/homebrew/share/zsh /opt/homebrew/share/zsh/site-functions /opt/homebrew/var/homebrew /opt/homebrew/var/homebrew/linked /opt/homebrew/Cellar /opt/homebrew/Caskroom /opt/homebrew/Frameworks

==> /usr/bin/sudo /bin/chmod go-w /opt/homebrew/share/zsh /opt/homebrew/share/zsh/site-functions

==> /usr/bin/sudo /usr/sbin/chown ziyin /opt/homebrew/bin /opt/homebrew/etc /opt/homebrew/include /opt/homebrew/lib /opt/homebrew/sbin /opt/homebrew/share /opt/homebrew/var /opt/homebrew/opt /opt/homebrew/share/zsh /opt/homebrew/share/zsh/site-functions /opt/homebrew/var/homebrew /opt/homebrew/var/homebrew/linked /opt/homebrew/Cellar /opt/homebrew/Caskroom /opt/homebrew/Frameworks

==> /usr/bin/sudo /usr/bin/chgrp admin /opt/homebrew/bin /opt/homebrew/etc /opt/homebrew/include /opt/homebrew/lib /opt/homebrew/sbin /opt/homebrew/share /opt/homebrew/var /opt/homebrew/opt /opt/homebrew/share/zsh /opt/homebrew/share/zsh/site-functions /opt/homebrew/var/homebrew /opt/homebrew/var/homebrew/linked /opt/homebrew/Cellar /opt/homebrew/Caskroom /opt/homebrew/Frameworks

==> /usr/bin/sudo /usr/sbin/chown -R ziyin:admin /opt/homebrew

==> /usr/bin/sudo /bin/mkdir -p /Users/ziyin/Library/Caches/Homebrew

==> /usr/bin/sudo /bin/chmod g+rwx /Users/ziyin/Library/Caches/Homebrew

==> /usr/bin/sudo /usr/sbin/chown -R ziyin /Users/ziyin/Library/Caches/Homebrew

==> Downloading and installing Homebrew...

remote: Enumerating objects: 276920, done.

remote: Counting objects: 100% (83296/83296), done.

......................................................................................................................................................

==> Resetting /opt/homebrew...

Reset branch 'stable'

==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.

Read the analytics documentation (and how to opt-out) here:

  https://docs.brew.sh/Analytics

No analytics data has been sent yet (nor will any be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:

  https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew#donations

==> Next steps:

- Run these two commands in your terminal to add Homebrew to your PATH:

    (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/ziyin/.zprofile

    eval "$(/opt/homebrew/bin/brew shellenv)"

- Run brew help to get started

- Further documentation:

    https://docs.brew.sh

此步骤成功

Password:

                完成  退出brew官方安装脚本  完成

==> 配置国内镜像源HOMEBREW BOTTLE   

此处如果显示Password表示需要再次输入开机密码,输入完后回车

sed: /Users/ziyin/.zprofile: No such file or directory

有些电脑xcode和git混乱,再运行一次,此处如果有error正常。

xcode-select: error: command line tools are already installed, use "Software Update" to install updates

        Homebrew已经安装成功,接下来配置国内软件下载源。

请选择今后brew install的时候访问那个国内镜像,例如阿里巴巴,输入5回车。

1、中科大国内源

2、清华大学国内源

3、上海交通大学国内源

4、腾讯国内源

5、阿里巴巴国内源(推荐)

请输入序号: 2

    你选择了清华大学国内源


        环境变量写入->/Users/ziyin/.zprofile

此步骤成功

==> 安装完成,brew版本

Homebrew 4.3.13

Homebrew前期配置成功

==> Updating Homebrew...

==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles-portable-ruby/portable-ruby-3.3.4_1.arm64_big_sur.bottle.tar.gz

###################################################################### 100.0%

==> Pouring portable-ruby-3.3.4_1.arm64_big_sur.bottle.tar.gz

==> Homebrew collects anonymous analytics.

Read the analytics documentation (and how to opt-out) here:

  https://docs.brew.sh/Analytics

No analytics have been recorded yet (nor will be during this `brew` run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:

  https://github.com/Homebrew/brew#donations

Already up-to-date.

        Homebrew自动安装程序运行完成

          国内地址已经配置完成

  之前步骤选了删除本机brew的话,桌面多出一个Old_Homebrew文件夹,可以删除。

              初步介绍几个brew命令

查看版本:brew -v  更新brew版本:brew update

查找:brew search python(其中python替换为要查找的关键字)

安装:brew install python(其中python替换为要安装的名称)

本地软件库列表:brew ls


        欢迎右键点击下方地址-打开链接 点个赞吧

        https://zhuanlan.zhihu.com/p/111014448

        如果遇到问题可以右键下面地址查看常见错误解决办法

        https://gitee.com/cunkai/HomebrewCN/blob/master/error.md

        brew官方地址:https://brew.sh/zh-cn/

安装成功 但还需要重启终端 或者 运行 source /Users/ziyin/.zprofile  否则国内地址无法生效

查看相关配置是否正确:

ziyin@bogon Caches % cat /Users/ziyin/.zprofile

  export HOMEBREW_PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple #ckbrew

  export HOMEBREW_API_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api  #ckbrew

  export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles #ckbrew

  eval $(/opt/homebrew/bin/brew shellenv) #ckbrew

ziyin@bogon Caches %

ziyin@bogon Caches % source /Users/ziyin/.zprofile

ziyin@bogon Caches % brew update

==> Updating Homebrew...

Already up-to-date.

ziyin@bogon Caches %

第二步:安装和配置Nginx

ziyin@bogon Caches % brew install nginx

==> nginx

Docroot is: /opt/homebrew/var/www

The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that

nginx can run without sudo.

nginx will load all files in /opt/homebrew/etc/nginx/servers/.

To start nginx now and restart at login:

  brew services start nginx

Or, if you don't want/need a background service you can just run:

  /opt/homebrew/opt/nginx/bin/nginx -g daemon\ off\;

ziyin@bogon Caches %                 

ziyin@ziyin-Air ~ % brew services start nginx

==> Tapping homebrew/services

Cloning into '/opt/homebrew/Library/Taps/homebrew/homebrew-services'...

remote: Enumerating objects: 3069, done.

remote: Counting objects: 100% (530/530), done.

remote: Compressing objects: 100% (195/195), done.

remote: Total 3069 (delta 349), reused 452 (delta 331), pack-reused 2539

Receiving objects: 100% (3069/3069), 859.92 KiB | 801.00 KiB/s, done.

Resolving deltas: 100% (1490/1490), done.

Tapped 1 command (47 files, 1MB).

==> Successfully started `nginx` (label: homebrew.mxcl.nginx)

ziyin@ziyin-Air ~ %

验证nginx服务:

ziyin@ziyin-MacBook-Air ~ % nginx -t      //测试配置文件是否正确

nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok

nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test is successful

ziyin@ziyin-MacBook-Air ~ % sudo nginx     //启动nginx(想重启服务可以使用sudo  nginx -s reload)

ziyin@ziyin-Air ~ % curl http://localhost:8080    //命令行访问网页测试

或者打开浏览器,输入http://localhost:8080/验证nginx是否正常访问。

==================Nginx 其它相关操作=================================

#启动nginx

$ sudo nginx

#重新加载|重启|退出

$ sudo nginx-s reload|reopen|quit

#brew操作,启动|停止|重启

$ brew services start|stop|restart nginx

============================================================

第三步:安装mysql

ziyin@ziyin-Air ~ % brew install mysql

==> mysql

Upgrading from MySQL <8.4 to MySQL >9.0 requires running MySQL 8.4 first:

- brew services stop mysql

- brew install mysql@8.4

- brew services start mysql@8.4

- brew services stop mysql@8.4

- brew services start mysql

We've installed your MySQL database without a root password. To secure it run:

    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:

    mysql -u root

To start mysql now and restart at login:

  brew services start mysql

Or, if you don't want/need a background service you can just run:

  /opt/homebrew/opt/mysql/bin/mysqld_safe --datadir\=/opt/homebrew/var/mysql

ziyin@ziyin-Air ~ %

验证MySQL:

ziyin@ziyin-Air ~ % brew services start mysql

Error: Formula `mysql` has not implemented #plist, #service or installed a locatable service file

ziyin@ziyin-Air ~ % ls -al /opt/homebrew/opt/mysql

lrwxr-xr-x  1 ziyin  admin  21  8  8 10:22 /opt/homebrew/opt/mysql -> ../Cellar/mysql/9.0.1

ziyin@ziyin-Air ~ % brew doctor

Please note that these warnings are just used to help the Homebrew maintainers

with debugging if you file an issue. If everything you use Homebrew for is

working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: Suspicious https://github.com/Homebrew/brew git origin remote found.

The current git origin is:

  https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew

With a non-standard origin, Homebrew won't update properly.

You can solve this by setting the origin remote:

  git -C "/opt/homebrew" remote set-url origin https://github.com/Homebrew/brew

Warning: You have unlinked kegs in your Cellar.

Leaving kegs unlinked can lead to build-trouble and cause formulae that depend on

those kegs to fail to run properly once built. Run `brew link` on these:

  mysql

Warning: Broken symlinks were found. Remove them with `brew cleanup`:

  /opt/homebrew/opt/mysql

  /opt/homebrew/opt/mysql@9.0

ziyin@ziyin-Air ~ %

以上错误是macOS老版本的一个bug,需要升级下macOS后,重新安装mysql即可解决

ziyin@ziyin-Air ~ % sudo softwareupdate -l    //查看更新软件包

ziyin@ziyin-Air ~ % sudo softwareupdate -i -a。//安装所有更新软件包

ziyin@ziyin-MacBook-Air ~ % brew reinstall mysql    //重新安装

ziyin@ziyin-MacBook-Air ~ % brew services start mysql   //开启mysql服务成功

==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

ziyin@ziyin-MacBook-Air ~ % mysql_secure_installation   //MySQL安全设置mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

The 'validate_password' component is installed on the server.

The subsequent steps will run with the existing configuration

of the component.

Using existing password for root.

Estimated strength of the password: 100 //设置密码强度

Change the password for root ? ((Press y|Y for Yes, any other key for No) : y   //修改 mysql的root账户的密码

New password:

Re-enter new password:

Estimated strength of the password: 100

Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y    //确认密码设置

By default, a MySQL installation has an anonymous user,

allowing anyone to log into MySQL without having to have

a user account created for them. This is intended only for

testing, and to make the installation go a bit smoother.

You should remove them before moving into a production

environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y    //删除匿名用户

Success.

Normally, root should only be allowed to connect from

'localhost'. This ensures that someone cannot guess at

the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y     //禁止mysql的root账户远程登录权限

Success.

By default, MySQL comes with a database named 'test' that

anyone can access. This is also intended only for testing,

and should be removed before moving into a production

environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y   //删除默认保留的test数据库

- Dropping test database...

Success.

- Removing privileges on test database...

Success.

Reloading the privilege tables will ensure that all changes

made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y  //重新加载权限表

Success.

All done!

ziyin@ziyin-MacBook-Air ~ %

至此,mysql数据库安装及配置完毕!

========================常用的mysql数据库维护操作======================

#启动|停止|重启 mysql服务

$ brew services start|stop|restart mysql

#本地登录MySQL

$ mysql-u用户名-p密码

#远程登录

$ mysql-hIP地址-u用户名-p密码-P端口号(默认3306)

===================================================================

第四步:安装php

注意:由于homebrew主库中没有PHP7.2 之前的版本,并且7.2,7.3版本也被标记成过时版本!

ziyin@ziyin-MacBook-Air ~ % brew search php  //搜索PHP,会搜索出PHP相关的资源,然后根据自己需求选择安装

==> Formulae

brew-php-switcher          php-code-sniffer          php-cs-fixer@2            php@8.1                    phpbrew                    phpmyadmin                phpunit                    pop

php                        php-cs-fixer              php@8.0                    php@8.2                    phpmd                      phpstan                    pup                        pcp

==> Casks

eclipse-php                                          phpstorm                                              phpwebstudy                                          pop

ziyin@ziyin-MacBook-Air ~ % brew install php     //安装PHP,不指定PHP版本时,默认安装最新版本

.........................

............

==> /opt/homebrew/Cellar/php/8.3.10/bin/pear update-channels

==> Caveats

To enable PHP in Apache add the following to httpd.conf and restart Apache:

    LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so

    <FilesMatch \.php$>

        SetHandler application/x-httpd-php

    </FilesMatch>

Finally, check DirectoryIndex includes index.php

    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:

    /opt/homebrew/etc/php/8.3/

To start php now and restart at login:

  brew services start php

Or, if you don't want/need a background service you can just run:

  /opt/homebrew/opt/php/sbin/php-fpm --nodaemonize

==> Summary

🍺  /opt/homebrew/Cellar/php/8.3.10: 524 files, 88.8MB

==> Running `brew cleanup php`...

Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.

Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

==> No outdated dependents to upgrade!

==> Checking for dependents of upgraded formulae...

Disable this behaviour by setting HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK.

Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

==> Reinstalling 1 dependent with broken linkage from source:

mysql

Error: Could not rename mysql keg! Check/fix its permissions:

  sudo chown -R ziyin /opt/homebrew/Cellar/mysql/9.0.1     //目录读取权限报错

ziyin@ziyin-MacBook-Air ~ %

ziyin@ziyin-MacBook-Air ~ % sudo chown -R ziyin /opt/homebrew/Cellar/mysql/9.0.1

Password:

ziyin@ziyin-MacBook-Air ~ %

ziyin@ziyin-MacBook-Air ~ % brew install php

==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api/formula.jws.json

-=#=- #      #        #                                                                                                                                                                             

==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api/cask.jws.json

##O=-#      #                                                                                                                                                                                       

Warning: php 8.3.10 is already installed and up-to-date.

To reinstall 8.3.10, run:

  brew reinstall php

ziyin@ziyin-MacBook-Air ~ %

至此,php自动安装完成,默认安装的是最新版本8.3.10!

接着,开始配置php环境,即将PHP加入 $PATH 中:

ziyin@ziyin-MacBook-Air ~ % whereis php     //找到php运行命令所在的位置

php: /opt/homebrew/bin/php /opt/homebrew/share/man/man1/php.1

ziyin@ziyin-MacBook-Air ~ %

===========================================================

#shell使用bash添加方式

$ vim ~/.bash_profile

export PATH="/opt/homebrew/bin:$PATH"

source ~/.bash_profile

# shell使用zsh添加方式

$ vim ~/.zprofile

export PATH="/opt/homebrew/bin:$PATH"

source ~/.zprofile

===========================================================

ziyin@ziyin-MacBook-Air ~ % php -v

PHP 8.3.10 (cli) (built: Aug  5 2024 00:56:36) (NTS)

Copyright (c) The PHP Group

Zend Engine v4.3.10, Copyright (c) Zend Technologies

    with Zend OPcache v8.3.10, Copyright (c), by Zend Technologies

ziyin@ziyin-MacBook-Air ~ %

最后,配置php-fpm.conf。php-fpm.confPHP-FPMFastCGI Process Manager)的配置文件,用于控制PHP-FPM管理进程的相关参数。这个配置文件包含了工作子进程的数量、运行权限、监听端口、慢请求等设置。PHP-FPM是一个PHP FastCGI管理器,旨在将FastCGI进程管理整合进PHP包中,特别是对于PHP 5.3.3之前的版本,PHP-FPM是一个必要的补丁包,需要通过patch到PHP源代码中,并在编译安装PHP后才能使用。php-fpm.conf配置文件中的设置对于优化PHP性能和确保PHP应用程序的稳定运行至关重要,包括但不限于设置display_errors为off、error_log的路径、memory_limit等参数。此外,该配置文件中的所有相对路径都是相对于PHP的安装前缀设置的,确保了配置的一致性和可维护性。

FPM配置文件为php-fpm.conf,其语法类似 php.ini 。

其php手册上也有详细的讲解:http://php.net/manual/zh/install.fpm.configuration.php

ziyin@ziyin-MacBook-Air ~ % vim /opt/homebrew/etc/php/8.3/php-fpm.conf

#去掉第17行和第24行前面的分号

17 ;pid = run/php-fpm.pid

24 ;error_log = log/php-fpm.log

#启动

ziyin@ziyin-MacBook-Air ~ % brew services start php

==> Successfully started `php` (label: homebrew.mxcl.php)

ziyin@ziyin-MacBook-Air ~ %

#查看php-fpm是否启动成功

ziyin@ziyin-MacBook-Air ~ % lsof -Pni4 | grep LISTEN | grep php

php-fpm  23903 ziyin    9u  IPv4 0xdcb33f2a9a7935d1      0t0  TCP 127.0.0.1:9000 (LISTEN)

php-fpm  23906 ziyin  10u  IPv4 0xdcb33f2a9a7935d1      0t0  TCP 127.0.0.1:9000 (LISTEN)

php-fpm  23907 ziyin  10u  IPv4 0xdcb33f2a9a7935d1      0t0  TCP 127.0.0.1:9000 (LISTEN)

ziyin@ziyin-MacBook-Air ~ %

至此php安装配置完成!

第五部:整合NGINX+PHP+MYSQL开发环境

修改nginx.conf配置文件

ziyin@ziyin-MacBook-Air ~ % vim /opt/homebrew/etc/nginx/nginx.conf

ziyin@ziyin-MacBook-Air ~ % grep -v ^$ /opt/homebrew/etc/nginx/nginx.conf | grep -v #

worker_processes  1;

error_log  /opt/homebrew/var/log/nginx/error.log;

pid  /opt/homebrew/var/run/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  /opt/homebrew/var/log/nginx/access.log  main;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen      8080;

        server_name  localhost;

        location / {

            root  html;

            index  index.html index.htm;

        }

        error_page  500 502 503 504  /50x.html;

        location = /50x.html {

            root  html;

        }

    }

    include servers/*;

    include vhosts/*;

}

ziyin@ziyin-MacBook-Air ~ %

修改配置说明:

1,监听的端口可以是8080或80(如果80没有被占用的话);

2,server_name即为主机域名,这里是localhost,可以在/etc/hosts里添加一个本地ip的域名指向,然后在nginx里添加该域名,即可通过浏览器输入域名地址访问;

3,nginx.conf有个备份文件,文件名nginx.conf.default,可以通过改文件覆盖掉当前运行的配置文件;

4,对应的access_log、error_log、pid的具体文件位置,需提前确认是否存在,否则nginx会报错,无法启动;

5,修改完nginx.conf保存退出,通过nginx -t来测试配置文件是否存在错误,如下:

ziyin@ziyin-MacBook-Air ~ % nginx -t

nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok

nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test is successful

ziyin@ziyin-MacBook-Air ~ %

以上返回表示配置文件没有错误。

配置虚拟主机(通过修改vhosts文件实现):

创建虚拟机主机前,先创建一个存放虚拟主机对应网站的存放目录,例如test目录;

ziyin@ziyin-MacBook-Air ~ % mkdir -p /opt/homebrew/var/www/test

接着,创建存放虚拟主机配置文件的目录,例vhosts目录;

ziyin@ziyin-MacBook-Air ~ % mkdir -p /opt/homebrew/etc/nginx/vhosts

最后,创建虚拟机主机的配置文件;

ziyin@ziyin-MacBook-Air ~ % vim /opt/homebrew/etc/nginx/vhosts/www.test.com.conf

ziyin@ziyin-MacBook-Air ~ % cat /opt/homebrew/etc/nginx/vhosts/www.test.com.conf

server {

    #监听端口

    listen 8080;

    #虚拟主机域名

    server_name www.test.com;

    #网站根目录

    root /opt/homebrew/var/www/test;

    #定义路径下默认访问的文件名

    index index.php index.html;


    location / {

          try_files $uri $uri  /index.php?$query_string;

          #打开目录浏览功能,可以列出整个目录

         #autoindex on;

    }


    access_log /opt/homebrew/var/log/nginx/www.test.com-access.log main;

    error_log /opt/homebrew/var/log/nginx/www.test.com-error.log error;

    #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    location ~ \.php$ {

        fastcgi_pass      127.0.0.1:9000;

        fastcgi_index      index.php;

        include            fastcgi_params;

        fastcgi_param      SCRIPT_FILENAME $document_root$fastcgi_script_name;

    }

}

ziyin@ziyin-MacBook-Air ~ %

修改完毕后,测试并验证下配置文件是否有错误的地方,可以通过如下命令:

ziyin@ziyin-MacBook-Air ~ % nginx -t

nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok

nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test is successful

ziyin@ziyin-MacBook-Air ~ %

以上反馈信息说明配置文件正确无误。

======================独立运行的二级栏目解析================================

以往MAC部署apache解析二级栏目,只需要在对应的目录下添加.htaccess文件,在文件中配置规则,而nginx不支持.htaccess,所以直接在对应的主机文件里配置

location /a {

      index index.php index.html;

      try_files$uri$uri/ /a/index.php?query_string;

}

========================================================================

重启相关的服务并测试是否生效:

ziyin@ziyin-MacBook-Air ~ % brew services restart nginx

Stopping `nginx`... (might take a while)

==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)

==> Successfully started `nginx` (label: homebrew.mxcl.nginx)

ziyin@ziyin-MacBook-Air ~ % brew services restart php

Stopping `php`... (might take a while)

==> Successfully stopped `php` (label: homebrew.mxcl.php)

==> Successfully started `php` (label: homebrew.mxcl.php

ziyin@ziyin-MacBook-Air ~ % brew services restart mysql

Stopping `mysql`... (might take a while)

==> Successfully stopped `mysql` (label: homebrew.mxcl.mysql)

==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

ziyin@ziyin-MacBook-Air ~ %

开始测试php开发环境:
ziyin@ziyin-MacBook-Air ~ % vim /opt/homebrew/var/www/test/index.php

ziyin@ziyin-MacBook-Air ~ % cat /opt/homebrew/var/www/test/index.php

<?php

phpinfo();

?>

ziyin@ziyin-MacBook-Air ~ %



设置MAC OS当前用户开机自动启动PHP,MYSQL,NGINX服务:

% launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php.plist 2>/dev/null

% launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist 2>/dev/null

% launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist 2>/dev/null 

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容