Laradock使用

2024-10-07  本文已影响0人  呦丶耍脾气

前瞻

Laradock 是一个基于 Docker 的全栈 PHP 开发环境,旨在为开发者提供一个快速、便捷的 PHP 开发环境。Laradock 支持多种常见的服务,如 Nginx、MySQL、Redis、Elasticsearch 等,并且这些服务都预先配置好,开发者可以直接使用。Laradock 的灵活性和可扩展性使得它成为 PHP 开发者的首选工具之一。
码云地址

支持镜像

安装laradock

请确保安装了DockerDocker Compose

可以替换为码云地址

git submodule add https://github.com/laradock/laradock.git
git clone https://github.com/laradock/laradock.git

配置环境

复制 .env.example 文件并重命名为 .env:

cp .env.example .env

根据自己需要修改对应的配置,如:

# 项目所在的目录
APP_CODE_PATH_HOST=../

# 项目在容器中的位置,也就是默认指向的目录,nginx配置文件中root的/var/www对应上面的APP_CODE_PATH_HOST目录
APP_CODE_PATH_CONTAINER=/var/www

# 数据持久化位置,比如Mysql/Redis/Mongo产生的物理文件
DATA_PATH_HOST=/data/laradock/data

# 修改成本地的IP(不修改的话,会出现无法捕获到断点情况)
DOCKER_HOST_IP=192.168.204.133

# 如果需要更改来源(即中国),请将change_SOURCE设置为true
CHANGE_SOURCE=true

# 设置时区,会影响到所有运行的容器
WORKSPACE_TIMEZONE=Asia/Shanghai

# 添加workspace镜像源
 # composer镜像源
WORKSPACE_COMPOSER_REPO_PACKAGIST=https://mirrors.aliyun.com/composer/
# nvm镜像源
WORKSPACE_NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
# npm镜像源
WORKSPACE_NPM_REGISTRY=https://registry.npmmirror.com

# mysql用户和密码
MYSQL_USER=root
MYSQL_ROOT_PASSWORD=root

#redis密码
REDIS_PASSWORD=secret_redis

运行

#多个启动
docker-compose up -d nginx mysql php-fpm
#单个启动
docker-compose up -d nginx
$ docker-compose ps
           Name                          Command               State                                                                                      Ports                                                                                   
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
laradock_docker-in-docker_1   dockerd-entrypoint.sh            Up      2375/tcp, 2376/tcp                                                                                                                                                         
laradock_mysql_1              docker-entrypoint.sh mysqld      Up      0.0.0.0:3306->3306/tcp,:::3306->3306/tcp, 33060/tcp                                                                                                                        
laradock_nginx_1              /docker-entrypoint.sh /bin ...   Up      0.0.0.0:443->443/tcp,:::443->443/tcp, 0.0.0.0:80->80/tcp,:::80->80/tcp, 0.0.0.0:81->81/tcp,:::81->81/tcp                                                                   
laradock_php-fpm_1            docker-php-entrypoint php-fpm    Up      9000/tcp                                                                                                                                                                   
laradock_workspace_1          /sbin/my_init                    Up      0.0.0.0:2222->22/tcp,:::2222->22/tcp, 0.0.0.0:3000->3000/tcp,:::3000->3000/tcp, 0.0.0.0:3001->3001/tcp,:::3001->3001/tcp, 0.0.0.0:4200->4200/tcp,:::4200->4200/tcp,        
                                                                       0.0.0.0:5173->5173/tcp,:::5173->5173/tcp, 0.0.0.0:8001->8000/tcp,:::8001->8000/tcp, 0.0.0.0:8080->8080/tcp,:::8080->8080/tcp 
$ docker-compose logs -f nginx
nginx_1                | Certificate request self-signature ok
nginx_1                | subject=CN=default, O=default, C=UK
nginx_1                | 192.168.204.1 - - [08/Oct/2024:11:22:05 +0000] "GET / HTTP/1.1" 404 178 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
nginx_1                | 192.168.204.1 - - [08/Oct/2024:11:22:05 +0000] "GET /favicon.ico HTTP/1.1" 404 178 "http://192.168.204.133/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
nginx_1                | 192.168.204.1 - - [08/Oct/2024:11:22:07 +0000] "GET / HTTP/1.1" 404 178 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
nginx_1                | 192.168.204.1 - - [08/Oct/2024:11:22:15 +0000] "GET /index.php HTTP/1.1" 404 178 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"

部署项目

我的APP_CODE_PATH_HOST目录是/data/www,项目目录是/data/www/test

server {
  
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    # For https
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server ipv6only=on;
    # ssl_certificate /etc/nginx/ssl/default.crt;
    # ssl_certificate_key /etc/nginx/ssl/default.key;

    server_name test-demo.com;
    root /var/www/test;
    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;
    }
    error_log /var/log/nginx/localhost_error.log;
    access_log /var/log/nginx/localhost_access.log;
}

/var/www对应APP_CODE_PATH_CONTAINER的配置,必须保持一致。
nginx的日志文件挂载在./logs/nginx/

127.0.0.1 test-demo.com
docker-compose restart nginx
<?php
echo 'hello world';
上一篇 下一篇

猜你喜欢

热点阅读