GithubHelp home page GithubHelp logo

laravel-served's Introduction

Laravel Served

Latest Version on Packagist Downloads on Packagist tests

Introduction

Laravel Served is a dockerized version of php artisan serve. It makes it easy to quickly start a development environment the laravel way (through a config file).

The only things you need to get started is

  • Php (cli)
  • Docker
  • Laravel

Beware: This package is under active development and major changes can occur at any point. It is therefore a good idea to read the documentation, and republish the config file after each new version.

Available services

These are the available services that Served provide. More will be added in the future. If you are missing something specific, just create a new issue, requesting it.

  • Php
  • Nginx
  • Apache2
  • Mysql
  • Postgres
  • Redis
  • Memcached
  • Mailhog

Installation

Install the package using composer

$ composer require sinnbeck/laravel-served --dev

Running for the first time

It is possible to just start a development server right away after installation. This will bring up 3 docker images:

Image Version Misc
Php-fpm 7.4 Preinstalled with composer, required php modules for running laravel and xdebug running on port 9000 with IDE key 'served'
Nginx 1.19 N/A
Mysql 5.7
  • Hostname: mysql
  • Database: laravel
  • Username: laravel
  • Password: password

To start the containers simply run

$ php artisan served:up

If this is your first time running the command, it will build the images first before starting the containers. If you have run the served:up command before, docker will quickly check the images for updates and served will start the containers again.

Starting and stopping served

After your first run, you can easily start and stop your containers without having to build either images or containers. Simply run

$ php artisan served:start

And to stop the containers again without removing anything.

$ php artisan served:stop

This is useful to free up ports if you have several projects using the same.

Ssh

To go into a container to work you can run

$ php artisan served:ssh container_name

The container_name is optional, and will default to php (where you can run artisan etc.).

Served doesn't actually ssh into the container, but rather just start a bash shell directly. served:sshjust sound better and is quick to type.

Clean up

It is possible to remove all data set up by served. To do this simply run

$ php artisan served:teardown

Configuration

While it is possible to just run served without any configuration, it is probably a good idea to configure served for your needs. To get started you need to publish the config file.

$ php artisan vendor:publish --provider="Sinnbeck\LaravelServed\ServedServiceProvider"

Name

To avoid naming conflicts between projects, you can define your own name for served configuration. This name will be used when creating network, images and containers. Make sure it is unique between projects! If no name is set, served will use the folder name of the laravel installation (a slug version)

It is important the name only consists of letters, numbers, ., - and _. Other special characters will throw an exception.

If you at some point wish to chance the name after having used served on a project, it is important to teardown both images and containers using served:teardown. If you have already changed the name and are having issues getting your containers up and running with the new name, just chance the name back, run teardown, and set it to the new name once more.

Php

Here you may specify how php should be built. Any options left blank or removed will be defaulted to using the defaults provided by served.

'php' => [
        'version' => env('SERVED_PHP_VERSION', '7.4'),
        'modules' => [
            'pdo_mysql',
            'zip',
        ],
        'npm' => true, //enable or disable npm in build
        'xdebug' => [
            'enabled' => env('SERVED_XDEBUG_ENABLED', true),
            'port' => 9001,
        ],
    ],

The array of modules can be filled with any module found in the url below (except parallel, pthreads and tdlib)

https://github.com/mlocati/docker-php-extension-installer

Xdebug

It is suggested to install xdebug to make debugging easier. To install it and set it up, simple make sure it is set as enabled in the config, while running php artisan served:up php

As Xdebug can slow down requests, it is possible to quickly turn it off and on, when needed.

Enable Xdebug

$ php artisan served:xdebug enable

Disable Xdebug

$ php artisan served:xdebug disable

Interactive toggle Xdebug

$ php artisan served:xdebug

Be aware that you need to run php artisan served:up php again if you decide to enable Xdebug in the config. It isn't possible to toggle it on an off if it isn't installed in the first place.

Web

Served currently supports nginx and apache. Simply service to whichever you want to use, and set the correct version (or delete the version to have served use a sensible default). Apache currently only supports the latest version and will ignore any version set.

'web' => [
        'service' => 'nginx', //or apache
        'version' => '1.9.2',
        'port' => env('SERVED_WEB_PORT', 8095),
        'ssl_port' => env('SERVED_WEB_SSL_PORT', 4443),
    ],

If you are trying to use the https address, you will be shown a certificate error. To fix this in Chrome, open chrome://settings/certificates and select the Authorities tab. Click import and find the localhost.crt in your /storage/app/served/web/ directory

Extras

Here you can define extra images that you wish to run. The array key is used as name, meaning it is possible to run the same service more than once, with different names (eg. two mysql instances).

The current supported images are:

Mysql

Port is used for when connecting to mysql from outside of laravel. Eg. 127.0.0.1:3306.

To connect to the database from laravel you need to use the config key (in the example that would be mysql) as hostname. The port is the default for mysql (3306) and not the one specified in the config.

If you wish to override the port you use connect to mysql from outside your docker, you can do so by adding 'SERVED_EXTERNAL_DB_PORT' to your .env

'mysql' => [
            'service' => 'mysql',
            'version' => '5.7',
            'port' => env('SERVED_EXTERNAL_DB_PORT', 3306),
            'root_password' => 'password',
            'database' => env('DB_DATABASE', 'laravel'),
            'username' => env('DB_USERNAME', 'laravel'),
            'password' => env('DB_PASSWORD', 'password'),
        ],

Postgres

To connect to postgresql from laravel you need to use the config key (in the example that would be postgres) as hostname. The port is the default for mysql (5432) and not the one specified in the config. To connect from outside of laravel, use the port specified in the config (eg. 54320) and 127.0.0.1

'postgres' => [
            'service' => 'postgres',
            'version' => '12.4',
            'port' => 54320,
            'database' => 'laravel',
            'username' => 'laravel',
            'password' => 'password',
        ],

Redis

Add redis to the modules in php and then add redis to your extras array.

'redis' => [
            'service' => 'redis',
        ]

Change your REDIS_HOST in .env to whatever you use as the key (eg. redis)

Memcached

Add memcached to the modules in php and then add memcached to your extras array.

'memcached' => [
            'service' => 'memcached',
        ]

Change your CACHE_DRIVER in .env to memcached and add MEMCACHED_HOST and set it to whatever you use as the key (eg. memcached)

Mailhog

Add mailhog to your extras array.

'mail' => [
            'service' => 'mailhog',
            'port' => 8025
        ]

Change your MAIL_HOST in .env to whatever you use as the key (eg. mail), and change MAIL_PORTto 1025. To see the mailbox, open http://localhost:8025 in your browser (replace 8025 with whatever port you set in config)

Testing

Run tests with

$ composer test

Todo

  • Testing!
  • Add more images
  • Allow user created services
  • Let served make a proper name if none is set (instead of defaulting to 'served')
  • Handle setting/adding volumes
  • Handle removal of volumes
  • Handle upgrades/downgrades of images
  • Pass cli output interface to other classes to allow outputting to cli from them
  • Test on other platforms than linux (Ubuntu)

laravel-served's People

Contributors

iammikek avatar pfaffenrodt avatar rsaalund avatar sinnbeck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-served's Issues

Https for web

I am trying to find some clever way to have the web service work with https. Both nginx and apache2

Issues on building Php 7.4-fpm

Development Environment:

  • Mac OS, M1

When running php artisan served:up

ERROR : : #1 [internal] load build definition from Dockerfile
#1 sha256:c4efc71d75273557a7c7be51362d361b9536dcdc8da29
#1 transferring dockerfile: 2.33kB done
#1 DONE 0.0s

#2 [internal] load .dockerignore
#2 sha256:41e4a846919871ed72ae59be3152c6ff843a8f948833

I am not sure where is the issue! please help

Stand alone "executable"

I consider switching to laravel zero and allow the user to publish a served executable to your project.

Will make it possible to simply run

php served up
php served list 
//etc

Any thoughts?

"cache" name

"cache" served name to enable clean up of old containers and images on name change

Let served make a proper name if none is set (instead of defaulting to 'served')

@sinnbeck I'm looking at ways we could introduce this feature and wonder how you'd like to approach it?

Are you imagining something like how heroku does eg "airy-craig" or "soaring-peaks"?

We would need to preserve the name once it was created. Would it be viable for us to write this name into the .env as SERVER_NAME?

I came across https://mattstauffer.com/blog/generating-synonymous-heroku-style-server-names-with-lumen/ which might give us a head start.

What do you think?

Flag for showing commands being run

Add a flag to commands that allows showing what commands are run. Will need some regex replacements to ensure proper values are show (not "variables")

Requests are really slow on mac

Hi,

Thanks for the awesome package. Im trying it in my new project and realise that the HTTP requests are really slow, ~2s each.

I guess its the docker file sync issue? Any way to improve that using this package?

P/S: Im using Mac.

Cheers,

Install issue with 0.6.0

installing into a new laravel8 install and running the following:

 composer require sinnbeck/laravel-served --dev
Using version ^0.6.0 for sinnbeck/laravel-served
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing sinnbeck/laravel-served (v0.6): Loading from cache
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [Sinnbeck\LaravelServed\Commands\ServedRunCommand] does not exist.

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:811
    807▕ 
    808▕         try {
    809▕             $reflector = new ReflectionClass($concrete);
    810▕         } catch (ReflectionException $e) {
  ➜ 811▕             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
    812▕         }
    813▕ 
    814▕         // If the type is not instantiable, the developer is attempting to resolve
    815▕         // an abstract type such as an Interface or Abstract Class and there is

Test database?

A test database would be nice. Should it be created by default or be an option (the user can add one manually, but that is more work)

PHP container failing to build

I just tried this package for the first time and I'm getting:

Starting php (Php 7.4-fpm) ...
ERROR : Unable to find image 'served/served_php:latest' locally

ERROR : docker: Error response from daemon: pull access denied for served/served_php, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.

The web and mysql seem to have started just fine.

Steps were as follows:

laravel new served --jet
cd served/
composer require sinnbeck/laravel-served --dev
php artisan served:up

'Tests\TestCase' not found in laravel-served/tests/Unit/DockerImageTest.php:13

If I run 'composer test' in root directory, I get the following error:

Class 'Tests\TestCase' not found in ~/laravel8/laravel-served/tests/Unit/DockerImageTest.php:13

This appears because the tests are being run outside of a Laravel install.

Switching to running tests within Laravel8 install

with the following

        "sinnbeck/laravel-served": "@dev"

and

 "repositories": [
        {
            "type": "path",
            "url": "~/laravel8/laravel-served",
            "options": {
                "symlink": true
            }
        }
    ]

in composer.json

./vendor/bin/phpunit ../laravel-served/tests/Unit

I get the following:

  1. Tests\Unit\DockerImageTest::it_can_make_apache_docker_file
    Error: Call to undefined method Tests\Unit\DockerImageTest::getExpectedContent()

and similar for other tests.

@sinnbeck can you let me know how you've got your package set up locally and what command you run to run your DockerImageTest class?

Setting up Laravel-served with phpstorm

Hiya sinnbeck,
Do you have any advice on how to set up phpstorm with Laravel-served?

I'm trying to set a remote PHP interpreter. When I select
Preferences > PHP > CLI Interpreter > Add New (Docker) I cant see the image from the list.

Have you had any success with this and be able to share how to do it?

Can't connect to mysql outside docker

probably just me, but whats your normal process for interacting with the mysql DB?

I've tried to connect via the settings:

'mysql' => [
            'service' => 'mysql',
            'version' => '5.7',
            'port' => 3306,
            'root_password' => 'password',
            'database' => 'laravel',
            'username' => 'laravel',
            'password' => 'password',
        ],

both with localhost + 127.0.0.1 via the Database Tab in phpstorm. No dice. Any advice?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.