前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【LNMP】Mac +Nginx + PHP7.* + Mysql5.7

【LNMP】Mac +Nginx + PHP7.* + Mysql5.7

作者头像
redszhao
发布2021-08-09 15:21:42
7230
发布2021-08-09 15:21:42
举报
文章被收录于专栏:北先生北先生

1、写在前面

自己动手搭配一套LNMP,想装什么环境,随便搞~

环境配置:

  • MacBook Pro Moyave
  • PHP 7.0、PHP 7.1、PHP 7.2
  • Mysql 5.7
  • Nginx 1.15.8

2、安装HomeBrew

HomeBrew 是 Mac 系统上一个包管理工具,官网链接

【LNMP】Mac +Nginx + PHP7.* + Mysql5.7
【LNMP】Mac +Nginx + PHP7.* + Mysql5.7

直接使用 ruby 命令运行完事:

Default

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

1

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

3、安装Nginx

【LNMP】Mac +Nginx + PHP7.* + Mysql5.7
【LNMP】Mac +Nginx + PHP7.* + Mysql5.7

Default

// 安装nginx brew install nginx

12

// 安装nginxbrew install nginx

检查是否安装成功 :

Shell

brew search nginx // 显示对号证明安装成功 ==> Formulae nginx ✔ homebrew/linuxbrew-core/nginx

12345

brew search nginx  // 显示对号证明安装成功==> Formulaenginx ✔                                                                                                                 homebrew/linuxbrew-core/nginx

查看安装目录:

Default

brew info nginx // 下面是安装目录 nginx: stable 1.15.12 (bottled), HEAD HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server https://nginx.org/ /usr/local/Cellar/nginx/1.15.8 (23 files, 1.4MB) * <strong> // 在这一行!!!!</strong> Poured from bottle on 2019-01-21 at 20:30:20 From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/nginx.rb ==> Dependencies Required: openssl ✔, pcre ✔ ==> Options --HEAD Install HEAD version ==> Caveats Docroot is: /usr/local/var/www 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/. To have launchd 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: nginx

1234567891011121314151617181920212223242526

brew info nginx // 下面是安装目录nginx: stable 1.15.12 (bottled), HEADHTTP(S) server and reverse proxy, and IMAP/POP3 proxy serverhttps://nginx.org//usr/local/Cellar/nginx/1.15.8 (23 files, 1.4MB) * <strong> // 在这一行!!!!</strong>  Poured from bottle on 2019-01-21 at 20:30:20From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/nginx.rb==> DependenciesRequired: openssl ✔, pcre ✔==> Options--HEAD Install HEAD version==> CaveatsDocroot is: /usr/local/var/www The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so thatnginx can run without sudo. nginx will load all files in /usr/local/etc/nginx/servers/. To have launchd start nginx now and restart at login:  brew services start nginxOr, if you don't want/need a background service you can just run:  nginx

添加 nginx 为开机启动项(使用上面的安装目录):

Default

mkdir -p ~/Library/LaunchAgents cp /usr/local/Cellar/nginx/1.15.8/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

123

mkdir -p ~/Library/LaunchAgentscp /usr/local/Cellar/nginx/1.15.8/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

nginx 的操作命令:

Default

// 启动nginx sudo nginx // 重新加载配置|重启|停止|退出 nginx sudo nginx -s reload|reopen|stop|quit // 测试配置是否有语法错误 nginx -t

12345678

// 启动nginxsudo nginx // 重新加载配置|重启|停止|退出 nginxsudo nginx -s reload|reopen|stop|quit // 测试配置是否有语法错误nginx -t

配置 nginx

Shell

vim /usr/local/etc/nginx/nginx.conf

1

vim /usr/local/etc/nginx/nginx.conf

Default

// 默认监听8080端口 server { listen 80; server_name localhost; # 设定网站根目录 root /Users/wanghuiyu/PhpstormProjects/stock/public; # 网站默认首页 index index.php index.html index.htm; #charset koi8-r; #access_log logs/host.access.log main; location / { # 修改为 Laravel 转发规则,否则PHP无法获取$_GET信息,提示404错误 try_files $uri $uri/ /index.php?$query_string; } #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$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_intercept_errors on; fastcgi_pass 127.0.0.1:9001; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /Users/wanghuiyu/PhpstormProjects/stock/public$fastcgi_script_name; include /usr/local/etc/nginx/fastcgi_params; } }

123456789101112131415161718192021222324252627282930313233343536373839

// 默认监听8080端口server {        listen       80;        server_name  localhost;                # 设定网站根目录        root /Users/wanghuiyu/PhpstormProjects/stock/public;        # 网站默认首页        index index.php index.html index.htm;         #charset koi8-r;         #access_log  logs/host.access.log  main;         location / {            # 修改为 Laravel 转发规则,否则PHP无法获取$_GET信息,提示404错误            try_files $uri $uri/ /index.php?$query_string;        }         #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$ {            fastcgi_split_path_info ^(.+\.php)(/.+)$;            fastcgi_intercept_errors on;            fastcgi_pass   127.0.0.1:9001;            fastcgi_index  index.php;            fastcgi_param  SCRIPT_FILENAME  /Users/wanghuiyu/PhpstormProjects/stock/public$fastcgi_script_name;            include        /usr/local/etc/nginx/fastcgi_params;        }    }

添加多个配置文件

Default

cd /usr/local/etc/nginx/servers vim zhaoshuai.me.conf

12

cd /usr/local/etc/nginx/serversvim zhaoshuai.me.conf

检查是否配置成功:http://127.0.0.1:8080/
【LNMP】Mac +Nginx + PHP7.* + Mysql5.7
【LNMP】Mac +Nginx + PHP7.* + Mysql5.7

4、安装Mysql

【LNMP】Mac +Nginx + PHP7.* + Mysql5.7
【LNMP】Mac +Nginx + PHP7.* + Mysql5.7
查看 Mysql 版本:

Default

brew search mysql

1

brew search mysql

安装 Mysql :

Shell

brew install mysql@5.7

1

brew install mysql@5.7

操作命令:

Default

// 停止、启动、重启 mysql.server stop mysql.server start mysql.server restart

1234

// 停止、启动、重启mysql.server stopmysql.server startmysql.server restart

连接数据库(默认密码为空):

Default

mysql -u root mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | sys | | ziroom | +--------------------+ 6 rows in set (0.00 sec)

12345678910111213

mysql -u rootmysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || school             || sys                || ziroom             |+--------------------+6 rows in set (0.00 sec)

遇到问题:

Default

$ mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

12

$ mysql -u rootERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

有可能是 mysql 没有启动,运行 mysql.server start

5、安装PHP

【LNMP】Mac +Nginx + PHP7.* + Mysql5.7
【LNMP】Mac +Nginx + PHP7.* + Mysql5.7
检查可用版本:

Shell

brew search php

1

brew search php

【LNMP】Mac +Nginx + PHP7.* + Mysql5.7
【LNMP】Mac +Nginx + PHP7.* + Mysql5.7
由于已经安装过 php@7.1了,我们这次安装 php@7.2 版本:

Default

brew install php@7.2

1

brew install php@7.2

安装完成设置环境变量:
【LNMP】Mac +Nginx + PHP7.* + Mysql5.7
【LNMP】Mac +Nginx + PHP7.* + Mysql5.7

Shell

echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc

12

echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.zshrcecho 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc

启动 php@7.2

Shell

brew services start php@7.2

1

brew services start php@7.2

配置 php-fpm :

Default

$ which php /usr/local/opt/php@7.1/bin/php // phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块 $ which phpize /usr/local/opt/php@7.1/bin/phpize // 指定php的配置 $ which php-config /usr/local/opt/php@7.1/bin/php-config

12345678910

$ which php/usr/local/opt/php@7.1/bin/php // phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块$ which phpize/usr/local/opt/php@7.1/bin/phpize // 指定php的配置$ which php-config/usr/local/opt/php@7.1/bin/php-config

安装目录:

Default

cd /usr/local/etc/php/7.1 // php.ini vim /usr/local/etc/php/7.1/php.ini // php-fpm 配置文件 vim /usr/local/etc/php/7.1/php-fpm.d/www.conf

1234567

cd /usr/local/etc/php/7.1 // php.ini vim /usr/local/etc/php/7.1/php.ini // php-fpm 配置文件vim /usr/local/etc/php/7.1/php-fpm.d/www.conf

启动 php-fpm 7.1:

Default

sudo -S /usr/local/opt/php@7.1/sbin/php-fpm

1

sudo -S /usr/local/opt/php@7.1/sbin/php-fpm

安装扩展:
  • pecl (容易失败)
  • 下载扩展包,自行编译(推荐)

示例,安装 phalcon 扩展:

Default

git clone git://github.com/phalcon/cphalcon.git cd cphalcon/ git checkout v3.0.3 cd  build/php7/64bits which phpize(查看phpize地址) /usr/local/opt/php@7.0/bin/phpize sudo make #中间会出现 warning 忽略 需要等待一会 sudo make install

12345678

git clone git://github.com/phalcon/cphalcon.gitcd cphalcon/git checkout v3.0.3cd  build/php7/64bitswhich phpize(查看phpize地址)/usr/local/opt/php@7.0/bin/phpizesudo make #中间会出现 warning 忽略  需要等待一会sudo make install

最后查看一下 php 版本:
【LNMP】Mac +Nginx + PHP7.* + Mysql5.7
【LNMP】Mac +Nginx + PHP7.* + Mysql5.7

6、写在后面:

如果你不想要开机就启动 “PHP + Mysql + Nginx”, 推荐一种一键启动脚本:

Default

#! /bin/zsh password={你的电脑密码} # mysql mysql.server start # nginx echo $password | sudo -S nginx # php-fpm echo $password | sudo -S /usr/local/opt/php@7.1/sbin/php-fpm

12345678

#! /bin/zshpassword={你的电脑密码}# mysqlmysql.server start# nginx echo $password | sudo -S nginx# php-fpmecho $password | sudo -S /usr/local/opt/php@7.1/sbin/php-fpm

我们可以把这个脚本放在用户目录的 Scripts 下面,然后设置一下 快捷键

Default

vim ~/.zshrc // 加入下面这行 alias launch="~/Scripts/AutoLaunch.sh"

123

vim ~/.zshrc// 加入下面这行alias launch="~/Scripts/AutoLaunch.sh"

在命令行运行 launch 即可~~

点个赞再走吧~~

喜欢(1) 打赏

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年5月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、写在前面
    • 2、安装HomeBrew
      • 3、安装Nginx
        • 4、安装Mysql
          • 5、安装PHP
            • 6、写在后面:
            相关产品与服务
            云数据库 SQL Server
            腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档