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

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

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

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

3、安装Nginx

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

// 安装nginx
brew install nginx

检查是否安装成功 :

brew search nginx 

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

查看安装目录:

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) *  // 在这一行!!!!
  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

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

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

nginx 的操作命令:

// 启动nginx
sudo nginx

// 重新加载配置|重启|停止|退出 nginx
sudo nginx -s reload|reopen|stop|quit

// 测试配置是否有语法错误
nginx -t

配置 nginx

vim /usr/local/etc/nginx/nginx.conf
// 默认监听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;
        }
    }

添加多个配置文件

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

检查是否配置成功:http://127.0.0.1:8080/

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

4、安装Mysql

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

查看 Mysql 版本:

brew search mysql

安装 Mysql :

brew install mysql@5.7

操作命令:

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

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

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

遇到问题:

$ mysql -u root
ERROR 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

检查可用版本:

brew search php

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

由于已经安装过 php@7.1了,我们这次安装 php@7.2 版本:

brew install php@7.2

安装完成设置环境变量:

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

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

启动 php@7.2

brew services start php@7.2

配置 php-fpm :

$ 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

安装目录:

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:

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

安装扩展:

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

示例,安装 phalcon 扩展:

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

最后查看一下 php 版本:

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

6、写在后面:

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

#! /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

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

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

在命令行运行 launch 即可~~

点个赞再走吧~~

喜欢(2) 打赏

评论1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #0
    Cialis Vendita In Italia Contrassegno Viagra 50 Posologie Purchasing Progesterone Website With Free Shipping Cialis Discount Generic Fluoxetine Internet In Germany Metformin Without A Prescription
    KelSilt2020-01-19 17:01:18回复

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

小北在线

小北在线

  • 扫描二维码,微信联系 扫描二维码,微信联系