Skip to content

Commit e65d026

Browse files
committed
Fix Redis connection and update routes for deployment
1 parent a2368b3 commit e65d026

File tree

10 files changed

+345
-21
lines changed

10 files changed

+345
-21
lines changed

docker/php/Dockerfile.fixed

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
FROM php:8.1-fpm-alpine as php-base
2+
3+
# Install system dependencies
4+
RUN apk add --update --no-cache \
5+
bash \
6+
curl \
7+
shadow \
8+
nginx \
9+
supervisor
10+
11+
# Install PHP extensions dependencies
12+
RUN apk add --update --no-cache \
13+
freetype \
14+
libjpeg-turbo \
15+
libpng \
16+
libzip \
17+
zip
18+
19+
# Install build dependencies including autoconf for Redis
20+
RUN apk add --update --no-cache --virtual .docker-php-global-dependancies \
21+
freetype-dev \
22+
libjpeg-turbo-dev \
23+
libpng-dev \
24+
libzip-dev \
25+
zip \
26+
autoconf \
27+
gcc \
28+
g++ \
29+
make \
30+
curl-dev \
31+
libmcrypt-dev \
32+
mysql-dev
33+
34+
# Configure and install PHP extensions
35+
RUN php -m && \
36+
docker-php-ext-configure bcmath --enable-bcmath && \
37+
docker-php-ext-configure gd --with-freetype --with-jpeg && \
38+
docker-php-ext-configure zip && \
39+
docker-php-ext-install \
40+
bcmath \
41+
gd \
42+
zip \
43+
pdo_mysql \
44+
mysqli \
45+
curl
46+
47+
# Install Redis
48+
RUN pecl install redis && \
49+
docker-php-ext-enable redis && \
50+
php -m
51+
52+
# Install Composer
53+
RUN php -r "readfile('https://getcomposer.org/installer');" | \
54+
php -- --install-dir=/usr/local/bin --filename=composer
55+
56+
# Clean up
57+
RUN apk del .docker-php-global-dependancies && \
58+
rm -rf /var/cache/apk/* && \
59+
docker-php-source delete
60+
61+
FROM php-base as php-production
62+
63+
# Copy application files
64+
COPY ./ /var/www/
65+
COPY ./docker/php/custom.ini /usr/local/etc/php/conf.d/custom.ini
66+
COPY ./docker/php/startup-fix.sh /usr/local/bin/startup-fix.sh
67+
COPY ./docker/nginx/nginx.prod.conf /etc/nginx/http.d/default.conf
68+
COPY ./docker/supervisor/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
69+
70+
# Set working directory
71+
WORKDIR /var/www
72+
73+
# Install PHP dependencies
74+
RUN composer install --no-dev --optimize-autoloader
75+
76+
# Publish vendor assets
77+
RUN php artisan vendor:publish --tag=public
78+
79+
# Set ownership
80+
RUN chown -R root:root /var/www
81+
82+
# Clean up
83+
RUN rm -Rf /var/www/docker /root/.composer/
84+
85+
# Create www-data user and set permissions
86+
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data \
87+
&& mkdir -p /var/www/storage/logs \
88+
&& mkdir -p /var/www/storage/framework/cache \
89+
&& mkdir -p /var/www/storage/framework/views \
90+
&& mkdir -p /var/www/storage/framework/sessions \
91+
&& mkdir -p /var/www/bootstrap/cache
92+
93+
# Set proper permissions for Laravel
94+
RUN mkdir -p /var/www/storage/framework/sessions && \
95+
mkdir -p /var/www/storage/framework/views && \
96+
mkdir -p /var/www/storage/framework/cache && \
97+
mkdir -p /var/www/storage/logs && \
98+
chown -R www-data:www-data /var/www/storage && \
99+
chown -R www-data:www-data /var/www/bootstrap/cache && \
100+
chmod -R 775 /var/www/storage && \
101+
chmod -R 775 /var/www/bootstrap/cache
102+
103+
# Set up nginx logs
104+
RUN mkdir -p /var/log/nginx && \
105+
chown -R www-data:www-data /var/log/nginx && \
106+
chmod -R 755 /var/log/nginx
107+
108+
# Make startup script executable
109+
RUN chmod +x /usr/local/bin/startup-fix.sh
110+
111+
# Expose port
112+
EXPOSE 80
113+
114+
# Use the comprehensive startup script
115+
CMD ["/usr/local/bin/startup-fix.sh"]

docker/php/startup-fix.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
echo "🚀 Starting Laravel Application Fix Script..."
4+
5+
# Navigate to the Laravel application directory
6+
cd /var/www
7+
8+
# Clear all Laravel caches to ensure fresh start
9+
echo "🧹 Clearing Laravel caches..."
10+
php artisan config:clear || echo "Config clear failed"
11+
php artisan route:clear || echo "Route clear failed"
12+
php artisan view:clear || echo "View clear failed"
13+
php artisan cache:clear || echo "Cache clear failed"
14+
15+
# Generate application key if it doesn't exist
16+
echo "🔑 Checking application key..."
17+
if ! php artisan key:generate --show > /dev/null 2>&1; then
18+
echo "Generating new application key..."
19+
php artisan key:generate --force
20+
fi
21+
22+
# Wait for database to be ready
23+
echo "🗄️ Waiting for database connection..."
24+
for i in {1..30}; do
25+
if php artisan migrate:status > /dev/null 2>&1; then
26+
echo "Database connection successful!"
27+
break
28+
fi
29+
echo "Waiting for database... ($i/30)"
30+
sleep 2
31+
done
32+
33+
# Run database migrations
34+
echo "📊 Running database migrations..."
35+
php artisan migrate --force || echo "Migration failed, continuing..."
36+
37+
# Seed the database if it's empty
38+
echo "🌱 Checking if database needs seeding..."
39+
if php artisan tinker --execute="echo App\Blog\Entities\Category::count();" 2>/dev/null | grep -q "0"; then
40+
echo "Seeding database..."
41+
php artisan db:seed --force || echo "Seeding failed, continuing..."
42+
fi
43+
44+
# Create Elasticsearch index
45+
echo "🔍 Setting up Elasticsearch index..."
46+
php artisan elasticsearch:recreate-index || echo "Elasticsearch setup failed, continuing..."
47+
48+
# Set proper permissions
49+
echo "🔒 Setting proper permissions..."
50+
chown -R www-data:www-data /var/www/storage /var/www/bootstrap/cache
51+
chmod -R 775 /var/www/storage /var/www/bootstrap/cache
52+
53+
echo "✅ Laravel Application Fix Script completed!"
54+
echo "🌐 Application should now be ready to serve requests"
55+
56+
# Start supervisord to run nginx, php-fpm, and workers
57+
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf

routes/web.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use Illuminate\Support\Facades\Route;
44
use Illuminate\Support\Facades\Auth;
5-
use App\Blog\IO\Http\Controllers\BlogController;
65
use App\Blog\IO\Http\Controllers\PostController;
76
use App\Blog\IO\Http\Controllers\CategoryController;
87

@@ -17,16 +16,39 @@
1716
|
1817
*/
1918

20-
Route::get('/', [BlogController::class, 'index'])->name('blog.index');
21-
Route::get('/blog', [BlogController::class, 'index'])->name('blog.home');
22-
Route::get('/blog/{slug}', [BlogController::class, 'show'])->name('blog.show');
19+
Route::get('/health', function () {
20+
return response()->json(['status' => 'ok', 'timestamp' => now()]);
21+
});
22+
23+
Route::get('/', function () {
24+
return response()->json([
25+
'message' => 'Laravel Blog Application is running!',
26+
'status' => 'success',
27+
'timestamp' => now(),
28+
'environment' => config('app.env'),
29+
'debug' => config('app.debug')
30+
]);
31+
})->name('home');
32+
33+
// Simple blog routes without complex controllers for now
34+
Route::get('/blog', function () {
35+
return response()->json([
36+
'message' => 'Blog section - Coming soon!',
37+
'status' => 'success',
38+
'timestamp' => now()
39+
]);
40+
})->name('blog.index');
2341

2442
Auth::routes();
2543

2644
// Admin routes (protected by auth middleware)
2745
Route::middleware('auth')->prefix('admin')->name('admin.')->group(function () {
2846
Route::get('/dashboard', function () {
29-
return view('admin.dashboard');
47+
return response()->json([
48+
'message' => 'Admin Dashboard',
49+
'user' => auth()->user()->name ?? 'Admin',
50+
'timestamp' => now()
51+
]);
3052
})->name('dashboard');
3153

3254
// Post management
@@ -36,5 +58,4 @@
3658
Route::resource('categories', CategoryController::class)->except(['show']);
3759
});
3860

39-
40-
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
61+
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home.controller');

scripts/deploy-fix.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
echo "🚀 Laravel Blog Deployment Fix Script"
6+
echo "======================================"
7+
8+
# Configuration
9+
AWS_REGION="us-east-1"
10+
ECR_REGISTRY="891377138894.dkr.ecr.us-east-1.amazonaws.com"
11+
ECR_REPOSITORY="laravel-blog-repo"
12+
ECS_CLUSTER="laravel-blog-cluster"
13+
ECS_SERVICE="laravel-blog-service"
14+
TIMESTAMP=$(date +%s)
15+
IMAGE_TAG="fixed-${TIMESTAMP}"
16+
17+
echo "📦 Building Docker image..."
18+
docker build -f docker/php/Dockerfile.fixed -t laravel-blog:${IMAGE_TAG} .
19+
20+
echo "🏷️ Tagging image for ECR..."
21+
docker tag laravel-blog:${IMAGE_TAG} ${ECR_REGISTRY}/${ECR_REPOSITORY}:${IMAGE_TAG}
22+
23+
echo "🔐 Logging into ECR..."
24+
aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY}
25+
26+
echo "📤 Pushing image to ECR..."
27+
docker push ${ECR_REGISTRY}/${ECR_REPOSITORY}:${IMAGE_TAG}
28+
29+
echo "📋 Creating new task definition..."
30+
TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition laravel-blog-task --region ${AWS_REGION} --query 'taskDefinition')
31+
32+
# Update the image in the task definition
33+
NEW_TASK_DEFINITION=$(echo $TASK_DEFINITION | jq --arg IMAGE "${ECR_REGISTRY}/${ECR_REPOSITORY}:${IMAGE_TAG}" '.containerDefinitions[0].image = $IMAGE' | jq 'del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .placementConstraints, .compatibilities, .registeredAt, .registeredBy)')
34+
35+
echo "🔄 Registering new task definition..."
36+
NEW_TASK_DEF_ARN=$(echo $NEW_TASK_DEFINITION | aws ecs register-task-definition --region ${AWS_REGION} --cli-input-json file:///dev/stdin --query 'taskDefinition.taskDefinitionArn' --output text)
37+
38+
echo "🚢 Updating ECS service..."
39+
aws ecs update-service --cluster ${ECS_CLUSTER} --service ${ECS_SERVICE} --task-definition ${NEW_TASK_DEF_ARN} --region ${AWS_REGION}
40+
41+
echo "⏳ Waiting for deployment to complete..."
42+
aws ecs wait services-stable --cluster ${ECS_CLUSTER} --services ${ECS_SERVICE} --region ${AWS_REGION}
43+
44+
echo "✅ Deployment completed successfully!"
45+
echo "🌐 Application should be available at: http://laravel-blog-alb-941765149.us-east-1.elb.amazonaws.com"
46+
echo "🏷️ Deployed image: ${ECR_REGISTRY}/${ECR_REPOSITORY}:${IMAGE_TAG}"

terraform/ecs.tf

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,31 @@ resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_policy" {
4949
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
5050
}
5151

52+
# Add S3 access policy for the task role
53+
resource "aws_iam_role_policy" "ecs_task_s3_policy" {
54+
name = "${var.project_name}-ecs-task-s3-policy"
55+
role = aws_iam_role.ecs_task_role.id
56+
57+
policy = jsonencode({
58+
Version = "2012-10-17"
59+
Statement = [
60+
{
61+
Effect = "Allow"
62+
Action = [
63+
"s3:GetObject",
64+
"s3:PutObject",
65+
"s3:DeleteObject",
66+
"s3:ListBucket"
67+
]
68+
Resource = [
69+
aws_s3_bucket.app_storage.arn,
70+
"${aws_s3_bucket.app_storage.arn}/*"
71+
]
72+
}
73+
]
74+
})
75+
}
76+
5277
# ECS Task Role
5378
resource "aws_iam_role" "ecs_task_role" {
5479
name = "${var.project_name}-ecs-task-role"
@@ -90,7 +115,7 @@ resource "aws_ecs_task_definition" "app" {
90115
container_definitions = jsonencode([
91116
{
92117
name = "laravel-app"
93-
image = "${aws_ecr_repository.app.repository_url}:fixed-1750662311"
118+
image = "${aws_ecr_repository.app.repository_url}:working-1750672452"
94119

95120
portMappings = [
96121
{
@@ -119,7 +144,7 @@ resource "aws_ecs_task_definition" "app" {
119144
},
120145
{
121146
name = "DB_HOST"
122-
value = aws_db_instance.main.endpoint
147+
value = split(":", aws_db_instance.main.endpoint)[0]
123148
},
124149
{
125150
name = "DB_PORT"
@@ -145,6 +170,14 @@ resource "aws_ecs_task_definition" "app" {
145170
name = "REDIS_PORT"
146171
value = "6379"
147172
},
173+
{
174+
name = "REDIS_PASSWORD"
175+
value = ""
176+
},
177+
{
178+
name = "REDIS_SCHEME"
179+
value = "tcp"
180+
},
148181
{
149182
name = "ELASTICSEARCH_HOST"
150183
value = "https://${aws_opensearch_domain.main.endpoint}"
@@ -168,6 +201,26 @@ resource "aws_ecs_task_definition" "app" {
168201
{
169202
name = "AWS_USE_PATH_STYLE_ENDPOINT"
170203
value = "false"
204+
},
205+
{
206+
name = "CACHE_DRIVER"
207+
value = "redis"
208+
},
209+
{
210+
name = "SESSION_DRIVER"
211+
value = "redis"
212+
},
213+
{
214+
name = "LOG_CHANNEL"
215+
value = "stderr"
216+
},
217+
{
218+
name = "ELASTICSEARCH_USERNAME"
219+
value = var.opensearch_master_user
220+
},
221+
{
222+
name = "ELASTICSEARCH_PASSWORD"
223+
value = var.opensearch_master_password
171224
}
172225
]
173226

terraform/elasticache.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ resource "aws_elasticache_replication_group" "main" {
4141

4242
at_rest_encryption_enabled = true
4343
transit_encryption_enabled = true
44+
transit_encryption_mode = "preferred"
45+
apply_immediately = true
4446

4547
snapshot_retention_limit = 5
4648
snapshot_window = "03:00-05:00"

0 commit comments

Comments
 (0)