From 8e8f5be77bda229c03305cb6acf49e46268295b9 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Fri, 1 Aug 2025 07:35:48 +0000 Subject: [PATCH] Update project --- .env | 4 ++ Dockerfile | 50 ++++++++++++++++ Dockerfile.db | 13 +++++ composer.lock | 130 ++++++++++++++++++++++++++++++++++++++++++ config-dev/vhost.conf | 42 ++++++++++---- docker-compose.yml | 19 ++++++ logs/.keep | 0 web/index.php | 4 +- web/tweet.php | 6 +- web/user.php | 6 +- 10 files changed, 254 insertions(+), 20 deletions(-) create mode 100644 .env create mode 100644 Dockerfile create mode 100644 Dockerfile.db create mode 100644 composer.lock create mode 100644 docker-compose.yml mode change 100644 => 100755 logs/.keep diff --git a/.env b/.env new file mode 100644 index 0000000..bff49be --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +MYSQL_ROOT_PASSWORD=rootpass +MYSQL_DATABASE=sample +MYSQL_USER=sampleuser +MYSQL_PASSWORD=samplepass diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ed08f0d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,50 @@ +# Usamos una imagen oficial de PHP con Apache +FROM php:7.4-apache + +# Instalar dependencias del sistema +RUN apt-get update && \ + apt-get install -y \ + git \ + zip \ + unzip \ + mariadb-client \ + && rm -rf /var/lib/apt/lists/* + +# Habilitar mod_rewrite de Apache +RUN a2enmod rewrite + +# Instalar extensiones de PHP necesarias +RUN docker-php-ext-install pdo pdo_mysql + +# Instalar Composer +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer + +# Copiar los archivos de la aplicación +COPY . /var/www/html/ + +# Establecer permisos adecuados + +RUN mkdir -p /var/www/html/logs && \ + chown -R www-data:www-data /var/www/html/logs && \ + chmod -R 775 /var/www/html/logs + +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 {} \; && \ + chmod -R 775 /var/www/html/ + +# Configurar Apache +COPY config-dev/vhost.conf /etc/apache2/sites-available/000-default.conf +RUN a2ensite 000-default.conf + +# Instalar dependencias de Composer +WORKDIR /var/www/html +USER www-data +RUN composer install --no-dev --optimize-autoloader +USER root + +# Puerto expuesto +EXPOSE 80 + +# Comando de inicio +CMD ["apache2-foreground"] diff --git a/Dockerfile.db b/Dockerfile.db new file mode 100644 index 0000000..04a1d73 --- /dev/null +++ b/Dockerfile.db @@ -0,0 +1,13 @@ +# Usamos la imagen oficial de MariaDB +FROM mariadb:10.5 + +# Las variables se pasan via docker-compose.yml o --env-file +ENV MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} +ENV MYSQL_DATABASE=${MYSQL_DATABASE} +ENV MYSQL_USER=${MYSQL_USER} +ENV MYSQL_PASSWORD=${MYSQL_PASSWORD} +# Copiar el script SQL para inicializar la base de datos +COPY sql/db.sql /docker-entrypoint-initdb.d/ + +# Puerto expuesto +EXPOSE 3306 diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..46f1600 --- /dev/null +++ b/composer.lock @@ -0,0 +1,130 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "6226c4e050e2b55576c081b6dd9120d0", + "packages": [ + { + "name": "twbs/bootstrap", + "version": "v3.4.1", + "source": { + "type": "git", + "url": "https://github.com/twbs/bootstrap.git", + "reference": "68b0d231a13201eb14acd3dc84e51543d16e5f7e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twbs/bootstrap/zipball/68b0d231a13201eb14acd3dc84e51543d16e5f7e", + "reference": "68b0d231a13201eb14acd3dc84e51543d16e5f7e", + "shasum": "" + }, + "replace": { + "twitter/bootstrap": "self.version" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.4.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jacob Thornton", + "email": "jacobthornton@gmail.com" + }, + { + "name": "Mark Otto", + "email": "markdotto@gmail.com" + } + ], + "description": "The most popular front-end framework for developing responsive, mobile first projects on the web.", + "homepage": "https://getbootstrap.com/", + "keywords": [ + "JS", + "css", + "framework", + "front-end", + "less", + "mobile-first", + "responsive", + "web" + ], + "support": { + "issues": "https://github.com/twbs/bootstrap/issues", + "source": "https://github.com/twbs/bootstrap/tree/v3.4.1" + }, + "time": "2019-02-13T15:55:38+00:00" + }, + { + "name": "willdurand/negotiation", + "version": "v2.3.1", + "source": { + "type": "git", + "url": "https://github.com/willdurand/Negotiation.git", + "reference": "03436ededa67c6e83b9b12defac15384cb399dc9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/willdurand/Negotiation/zipball/03436ededa67c6e83b9b12defac15384cb399dc9", + "reference": "03436ededa67c6e83b9b12defac15384cb399dc9", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.3-dev" + } + }, + "autoload": { + "psr-4": { + "Negotiation\\": "src/Negotiation" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "William Durand", + "email": "will+git@drnd.me" + } + ], + "description": "Content Negotiation tools for PHP provided as a standalone library.", + "homepage": "http://williamdurand.fr/Negotiation/", + "keywords": [ + "accept", + "content", + "format", + "header", + "negotiation" + ], + "support": { + "issues": "https://github.com/willdurand/Negotiation/issues", + "source": "https://github.com/willdurand/Negotiation/tree/2.x" + }, + "time": "2017-05-14T17:21:12+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": {}, + "prefer-stable": false, + "prefer-lowest": false, + "platform": {}, + "platform-dev": {}, + "plugin-api-version": "2.6.0" +} diff --git a/config-dev/vhost.conf b/config-dev/vhost.conf index 0c8a24e..e2245d8 100644 --- a/config-dev/vhost.conf +++ b/config-dev/vhost.conf @@ -1,17 +1,35 @@ -php_value auto_prepend_file ./../bootstrap.php + + DocumentRoot /var/www/html/web -AddOutputFilterByType DEFLATE application/json + + Options Indexes FollowSymLinks + AllowOverride All + Require all granted -RewriteEngine On + RewriteEngine On + RewriteBase / -# GET http://sample/ -RewriteCond %{REQUEST_METHOD} =GET -RewriteRule ^/$ /index.php [L] + # No reescribir si es un archivo o directorio real + RewriteCond %{REQUEST_FILENAME} -f [OR] + RewriteCond %{REQUEST_FILENAME} -d + RewriteRule ^ - [L] -# GET http://sample/ -RewriteCond %{REQUEST_METHOD} =GET -RewriteRule ^/([^/]+)$ /user.php?id=$1 [L] + # Ruta raíz → index.php + RewriteRule ^$ index.php [L] -# GET http://sample//status/ -RewriteCond %{REQUEST_METHOD} =GET -RewriteRule ^/([^/]+)/status/([^/]+)$ /tweet.php?user=$1&id=$2 [L] + # /username → user.php?id=username + RewriteRule ^([^/]+)$ user.php?id=$1 [L,QSA] + + # /username/status/id → tweet.php?user=username&id=id + RewriteRule ^([^/]+)/status/([^/]+)$ tweet.php?user=$1&id=$2 [L,QSA] + + + php_value auto_prepend_file /var/www/html/bootstrap.php + + AddOutputFilterByType DEFLATE application/json + + RewriteEngine On + RewriteRule ^$ index.php [L] + + LogLevel alert rewrite:trace3 + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f4dc5da --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +services: + web: + image: jmguzman11/php-sample-app:latest + ports: + - "8000:80" + depends_on: + - db + environment: + - APACHE_HOST=localhost + db: + image: jmguzman11/php-sample-db:latest + env_file: .env + ports: + - "3307:3306" + volumes: + - db_data:/var/lib/mysql + +volumes: + db_data: diff --git a/logs/.keep b/logs/.keep old mode 100644 new mode 100755 diff --git a/web/index.php b/web/index.php index ec8e4c1..24e1247 100644 --- a/web/index.php +++ b/web/index.php @@ -1,8 +1,8 @@ getLastJoined(); +$lastJoinedUsers = (require __DIR__ . "/../dic/users.php")->getLastJoined(); -switch (require "dic/negotiated_format.php") { +switch (require __DIR__ . "/../dic/negotiated_format.php") { case "text/html": (new Views\Layout( "Twitter - Newcomers", new Views\Users\Listing($lastJoinedUsers), true diff --git a/web/tweet.php b/web/tweet.php index a3a5c56..17d6828 100644 --- a/web/tweet.php +++ b/web/tweet.php @@ -1,6 +1,6 @@ getById($_GET["id"]); +$tweet = (require __DIR__ . "/../dic/tweets.php")->getById($_GET["id"]); if ($tweet === null) { http_response_code(404); @@ -14,12 +14,12 @@ exit; } -switch (require "dic/negotiated_format.php") { +switch (require __DIR__ . "/../dic/negotiated_format.php") { case "text/html": (new Views\Layout( "@$_GET[user] - \"$tweet->message\"", new Views\Tweets\Page( - (require "dic/users.php")->getById($_GET["user"]), + (require __DIR__ . "/../dic/users.php")->getById($_GET["user"]), $tweet ) ))(); diff --git a/web/user.php b/web/user.php index f88a31c..9909476 100644 --- a/web/user.php +++ b/web/user.php @@ -1,18 +1,18 @@ getById($_GET["id"]); +$user = (require __DIR__ . "/../dic/users.php")->getById($_GET["id"]); if ($user === null) { http_response_code(404); return; } -$tweetsService = (require "dic/tweets.php"); +$tweetsService = (require __DIR__ . "/../dic/tweets.php"); $tweets = $tweetsService->getLastByUser($_GET["id"]); $tweetsCount = $tweetsService->getTweetsCount($_GET["id"]); -switch (require "dic/negotiated_format.php") { +switch (require __DIR__ . "/../dic/negotiated_format.php") { case "text/html": (new Views\Layout( "Tweets from @$_GET[id]",