Initial Commit, Working Template

This commit is contained in:
DylanBanta 2024-11-24 22:31:15 -05:00
commit 613967e9f4
8 changed files with 299 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
logs/*

28
Dockerfile.template Normal file
View File

@ -0,0 +1,28 @@
# Use the official PHP image with FPM
FROM php:${PHP_VERSION}
# Set working directory
WORKDIR /var/www/html
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions required by Laravel
RUN docker-php-ext-install ${PHP_EXTENSIONS}
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Expose port 9000 for PHP-FPM
EXPOSE 9000
# Default command to start PHP-FPM
CMD ["php-fpm"]

61
README.md Normal file
View File

@ -0,0 +1,61 @@
### **How It Works**
1. **Create Environment Files**:
- Copy `config/example.env` to a new file (e.g., `config/testing.env`) and edit the variables.
2. **Run `setup.sh`**:
- Pass the `.env` file as an argument:
```bash
./setup.sh config/testing.env
```
3. **Generated Output**:
- `setup.sh` replaces all `{PROJECT_NAME}` placeholders dynamically.
- Example for `PROJECT_NAME=testing`:
- `APP_SERVICE_NAME` becomes `app-testing`.
- `MYSQL_DATABASE` becomes `testing_db`.
---
### **Test Example**
**Input**: `config/testing.env`:
```env
# General Configurations
PROJECT_NAME=testing.example.com
APP_SERVICE_NAME=app-{PROJECT_NAME}
# Network
NETWORK_NAME={PROJECT_NAME}_network
# Nginx
NGINX_SERVICE_NAME=nginx-{PROJECT_NAME}
NGINX_PORT=4444
# Database Configuration
MYSQL_SERVICE_NAME=mysql-{PROJECT_NAME}
MYSQL_DATABASE={PROJECT_NAME}_db
MYSQL_USER={PROJECT_NAME}_user
MYSQL_PASSWORD=my_password
MYSQL_ROOT_PASSWORD=root_password
MYSQL_PORT=4445
# Dockerfile Configurations
PHP_VERSION=8.3-fpm
PHP_EXTENSIONS=pdo_mysql mbstring exif pcntl bcmath gd
```
**Command**:
```bash
./setup.sh config/testing.env
```
**Output Directory**:
```
testing.example.com/
├── Dockerfile
├── docker-compose.yml
├── app-volume-testing/
├── nginx-volume-testing/
│ └── default.conf
├── mysql-volume-testing/
```

22
config/example.env Normal file
View File

@ -0,0 +1,22 @@
# General Configurations
PROJECT_NAME=example.com
APP_SERVICE_NAME=app-{PROJECT_NAME}
# Network
NETWORK_NAME={PROJECT_NAME}_network
# Nginx
NGINX_SERVICE_NAME=nginx-{PROJECT_NAME}
NGINX_PORT=80
# Database Configuration
MYSQL_SERVICE_NAME=mysql-{PROJECT_NAME}
MYSQL_DATABASE={PROJECT_NAME}_db
MYSQL_USER={PROJECT_NAME}_user
MYSQL_PASSWORD=my_password
MYSQL_ROOT_PASSWORD=root_password
MYSQL_PORT=3306
# Dockerfile Configurations
PHP_VERSION=8.3-fpm
PHP_EXTENSIONS=pdo_mysql mbstring exif pcntl bcmath gd

22
config/testing.env Normal file
View File

@ -0,0 +1,22 @@
# General Configurations
PROJECT_NAME=testing.example.com
APP_SERVICE_NAME=app-{PROJECT_NAME}
# Network
NETWORK_NAME={PROJECT_NAME}_network
# Nginx
NGINX_SERVICE_NAME=nginx-{PROJECT_NAME}
NGINX_PORT=4444
# Database Configuration
MYSQL_SERVICE_NAME=mysql-{PROJECT_NAME}
MYSQL_DATABASE={PROJECT_NAME}_db
MYSQL_USER={PROJECT_NAME}_user
MYSQL_PASSWORD=my_password
MYSQL_ROOT_PASSWORD=root_password
MYSQL_PORT=4445
# Dockerfile Configurations
PHP_VERSION=8.3-fpm
PHP_EXTENSIONS=pdo_mysql mbstring exif pcntl bcmath gd

View File

@ -0,0 +1,47 @@
services:
${APP_SERVICE_NAME}:
build:
context: .
dockerfile: Dockerfile
image: ${PROJECT_NAME}-app
container_name: ${APP_SERVICE_NAME}
restart: unless-stopped
working_dir: /var/www/html
volumes:
- ./app-volume-${PROJECT_NAME}:/var/www/html
networks:
- ${NETWORK_NAME}
${NGINX_SERVICE_NAME}:
image: nginx:latest
container_name: ${NGINX_SERVICE_NAME}
restart: unless-stopped
ports:
- "${NGINX_PORT}:80"
volumes:
- ./app-volume-${PROJECT_NAME}:/var/www/html
- ./nginx-volume-${PROJECT_NAME}/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- ${APP_SERVICE_NAME}
networks:
- ${NETWORK_NAME}
${MYSQL_SERVICE_NAME}:
image: mysql:8.1
container_name: ${MYSQL_SERVICE_NAME}
restart: unless-stopped
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
ports:
- "${MYSQL_PORT}:3306"
volumes:
- ./mysql-volume-${PROJECT_NAME}:/var/lib/mysql
networks:
- ${NETWORK_NAME}
networks:
${NETWORK_NAME}:
driver: bridge

24
nginx.conf.template Normal file
View File

@ -0,0 +1,24 @@
server {
listen 80;
server_name localhost;
root /var/www/html/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass ${APP_SERVICE_NAME}:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}

94
setup.sh Executable file
View File

@ -0,0 +1,94 @@
#!/bin/bash
# Check if the environment file is provided
if [ -z "$1" ]; then
echo "Usage: $0 <environment file>"
echo "Example: $0 config/testing.env"
exit 1
fi
# Load the environment file
ENV_FILE=$1
if [ ! -f "$ENV_FILE" ]; then
echo "Environment file '$ENV_FILE' not found."
exit 1
fi
# Function to replace placeholders in variables
function process_placeholders() {
local input=$1
echo "$input" | sed -e "s/{PROJECT_NAME}/${PROJECT_NAME}/g"
}
# Source and process the environment variables
declare -A ENV_VARS
while IFS='=' read -r key value; do
key=$(echo "$key" | xargs) # Trim whitespace
value=$(echo "$value" | xargs) # Trim whitespace
if [[ -n "$key" && "$key" != "#"* ]]; then
processed_value=$(process_placeholders "$value")
export "$key=$processed_value"
ENV_VARS["$key"]="$processed_value"
fi
done < "$ENV_FILE"
# Create the project directory
PROJECT_DIR="../${PROJECT_NAME}"
echo "Creating project directory: $PROJECT_DIR"
mkdir -p "$PROJECT_DIR"
# Create project-specific subdirectories
mkdir -p "$PROJECT_DIR/app-volume-${PROJECT_NAME}"
mkdir -p "$PROJECT_DIR/nginx-volume-${PROJECT_NAME}"
mkdir -p "$PROJECT_DIR/mysql-volume-${PROJECT_NAME}"
# Generate Nginx configuration
echo "Generating Dockerfile..."
sed \
-e "s|\${APP_SERVICE_NAME}|${APP_SERVICE_NAME}|g" \
< nginx.conf.template > "$PROJECT_DIR/nginx-volume-$PROJECT_NAME/default.conf"
# Generate Dockerfile
echo "Generating Dockerfile..."
sed \
-e "s|\${PHP_VERSION}|${PHP_VERSION}|g" \
-e "s|\${PHP_EXTENSIONS}|${PHP_EXTENSIONS}|g" \
< Dockerfile.template > "$PROJECT_DIR/Dockerfile"
# Generate docker-compose.yml
echo "Generating docker-compose.yml..."
sed \
-e "s|\${PROJECT_NAME}|${PROJECT_NAME}|g" \
-e "s|\${APP_SERVICE_NAME}|${APP_SERVICE_NAME}|g" \
-e "s|\${NGINX_SERVICE_NAME}|${NGINX_SERVICE_NAME}|g" \
-e "s|\${MYSQL_SERVICE_NAME}|${MYSQL_SERVICE_NAME}|g" \
-e "s|\${NETWORK_NAME}|${NETWORK_NAME}|g" \
-e "s|\${NGINX_PORT}|${NGINX_PORT}|g" \
-e "s|\${MYSQL_PORT}|${MYSQL_PORT}|g" \
-e "s|\${MYSQL_DATABASE}|${MYSQL_DATABASE}|g" \
-e "s|\${MYSQL_USER}|${MYSQL_USER}|g" \
-e "s|\${MYSQL_PASSWORD}|${MYSQL_PASSWORD}|g" \
-e "s|\${MYSQL_ROOT_PASSWORD}|${MYSQL_ROOT_PASSWORD}|g" \
< docker-compose.yml.template > "$PROJECT_DIR/docker-compose.yml"
# Start the containers
echo "Starting containers..."
cd "$PROJECT_DIR" || exit
docker compose up --build -d
# Wait for the app container to become ready
echo "Waiting for app container (${APP_SERVICE_NAME}) to initialize..."
while ! docker exec -it "${APP_SERVICE_NAME}" bash -c "php -v" >/dev/null 2>&1; do
echo "Waiting for ${APP_SERVICE_NAME} to become ready..."
sleep 2
done
# Run Composer to create the Laravel project inside the container
echo "Creating Laravel project inside the app container..."
docker exec -it "${APP_SERVICE_NAME}" bash -c "composer create-project --prefer-dist laravel/laravel ."
# Adjust permissions for Laravel's storage and cache directories
echo "Setting permissions for Laravel directories..."
docker exec -it "${APP_SERVICE_NAME}" bash -c "chown -R www-data:www-data /var/www/html && chmod -R 775 /var/www/html"
echo "Laravel project setup complete. Access your application at http://<your-server>:${NGINX_PORT}"