How to Use Docker Compose to Deploy a LAMP Stack on Ubuntu
How to Use Docker Compose to Deploy a LAMP Stack on Ubuntu
Deploying a LAMP stack (Linux, Apache, MySQL, and PHP) can be a straightforward process, especially when using Docker Compose. This tool allows you to define and manage multi-container Docker applications with ease. In this guide, we will walk through the steps to set up a LAMP stack on an Ubuntu server using Docker Compose, ensuring that you can efficiently manage and scale your applications.
Prerequisites
Before we begin, make sure you have the following:
- Ubuntu Server: Ensure you have an Ubuntu server running (18.04 or later).
- Docker: Install Docker on your server if you haven’t done so already. You can install Docker by following these commands:
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
Docker Compose: Install Docker Compose by running the following command:
sudo apt install docker-compose
Basic Knowledge of Terminal Commands: Familiarity with the command line will be helpful as we proceed through the setup.
Step 1: Create a Project Directory
First, create a new directory for your LAMP stack project. This directory will contain the docker-compose.yml file and any additional files you may need.
mkdir ~/lamp-stack
cd ~/lamp-stack
Step 2: Create the docker-compose.yml File
Next, create a file named docker-compose.yml in your project directory. This YAML file will define the services that make up your LAMP stack.
version: ‘3.8’
services:
web:
image: php:7.4-apache
container_name: lamp_web
ports:
– “80:80”
volumes:
– ./html:/var/www/html
networks:
– lamp_networkdb:
image: mysql:5.7
container_name: lamp_db
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: mydatabase
MYSQL_USER: user
MYSQL_PASSWORD: userpassword
networks:
– lamp_network
- networks:
- lamp_network:
- driver: bridge
- Explanation of the docker-compose.yml File
- Version: Specifies the Docker Compose file format version.
- Services: Defines the individual services for the application.
- web: This service uses the PHP 7.4 Apache image, maps port 80 of the container to port 80 of the host, and mounts a local directory (./html) to the container’s web root (/var/www/html).
- db: This service uses the MySQL 5.7 image. It sets up the root password and creates a database and user with defined permissions.
- Networks: Defines a custom network for communication between containers.
Step 3: Create the HTML Directory
Now, create the html directory where you will store your web files.
mkdir html
You can create an index.php file inside this directory to test your setup.
echo “” > html/index.php
Step 4: Start the LAMP Stack
With everything in place, you can now start your LAMP stack using Docker Compose.
docker-compose up -d
This command will start your containers in detached mode. You can check the status of your containers by running:
docker-compose ps
Step 5: Access Your Application
Once your containers are running, open a web browser and navigate to http://your_server_ip. You should see the PHP information page if everything has been set up correctly. This page confirms that your PHP is working with Apache.
Step 6: Managing Your LAMP Stack
Stopping the Stack
To stop your services, run:
docker-compose down
Viewing Logs
To view the logs for your running services, use the following command:
docker-compose logs
Executing Commands in Containers
If you need to execute commands inside your containers, you can do so with:
docker exec -it lamp_web bash
This command will open a bash shell in the lamp_web container, allowing you to interact with the environment directly.
For further reading and advanced configurations, refer to the following resources:
Docker Compose Documentation
PHP Docker Official Images
MySQL Docker Official Images
We appreciate your visit to our page! If you’re interested in exploring more articles about Linux systems and using OwnCloud for personal cloud storage, feel free to check out the links below.
How to Install and Use OwnCloud for Personal Cloud Storage
Additionally, by renting a server from our site, you can perform your tests in a reliable and efficient environment, allowing you to enhance your skills more quickly. Keep up the great work! 🙂