Mac本地搭建服务器nginx+php+mysql

安装Homebrew

  • Homebrew官网获取安装指令,官网地址:https://brew.sh

brew安装php

#搜索PHP
brew search php
# 安装PHP(不指定PHP版本时,默认安装最新版本)
brew install [email protected]
# 安装完成后,根据提示配置环境变量
# 示例
# echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
# echo 'export PATH="/usr/local/opt/[email protected]/sbin:$PATH"' >> ~/.zshrc
# 刷新环境变量
source ~/.zshrc
# 查看版本信息
php -v
# 配置php-fpm.conf,不配置好像也没事
vim /usr/local/etc/php/7.4/php-fpm.conf
#去掉第17行和第24行前面的分号
17 ;pid = run/php-fpm.pid
24 ;error_log = log/php-fpm.log
# 启动|停止|重启
brew services start|stop|restart [email protected]
# 查看php-fpm是否启动成功
lsof -Pni4 | grep LISTEN | grep php

brew安装mysql

# 搜索mysql
brew search mysql
# 安装mysql(不指定mysql版本时,默认安装最新版本)
brew install [email protected]
# 启动|停止|重启 mysql服务
brew services start|stop|restart mysql
# 本地登录MySQL
mysql -u用户名 -p密码
# 修改密码
set password for 用户名@localhost = password(新密码);

brew安装nginx

brew install nginx
#查看nginx安装目录,根目录,配置文件目录
brew info nginx
# /opt/homebrew/Cellar/nginx/1.21.6_1
# Docroot is: /opt/homebrew/var/www
# /opt/homebrew/etc/nginx/servers/
# 启动|停止|重启 nginx
brew services start|stop|restart nginx
# 重启nginx,有时候brew restart不起作用,找到安装目录下的nginx启动文件
sudo /opt/homebrew/Cellar/nginx/1.21.6_1/bin/nginx -s reload
# nginx启动后默认访问地址
# http://localhost:8080

nginx配置

# 打开finder,command+shift+G,打开配置文件目录
# /opt/homebrew/etc/nginx/servers/
# 新建配置文件a.conf,右键打开方式-文本编辑
# 我的站点是用wordpress做的,自定义域名c.wordpress.cn
# 
server {
    listen       80;
    server_name   test.wordpress.cn;
    root   /opt/homebrew/var/www/wordpress/;
index  index.php index.html index.htm;

location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;
}
# 禁止某个目录访问
#location ^~ /wp-content/plugins/wp-file-manager
  # {
     #deny    all;
   #}
# Rewrite multisite '.../wp-.*' and '.../*.php'.
if (!-e $request_filename) {
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
    rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}

#proxy the php scripts to php-fpm
    location ~ \.php$ {
    # fastcgi配置
    include /opt/homebrew/etc/nginx/fastcgi.conf;
    # 指定是否传递4xx和5xx错误信息到客户端
    fastcgi_intercept_errors on;
    # 指定FastCGI服务器监听端口与地址,可以是本机或者其它
    fastcgi_pass   127.0.0.1:9000;
    }
}
# 编辑好后保存,重启nginx
# 如果重启失败,直接mac重新启动

本地host

$ vi /etc/hosts
# 添加一行
127.0.0.1 test.wordpress.cn
# 保存退出

然后就可以用test.wordpress.cn访问wordpress站点啦。

你可能感兴趣的:(Mac本地搭建服务器nginx+php+mysql)