盒子
盒子
Posts List
  1. 一.前言
  2. 二.一样的套路 homebrew
  3. 三.php
    1. 1.安装php
    2. 2.启动与配置php
      1. 启动php-fpm
      2. 设置开启启动
      3. 配置php-fpm.conf
    3. 四.nginx
    4. 1.安装nginx
    5. 2.配置nginx
      1. 设置监听80端口
      2. 设置开机启动
      3. nginx.conf配置
    6. 3.启动nginx
    7. nginx 403 permission denied 问题
  4. 五.mysql
    1. 安装mysql
    2. 设置mysql为自启动服务
    3. 设置root 密码
    4. 忘记root密码重新设置
    5. 格式化查询输出
  5. 六.memcached
    1. 安装memcached
    2. 启动memcached
    3. 停止memcached
    4. 测试memcached
    5. 关于memcache 和 memcached

mac安装配置lnmp

一.前言

终于决定用mac book 作为工作用本,这样就需要把之前的工作环境移植到现在的本上。首要的就是lnmp:
lnmp = linux(mac osx) + nginx + mysql + php

注意:在安装之前,如果已经安装过xcode,那电脑上已经有了php 和 mysql模块了。

二.一样的套路 homebrew

homebrew是mac上的包(组件)依赖管理器,类似于:

nodejs ==> npm

python ==> pip

php ==> composer

android ==> maven/jcenter

安装homebrew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/php

这样我们就可以简单的使用
brew install xxx 来安装需要的包

三.php

1.安装php

brew install php56 \
--without-snmp \
--without-apache \
--with-debug \
--with-fpm \
--with-intl \
--with-homebrew-curl \
--with-homebrew-libxslt \
--with-homebrew-openssl \
--with-imap \
--with-mysql \
--with-tidy

2.启动与配置php

启动php-fpm

export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
/usr/local/opt/php56/sbin/php-fpm --fpm-config /usr/local/etc/php/5.6/php-fpm.conf &

php-fpm 是php模块对外通信的接口,如与nginx通信。它实现了http模块与php模块的解耦和。

设置开启启动

ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

配置php-fpm.conf

php-fpm.conf 在启动的时候可以使用–fpm-config指定配置地址

/usr/local/opt/php56/sbin/php-fpm --fpm-config /usr/local/etc/php/5.6/php-fpm.conf &

最后,可以通过

killall php-fpm

来杀死所有php-fpm进程。

检测php-fpm是否启动成功:

ps aux | grep php-fpm

四.nginx

1.安装nginx

brew install nginx

2.配置nginx

设置监听80端口

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

设置开机启动

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

nginx.conf配置

mkdir -p /usr/local/var/logs/nginx
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl
vim /usr/local/etc/nginx/nginx.conf

修改nginx配置如下:

#user nobody;
worker_processes 1;
error_log /usr/local/var/logs/nginx/error.log debug;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/var/run/nginx.pid;
#pid logs/nginx.pid;
events {
worker_connections 256;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_x_forwarded_for" $host $request_time $upstream_response_time $scheme '
'$cookie_evalogin';
access_log /usr/local/var/logs/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include servers/*;
include /usr/local/etc/nginx/sites-enabled/*;
}

独立出各个站点配置

vim /usr/local/etc/nginx/sites-enabled/default

内容如下:

server {
listen 80;
server_name localhost;
root /Users/hucaihua/Documents/code/cmssp/server/;
location / {
index index.html index.htm index.php;
include /usr/local/etc/nginx/conf.d/php-fpm;
}
}

独立的php-fpm配置

vim /usr/local/etc/nginx/conf.d/php-fpm

内容如下:

location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
include /usr/local/etc/nginx/fastcgi.conf;
}

检测nginx 配置 是否正确

nginx -t

3.启动nginx

nginx -s quit
sudo nginx

检测nginx是否启动成功

ps aux | grep nginx

nginx 403 permission denied 问题

当我修改了网站根目录时,访问

http://localhost

提示出错,permission denied.

我使用的网站根目录为:
/Users/hucaihua/Documents/code/cmssp/server/

可在error.log中看到完整日志,error.log的位置,在nginx.conf中配置了:
/usr/local/var/logs/nginx/error.log

错误内容为:

"/Users/hucaihua/Documents/code/cmssp/server/index.html" is forbidden (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"

查询nginx 进程 发现 nginx worker进程属于 nobody 用户 ,如图所示:

image

而cmssp/server的属主是 hucaihua ,因此出现403 forbidden.

修正方法为:

  • 修改nginx.conf配置 user nobody 改为 user root wheel

    user root wheel;
    worker_processes 1;
  • 修改 cmssp/server 属主为 root:wheel

    chown -R root:wheel server

再次访问http://localhost 正常,至此问题解决。

五.mysql

安装mysql

brew install mysql

设置mysql为自启动服务

brew services start mysql

设置root 密码

mysqladmin -uroot password "xxx"

忘记root密码重新设置

sudo mysqld_safe --skip-grant-tables
mysql -u root
use mysql;
update user set password=PASSWORD("XXX") where user="root";
flush priviledges;

格式化查询输出

select * from user \G;

六.memcached

安装memcached

brew install memcached

启动memcached

memcached -d

停止memcached

killall memcached

测试memcached

telnet 127.0.0.1 11211
set keyname 111 120 4
get keyname
stats

set keyname 111 120 4 意思是写入keyname的key,它的值为 111,过期时间为120秒 字节数为4个字节。

关于memcache 和 memcached

php 有两个扩展 memcache 和 memcached ,本质的区别就是memcache 是php实现的分布式缓存组件 memcached是用C扩展的分布式缓存组件。


最后脑补两个mac/linux命令
查看进程信息

ps aux | grep nginx
ps aux | grep php-fpm

查看端口占用
lsof -i tcp:9000 | grep XXX

支持一下
扫一扫,支持牛头码农