Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MYSQL_ROOT_PASSWORD=rootpass
MYSQL_DATABASE=sample
MYSQL_USER=sampleuser
MYSQL_PASSWORD=samplepass
50 changes: 50 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
13 changes: 13 additions & 0 deletions Dockerfile.db
Original file line number Diff line number Diff line change
@@ -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
130 changes: 130 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 30 additions & 12 deletions config-dev/vhost.conf
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
php_value auto_prepend_file ./../bootstrap.php
<VirtualHost *:80>
DocumentRoot /var/www/html/web

AddOutputFilterByType DEFLATE application/json
<Directory /var/www/html/web>
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/<user>
RewriteCond %{REQUEST_METHOD} =GET
RewriteRule ^/([^/]+)$ /user.php?id=$1 [L]
# Ruta raíz → index.php
RewriteRule ^$ index.php [L]

# GET http://sample/<user>/status/<tweet>
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]
</Directory>

php_value auto_prepend_file /var/www/html/bootstrap.php

AddOutputFilterByType DEFLATE application/json

RewriteEngine On
RewriteRule ^$ index.php [L]

LogLevel alert rewrite:trace3
</VirtualHost>
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
Empty file modified logs/.keep
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions web/index.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

$lastJoinedUsers = (require "dic/users.php")->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
Expand Down
6 changes: 3 additions & 3 deletions web/tweet.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$tweet = (require "dic/tweets.php")->getById($_GET["id"]);
$tweet = (require __DIR__ . "/../dic/tweets.php")->getById($_GET["id"]);

if ($tweet === null) {
http_response_code(404);
Expand All @@ -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
)
))();
Expand Down
6 changes: 3 additions & 3 deletions web/user.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php

$user = (require "dic/users.php")->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]",
Expand Down