diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..68aac31 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +.git +runtime \ No newline at end of file diff --git a/.env.example b/.env.example index 512046a..dee47ba 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,3 @@ -DB_DSN = mysql:host=localhost;port=3306;dbname=mvc_framework -DB_USER = root -DB_PASSWORD = \ No newline at end of file +DB_DSN = mysql:host=db;port=3306;dbname=php_mvc +DB_USER = php_mvc +DB_PASSWORD = php_mvc \ No newline at end of file diff --git a/README.md b/README.md index d2d3bc2..3c025b9 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,17 @@ Minimalistic custom framework created for educational purposes. 7. Start php server by running command `php -S 127.0.0.1:8080` 8. Open in browser http://127.0.0.1:8080 +------ +## Installation using docker +Make sure you have docker installed. To see how you can install docker on Windows [click here](https://youtu.be/2ezNqqaSjq8).
+Make sure `docker` and `docker-compose` commands are available in command line. + +1. Clone the project using git +1. Copy `.env.example` into `.env` (Don't need to change anything for local development) +1. Navigate to the project root directory and run `docker-compose up -d` +1. Install dependencies - `docker-compose exec app composer install` +1. Run migrations - `docker-compose exec app php migrations.php` +8. Open in browser http://127.0.0.1:8080 > The project was created along with Youtube Video Series "[Build PHP MVC Framework](https://www.youtube.com/playlist?list=PLLQuc_7jk__Uk_QnJMPndbdKECcTEwTA1)". > I appreaciate if you share it. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..8531289 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: "3.7" + +services: + app: + build: ./docker + image: thecodeholic/php_mvc + ports: + - "8080:80" + volumes: + # Mount source-code for development + - ./:/var/www + db: + image: mysql:8 + ports: + - "3307:3306" + volumes: + - mysql-data:/var/lib/mysql + - ./docker/mysql-config.cnf:/etc/mysql/conf.d/config.cnf + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: php_mvc + MYSQL_USER: php_mvc + MYSQL_PASSWORD: php_mvc + +volumes: + mysql-data: \ No newline at end of file diff --git a/docker/000-default.conf b/docker/000-default.conf new file mode 100644 index 0000000..3b74b4b --- /dev/null +++ b/docker/000-default.conf @@ -0,0 +1,10 @@ + + ServerAdmin webmaster@localhost + DocumentRoot /var/www/public + + + Options Indexes FollowSymLinks + AllowOverride All + Require all granted + + \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..9740c1d --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,46 @@ +FROM php:8.1-apache + +# Copy virtual host into container +COPY 000-default.conf /etc/apache2/sites-available/000-default.conf + +# Enable rewrite mode +RUN a2enmod rewrite + +# Install necessary packages +RUN apt-get update && \ + apt-get install \ + libzip-dev \ + wget \ + git \ + unzip \ + -y --no-install-recommends + +# Install PHP Extensions +RUN docker-php-ext-install zip pdo_mysql + +# RUN pecl install -o -f xdebug-3.1.3 \ +# && rm -rf /tmp/pear + +# Copy composer installable +COPY ./install-composer.sh ./ + +# Copy php.ini +COPY ./php.ini /usr/local/etc/php/ + +# Cleanup packages and install composer +RUN apt-get purge -y g++ \ + && apt-get autoremove -y \ + && rm -r /var/lib/apt/lists/* \ + && rm -rf /tmp/* \ + && sh ./install-composer.sh \ + && rm ./install-composer.sh + +# Change the current working directory +WORKDIR /var/www + +# Change the owner of the container document root +RUN chown -R www-data:www-data /var/www + +# Start Apache in foreground +CMD ["apache2-foreground"] + diff --git a/docker/install-composer.sh b/docker/install-composer.sh new file mode 100644 index 0000000..bcb1e7a --- /dev/null +++ b/docker/install-composer.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" +SIGNATURE=$(php -r "echo hash_file('SHA384', 'composer-setup.php');") +EXPECTED_SIGNATURE=$(wget -q -O - https://composer.github.io/installer.sig) + +if [ "$EXPECTED_SIGNATURE" != "$SIGNATURE" ] +then + echo 'ERROR: Invalid installer signature' + rm composer-setup.php + exit 1 +fi + +php composer-setup.php --quiet --install-dir=/usr/local/bin --filename=composer +RESULT=$? +rm composer-setup.php +exit $RESULT \ No newline at end of file diff --git a/docker/mysql-config.cnf b/docker/mysql-config.cnf new file mode 100644 index 0000000..1f3bb11 --- /dev/null +++ b/docker/mysql-config.cnf @@ -0,0 +1,11 @@ +[client] +default-character-set = utf8mb4 + +[mysql] +default-character-set = utf8mb4 + +[mysqld] +init-connect='SET NAMES utf8mb4' +collation_server=utf8mb4_unicode_ci +character_set_server=utf8mb4 +default_authentication_plugin= mysql_native_password \ No newline at end of file diff --git a/docker/php.ini b/docker/php.ini new file mode 100644 index 0000000..c6d1b76 --- /dev/null +++ b/docker/php.ini @@ -0,0 +1,3 @@ +; General +upload_max_filesize = 200M +post_max_size = 220M