Developers often deal with a lot of problems when trying to create a PHP Development Environment for a new application. And if we’re talking about a team instead of a single developer, they must deal with environment differences and the oft-quoted phrase “It works on my machine”.
I want to mitigate these problems by showing you how to create a development environment for a Symfony 2 PHP application using a couple of tools I’m sure you’ve heard of: Docker and Docker Compose.
Keep reading about developers management tools Is DevOps or Full Stack Developer a new Role?
Install the Tools
First of all, you’ll need to install the following tools:
- Docker: https://docs.docker.com/engine/installation
- Docker Compose: https://docs.docker.com/compose/install
- PHP 5.6+ (CLI): This is only needed to use Composer – Install this package using your S.O package manager.
- Composer: https://getcomposer.org/download
- Symfony Installer: Follow the instructions on this link to download the Symfony installer.
If you’ve never heard of any of these applications, each of the sites mentioned in this list has a great deal of information on them.
Create a Sample Application to try PHP Development Environment
The following command creates a Symfony application on directory sample_application:
symfony new sample_application;
Next, install all the vendors with Composer (we use –ignore-platform-reqs to avoid problems with required extensions; everything we need is inside our Docker container so we can’t skip requirements outside from it):
composer install --ignore-platform-reqs;
Now create a docker/docker-compose.yml file that contains the following:
Basically, we are creating a single container with PHP 7.0 + Apache. We will use port 10080 to access port 80 inside the container (which is the Apache default port). Our main entrypoint is /development/docker/start.sh which will execute initialization tasks. Our project will be mounted on directory /development inside the container. Additionally, we add the environment variable TERM with xterm value to fix issues on the terminal like not being able to use the clear command, etc.
Next step, create a custom ini file for PHP on file docker/php.ini with the following contents:
Now, create a file docker/start.sh with the following contents:
echo "Starting our Sample PHP Application...";
echo "Adding php.ini...";
cp -f /development/docker/php.ini /usr/local/etc/php/conf.d/php.ini;
echo "Installing and enabling xdebug"
pecl install xdebug;
echo "Creating symlink to htdocs...";
rm -f /var/www/html/sample_app;
ln -s /development/web /var/www/html/sample_app;
echo "Starting Apache...";
service apache2 start;
echo "Initializing log files...";
touch var/logs/dev.log var/logs/prod.log /tmp/php_errors.log;
chown www-data:www-data /development/var/logs/*.log;
echo "Warming up cache...";
bin/console cache:warmup --env=dev;
bin/console cache:warmup --env=prod;
echo "Giving permissions to logs, cache and sessions folders...";
chown -R www-data:www-data var/cache var/logs var/sessions;
echo "Tailing logs...";
tail -F /development/var/logs/dev.log /development/var/logs/prod.log /tmp/php_errors.log;
Remember to give execution permissions to this script:
chmod +x docker/start.sh;
Now, create your Docker container with the following command:
docker-compose -f docker/docker-compose.yml up;
This will create the Docker container with your development environment, and it will tail all the logs you need to start working.
You can access your Docker container with the following command:
docker exec -ti my_sample_php_app bash;
Now, enter the following URL:
You are ready to start working on your application, making sure your team has exactly the same development environment. This example environment has Apache and PHP 7.0, but you can choose any Docker image that fits your needs.
Check out the following link which contains all of the PHP official Docker images:
PHP Docker Images
If you need to customize your environment to install additional libraries or extensions, just edit the file docker/start.sh. You can also create a Dockerfile.
It’s critical to have a development environment that you can recreate and share with your team. Using Docker and Docker Compose is a very easy way to achieve this objective. You can imitate as much of your production environment as possible. The smaller the differences between both environments, the less you’ll have to worry about unexpected surprises when deploying your application.
Further reading of Docker configurations on Configuring Docker Containers