搭建 Docker+Nginx+PHP+Laravel 运行环境

关于 Docker+Nginx+PHP 的运行环境,参考我之前的博文
《搭建 DOCKER+NGINX+TOMCAT+PHP+MYSQL 运行环境》

但是如果要运行 Laravel 框架,还需要做更多的操作。

安装必要的工具

安装 Laravel 框架,必须要用到 composer 和 php-cli, 先用 docker 把它们 pull 下来

1
2
docker pull php
docker pull composer

php-cli 运行的例子:

1
docker run -it --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp php php -r "echo 'hello world';"

composer 运行的例子:

1
docker run -it --rm --privileged -v $PWD:/app composer --version

安装 Laravel

去 github 下载 Laravel 的源码

1
2
3
curl -sSL https://github.com/laravel/laravel/archive/master.tar.gz   tar zxv
mv laravel-master /dmlkdata/apps/php/var/www/html/laravel
cd /dmlkdata/apps/php/var/www/html/laravel

编辑 composer.json,添加仓库镜像节点(Packagist 镜像使用方法),

1
2
3
4
5
6
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}

利用 composer 安装依赖文件

1
docker run --rm -it --privileged -v $PWD:/app composer install

配置 Laravel

配置.env 文件

1
cp -p .env.example .env

生成 APP_KEY

1
docker run -it --rm --privileged -v "$PWD":/usr/src/myapp -w /usr/src/myapp php php artisan key:generate

安装 php 的扩展,比如数据库的 PDO 等:

1
2
3
4
docker exec dmlkdockerphp docker-php-ext-install pdo pdo_mysql
docker restart dmlkdockerphp
docker diff dmlkdockerphp
docker commit -m "add php ext:pdo,pdo_mysql" -a "Broly" dmlkdockerphp dmlkdockerphp:1.0.1

编辑.env 文件,配置数据库相关信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:pdgdc418iNuC3H20Mnh7OltF3XEb8VY7hD3L7lXPakg=
APP_DEBUG=true
APP_LOG=daily
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=dmlkdockermysql
DB_PORT=3306
DB_DATABASE=dmlklaravel
DB_USERNAME=broly
DB_PASSWORD=admin

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

更改文件夹权限

1
2
chown -R 33.33 bootstrap/cache
chown -R 33.33 storage/

初始化数据库等

1
2
3
docker run -it --rm --privileged --link dmlkdockermysql:dmlkdockermysql -v "$PWD":/usr/src/myapp -w /usr/src/myapp dmlkdockerphp:1.0.1 php artisan migrate
docker run -it --rm --privileged -v "$PWD":/usr/src/myapp -w /usr/src/myapp php php artisan make:auth
docker run -it --rm --privileged -v "$PWD":/usr/src/myapp -w /usr/src/myapp php php artisan make:controller DemoController --resource

配置 Nginx

新建配置文件 /dmlkdata/apps/nginx/etc/nginx/conf.d/laravel.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
server {
listen 80;
server_name laravel.dmlk.dev;

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

root /var/www/html/laravel/public;
index index.php index.html index.htm;

location / {
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$ {
fastcgi_pass dmlkdockerphp:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$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;
#}
}

重启 Nginx

1
docker restart dmlkdockernginx

运行结果

更改 hosts 文件,方便开发测试用:

1
[root@localhost ~]# ip addr

找到本机服务器 IP 是 192.168.95.128 添加到 C:\Windows\System32\drivers\etc\hosts

1
192.168.95.128 laravel.dmlk.dev

Docker+Nginx+PHP+Laravel-0