Mac下配置PHP+Nginx+Redis环境

Mac下是自己安装了Apache+PHP的,但是Mac OS版本每次变更后都需要重新配置下环境,参考:Mac升级系统macOS Sierra后PHP不编译

1.安装Homebrew

Mac没有预装 Homebrew,安装Homebrew 在终端执行:

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

如果提示安装xcode命令行工具,在App Store中下载最新Xcode,然后根据提示安装xcode命令行工具,终端输入:

xcode-select --install

成功后继续安装Homebrew

2.安装Nginx

成功安装brew后,在终端执行:

brew install nginx

安装成功后,查看nginx信息,在终端执行:

brew info nginx

以下为提示信息:
nginx: stable 1.12.2 (bottled), devel 1.13.6, HEAD
...
...
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

Mac是自带Apache的,和Nginx会冲突吗?答案是不会,因为默认端口不同,Apache默认80端口,Nginx默认8080端口
ok,终端执行:

sudo nginx

浏览器访问:localhost:8080

Nginx相关命令

//启动 nginx服务
sudo nginx

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

//测试配置是否有语法错误
nginx -t
3.安装PHP56

macOSX自带PHP,会跟随系统升级,目前macOS v10.13.x 跟随PHP已经是PHP7.0,所以我们还是需要安装PHP56,执行:

brew install php56 --build-from-source

安装成功,提示:

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

配置php.ini

sudo vim /usr/local/etc/php/5.6/php.ini

设置开机启动php-fpm:

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

php56-fpm 启动/停止… :

/usr/local/opt/php56/sbin/php56-fpm {start|stop|force-quit|restart|reload|status}

现在我们先执行php56 -v来查看版本要怎么办呢?执行:

sudo ln -s /usr/local/Cellar/php56/5.6.32_8/bin/php /usr/local/bin/php56

设置一下符号链接(相当于windows快捷方式) ,现在php56直接指向了/usr/local/Cellar/php56/5.6.32_8/bin/php 【参考链接】Linux环境变量和符号链接
设置完成,查看PHP版本:

php56 -v
4. 安装Redis

终端执行:

brew search redis

查询结果:
hiredis                       homebrew/php/php56-redis      redis                         redis@3.2
homebrew/php/php53-redis      homebrew/php/php70-redis      redis-leveldb
homebrew/php/php54-redis      homebrew/php/php71-redis      redis@2.6
homebrew/php/php55-redis      homebrew/php/php72-redis      redis@2.8

然后(注意区分下php的redis扩展)

brew install redis
To have launchd start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  redis-server /usr/local/etc/redis.conf

开启redis:

redis-server /usr/local/etc/redis.conf

Nginx实战基础篇五 如何让用户通过用户名密码认证访问我们的web站点

Nginx实战基础篇五 如何让用户通过用户名密码认证访问我们的web站点

有时我们会有这么一种需求,就是你的网站并不想提供一个公共的访问或者某些页面不希望公开,我们希望的是某些特定的客户端可以访问。那么我们可以在访问时要求进行身份认证,就如给你自己的家门加一把锁,以拒绝那些不速之客。

实验环境:

现在公司开通了一个内部使用网站为http://www.luzirq.com,要求所有人查看网页信息之前都必须要输入内定的用户名及密码方可浏览网页

http://www.luzirq.com进行测试。

http://www.luzirq.com 192.168.100.107

DNS1 192.168.100.102

一、查看当前系统参数

  1. [root@rhel6u3-2 ~]# uname –r  //查看系统内核版本号
  2. 2.6.32-279.el6.i686
  3. [root@rhel6u3-2 ~]# cat /etc/redhat-release //查看系统版本号
  4. Red Hat Enterprise Linux Server release 6.3 (Santiago)

 

二、编辑虚拟主机配置文件

 

  1. [root@rhel6u3-7 ~]# vim /usr/local/nginx/conf/nginx.conf //主配置文件中http字段中添加以下语句,将虚拟主机的配置文件指向www.luzirq.com
  2. include /usr/local/nginx/server/www.luzirq.com;
  3. [root@rhel6u3-7 ~]# vim /usr/local/nginx/server/www.luzirq.com
  4. server {
  5.      listen       80;  //监听端口为80
  6.      server_name  www.luzirq.com;  //虚拟主机网址
  7.       location / {
  8.             root   sites/www1;  //虚拟主机网站根目录
  9.             index  index.html index.htm;  //虚拟主机首页
  10.             auth_basic “secret”;  //虚拟主机认证命名
  11.             auth_basic_user_file /usr/local/nginx/passwd.db; //虚拟主机用户名密码认证数据库
  12.         }
  13.         location /status {
  14.                 stub_status on;  //开启网站监控状态
  15.                 access_log /usr/local/nginx/logs/www.luzirq.com; //监控日志
  16.                 auth_basic “NginxStatus”; }
  17.     }

 

三、通过htpasswd命令生成用户名及对应密码数据库文件。

 

  1. [root@rhel6u3-7 server]# htpasswd -c /usr/local/nginx/passwd.db username  //创建认证信息,username为认证用户名
  2. New password: *******  //输入认证密码
  3. Re-type new password: ********  //再次输入认证密码
  4. Adding password for user username
  5. [root@rhel6u3-7 server]#
  6. [root@rhel6u3-7 ~]# chmod 400 /usr/local/nginx/passwd.db  //修改网站认证数据库权限
  7. [root@rhel6u3-7 ~]# chown nginx.  /usr/local/nginx/passwd.db  //修改网站认证数据库属主和属组
  8. [root@rhel6u3-7 ~]# cat /usr/local/nginx/passwd.db  //可以看到通过htpasswd生成的密码为加密格式
  9. username:8eZAz7BqcrXmY
  10. [root@rhel6u3-7 ~]#

 

四、平滑重启nginx服务

 

  1. [root@rhel6u3-7 ~]# /etc/rc.d/init.d/nginx reload  //平滑重启nginx服务
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. Reloading nginx:                                           [  OK  ]

 

五、DNS服务器上添加www1 A记录

 

  1. www    A   192.168.100.107

 

最后至为关键的位置(独门绝技,此方法在网上是无法搜到的):对于nginx环境的linux是不支持htpasswd命令的,它是属于apache下的,需要安装httpd才行,但是在nginx环境下安装,难免会出现冲突,安装nginx和apache非常复杂,没有lnmpa是不很难配置成功了。网上不管怎么搜,都是搜不到解决方案的,武汉网站建设在这里献上亲自经历的方法:在本地的apache下用htpasswd生成密码串,然后从本地生成之后复制到nginx上。或者自己写一个php代码,武汉网站建设也献上下载。

nginx环境htpasswd下载