docker-compose.yml file, and configure Nginx as a reverse proxy. This approach ensures that your MySQL database, PHP application, and web server start together in the correct order, providing a consistent and reproducible development environment without manual intervention or complex script dependencies.
Project Structure and Directory Setup
Before writing configuration files, establish a clean directory structure to keep your project organized. A standard layout separates configuration files from application code and persistent data. Create a root folder for your project, then inside it, create directories fornginx, php, and mysql. You should also create a data directory to store MySQL database files so they persist across container restarts.
my-lamp-project/
├── docker-compose.yml
├── nginx/
│ └── default.conf
├── php/
│ └── index.php
└── data/
└── mysql/
php directory lets you edit them on your host machine while the container serves them. Similarly, storing Nginx configuration in the nginx directory makes it easy to update routing rules without rebuilding images.
Configuring the Database Service
The core of any LAMP stack is the relational database. In Docker, you do not need to install MySQL manually; you simply pull the official image. However, you must ensure the database volume is mounted correctly to prevent data loss when containers are recreated. In yourdocker-compose.yml, define the db service using the mysql:5.7 or mysql:8.0 image. Set environment variables for the root password and initial database name. Crucially, map the /var/lib/mysql internal path to your local ./data/mysql directory. This step is vital because Docker containers are ephemeral; without this volume mapping, all your data will vanish if the container stops or crashes.
Service Definition Example
The service definition should look like this:
services:
db:
image: mysql:8.0
volumes:
- ./data/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: myapp
MYSQL_USER: user
MYSQL_PASSWORD: password
networks:
- app-network
By defining a custom network called app-network, you allow other services to communicate with the database using the service name db as the hostname, rather than dealing with dynamic IP addresses.
Setting Up PHP and Application Code
Next, configure the PHP service. Use the officialphp:8.1-apache or php:8.1-fpm image depending on whether you want Apache or Nginx to handle PHP processing. Since we are using Nginx as a front-end, using php-fpm is often more efficient. Mount your local ./php directory to the container's working directory.
You can also add extensions directly in the Dockerfile if needed, but for simple setups, mounting the code is sufficient. Ensure the permissions are set correctly so the web server can read your files. If you encounter permission issues, consider adding a usermod command in a custom Dockerfile, but for most local development, the default settings work fine.
Configuring Nginx as a Reverse Proxy
Nginx acts as the entry point for your application. It receives HTTP requests and forwards them to the PHP-FPM container. Create adefault.conf file in your nginx directory. This configuration file tells Nginx where to find the PHP socket and how to route traffic.
The server block should listen on port 80 and use the proxy_pass directive to send requests to the PHP service. You must also include the standard FastCGI parameters to ensure PHP variables like SCRIPT_FILENAME are passed correctly.
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
}
}
php:9000 refers to the service name defined in docker-compose.yml and the default port for PHP-FPM. If you modify this configuration later, refer to guides on Troubleshooting Nginx-proxy Ignoring Server Context to resolve common routing conflicts.
Defining the Compose File and Networking
Combine all services into thedocker-compose.yml file. Define the nginx service, mapping port 80 on your host to port 80 in the container. Mount the ./nginx/default.conf file to /etc/nginx/conf.d/default.conf inside the container. This overrides the default Nginx configuration provided by the image.
Ensure all services share the same app-network. This creates an isolated network bridge where services can discover each other by name. Without this shared network, Nginx would not be able to reach the PHP container, resulting in connection refused errors.
Final Compose Structure
Your final docker-compose.yml should list db, php, and nginx under the services key, all attached to app-network. The depends_on directive can be used to ensure the database starts before PHP, though proper health checks are preferred for production environments.
docker-compose up -d to start the stack in detached mode. Verify the status with docker-compose ps to ensure all containers are running. Visit http://localhost in your browser to see your PHP application served through Nginx.
If you face issues with secure connections later, check resources on SSL Certificates Not Generated with Certbot for Nginx in Docker to understand why automatic certificate generation might fail in your specific setup.
Frequently Asked Questions
Why does my Nginx return a 502 Bad Gateway error?
This usually happens when Nginx cannot connect to the PHP-FPM service. Check that the service name in thefastcgi_pass directive matches the service name in docker-compose.yml exactly. Also, verify that the PHP container is actually running and listening on port 9000.
How do I make sure my database data persists after restarting?
You must bind-mount the MySQL data directory. In your compose file, map a local directory (like./data/mysql) to /var/lib/mysql inside the container. Without this volume mount, any data written to the database will be lost when the container is removed or restarted.
Can I use Apache instead of Nginx for the LAMP stack?
Yes, you can replace the Nginx service with an Apache service. Simply change the image tohttpd and adjust the volume mounts accordingly. However, using Nginx as a reverse proxy in front of PHP-FPM is often recommended for better performance and flexibility in handling static files.
What should I do if PHP extensions are missing?
If you need additional PHP extensions, you can either build a custom image by extending the official PHP image with aDockerfile that installs the extensions, or use a pre-built image that includes them. Editing the docker-compose.yml to point to a custom image is the cleaner approach for maintaining dependencies.
How can I access the MySQL database from my host machine?
You can expose the MySQL port to your host by adding"3306:3306" to the ports section of the db service in docker-compose.yml. Then, use a database client like MySQL Workbench to connect to localhost using the credentials defined in your environment variables.