How to install a PHP development environment in Ubuntu with Docker
By: Kenny Lee
Tags: MySQL, Nginx, Ubuntu, PHP, Docker

The PHP development environment uses docker containers to run various software, including: nginx, php/laravel/voyager, mysql. Nginx is a reverse proxy service that can forward HTTP/S requests to PHP for processing. mysql is the database server.

Prerequisites

*Ubuntu 18.04.4 LTS

*Internet Connection

*root user permission

Install docker



$ sudo apt-get install \

apt-transport-https \

ca-certificates \

curl \

gnupg-agent \

software-properties-common

$ curl -fsSL

$ sudo apt-key fingerprint 0EBFCD88

$ sudo add-apt-repository \

"deb [arch=amd64]

$(lsb_release -cs) \

stable"

$ sudo apt-get update

$ sudo apt-get install docker-ce docker-ce-cli containerd.io

$ sudo usermod -aG docker $USER

$ newgrp docker

$ sudo systemctl enable docker $ sudo apt-get update$ sudo apt-get install \apt-transport-https \ca-certificates \curl \gnupg-agent \software-properties-common$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -$ sudo apt-key fingerprint 0EBFCD88$ sudo add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) \stable"$ sudo apt-get update$ sudo apt-get install docker-ce docker-ce-cli containerd.io$ sudo usermod -aG docker $USER$ newgrp docker$ sudo systemctl enable docker

install docker-compose



$ sudo chmod +x /usr/local/bin/docker-compose

$ docker-compose --version $ sudo curl -L " https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose$ sudo chmod +x /usr/local/bin/docker-compose$ docker-compose --version

install php/mysql/nginx and the laravel/voyager framework

download laravel framework



$ cd ~/laravel-app

$ docker run --rm -v $(pwd):/app composer install

$ sudo chown -R $USER:$USER ~/laravel-app $ git clone https://github.com/laravel/laravel.git laravel-app$ cd ~/laravel-app$ docker run --rm -v $(pwd):/app composer install$ sudo chown -R $USER:$USER ~/laravel-app

Create file “docker-compose.yml” and “Dockerfile” in the laravel-app directory

docker-compose.yml:

version: '3'

services: #PHP Service

app:

build:

context: .

dockerfile: Dockerfile

image: php

container_name: app

restart: unless-stopped

tty: true

environment:

SERVICE_NAME: app

SERVICE_TAGS: dev

working_dir: /var/www

volumes:

- ./:/var/www

- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini

networks:

- app-network #Nginx Service

webserver:

image: nginx:alpine

container_name: webserver

restart: unless-stopped

tty: true

ports:

- "80:80"

- "443:443"

volumes:

- ./:/var/www

- ./nginx/conf.d/:/etc/nginx/conf.d/

networks:

- app-network #MySQL Service

db:

image: mysql:8.0.21

container_name: db

restart: unless-stopped

tty: true

ports:

- "3306:3306"

environment:

MYSQL_DATABASE: vfhdb

MYSQL_ROOT_PASSWORD: vfh@2019VFH

SERVICE_TAGS: dev

SERVICE_NAME: mysql

volumes:

- dbdata:/var/lib/mysql/

- ./mysql/my.cnf:/etc/mysql/my.cnf

networks:

- app-network #Docker Networks

networks:

app-network:

driver: bridge

#Volumes

volumes:

dbdata:

driver: local

Dockerfile:

FROM php:7.2-fpm # Copy composer.lock and composer.json

COPY composer.lock composer.json /var/www/ # Set working directory

WORKDIR /var/www # Install dependencies

RUN apt-get update && apt-get install -y \

build-essential \

libpng-dev \

libjpeg62-turbo-dev \

libfreetype6-dev \

locales \

zip \

jpegoptim optipng pngquant gifsicle \

vim \

unzip \

git \

curl # Clear cache

RUN apt-get clean && rm -rf /var/lib/apt/lists/* # Install extensions

RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl

RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/

RUN docker-php-ext-install gd

RUN curl -sS # Install composerRUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # Add user for laravel application

RUN groupadd -g 1000 www

RUN useradd -u 1000 -ms /bin/bash -g www www # Copy existing application directory contents

COPY . /var/www # Copy existing application directory permissions

COPY --chown=www:www . /var/www # Change current user to www

USER www # Expose port 9000 and start php-fpm server

EXPOSE 9000

CMD ["php-fpm"]

Configue PHP

Create PHP directory and configuration file.

$ cd ~/laravel-app/

$ mkdir php

$ cd php

$ vi local.ini

The following is the content of local.ini:

upload_max_filesize=40M

post_max_size=40M

Configue Nginx

$ mkdir -p ~/laravel-app/nginx/conf.d

$ vi ~/laravel-app/nginx/conf.d/app.conf

The following is the content of app.conf:

server {

listen 80;

index index.php index.html;

error_log /var/log/nginx/error.log;

access_log /var/log/nginx/access.log;

root /var/www/public;

location ~ \.php$ {

try_files $uri =404;

fastcgi_split_path_info ^(.+\.php)(/.+)$;

fastcgi_pass app:9000;

fastcgi_index index.php;

include fastcgi_params;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_path_info;

}

location / {

try_files $uri $uri/ /index.php?$query_string;

gzip_static on;

}

}

Configue Mysql

$ mkdir ~/laravel-app/mysql

$ cd ~/laravel-app/mysql

$ vi my.cnf

The following is the conent of my.cnf:

[mysqld]

general_log = 1

general_log_file = /var/lib/mysql/general.log

secure-file-priv= NULL

Configue laravel’s Database connection

$ cd ~/laravel-app/

$ cp .env.example .env

$ vi .env

The following is the content of .env :

DB_CONNECTION=mysql

DB_HOST=db

DB_PORT=3306

DB_DATABASE=mydb

DB_USERNAME=kenny

DB_PASSWORD=FullStackIsVeryCool2020

start all services(nginx,mysql,php)

$ docker-compose up -d

generate laravel app key

$ docker-compose exec app php artisan key:generate

create an user in mysql

We need to create the database user defined in .env file.

$ docker-compose exec db bash

#mysql -uroot -p

mysql>CREATE USER 'kenny'@'%' IDENTIFIED BY 'FullStackIsVeryCool2020';

mysql>GRANT ALL PRIVILEGES ON kenny.* TO 'kenny'@'%' WITH GRANT OPTION;

mysql>ALTER USER 'kenny'@'%' IDENTIFIED WITH mysql_native_password BY 'FullStackIsVeryCool2020';

mysql>exit;

#exit

Test database connection

$ docker-compose exec app php artisan migrate

Install Voyager framework

$ cd ~/laravel-app/

$ docker run --rm -v $(pwd):/app composer require tcg/voyager

$ docker-compose exec app php artisan voyager:install --with-dummy

$ docker-compose exec app php artisan config:cache

Enjoy it

Now, Pls visit http://you_server_ip_address/admin,

Use these email and password login:

admin@admin.com

password