Docker搭建lnmp过程记录

132 阅读2分钟

前言

今天不知道怎么回事,使用MAMP当做开发服务器的时候,TTFP总是超过了20s才请求才会到达服务器,一下午开发的我是非常不爽的。其实自从MAMP升级到6.6.1以后,我发现MAMP PRO是越来越难用了。所以索性想着换一种集成换成,下午也试了试MxSrvs。说实话,反正怎么用怎么不得劲。这不巧了嘛, 正好会一点docker,直接找个docker的镜像不就完了? 所以决定使用docker搭建我的PHP开发环境,我发誓,我以后肯定不会换集成开发环境了。

准备工作(MAC环境下)

快速搭建

  1. clone项目

        $ git clone https://github.com/yeszao/dnmp.git
        # 假如速度太慢,可以使用加速拉取镜像
        $ git clone https://github.com.cnpmjs.org/yeszao/dnmp.git
    
  2. 拷贝并命名配置文件(Windows系统请用copy命令),启动

    $ cd dnmp                                           # 进入项目目录
    $ cp env.sample .env                                # 复制环境变量文件
    $ cp docker-compose.sample.yml docker-compose.yml   # 复制 docker-compose 配置文件。默认启动3个服务:
                                                        # Nginx、PHP7和MySQL8。要开启更多其他服务,如Redis、
                                                        # PHP5.6、PHP5.4、MongoDB,ElasticSearch等,请删
                                                        # 除服务块前的注释
    $ docker-compose up                                 # 启动
    
  3. 可以执行访问 http://localhost。

其实正常来讲这三步就已经算是搭建完成了整个lnmp的环境。

  1. 部署自己的项目(我按照tp5项目进行部署) 拷贝tp5项目到www文件夹下 如图所示

image.png

  1. 配置nginx虚拟主机

image.png

  • 下面第二行 default 只能在一个.conf文件中 有多个conf文件的时候也只有一个defult
server {
    listen       80  default;  # 监听端口
    server_name  jyoa.local;   # 本地访问的路径
    root   /www/local_jyoa/public; # 项目的访问路径
    index  index.php index.html index.htm; # 索引
    
    access_log /dev/null;
    #access_log  /var/log/nginx/nginx.localhost.access.log  main;
    error_log  /var/log/nginx/nginx.localhost.error.log  warn;
    
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location / {
        index index.php index.html;
        
        # tp5需要注意的
        if (!-e $request_filename){  
            rewrite ^/(.*)$ /index.php?s=/$1 last;break;
        }
        autoindex  off;
    }

    location ~ \.php(.*)$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  1. 修改host文件 重要的是事情说三遍
# 这个要是忘记了,那可就打死也访问不了本地的项目
vim /etc/host
  1. 开心的到浏览器运行你的网站吧

其他问题

后续遇到其他问题,我也会及时在这里更新的