docker php 环境搭建 并运行 Laravel

要点提示

  1. 本问nginx目录位于/home/laolang/docker/nginx-1.8.1/www
  2. nginxphp应该挂在同一个目录
  3. laravel 项目目录直接 sudo chmod -R 777 xxx

获取nginx配置文件

# 先安装一个nginx
docker pull nginx:1.8.1
docker run -p 80:80 --name nginx -d nginx:1.8.1
# 将容器中nginx的配置文件复制到本地
docker cp -a nginx:/etc/nginx /home/laolang/docker/nginx-1.8.1/conf
# 停止并删除容器
docker stop nginx
docker rm nginx
# nginx挂在目录所在位置与目录结构
laolang@laolang-PC:~/docker/nginx-1.8.1$ pwd
/home/laolang/docker/nginx-1.8.1
laolang@laolang-PC:~/docker/nginx-1.8.1$ l
总用量 16K
drwxr-xr-x 3 laolang laolang 4.0K 4月   6  2016 conf/
drwxr-xr-x 2 laolang laolang 4.0K 9月  14 19:38 logs/
drwxr-xr-x 4 laolang laolang 4.0K 9月  14 22:05 www/
drwxr-xr-x 2 laolang laolang 4.0K 9月  14 19:46 wwwlogs/
laolang@laolang-PC:~/docker/nginx-1.8.1$

安装php

# 获取php镜像
docker pull php:7.4.9-fpm-alpine3.12
# 创建容器
docker run --privileged=true --name php-7.4.9-fpm-alpine3.12 -v /home/laolang/docker/nginx-1.8.1/www:/www -p 9100:9000 -d php:7.4.9-fpm-alpine3.12

安装nginx

docker run -p 80:80 --privileged=true --name nginx-1.8.1 --link php-7.4.9-fpm-alpine3.12 -v /home/laolang/docker/nginx-1.8.1/www:/www -v /home/laolang/docker/nginx-1.8.1/conf/:/etc/nginx/ -v /home/laolang/docker/nginx-1.8.1/logs:/var/log/nginx -v /home/laolang/docker/nginx-1.8.1/wwwlogs:/wwwlogs -d nginx:1.8.1

此时可以在/home/laolang/docker/nginx-1.8.1/www中添加一个index.html,然后访问localhost即可

composer

配置composer

添加如下目录

mkdir -p ~/dnmp/composer

.bashrc中添加如下内容

composer () {
    tty=
    tty -s && tty=--tty
    docker run         $tty         --interactive         --rm         --user $(id -u):$(id -g)         --volume ~/dnmp/composer:/tmp         --volume /etc/passwd:/etc/passwd:ro         --volume /etc/group:/etc/group:ro         --volume $(pwd):/app         composer "$@"
}

镜像

composer config -g repo.packagist composer https://developer.aliyun.com/composer

laravel

创建项目

进入/home/laolang/docker/nginx-1.8.1/www目录执行如下命令

composer create-project --prefer-dist laravel/laravel kmphp
sudo chmod -R 777 kmphp

添加nginx配置文件

进入/home/laolang/docker/nginx-1.8.1/conf/conf.d添加一个kmphp.conf文件,内容如下

    listen       80;
    server_name  www.kmphp.com kmphp.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /www/kmphp/public;
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php?$query_string;  
    }

    #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 ~ \.php$ {
    #    root           html;
        fastcgi_pass   php-7.4.9-fpm-alpine3.12:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/kmphp/public/$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache‘s document root
    # concurs with nginx‘s one
    #
    location ~ /\.ht {
        deny  all;
    }
}

hosts

添加域名映射

重启nginx

不需要重启容器,可以使用如下命令进入nginx容器

docker exec -it nginx-1.8.1 /bin/bash

然后使用如下命令重启nginx

service nginx reload

验证

此时即可在浏览器输入www.kmphp.com验证

docker php 环境搭建 并运行 Laravel

docker php 环境搭建 并运行 Laravel

上一篇:实验 2 :Mininet 实验 —— 拓扑的命令脚本生成


下一篇:js中settimeout和setinterval的区别是什么?