• 欢迎访问南思工作室官方站点.
  • 文章内容如有失效请文章下留言,我们看到后会第一时间处理。
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏南思工作室吧。
  • 图片服务器和主服务器都挂了CDN,如有异常,请留言,我们会尽快处理。

mac系统搭建常用网站环境教程

前端 nansi 7个月前 (05-04) 261次浏览 0个评论
文章目录[隐藏]

mac 系统搭建常用网站环境教程

环境介绍

macOS Series 10.12.2

Homebrew 1.1.7

1、Homebrew

习惯了 Ubuntu 的小伙伴,可能热衷于 apt-get 命令行,在这里 homebrew 有着相类似的功能,是 mac 上一个套间管理器。

我们来先安装 homebrew,将下面这行代码粘贴到命令行即可进行安装:

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

更新 brew

brew update

2、Nginx

2.1 安装 nginx

Nginx 作为一个反向代理服务器,其在处理高并发情况下有着 Apache 不可比拟的优势。我们还是来试试 nginx 吧。
Mac 新版本里面带有 Apache 和 php,但是他们不是特别优雅,

在安装 nginx 之前我们可以先关闭 apache:

sudo apachectl stop  #关闭 apache,如果事先没开启过,可以忽略报错信息

如果 apache 已经加入了 launchctl,使用下面的命令来关闭:

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

使用下面两个命令来安装 nginx(当前版本 1.10.2):

    brew search nginx
    brew install nginx

运行 nginx:

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

用法详解:

    nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
    选项列表
    -?,-h           : 打开帮助信息
    -v              : 显示版本信息并退出
    -V              : 显示版本和配置选项信息,然后退出
    -t              : 检测配置文件是否有语法错误,然后退出
    -q              : 在检测配置文件期间屏蔽非错误信息
    -s signal       : 给一个 nginx 主进程发送信号:stop(停止), quit(退出), reopen(重启), reload(重新加载配置文件)
    -p prefix       : 设置前缀路径(默认是:/usr/local/Cellar/nginx/1.2.6/)
    -c filename     : 设置配置文件(默认是:/usr/local/etc/nginx/nginx.conf)
    -g directives   : 设置配置文件外的全局指令

安装完以后,可以在终端输出的信息里看到一些配置路径:
/usr/local/etc/nginx/nginx.conf (配置文件路径)
/usr/local/var/www (服务器默认路径)
/usr/local/Cellar/nginx/1.10.2 (貌似是安装路径)

2.2 修改配置

nginx 安装完成之后,默认是 8080 端口,在浏览器打开会看到一个欢迎界面。
可以修改端口为 80:

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

给予管理员权限

    sudo chown root:wheel /usr/local/opt/nginx/bin/nginx
    sudo chmod u+s /usr/local/opt/nginx/bin/nginx

设置为开机启动:

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

配置 Nginx 支持 PHP-FPM

    sudo vim /usr/local/etc/nginx/nginx.conf    
     
    server {
            listen       80;
            server_name  localhost;
            #charset koi8-r;
            #access_log  logs/host.access.log  main;
     
           # 添加默认首页 php
           index  index.php index.html index.htm;
            location / {
                root   /usr/local/var/www;
                index  index.php  index.html index.htm;
                autoindex on; 
                autoindex_exact_size on;
            }
          # 取消以下内容的注释,并做修改
          location ~ \.php$ {
                 root           html;
                 fastcgi_pass   127.0.0.1:9999;    这里的端口要和 php-fpm 的端口保持一致
                 fastcgi_index  index.php;
                 fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                 include        fastcgi_params;
            }
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            location ~ /\.ht {
                deny  all;
            }
    }

3、mysql

安装 mysql(mysql 没有 my.conf 文件)

brew install mysql

加入 launchctl 开机启动控制

    mkdir -p ~/Library/LaunchAgents/
    cp /usr/local/opt/mysql/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    #取消启动
    #launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

如果出现 无法通过 mysql.sock 连接 mysql 的错误,很有可能你没有启动 mysql,使用下面命令:

mysql.server start

安装完成之后,mysql5.7 的一个新特性就是 root 初始化密码不为空!所以我们要改密码~~

3.1 第一种情况,我们初始化密码:

3.1.1 首先要开启 mysql,然后运行 mysql_secure_installation 初始化密码:

/usr/local/opt/mysql/bin/mysql_secure_installation

运行之后,会有一些选项:

  1. 输入 root 的密码
  2. 会让你选择 root 密码的保密级别(看个人了,不想设置可以输入 n)
  3. 是否删除匿名用户
  4. 是否允许远程登录
  5. 是否删除 test 数据库
  6. 是否刷新授权表使修改生效

代码如下:

    # zhaoshuai @ zhaoshuaideMacBook-Air in /usr/local/opt/mysql/bin [9:57:09] C:1
    $ mysql_secure_installation 
     
    Securing the MySQL server deployment.
     
    Enter password for user root: ********(在这里输入你的密码)
     
    VALIDATE PASSWORD PLUGIN can be used to test passwords
    and improve security. It checks the strength of password
    and allows the users to set only those passwords which are
    secure enough. Would you like to setup VALIDATE PASSWORD plugin?
     
    Press y|Y for Yes, any other key for No: y
     
    There are three levels of password validation policy:
     
    LOW    Length >= 8
    MEDIUM Length >= 8, numeric, mixed case, and special characters
    STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
     
    Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
    Using existing password for root.
     
    Estimated strength of the password: 25 
    Change the password for root ? ((Press y|Y for Yes, any other key for No) : 
     
     ... skipping.
    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)
     
     ... skipping.
     
     
    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) : (是否禁止 root 远程登录,
    根据自己的需求选择 Y/n 并回车,建议禁止)
     
     ... skipping.
    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) : (是否删除 test 数据库 y/n)
    )
     
     ... skipping.
    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) : (是否刷新授权表使修改生效,可以直接回车)
     
     ... skipping.
    All done! 

登录 mysql:

mysql -u root -p 

3.2 第二种情况,忘记了 root 密码

3.2.1 打开终端,这次首先我们要关掉 mysql:

sudo mysql.server stop 

或者,我们可以在进程中杀死,

    ps -A | grep mysqld mysqld_safe
    kill -9  ****

3.2.2 然后我们进入到 /usr/local/opt/mysql/bin ,输入:

mysqld_safe --skip-grant-tables

会出现:

    2017-04-13T01:55:47.6NZ mysqld_safe Logging to '/usr/local/var/mysql/zhaoshuaideMacBook-Air.local.err'.
    2017-04-13T01:55:47.6NZ mysqld_safe Logging to '/usr/local/var/mysql/zhaoshuaideMacBook-Air.local.err'.
    2017-04-13T01:55:47.6NZ mysqld_safe A mysqld process already exists

3.2.3 然后我们输入:

mysql -u root -p

这时候会出现 mysql> 了,输入

use mysql

3.2.4 最后输入:

update user set authentication_string=password('你需要设置的密码') where user='root';

4、php

在 brew 没有 php 的包,所以我们需要配置一下:

    brew tap homebrew/dupes
    brew tap homebrew/versions  
    brew tap homebrew/homebrew-php

4.1 安装 php7.0:

brew install php70 --with-imap --with-tidy --with-debug --with-mysql --with-fpm  --without-apache

加入 launchctl 启动控制:

    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/opt/php70/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist

4.2 配置文件

可能会遇到的问题:

    Error: Cannot install homebrew/php/php70 because conflicting formulae are installed.
    php55: because different php versions install the same binaries.
    Please `brew unlink php55` before continuing.
    Unlinking removes a formula's symlinks from /usr/local. You can
    link the formula again after the install finishes. You can --force this
    install, but the build may fail or cause obscure side-effects in the
    resulting software.

解决方法(根据提示):

    根据提示执行: brew unlink php55
    继续安装: brew install php70

将 php55 替换成 php70:

vim ~/.zshrc

export PATH="$(brew --prefix php55)/bin:$PATH"

替换成:

export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"

    alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
    alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php5.plist"
    alias php-fpm.restart='php-fpm.stop && php-fpm.start'

替换成:

    alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist"
    alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist"
    alias php-fpm.restart='php-fpm.stop && php-fpm.start'

完成之后重新加载配置文件:

source ~./zshrc

4.3 验证版本:

(1)php 的版本

    zhaoshuaideMacBook-Air:nginx zhaoshuai$ php -v
    PHP 7.0.14 (cli) (built: Dec  9 2016 07:34:25) ( NTS )
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

(2)php-fpm 版本

    PHP 7.0.14 (fpm-fcgi) (built: Dec  9 2016 07:34:31)
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

4.4 启动 php-fpm

sudo php-fpm start

4.5 关闭 php-fpm

sudo pkill -INT -o php-fpm  // 这条命令简单粗暴!直接有效

4.6 安装扩展

redis 扩展

    brew install php70-redis
    配置文件
    /usr/local/etc/php/7.0/conf.d/ext-redis.ini

memcached 扩展

    brew install php70-memcached
    配置文件
    /usr/local/etc/php/7.0/conf.d/ext-memcached.ini

swoole 扩展

    brew install php70-swoole
    配置文件
    /usr/local/etc/php/7.0/conf.d/ext-swoole.ini

总结

mac 上不比 Ubuntu,比较麻烦,希望在配置环境过程多去看一些资料,遇到问题多去 google,会解决的。
啊哈,总之就是这样~
mac 体验很好~~~

感谢分享:

homebrew 安装 mysql  :http://blog.neten.de/posts/2014/01/27/install-mysql-using-homebrew/

Mac 下配置 Nginx :http://www.cnblogs.com/cheemon/p/5638394.html


南思工作室 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mac 系统搭建常用网站环境教程
免责声明:本站一切资源仅用作交流学习,请勿用作商业或违法行为!如造成任何后果,本站概不负责!
喜欢 (8)
关于作者:
南思工作室管理员
发表我的评论
取消评论
表情 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址