Deploying a PHP Web App with Docker Compose, Nginx, and MariaDB: A Complete Guide

Suresh yadav
Cypik
Published in
5 min readMay 29, 2024

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

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

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

nano docker-compose.yml

3. Add the following configuration to thedocker-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:

sudo docker-compose up -d

5. Verify that the Nginx container is running:

sudo docker ps

You should see an output similar to this:

sudo docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dff2cb2c0197 nginx:latest "/docker-entrypoint.…" 9 seconds ago Up 8 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

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

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

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

3. Create a Dockerfile for the PHP container.

nano ~/docker-suresh-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

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

mkdir ~/docker-suresh-project/nginx

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

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

7. Add the following Nginx configuration to thedefault.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-suresh-project/nginx/Dockerfile

9. Add the following lines to the Dockerfile:

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

10. Update thedocker-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-suresh-project
sudo docker-compose up -d

12. Verify that the containers are running.

sudo docker ps

Open your web browser and access the URLhttp://your-server-ip 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.

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

2. Update thedocker-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:

sudo docker-compose up -d

3. Create a CLI session inside the MariaDB container:

docker exec -it [db container id] /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 theindex.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.

Enjoy it! 🍻 That’s It; we are done...

For seamless Cloud Management incorporating DevOps as the core of the methodology, reach out to us at info@cypik.com

Cypik

About the author:

My name is Suresh Yadav, and I am an experienced Linux enthusiast and DevOps engineer. I’m passionate about automating and streamlining development processes, and currently, I work as a DevOps Engineer at Cypik. I specialize in cloud technologies, with a focus on Google Cloud Platform (GCP), AWS cloud services, and Terraform’s ability to streamline operations and increase efficiency.

--

--

Suresh yadav
Cypik
Writer for

DevOps Engineer || GCP || Aws || Jenkins || Networking || Terraform || Github || Mysql || Ansible || Linux || Docker