[Tutorial] One Single Host - Multiple AppGini Apps with Different PHP Versions Using Docker

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
User avatar
D Oliveira
AppGini Super Hero
AppGini Super Hero
Posts: 348
Joined: 2018-03-04 09:30
Location: David

[Tutorial] One Single Host - Multiple AppGini Apps with Different PHP Versions Using Docker

Post by D Oliveira » 2024-02-17 03:35

Struggling with hosting multiple AppGini applications on a single host machine due to differing PHP version requirements? This tutorial will help you encapsulate each app in its own Docker container, allowing for simultaneous hosting of multiple PHP versions.

Requirements: Configuration Steps:

1. Modify config.php

Code: Select all

$dbServer = 'localhost';
$dbUsername = 'root';
$dbPassword = 'root';
$dbDatabase = 'mydatabase';
2. Create docker-compose.yml

Code: Select all

version: '3.8'
services:
  app:
    build: .
    ports:
      - "81:80"
    volumes:
      - .:/var/www/html
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=mydatabase
3. Create Dockerfile

Code: Select all

FROM php:7.4-apache

# Install system dependencies
RUN apt-get update && apt-get install -y default-mysql-server

# Install PHP extensions
RUN docker-php-ext-install mysqli pdo pdo_mysql

# Set the default MySQL socket for PHP
RUN echo "mysqli.default_socket=/var/run/mysqld/mysqld.sock" > /usr/local/etc/php/conf.d/mysql_socket.ini

# Copy your PHP application to the container
COPY . /var/www/html/
RUN chown -R www-data:www-data /var/www/html && \
    find /var/www/html/ -type d -exec chmod 755 {} \; && \
    find /var/www/html/ -type f -exec chmod 644 {} \;

# Copy the SQL dump file to the container
COPY mydatabase.sql /mydatabase.sql

# Copy custom MySQL configuration file
COPY my_custom.cnf /etc/mysql/conf.d/my_custom.cnf

# Copy the entrypoint script to the container
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Expose port 80 to the outside world
EXPOSE 80

# Set the working directory to the root of apache
WORKDIR /var/www/html

# Start Apache and MySQL services
ENTRYPOINT ["/entrypoint.sh"]
4. Create my_custom.cnf

Code: Select all

[mysqld]
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
socket=/var/run/mysqld/mysqld.sock
datadir=/var/lib/mysql

# Custom settings to allow larger row sizes
innodb_strict_mode = 0
innodb_file_format = Barracuda
innodb_file_per_table = 1
innodb_large_prefix = 1
5. Create entrypoint.sh

Code: Select all

#!/bin/bash
set -e

# Fix ownership and permissions of the MySQL data directory
chown -R mysql:mysql /var/lib/mysql
chmod 755 /var/lib/mysql

# Start MySQL in the background
mysqld_safe --skip-networking &

# Wait for MySQL to start
while ! mysqladmin ping --silent; do
    sleep 1
done

# Create the database and import the SQL dump
mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS mydatabase;"
mysql -uroot -proot mydatabase < /mydatabase.sql

# Update root user with new password and grant privileges
mysql -uroot -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;"

# Start Apache in the foreground
apache2-foreground

To start each container cd to your app directory then run:

Code: Select all

docker-compose up -d --build  
and if you wish to delete everything:

Code: Select all

docker-compose down 
docker system prune -a --volumes --force
To access your apps:

Code: Select all

http://localhost:81/index.php

http://localhost:82/index.php

http://localhost:83/index.php
End Note:
By following this tutorial, you can host multiple AppGini apps with different PHP versions on the same machine. Each app will run in its own Docker container, accessible on a unique port. Enjoy your versatile hosting environment!

Image

--

. Dockerfile for Other PHP Versions
Duplicate the Dockerfile and other files and replace the base image with the desired PHP version. Additionally, adjust the ports in docker-compose.yml accordingly, for example:

Code: Select all

FROM php:7.4-apache

Code: Select all

ports:
  - "82:80" # Set this to 82 on your next version on another php version, 83, 84.... you get the idea

User avatar
D Oliveira
AppGini Super Hero
AppGini Super Hero
Posts: 348
Joined: 2018-03-04 09:30
Location: David

Re: [Tutorial] One Single Host - Multiple AppGini Apps with Different PHP Versions Using Docker

Post by D Oliveira » 2024-02-17 16:32

+ a friendly html snippet so that you can bookmark the page and have quick access to your apps ;)

Code: Select all

<!DOCTYPE html>
<html>
    <head>
        <style>
            body {
                font-family: Arial, sans-serif;
                background-color: #f2f2f2;
                text-align: center;
                padding-top: 50px;
            }

            .container {
                background-color: #fff;
                width: 300px;
                margin: 0 auto;
                padding: 20px;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            }

            button {
                background-color: #4CAF50;
                color: white;
                padding: 14px 20px;
                margin: 10px 0;
                border: none;
                cursor: pointer;
                width: 100%;
                transition: 0.3s;
            }

            button:hover {
                background-color: #45a049;
            }

            br {
                line-height: 1.5;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <button onclick="window.location.replace('http://localhost:81')">App 1</button>
            <br>
            <button onclick="window.location.replace('http://localhost')">App2</button>
        </div>
    </body>
</html>

User avatar
D Oliveira
AppGini Super Hero
AppGini Super Hero
Posts: 348
Joined: 2018-03-04 09:30
Location: David

Re: [Tutorial] One Single Host - Multiple AppGini Apps with Different PHP Versions Using Docker

Post by D Oliveira » 2024-02-17 17:27

small correction, we wanna create the db and import the dump only if it doesn't exist

entrypoint.sh

Code: Select all

#!/bin/bash
set -e

# Fix ownership and permissions of the MySQL data directory
chown -R mysql:mysql /var/lib/mysql
chmod 755 /var/lib/mysql

# Start MySQL in the background
mysqld_safe --skip-networking &

# Wait for MySQL to start
while ! mysqladmin ping --silent; do
    sleep 1
done

# Check if the mydatabase database exists
DB_EXISTS=$(mysql -uroot -proot -sse "SELECT 1 FROM information_schema.tables WHERE table_schema='mydatabase' LIMIT 1;")

# Import the SQL dump only if the database does not exist
if [ -z "$DB_EXISTS" ]; then
    mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS mydatabase;"
    mysql -uroot -proot mydatabase < /mydatabase.sql
fi


# Update root user with new password and grant privileges
mysql -uroot -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;"

# Start Apache in the foreground
apache2-foreground

Post Reply