How To Install Linux, Nginx, MySQL, PHP (LEMP stack) in under 10 minutes using Docker

Recently, for my Effective Programmer blog I wanted to showcase performance improvements for a RESTful API and I decided to use PHP. I realized that not everyone has a ready to go LEMP environment at their disposal. So, here is a simple guide on how to set one up quickly using Docker.

What is LEMP

In case you don’t already know LEMP stands for Linux OS, with an Nginx (pronounced “Engine-X”) web server. A MySQL database and PHP as the programming language.

Photo by Buenosia Carol from Pexels

Setup Docker and Docker Compose

Docker is a container management system that allows you run a Linux system on any host machine. Its free and pretty easy to install and run on virtually any modern computer. I have a Mac so I used brew but the instructions on Docker’s website are also very easy to follow.

Create a Docker Compose File

Once Docker is up and running on your computer the first step is to create a new directory for your application and start by creating a docker-compose.yml file as follows:

version: '3'
services
:
web:
image: naveed125/nginx-php-fpm:latest
restart: always
ports:
- "80:80"
volumes
:
- ./src:/var/www/html
links:
- db
environment:
- DEPLOYMENT=development

db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: ********
MYSQL_DATABASE: appdb
MYSQL_USER: appdbuser
MYSQL_PASSWORD: ********
volumes:
- ./data/mysql:/var/lib/mysql

Setup the directory structure

Next, In the same directory create a data directory to house your MySQL data files and then a src directory for your application source files as well as other web assets like any html, css and javascript source. Just remember that any file in this src directory will be accessible to your website root as shown below.

+ app
+ data
+ src
- index.php
- docker-compose.yml
git clone https://github.com/naveed125/docker-lemp-app.git

Start the LEMP environment

The last part of this exercise is to start your docker LEMP instance

docker-compose pull
docker-compose up -d

Test your setup

Setup is complete, try hitting http://localhost using your browser or curl command to make sure its working. Note again that anything you put in your src directory will be available through your web server as we are mapping the volume in the docker-compose file.

$ git clone https://github.com/naveed125/docker-lemp-app.git
Cloning into 'docker-lemp-app'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 12 (delta 0), reused 8 (delta 0), pack-reused 0
Unpacking objects: 100% (12/12), done.
$ cd docker-lemp-app/$ docker-compose pull
Pulling db ... done
Pulling web ... done
$ docker-compose -p lemp up -d
Creating network "lemp_default" with the default driver
Creating lemp_db_1 ... done
Creating lemp_web_1 ... done
$ curl localhost
OK - Sun, 12 May 19 00:56:49 +0000
$ docker-compose -p lemp down
Stopping lemp_web_1 ... done
Stopping lemp_db_1 ... done
Removing lemp_web_1 ... done
Removing lemp_db_1 ... done
Removing network lemp_default

Additional Thoughts

This kind of docker-compose based environment is great way to get up and running in a development system but not really recommended for a production environment. Let me know in the comments below if you are interested and I can write about setting up a production PHP environment.

Technology leader, entrepreneur and angel investor. Director of Engineering at Kinoo. I write about software engineering at medium and effective-programmer.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store