Mac搭建lnmp环境


个人博客: alex-my.xyz

1 说明

通过Homebrew安装nginx和php5.6, 没有安装Homebrew请先安装。

2 安装php

安装php时禁用apache。

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

在终端中输入php –version,此时指向的是系统自带的php, 我这边为php5.5版本。
打开 ~/.bash_profile
加入以下语句:

export PATH=/usr/local/bin:/usr/local/sbin:${PATH}

此时输入php –version,显示:

PHP 5.6.24 (cli) (built: Dec 21 2016 18:31:29) (DEBUG)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

如果需要安装扩展,可以使用brew search php56进行搜索。
安装扩展时遇见问题见 7-1, 7-2。
php-fpm的启动与关闭

# 开启
php-fpm -D
# 关闭
killall php-fpm
# 开机启动
ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

3 安装nginx

# 安装
brew install nginx
# 启动
nginx
# 关闭
nginx -s quit
# 其余操作
nginx -s reload/reopen/stop
# 开启启动
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

4 配置多个web项目

nginx配置文件位置:

/usr/local/etc/nginx

nginx日志文件位置:

 /usr/local/var/logs/nginx 

/usr/local/etc/nginx/nginx.conf:

worker_processes  1;
    error_log   /usr/local/var/logs/nginx/error.log debug;
    pid        /usr/local/var/run/nginx.pid;
    worker_rlimit_nofile 1000;

    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/access.log  main;

        sendfile        on;
        keepalive_timeout  65;
        port_in_redirect off;

        include /usr/local/etc/nginx/sites-enabled/*.conf;
    }

做了以下修改:

增加了(数字太大可能无效,最好同时修改系统的ulimit)
worker_rlimit_nofile 1000;
将
include /usr/local/etc/nginx/sites-enabled/*
改为
include /usr/local/etc/nginx/sites-enabled/*.conf;

需要注意的是, 如果找不到sites-enabled目录, 请查看nginx.conf的最后一行:

include /usr/local/etc/nginx/sites-enabled/*
# 有的是
include /usr/local/etc/nginx/server/*

sites-enabled 下将建立多个的conf文件,一个项目对应一个。
创建 /usr/local/etc/nginx/sites-enabled/site1.conf

server {
    listen       80;
    server_name  localhost;
    # 自定义的位置, 默认为/opt/htdocs/
    root         /Users/alex/WWW/site1;

    location / {
        index  index.html index.htm index.php;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }
}

创建 /usr/local/etc/nginx/sites-enabled/site2.conf

server {
    listen       8080;
    server_name  localhost;
    # 自定义的位置, 默认为/opt/htdocs/
    root         /Users/alex/WWW/site2;

    location / {
        index  index.html index.htm index.php;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }
}

在site1和site2目录下,编写一个简单的index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index site</title>
</head>
<body>
    <h2>Hello</h2>
</body>
</html>

如果site1和site2为空文件夹,则会报错:

nginx 403 forbidden

分别输入以下地址,即可访问:

# 访问site1项目
127.0.0.1
# 访问site2项目
127.0.0.1:8080

5 使用本地域名

使用ip地址访问,多有不便,做些小修改即可使用域名访问,域名可以随意定义。
修改 /usr/local/etc/nginx/sites-enabled/site1.conf

server {
    listen       80;
    # 仅修改了此处
    server_name  site1.com;
    root         /Users/alex/WWW/site1;

    location / {
        index  index.html index.htm index.php;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }
}

修改 /usr/local/etc/nginx/sites-enabled/site2.conf

server {
    listen       8080;
    server_name  site2.com;
    root         /Users/alex/WWW/site2;

    location / {
        index  index.html index.htm index.php;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }
}

修改 /etc/hosts, 使用sudo vim

# 添加以下两行
127.0.0.1    site1.com
127.0.0.1    site2.com

重载

nginx -s reload

分别输入以下地址,即可访问:

# 访问site1项目
site1.com
# 访问site2项目
site2.com:8080

如果没有反应,请清空浏览器缓存, Safari–开发–清空缓存

6 共用一个端口

在/usr/local/etc/nginx/sites-enabled/site2.conf

listen       8080;
修改为
listen       80;

重启nginx,清空浏览器缓存。

分别输入以下地址,即可访问:

# 访问site1项目
site1.com
# 访问site2项目
site2.com

7 遇见的问题

7-1 之前使用brew安装过php扩展

在本次安装之前使用brew uninstall将扩展都卸载,安装完php后,输入php –version, 有警告提示:
以php56-redis为例:

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/opt/php56-redis/redis.so' - dlopen(/usr/local/opt/php56-redis/redis.so, 9): image not found in Unknown on line 0

解决: 仅使用brew uninstall 并不能完全卸载,而还需要手动将/usr/local/etc/php/5.6/conf.d/下相应的ext-*.ini删除。

7-2 使用brew install直接安装php扩展失败

安装时候显示成功,但是使用php –version出现警告,类似于6-1。
此时可以卸载,重新安装,带上 –build-from-source

brew install php56-igbinary --build-from-source
brew install php56-redis --build-from-source

8 参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值