Mac搭建lnmp环境

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 说明安装php安装nginx配置多个web项目使用本地域名共用一个端口遇见的问题7-1 之前使用brew安装过php扩展7-2 使用brew install直接安装php扩展失败参考个人博客: alex-my.xyz1 说明通过Homebrew安装nginx和php5.6, 没有安装Homebrew请先安装。2 安装


个人博客: 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 参考

相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
6月前
|
Java 关系型数据库 MySQL
mac,linux环境的基础工具安装【jdk,tomcat】
mac,linux环境的基础工具安装【jdk,tomcat】
90 1
|
6月前
|
Go iOS开发 MacOS
手把手教你在Mac上从零搭建Go语言开发环境
手把手教你在Mac上从零搭建Go语言开发环境
389 0
|
7月前
|
前端开发 开发工具 git
mac前端开发环境
mac前端开发环境
122 0
|
7月前
|
关系型数据库 MySQL 应用服务中间件
手动部署LNMP环境(Alibaba Cloud Linux 2)
本场景带您体验如何在Alibaba Cloud Linux 2.1903 LTS 64位操作系统的云服务器上搭建LNMP环境。
203 0
|
4月前
|
关系型数据库 应用服务中间件 nginx
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
|
22天前
|
Java
Mac环境下反编译apk
Mac环境下反编译apk
17 0
|
1月前
QT 5.14.2版本 MAC环境安装部署流程
QT 5.14.2版本 MAC环境安装部署流程
|
7月前
|
应用服务中间件 PHP nginx
基于Anolis OS 3快速搭建LNMP环境制作KodBox
本教程介绍如何搭建LNMP环境,其中本实验的LNMP分别代表Anolis OS 3、Nginx、Mariadb和PHP。
135 0
|
7月前
|
关系型数据库 MySQL 应用服务中间件
快速搭建LNMP环境
Nginx是一款小巧而高效的Web服务器软件,可帮您在Linux系统下快速方便地搭建出LNMP Web服务环境。本教程介绍如何搭建LNMP环境,其中LNMP分别代表Linux、Nginx、MySQL和PHP。
287 2
|
3月前
|
API PHP 数据库
Docker六脉神剑(四) 使用Docker-Compose进行服务编排搭建lnmp环境
Docker六脉神剑(四) 使用Docker-Compose进行服务编排搭建lnmp环境
33 0