【问题标题】:How can I create a laravel 5 dev site with DDEV?如何使用 DDEV 创建 laravel 5 开发站点?
【发布时间】:2019-09-18 01:23:48
【问题描述】:

如何使用 DDEV 创建 laravel 5 开发站点? (见https://github.com/drud/ddev/issues/898#issuecomment-463203604

【问题讨论】:

  • 要在 Stack Overflow 上做出重大贡献,您需要提供一个完整的问题(并可选择自己回答)。但是人们不只是想要链接到其他地方,他们想要一个强有力的问题和一个很好的答案。我知道这是您第一次来这里,但请继续将此问题更新为您想要的,如果您知道一个好的答案,请自己添加答案。

标签: laravel ddev


【解决方案1】:

我使用带有 Linux 容器的 DDEV 和 Docker Desktop 在 Win10 上创建了一个 Laravel 5 开发站点。注意,Win10不需要安装composer,而是使用了DDEV在web容器中安装的composer。
步骤如下:

  • 创建了一个d:\laravel5 文件夹

  • 以管理员身份打开 PowerShell 窗口并切换到此文件夹

  • ddev config并选择默认的“php”项目类型:

PS D:\laravel5> ddev config
Creating a new ddev project config in the current directory (D:\laravel5)
Once completed, your configuration will be written to D:\laravel5\.ddev\config.yaml

Project name (laravel5):

The docroot is the directory from which your site is served.
This is a relative path from your project root at D:\laravel5
You may leave this value blank if your site files are in the project root
Docroot Location (current directory):
Found a php codebase at D:\laravel5.
Project Type [drupal6, drupal7, drupal8, wordpress, typo3, backdrop, php] (php):
Project type has no settings paths configured, so not creating settings file.
Configuration complete. You may now run 'ddev start'.
Instrumentation is opted in, but SentryDSN is not available.
Instrumentation is opted in, but SegmentKey is not available.
PS D:\laravel5>

DDEV 创建了一个d:\laravel5\.ddev 文件夹,将在运行ddev start 命令之前添加4 个文件来修改该文件夹。

请注意,对于 DDEV “php”项目类型,Laravel 5 PHP 先决条件均已满足
PHP >= 7.2.0
BCMath PHP 扩展
Ctype PHP 扩展
JSON PHP 扩展
Mbstring PHP 扩展
OpenSSL PHP 扩展
PDO PHP 扩展
Tokenizer PHP 扩展
XML PHP 扩展

  • 创建了带有内容的文件.ddev\config.laravel5.yaml
docroot: blog/public
  • 创建了带有内容的文件.ddev\docker-compose.laravel5.yaml
version: '3.6'

services:
  web:
    environment:
      - DB_HOST=db
      - DB_PORT=3306
      - DB_DATABASE=db
      - DB_USERNAME=db
      - DB_PASSWORD=db
  • 创建了带有内容的文件.ddev\nginx-site.conf
# ddev default config

# You can override ddev's configuration by placing an edited copy
# of this config (or one of the other ones) in .ddev/nginx-site.conf
# See https://ddev.readthedocs.io/en/stable/users/extend/customization-extendibility/#providing-custom-nginx-configuration

## Set https to 'on' if x-forwarded-proto is https
#map $http_x_forwarded_proto $fcgi_https {
#    default off;
#    https on;
#}

server {

    listen 80;
    server_name laravel5.ddev.site;
    # The WEBSERVER_DOCROOT variable is substituted with
    # its value when the container is started.
    root $WEBSERVER_DOCROOT;

    include /etc/nginx/monitoring.conf;
    include /mnt/ddev_config/nginx/*.conf;

}
  • 创建了文件.ddev\nginx\laravel5.conf,内容为:
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    ## Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html
    #sendfile off;
    error_log /dev/stdout info;
    access_log /var/log/nginx/access.log;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        #fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
  • ddev start:
PS D:\laravel5> ddev start
Starting laravel5...
Using custom nginx configuration in nginx-site.conf
Using custom nginx partial configuration: [D:\laravel5\.ddev\nginx\laravel5.conf]
Custom configuration takes effect when container is created,
usually on start, use 'ddev restart' if you're not seeing it take effect.
Creating volume "laravel5-mariadb" with default driver
Building db
Building web
Creating ddev-laravel5-db ... done                                                                                                     Creating ddev-laravel5-dba ... done                                                                                                    Creating ddev-laravel5-web ... done                                                                                                    
ddev-router is up-to-date
Successfully started laravel5
Project can be reached at https://laravel5.ddev.site https://127.0.0.1:32789
Instrumentation is opted in, but SentryDSN is not available.
Instrumentation is opted in, but SegmentKey is not available.
PS D:\laravel5>
  • 运行ddev ssh并在Web容器中执行以下shell命令
cd /var/www/html
composer create-project --prefer-dist laravel/laravel blog "5.8.*"

输出如下:

freefall322@laravel5-web:/var/www/html$ cd /var/www/html
freefall322@laravel5-web:/var/www/html$ composer create-project --prefer-dist laravel/laravel blog "5.8.*"
Installing laravel/laravel (v5.8.35)
  - Installing laravel/laravel (v5.8.35): Loading from cache
Created project in blog
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 80 installs, 0 updates, 0 removals
  - Installing symfony/polyfill-ctype (v1.12.0): Loading from cache
  - Installing phpoption/phpoption (1.5.0): Loading from cache
  - Installing vlucas/phpdotenv (v3.6.0): Loading from cache
  - Installing symfony/css-selector (v4.3.4): Loading from cache
  - Installing tijsverkoyen/css-to-inline-styles (2.2.1): Loading from cache
  - Installing symfony/polyfill-php72 (v1.12.0): Loading from cache
  - Installing symfony/polyfill-mbstring (v1.12.0): Loading from cache
  - Installing symfony/var-dumper (v4.3.4): Loading from cache
...
(skipping many lines of output)
...
phpunit/phpunit suggests installing phpunit/php-invoker (^2.0)
phpunit/phpunit suggests installing ext-xdebug (*)
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: beyondcode/laravel-dump-server
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
> @php artisan key:generate --ansi
Application key set successfully.
freefall322@laravel5-web:/var/www/html$  
Route::get('/foo', function () {
    //
    try {
        DB::connection()->getPdo();
        if(DB::connection()->getDatabaseName()){
            echo "Yes! Successfully connected to the DB: " . DB::connection()->getDatabaseName();
        }else{
            die("Could not find the database. Please check your configuration.");
        }
    } catch (\Exception $e) {
        die("Could not open connection to database server.  Please check your configuration.");
    }
});

注意:安装最新版本的 Laravel,而不是

composer create-project --prefer-dist laravel/laravel blog "5.8.*"

删除版本说明符并直接使用

composer create-project --prefer-dist laravel/laravel blog

我今天试过了,它安装了 Laravel 6.0

【讨论】:

    【解决方案2】:

    您可以使用 ddev 创建一个 Laravel 5 开发站点,如下所示:

    mkdir projectName
    cd projectName
    ddev config --project-type=laravel --docroot=public --create-docroot
    ddev start
    ddev composer create --prefer-dist laravel/laravel:5.8.*
    ddev exec "cat .env.example | sed  -E 's/DB_(HOST|DATABASE|USERNAME|PASSWORD)=(.*)/DB_\1=db/g' > .env"
    ddev exec "php artisan key:generate"
    ddev launch
    

    请参阅ddev Laravel quick start 文档。

    【讨论】:

      猜你喜欢
      • 2019-11-15
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 2015-04-07
      相关资源
      最近更新 更多