Home >Backend Development >PHP Tutorial >Docker for PHP begginers as simple as possible
This tutorial guides beginners on setting up a simple PHP development environment using Docker. While I'm using Windows with WSL2, the process is largely the same for macOS and Linux. This approach prioritizes simplicity; advanced configurations are omitted.
Prerequisites:
Docker must be installed. Instructions vary by operating system but are generally straightforward. For Windows users, WSL2 is highly recommended (see Microsoft's documentation on installing Linux on Windows with WSL).
Steps:
Start Docker: Ensure the Docker Desktop application is running. The running status is usually indicated by an icon in your system tray.
Open Your IDE: Launch your preferred PHP IDE or editor (I use IntelliJ PHPStorm).
Create a Project: Create a new project within your Ubuntu WSL2 environment. (Example path: \wsl.localhost\Ubuntu\home\development\docker-php-simple
).
Project Structure: Create the following directory structure and files within your project:
File Content: Add the following code to the respective files:
public/index.php
:
<code class="language-php"><?php echo 'Hello, Developer!'; ?></code>
.docker/apache/sites-available/000-default.conf
:
<code class="language-apache"><VirtualHost *:80> DocumentRoot "/var/www/html/public" </VirtualHost></code>
docker-compose.yml
:
<code class="language-yaml">version: "3.9" services: app: image: php:8.4-apache container_name: docker-php-simple tty: true volumes: - ./:/var/www/html - ./.docker/apache/sites-available/000-default.conf:/etc/apache2/sites-available/000-default.conf ports: - "8080:80"</code>
Running the Application:
Choose one of the following methods:
Option 1: Using Your IDE's Docker Plugin
docker-compose.yml
in your IDE.Option 2: Using the Command Line
docker-compose up -d
After the containers start, your application should be accessible.
Verification:
Check the Docker application; you should see a running container named docker-php-simple
. The container's status should be "Running." You can manage it (stop, restart, view logs) from the Docker dashboard.
Access your application in your browser at http://localhost:8080/
. You should see "Hello, Developer!" displayed.
The above is the detailed content of Docker for PHP begginers as simple as possible. For more information, please follow other related articles on the PHP Chinese website!