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.
Note: The instructions below use Docker but in case that is not an option, you can follow this well written guide that shows the traditional approach of setting up on a Linux machine directly. If you don’t have access to a Linux system you can get one for free using the AWS Free tier. Or rent one from Digital Ocean for as low as $5 per month. For the guide below I am assuming we are using Docker.
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:
Setup the directory structure
Next, In the same directory create a
data directory to house your MySQL data files and then a
src directory will be accessible to your website root as shown below.
At this point your directory structure should look like this:
Important: You can save time here by cloning the following git repository
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 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.
Thats it! your are all set. Here is the whole exercise in one go for reference:
$ 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
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.
As always, don’t forget to visit my blog at Effective Programmer for great ideas on improving yourself as a software engineer :)