TMDBug

Code Change The World And Make It Better!

TMD.Bug

Mac下安装LNMP

由于用惯了Nginx,Mac下自带的是Apache,所以更换成自己熟悉的那套。

Mac下安装Nginx、MySQL、PHP。

此次安装用的是brew的方式,当前系统版本是MacOS 10.13。

什么是brew? 官方解释:macOS 缺失的软件包管理器。

brew官方地址(https://brew.sh/index_zh-cn.html)

Mac下默认没有brew,首先安装brew

官方安装命令:

安装完brew之后,安装Nginx

之后开始跑,到此界面

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
==> Summary
🍺  /usr/local/Cellar/nginx/1.12.2_1: 23 files, 1MB

Nginx安装完成,测试一下吧。

输出nginx版本:

nginx version: nginx/1.12.2

然后安装mysql

之后开始跑,跑到这里

We’ve installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
    mysql -uroot
To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don’t want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mysql/5.7.20: 324 files, 233.7MB

MySQL安装完成。

说 如果你去设置安全策略,运行mysql_secure_installation,MySQL只允许本地连接,去链接用:mysql -uroot 命令。

如果你要加入launchd,就是开机自启动,就要运行 brew services start mysql

或者 你想或许要后台开启服务 就要运行 msql.server start

运行 mysql_secure_installation 设置mysql的安全策略

之后显示:

Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
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:

这里说 mysql默认没有密码, 是否需要设置一个密码?输入: 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:

这里是说密码有三个级别

0 弱:大于等于八位数

1 中:大于等于八位数 必须有数字和字符串

2 强:大于等于八位 必须有数字特殊字符字符串 密码字典

这里 我选了 0

Please set the password for root here.
New password:

输入新密码

我输入的 rootadmin

Re-enter new password:

再次输入 rootadmin

Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :

这里说你的密码强度是50

然后 你要继续的话,就得输密码,选择 Y

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) :

这时 提示说,在MySQL中有一个默认的匿名用户,允许任何人登录MySQL而不必创建的用户帐户。这只是为了测试,并使安装变得更顺畅一点。在进入生产环境之前你应该把它们移走。

是不是要删除匿名用户?选则:Y

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) :
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) :
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应该只允许连接“localhost”。这确保了某人不能猜到来自网络的根密码。

禁止root远程登录?(按Y | 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) :

这里跳过了禁止远程root登录,然后说 默认情况下,MySQL附带一个名为“test”的数据库。任何人都可以访问。这也仅用于测试,在进入生产环境之前应该被移除。

删除测试数据库并访问它?(按Y | Y是的,有没有其他的钥匙):Y

 – Dropping test database…
Success.
 – Removing privileges on test database…
Success.

删除成功,这时提示

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) : Y

是否要刷新权限表,选 Y

Success.
All done!

刷新成功,MySQL安全策略配置完成。

接下来 安装PHP

其实Mac下是带PHP的,但是由于更方便的管理这几项,以及使用新的PHP版本,所以还是自己再装一次。装完后要把系统的PHP环境变量改成自己安装的php环境变量,装了这么多,有木有被吓到,咱们接着开始干。

brew没有默认的php包,需要先加入Homebrew官方的几个软件源

运行完这三个命令后,开始安装PHP

PHP如果采用默认配置安装,会编译mod_php模块并只运行在Apache环境下,为了使用Nginx,这里需要编译php-fpm并且禁用apache,主要通过参数–without-fpm –without-apache来实现。

这里可以参考(http://avnpc.com/pages/install-lnmp-on-osx

结果报错:

checking for krb5-config… /usr/bin/krb5-config
checking for DSA_get_default_method in -lssl… no
checking for X509_free in -lcrypto… yes
checking for RAND_egd… no
checking for pkg-config… no
checking for OpenSSL version… >= 0.9.6
checking for CRYPTO_free in -lcrypto… yes
checking for SSL_CTX_set_ssl_version in -lssl… yes
checking for PCRE library to use… bundled
checking whether to enable the SQLite3 extension… yes
checking bundled sqlite3 library… yes
checking for ZLIB support… yes
checking if the location of ZLIB install directory is defined… no
configure: error: Cannot find libz
READ THIS: https://docs.brew.sh/Troubleshooting.html
These open issues may also help:
Can’t install php56 –with-libmysql  🙁 https://github.com/Homebrew/homebrew-php/issues/4501
Installing php55 and php56 makes my mac terminal dead https://github.com/Homebrew/homebrew-php/issues/4602
php56-mongodb _clock_gettime error at runtime https://github.com/Homebrew/homebrew-php/issues/3737

百度了一下 configure: error: Cannot find libz

看了几个帖子都说 需要运行一下这个命令

安装xcode的Command Line Tools。

于是就有一个小窗口开始安装Command Line Tools,安装完成后 继续安装PHP

漫长的等待,随着机器CPU风扇的呼呼声,还以为电脑会炸掉,结果 显示安装成功了。

The php.ini file can be found in:
    /usr/local/etc/php/5.6/php.ini
✩✩✩✩ Extensions ✩✩✩✩
If you are having issues with custom extension compiling, ensure that you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:
      PATH=”/usr/local/bin:$PATH”
PHP56 Extensions will always be compiled against this PHP. Please install them using –without-homebrew-php to enable compiling against system PHP.
✩✩✩✩ PHP CLI ✩✩✩✩
If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc, ~/.zshrc, ~/.profile or your shell’s equivalent configuration file:
  export PATH=”$(brew –prefix homebrew/php/php56)/bin:$PATH”
✩✩✩✩ FPM ✩✩✩✩
To launch php-fpm on startup:
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
The control script is located at /usr/local/opt/php56/sbin/php56-fpm
OS X 10.8 and newer come with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH:
  PATH=”/usr/local/sbin:$PATH”
You may also need to edit the plist to use the correct “UserName”.
Please note that the plist was called ‘homebrew-php.josegonzalez.php56.plist’ in old versions of this formula.
With the release of macOS Sierra the Apache module is now not built by default. If you want to build it on your system you have to install php with the –with-httpd option. See  brew options php56 for more details.
To have launchd start homebrew/php/php56 now and restart at login:
  brew services start homebrew/php/php56
==> Summary
🍺  /usr/local/Cellar/php56/5.6.32_8: 331 files, 42.7MB, built in 4 minutes 4 seconds

此时PHP安装成功。

由于OSX已经自带了PHP环境,因此需要修改系统路径,优先运行brew安装的版本,在~/.bashrc里加入

如果要安装新的php扩展,可以直接安装而不用每次重新编译php,所有的扩展可以通过

我下面只安装 PHP的redis 和 memcache和mongo扩展(如果要安装程序 可以看:http://avnpc.com/pages/install-lnmp-on-osx)

PHP-FPM的加载与启动

安装完毕后可以通过以下指令启动和停止php-fpm

这时 试着去配置一个PHP页面

Nginx 的网站根目录是:

/usr/local/var/www/

可以弄一个PHP页面 打印phpinfo();

比如新建一个index.php, 然后在页面中输入:echo phpinfo();

然后配置nginx.conf

Nginx配置文件目录是:

/usr/local/etc/nginx/

编辑nginx.conf

将加载php的配置打开

location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

然后 重启nginx

此时访问http://localhost/index.php

就可以看到 index.php页面了。

评论回复

  1. 回复 TMDBug

    Mac系统升级后 好像把PHP也升级到了7.1,这时候启动我们Homebrew安装的PHP,可能会报错:
    ERROR: failed to open configuration file ‘/private/etc/php-fpm.conf’: No such file or directory
    ERROR: failed to load configuration file ‘/private/etc/php-fpm.conf’
    ERROR: FPM initialization failed

    ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
    ERROR: failed to post process the configuration
    ERROR: FPM initialization failed
    这时php -v,php-fpm -v,查看是否是自己homebrew中的版本,如果不是则需要重新指定路径到自己安装的php路径
    这时需要在~/.bash_profile中添加路径,在命令行下运行以下几行:

    echo ‘export PATH=”$(brew –prefix php56)/bin:$PATH”‘ >> ~/.bash_profile #for php
    echo ‘export PATH=”$(brew –prefix php56)/sbin:$PATH”‘ >> ~/.bash_profile #for php-fpm
    echo ‘export PATH=”/usr/local/bin:/usr/local/sbib:$PATH”‘ >> ~/.bash_profile #for other brew install soft
    // 生效
    source ~/.bash_profile

2 + 8 =

回到顶部