Docker容器Laravel开发实践laravel学习笔记

Windows环境中使用Docker 配置LaraDock P

2020-01-04  本文已影响0人  KingJW

现在PHP在服务端还是占有百分之79,占比稳居第一,Laravel作为一个优秀的PHP框架,新手PHPer只会用phpstudy wampserver是无法在公司生存下去的,今天简单为新手PHPer 避免踩雷。

1.下载Docker

https://www.docker.com/

注册帐号后重复以上动作,下载到电脑上,安装步骤一直默认即可

注:windows 10 系统需要开启 Hyper-V,开启步骤自行百度

任意位置打开 Windows PowerShell或者cmd命令提示符均可

// 查看 docker 版本

docker--v


以上状态和我的一样就是安装成功,服务在运行啦!

现在我们开始下载laradock

gitclonehttps://github.com/Laradock/laradock.git

放到你合适的位置
箭头所指的env-example复制一份,重命名为.env

在目录下laradock输入docker-compose up -d nginx mysql redis workspace php-fpm开始安装容器,耐心等待,需要等待很久,如果有报错,在完成后,继续运行此命令,如果继续报错,请坚持不懈,继续运行这行代码,直至无错误。,如果你的网络没有翻墙的话请在hosts文件中添加一句话

nameserver 223.5.5.5

nameserver  223.6.6.6

通常你windows路径下的hosts文件在这个路径下

C:\Windows\System32\drivers\etc

docker-compose exec --user=laradock workspace bash // 进入容器


打开刚刚复制的.env结尾添加几句话

DB_HOST=mysql

REDIS_HOST=redis

QUEUE_HOST=beanstalkd

在laradock同级目录下创建wwwroot文件夹作为你的项目存放处

创建demo目录,新建index.php,内容如上 修改为上图 修改 nginx 的配置文件,建立映射关系

server {

    listen 80;

    listen [::]:80;

    server_name demo.com;

    root /var/www/demo;

    index index.php index.html index.htm;

    location / {

          try_files $uri $uri/ /index.php$is_args$args;

    }

    location ~ \.php$ {

        try_files $uri /index.php =404;

        fastcgi_pass php-upstream;

        fastcgi_index index.php;

        fastcgi_buffers 16 16k;

        fastcgi_buffer_size 32k;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        #fixes timeouts

        fastcgi_read_timeout 600;

        include fastcgi_params;

    }

    location ~ /\.ht {

        deny all;

    }

    location /.well-known/acme-challenge/ {

        root /var/www/letsencrypt/;

        log_not_found off;

    }

}

上面是nginx的内容

重启 Docker 的 Nginx

docker-compose up -d nginx

hosts 文件中添加

127.0.0.1demo.com

在浏览器中访问 demo.com 说明环境搭建成功,每次重启电脑,需要重新运行容器哦

docker-compose exec --user=laradock workspace bash // 进入容器

下面是你可能会需要用到的命令

docker-compose exec mysql bash 进入 mysql 进入数据库

docker-compose exec nginx nginx -t // 检查nginx配置文件的语法

docker-compose exex nginx nginx -s reload // 重新加载配置,可以平滑启动

上一篇 下一篇

猜你喜欢

热点阅读