创建本地开发环境

本地开发环境需要安装

  • nginx
  • php (version 7+)
  • MySQL 5.7+
  • Redis

以下是几个操作系统的相关引导: macOS

macOS

cnblogs-Mac 配置nginx、php运行环境

安装nginx

brew install nginx

如果你没有安装brew,请参考https://blog.csdn.net/shuai9201/article/details/122398111

将nginx加入到PATH中

echo 'export PATH=/usr/local/opt/nginx/bin:$PATH' >> ~/.zshrc
source ~/.zshrc
nginx

To start nginx now and restart at login:

brew services start nginx
nginx.conf

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

通过浏览器访问 http://localhost:8080 可以看到nginx欢迎页

如果需要修改配置,如www目录,可以参考 https://segmentfault.com/a/1190000019294598

安装php7+ 或者php8

通过指令查看电脑是否已经安装php

php -v

如果有显示版本信息,则已经安装可以跳过

当前的开发版本使用8.2,其他版本请自行酌情选择,不支持php7以下版本

brew install php

添加PHP网站的nginx配置

{VAR} 代指一个你自行定义的变量

mkdir -p /usr/local/etc/nginx/servers
vi /usr/local/etc/nginx/servers/{MYSITE}.conf

参考以下内容添加PHP网站的相关配置

server {
        listen       10001;      # 端口
        server_name  localhost;  # 域名
        root   "/Users/mac/dev/web/{MYSITE}"; # 项目地址 本地绝对路径
        location / {

            # 伪静态
            # 图片裁切服务 Image Crop Service
            location ~ \.(jpg|jpeg|png|webp)!(.*)$ {

            rewrite ^(.*)\.(jpg|jpeg|png|webp)!(.*)$ $1/crop/$3.$2;

            if ( -e $request_filename ) {
            break;
            }

            rewrite ^(.*)/crop/(.*)\.(.*)$ $1&ext=.$3&file=$1.$3&method=$2;
            rewrite ^(/website.*)*$ /api/common/imageCrop$1;
            }

            location /favicon.ico {break;}
            location /website/static { break; }
            location /website/theme { break; }
            location /manager/theme { break; }
            location /api {
            rewrite ^(/api.*)*$ /api/?path=$1 break;
            }
            location /tester {
            rewrite ^(/tester.*)*$ /tester/?path=$1 break;
            }
            location /manager {
            rewrite ^(/manager.*)*$ /manager/?path=$1 break;
            }
            location /website {
            rewrite ^(/website.*)*$ /website/?path=$1 break;
            }
            location / {
            rewrite ^(/.*)*$ /website/?path=$1 break;
            }

            # 伪静态结束                    

            index index.php index.html error/index.html;
            error_page 400 /error/400.html;
            error_page 403 /error/403.html;
            error_page 404 /error/404.html;
            error_page 500 /error/500.html;
            error_page 501 /error/501.html;
            error_page 502 /error/502.html;
            error_page 503 /error/503.html;
            error_page 504 /error/504.html;
            error_page 505 /error/505.html;
            error_page 506 /error/506.html;
            error_page 507 /error/507.html;
            error_page 509 /error/509.html;
            error_page 510 /error/510.html;
            include /Users/mac/dev/web/{MYSITE}/nginx.htaccess;
            autoindex  off;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

通过重载配置将当前网站激活

nginx -s reload

此时已经可以通过 http://localhost:10001 访问网站

  • 伪静态的部分为AppSite开发必备的规则

MySQL

brew install mysql@5.7
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

如果出现 error 2002

brew services start mysql@5.7

使用mysql -uroot 可以进入mysql

在系统设置中找到,隐私与安全性 允许启动系统扩展 重启

Redis

安装Redis

brew install redis
brew services start redis

安装php-redis扩展

cd /usr/local/Cellar/php/8.3.1
pecl install igbinary

Error Php Write Access Denied

php通常使用www用户进行文件访问,这在mac上往往造成权限问题

找到php-fpm.conf 或者pfp-fpm.d下的conf文件

vi /usr/local/etc/php/8.3/php-fpm.conf
vi /usr/local/etc/php/8.3/php-fpm.d/www.conf

将其中找到的 user, group

user = _www
group = _www

改成和你网站目录一样的用户名和用户组,如

user = mac
group = staff

重启

Windows

Ubuntu

results matching ""

    No results matching ""