loading...

Laradock - A PHP Developer's best friend.

dendihandian profile image Dendi Handian Updated on ・4 min read

Explore Laradock Services (8 Part Series)

1) Laradock - A PHP Developer's best friend. 2) Redis in Laradock 3 ... 6 3) Mailhog in Laradock 4) PHP-Worker in Laradock 5) PHP-Worker for Scheduler in Laradock 6) RabbitMQ in Laradock 7) Elasticsearch in Laradock 8) HTTPS Local Development using Caddy in Laradock

When you are about to develop a PHP application, you might run into these local web server solutions: XAMPP , WAMP , Laragon

XAMPP: This might be the very first thing you would use if you are a beginner in the PHP web application development. XAMPP provides only the most basic services for PHP web application, which are Apache2 as web server and MySQL as database server, and maybe database manager like phpMyAdmin.

WAMP: It's like XAMPP, but it has virtualhost manager that can be used to make your local website URL customizable and looks prettier.

Laragon: It's like WAMP, but it's supercharged with some modern services to support your development, like Redis, Memcached, mail catcher, and more. It's the second most complete local web server solution of all I about to tell you here in this post.

Those local web servers solutions are good, but what if I want ... more?

A ready to use Docker-based solution

Meet Laradock, a full PHP development environment based on Docker, it's pre-configured and ready to use. Using a modern services like Elasticsearch, Selenium, Sonarqube locally on your machine is not a dream anymore.

Setting Up Laradock for Local Development

Before we get started to use Laradock, make sure you have docker and docker-compose installed on your machine. Any operating systems (Linux, Windows, MacOS) should support the docker.

Let's assume you have your own awesome Laravel application and it's placed in a directory named projects.

- projects
  |_ my-awesome-laravel-app 

Now grab the latest Laradock project from its official github page or clone it using git and place it in projects directory.

- projects
  |_ my-awesome-laravel-app 
  |_ laradock

Inside the laradock directory, you will find env-example file. Clone it into a new file named .env

- projects
  |_ my-awesome-laravel-app 
  |_ laradock
     |_ ...
     |_ env-example
     |_ .env

We are going to use the essential containers for our awesome Laravel app, they are nginx, mysql and probably phpMyAdmin

Setting up Nginx

Go to laradock > nginx > sites directory. You will find laravel.conf.example, clone it into a new file named my-awesome-laravel-app.test.conf (you can give this file any name you want but make sure it ends with .conf).

- projects
  |_ my-awesome-laravel-app 
  |_ laradock
     |_ ...
     |_ nginx
        |_ sites
           |_ ...
           |_ my-awesome-laravel-app.test.conf
           |_ laravel.conf.example
     |_ env-example
     |_ .env

Inside the my-awesome-laravel-app.test.conf file, change the server_name into your desired domain URL and root into your awesome laravel app public folder path.

server {

    ...

    server_name my-awesome-laravel-app.test;
    root /var/www/my-awesome-laravel-app/public;
    index index.php index.html index.htm;

    ...
}

That's it for nginx !

Setting up MySQL and PhpMyAdmin

No need to setup here...

The pre-configured users that can be used here are:

  • Root User:
    • username: root | password: root
  • Non-Root User:
    • username: default | password: secret

Setting up your awesome Laravel app .env file

Your app won't running, unless you configure the my-awesome-laravel-app/.env file right. The odd one is the DB_HOST, which its value must be mysql instead of localhost. Here are the env parameters you have to check:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:WqvIsN6XYA9NMHjTWIthKC1Mdi+WWi7/iRtz/GnibLQ=
APP_DEBUG=true
APP_URL=http://my-awesome-laravel-app.test

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=your_awesome_database
DB_USERNAME=default
DB_PASSWORD=secret

...

Running the containers

If you are using services like apache2 or mysql on your machine that uses 8080, 80, 3306 ports, please stop them first before you run the laradock's containers.

By using your favorite command-line interface (I'm using Terminus by the way. It's a cool CLI) , go inside laradock directory and execute this docker-compose command:

docker-compose up -d nginx mysql phpmyadmin

The above command will pull and build only the used containers (nginx, mysql, phpmyadmin) and the basic laradock container named workspace. The pull and build process will take long time (and your internet connection too) for the first time.

After all done, you can check the containers statuses whether they are running or not by executing:

docker-compose ps

Now, the only one thing you have to do is adding the my-awesome-laravel-app.test domain into you machine hosts file. For Windows, it should be in C:\Windows\System32\drivers\etc\hosts and for linux-based, it should be in /etc/hosts.


...

127.0.0.1   my-awesome-laravel-app.test

...

Now, your awesome laravel app should be accessible by opening the http://my-awesome-laravel-app.test on your browser.

Creating database using phpMyAdmin

Don't forget to create your_awesome_database using phpMyAdmin. It's should be accessible in http://localhost:8080 . You can use the any given user credentials above to login, and make sure you fill the server form with mysql instead of localhost.

Recently if you have an error that the 8080 port already taken by the workspace, you can set PMA_PORT=8081 in laradock/.env file, restart the container using docker-compose restart phpmyadmin and open http://localhost:8081 instead.

Entering the Workspace Container bash

You might be forget to run the composer install inside the laravel app. Don't worry, you can use the workspace bash that has all the command-line utilities needed for your laravel app. To enter the bash, make sure you are inside laradock directory in your CLI and execute this docker-compose command:

docker-compose exec --user=laradock workspace bash

The directory starting point should be in /var/www/ and your laravel app should be here too. You can go there to execute composer install command and some artisan command like php artisan migrate or maybe create another project using composer's composer create-project laravel/laravel my-another-laravel-app command.


There are a lot of laradock services that I want to cover later, but you can explore it yourself. Have fun!

Laravel version used: 5.5 LTS
Laradock MySQL version used: 8.0.20

Explore Laradock Services (8 Part Series)

1) Laradock - A PHP Developer's best friend. 2) Redis in Laradock 3 ... 6 3) Mailhog in Laradock 4) PHP-Worker in Laradock 5) PHP-Worker for Scheduler in Laradock 6) RabbitMQ in Laradock 7) Elasticsearch in Laradock 8) HTTPS Local Development using Caddy in Laradock

Posted on by:

dendihandian profile

Dendi Handian

@dendihandian

Please teach me how to prettify database schema, implement any design patterns and do TDD in Drupal / Wordpress and how to make them not slow as ever.

Discussion

markdown guide