Deploying a PHP Web App with Docker Compose, Nginx, and MariaDB

Rapidcode Technologies
5 min readSep 23, 2023

--

Introduction: In this tutorial, we will walk you through the process of deploying a PHP web application using Docker Compose, Nginx as the web server, and MariaDB as the database. Docker Compose simplifies the management of multi-container applications, making it easy to set up and run your PHP web app with all its dependencies.

Let’s dive into the steps to create and deploy this PHP web application.

Step 1 — Create a Nginx Container

Before we begin, we’ll set up an Nginx container to host our PHP application. Follow these steps:

  1. Create a directory for your project and navigate to it:
mkdir ~/docker-project
cd ~/docker-project

2. Create a docker-compose.yml file for launching the Nginx container:

nano docker-compose.yml

3. Add the following configuration to the docker-compose.yml file:

version: "3.9"
services:
nginx:
image: nginx:latest
container_name: nginx-container
ports:
- 80:80

This configuration ensures that the Nginx container runs on port 80. Save and close the file.

4. Launch the Nginx container:

docker-compose up -d

5. Verify that the Nginx container is running:

docker ps

You should see an output similar to this:

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                               NAMES
c6641e4d5bbf nginx:latest "/docker-entrypoint.…" 5 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-container

6. Open your web browser and access your Nginx container using the URL http://your-server-ip. You should see the Nginx test page.

Step 2 — Create a PHP Container

In this step, we’ll set up a PHP container to host our PHP application. Follow these steps:

  1. Create a directory for your PHP code inside your project:
mkdir ~/docker-project/php_code

2. Clone your PHP code into the php_code directory. Replace [GitHub URL] with the actual URL of your PHP code:

git clone https://github.com/rapidcode-technologies-private-limited/php-e-commerce.git ~/docker-project/php_code/

3. Create a Dockerfile for the PHP container:

nano ~/docker-project/php_code/Dockerfile

4. Add the following lines to the Dockerfile:

FROM php:7.0-fpm
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN docker-php-ext-enable mysqli

Save and close the file.

5. Create a directory for Nginx inside your project directory:

mkdir ~/docker-project/nginx

6. Create an Nginx default configuration file to run your PHP application:

nano ~/docker-project/nginx/default.conf

7. Add the following Nginx configuration to the default.conf file:

server {  

listen 80 default_server;
root /var/www/html;
index index.html index.php;

charset utf-8;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

access_log off;
error_log /var/log/nginx/error.log error;

sendfile off;

client_max_body_size 100m;

location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}

location ~ /.ht {
deny all;
}
}

Save and close the file.

8. Create a Dockerfile inside the nginx directory to copy the Nginx default config file:

nano ~/docker-project/nginx/Dockerfile

9. Add the following lines to the Dockerfile:

FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf

10. Update the docker-compose.yml file with the following contents:


version: "3.9"
services:
nginx:
build: ./nginx/
ports:
- 80:80

volumes:
- ./php_code/:/var/www/html/

php:
build: ./php_code/
expose:
- 9000
volumes:
- ./php_code/:/var/www/html/

11. Launch the containers:

cd ~/docker-project
docker-compose up -d

12. Verify that the containers are running:

docker ps

Open your web browser and access the URL http://your-server-ip or localhost. You should now see your PHP web content.

Step 3 — Create a MariaDB Container

In this final step, we’ll set up a MariaDB database container and configure it to work with our PHP application.

Follow these steps:

  1. Edit the docker-compose.yml file to add an entry for a MariaDB container:
nano ~/docker-project/docker-compose.yml

2. Update the docker-compose.yml file with the provided MariaDB configuration.


version: "3.9"
services:
nginx:
build: ./nginx/
ports:
- 80:80

volumes:
- ./php_code/:/var/www/html/

php:
build: ./php_code/
expose:
- 9000
volumes:
- ./php_code/:/var/www/html/


db:
image: mariadb
volumes:
- mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: mariadb
MYSQL_DATABASE: ecomdb


volumes:
mysql-data:

Run the following command:

docker-compose up -d

3. Create a CLI session inside the MariaDB container:

docker exec -it [db container id or name] /bin/sh

4. Access MariaDB as the root user:

mariadb -u root -pmariadb

5. Create a new user for the database:

CREATE USER 'rapidcode'@'%' IDENTIFIED BY "rapidcode123";

6. Grant all privileges to the new user:

GRANT ALL PRIVILEGES ON *.* TO 'rapidcode'@'%';
FLUSH PRIVILEGES;

7. Exit the MariaDB shell:

exit

8. Load product inventory information into the database:


cat > db-load-script.sql <<-EOF
USE ecomdb;
CREATE TABLE products (id mediumint(8) unsigned NOT NULL auto_increment,Name varchar(255) default NULL,Price varchar(255) default NULL, ImageUrl varchar(255) default NULL,PRIMARY KEY (id)) AUTO_INCREMENT=1;

INSERT INTO products (Name,Price,ImageUrl) VALUES ("Laptop","100","c-1.png"),("Drone","200","c-2.png"),("VR","300","c-3.png"),("Tablet","50","c-5.png"),("Watch","90","c-6.png"),("Phone Covers","20","c-7.png"),("Phone","80","c-8.png"),("Laptop","150","c-4.png");

EOF

9. Run the SQL script:

mariadb -u root -pmariadb < db-load-script.sql

10. Exit the MariaDB container shell:

exit

11. Make sure that the index.php file in your PHP code is properly configured with the username and password that we have created above to connect to the MariaDB database:

Check the URL again and refresh it. You should now see your PHP web application fetching data from the MariaDB database.

--

--

Rapidcode Technologies

Architecting the future of innovation and design with cloud-native skills. 🌟 Let's transform your business! 🌟 #Innovation #Perseverance