Important
This article is obsolete and may contain outdated information. Please refer to the most recent documentation for updated information.
Laravel is a free, open-source and most popular PHP framework that allows you to develop scalable and flexible web applications easily. It is a simple, flexible and lightweight PHP framework, and has a readable syntax for developing modern and powerful applications from scratch.
Laravel is the best choice for you if you are looking to design a next generation application.
In this tutorial, we will show you how to dockerizing a web application using the Laravel framework and Nginx as a web server on Ubuntu 18.04 server.
Prerequisites
• A server running Ubuntu 18.04 with a minimum 2 GB of RAM.
• A root password configured on your server.
Getting Started
Before starting, it is recommended to update your system with the latest version. You can do it by running the following command:
# apt-get update && apt-get upgrade -y
Once your system is updated, restart it to apply the changes.
Next, you will need to install some required packages in your system. You can install all of them by running the following command:
# apt-get install apt-transport-https ca-certificates curl software-properties-common -y
Once all the packages are installed, you can proceed to the next step.
Install Docker and Docker Compose
Next, you will need to install Docker and Docker Compose on your server. By default, the latest version of Docker is not available in the Ubuntu 18.04 default repository. So you will need to add the repository for that.
First, download and add the GPG key using the following command:
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
Next, add the Docker repository with the following command:
# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Once the repository is added, update your system and install Docker, Docker Compose by running the following command:
# apt-get update -y
# apt install docker-ce -y
# apt-get install docker-compose -y
Once the installation has been completed, you can check the status of Docker service with the following command:
# systemctl status docker
Download Laravel and Install Dependencies
Next, you will need to download the Laravel framework and install required dependencies with the PHP composer docker image.
First, download the latest version of the Laravel with the following command:
# git clone https://github.com/laravel/laravel.git laravel
Next, install the required dependencies with the following command:
# cd laravel
# docker run --rm -v $(pwd):/app composer install
You should see the following output:
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
The above command will creates an ephemeral container and copy the contents of your ~/laravel directory to the container.
Dockerizing the Laravel
Next, you will need to create a new docker-compose.yml file and Dockerfile for your Laravel project.
First, create a docker-compose file inside your ~/laravel directory:
# nano ~/laravel/docker-compose.yml
Add the following lines:
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: php:7.2-fpm-buster
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:5.7.22
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: password
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
Save and close the file when you are finished.
Next, create a Dockerfile to create custom images with all the software required by your application inside your ~/laravel directory:
# nano ~/laravel/Dockerfile
Add the following lines:
FROM php:7.2-fpm-buster
# 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 \
mysql-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
nano \
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
# Install composer
RUN 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"]
Save and close the file when you are finished.
Configure PHP, MySQL and Nginx for Laravel
Next, you will need to create a php directory inside your ~/laravel directory and create the local.ini inside ~/laravel/php directory:
# mkdir ~/laravel/php
# nano ~/laravel/php/local.ini
Add the following lines:
# upload_max_filesize=40M
# post_max_size=40M
Save and close the file when you are finished.
Next, you will need to create the nginx/conf.d directory inside your ~/laravel directory and create an app.conf file for Nginx configuration:
# mkdir -p ~/laravel/nginx/conf.d
# nano ~/laravel/nginx/conf.d/app.conf
Add the following lines:
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;
}
}
Save and close the file when you are finished.
Next, create the mysql directory inside your ~/laravel directory and create the my.cnf file with required settings:
# mkdir ~/laravel/mysql
# nano ~/laravel/mysql/my.cnf
Add the following lines:
[mysqld]
general_log = 1
general_log_file = /var/lib/mysql/general.log
Deploy Laravel with Nginx and MySQL Services
AT this point we have defined all services in the docker-compose file and create a configuration file for each service. Next, create a copy of the default .env.example file with the following command:
# cp .env.example .env
Now, run the following command to download all required docker images and build container services based on the 'docker-compose.yml' configuration with the following command:
# docker-compose up -d
Once the process is completed, you can check all the running containers with the following command:
# docker ps
You should see the following output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5a749a605dc digitalocean.com/php "docker-php-entrypoi…" About a minute ago Up About a minute 9000/tcp app
79eae5da35c6 mysql:5.7.22 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp db
b8181f7a7a54 nginx:alpine "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp webserver
Configure Laravel Container
Next, you will need to modify the .env file on the app container for Laravel.
You can easily edit any file inside the container with "docker-compose exec" command. First, open .env file inside app container with the following command:
# docker-compose exec app nano .env
Change the following lines:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=laraveluser
DB_PASSWORD=password
Save and close the file. Then, generate the Laravel application key and clear the cache with the following command:
# docker-compose exec app php artisan key:generate
# docker-compose exec app php artisan config:cache
Next, migrate the database using the following command.
docker-compose exec app php artisan migrate
Access Laravel Web Interface
At this point, we have dockerizing Laravel with Nginx and MySQL. Now, open your web browser and type the URL http://your-server-ip. You will be redirected to the Laravel default dashboard:
Congratulations! You have dockerized a web application using the Laravel framework and Nginx as a web server on Ubuntu 18.04 server.