1.0 Installation (Localhost) - germanngc/invoiceSender GitHub Wiki

Below instructions are intended for MacOS users and application running inside a Docker containers. The main core is using:

  • Composer 2
  • Laravel 8
  • Docker 3
  • MySQL 8
  • Nginx 1.21
  • PHP 7.4

For the environment, we will be routing ports to access from outside the containers, so we will enable port 8081 for web access and port 3308 for database access.

You can configure these options at will, just make sure you it will be compatible.

1.1 Composer

1.1.1 Install Composer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php --install-dir=/usr/local/bin/composer --filename=composer
composer -V
php -r "unlink('composer-setup.php');"

1.1.2 Get Laravel Installer

composer global require "laravel/installer"

1.1.3 Create the Laravel Project

composer create-project laravel/laravel=8 invoiceSender

1.1.4 Update components

composer update

1.2 Add to Github

cd invoiceSender
git init
git add .
git commit -m 'Initial Laravel 8'
git remote add upstream https://github.com/germanngc/invoiceSender.git
git remote add origin https://github.com/germanngc/invoiceSender.git
git remote -v
git push -u origin --all

1.3 Laravel Configuration

1.3.1 Environment File

Get a copy of the env file of Laravel

cp .env.example .env

Add basic configuration to your Laravel environment file, modify at least below variables.

APP_NAME=invoiceSender
APP_URL=http://localhost:8081
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=invSend
DB_USERNAME=invSend
DB_PASSWORD=invSend123456

1.3.2 Generate the APP Key

php artisan key:generate

1.4 Docker

1.4.1 Installing Docker

1.4.2 Add a Dockerfile

vim Dockerfile

Add below content

FROM php:7.4-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
	git \
	curl \
	libjpeg62-turbo-dev \
	libpng-dev \
	libonig-dev \
	libwebp-dev \
	libxml2-dev \
	libzip-dev \
	sudo \
	unzip \
	zip \
	zlib1g-dev

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Configure PHP extensions
RUN docker-php-ext-configure gd --with-webp --with-jpeg

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

USER $user

1.4.3 Add docker-compose.yml

vim docker-compose.yml

Add below content

version: "3.7"
services:
    app:
        build:
            args:
                user: invoicesender
                uid: 1000
            context: ./
            dockerfile: Dockerfile
        image: invoicesender
        container_name: invoicesender-app
        restart: unless-stopped
        working_dir: /var/www/
        volumes:
            - ./:/var/www
            - ./docker-compose/php/invoicesender.ini:/usr/local/etc/php/conf.d/invoicesender.ini
        networks:
            - invoicesender
    db:
        image: mysql:8
        container_name: invoicesender-db
        restart: unless-stopped
        ports:
            - 3308:3306
        environment:
            MYSQL_DATABASE: ${DB_DATABASE}
            MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
            MYSQL_PASSWORD: ${DB_PASSWORD}
            MYSQL_USER: ${DB_USERNAME}
            SERVICE_TAGS: dev
            SERVICE_NAME: mysql
        volumes:
            - ./docker-compose/mysql/init:/docker-entrypoint-initdb.d
            - ./docker-compose/mysql/persistent:/var/lib/mysql
        networks:
            - invoicesender
    nginx:
        image: nginx:1.21-alpine
        container_name: invoicesender-nginx
        restart: unless-stopped
        ports:
            - 8081:80
        volumes:
            - ./:/var/www
            - ./docker-compose/nginx:/etc/nginx/conf.d
        networks:
            - invoicesender
networks:
    invoicesender:
        driver: bridge

1.4.4 Adding Composer Folders

mkdir -p docker-compose/mysql/init
mkdir -p docker-compose/mysql/persistent
mkdir -p docker-compose/nginx
mkdir -p docker-compose/php

1.4.5 Adding Docker Nginx Config

vim docker-compose/nginx/invoicesender.conf

Add below content

server {
	listen 80;
	client_max_body_size 0;

	index index.php index.html;

	error_log  /var/log/nginx/error.log;
	access_log /var/log/nginx/access.log;

	root /var/www/public;

	location / {
		try_files $uri $uri/ /index.php?$query_string;
		gzip_static on;
	}

	location ~ \.php$ {
		try_files $uri =404;
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass app:9000;
		fastcgi_index index.php;
		include fastcgi_params;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		fastcgi_param PATH_INFO $fastcgi_path_info;
	}
}

1.4.6 Adding Docker PHP Config

vim docker-compose/php/invoicesender.ini

Add below content

file_uploads = On
memory_limit = 256M
upload_max_filesize = 24M
post_max_size = 24M
max_execution_time = 600

1.4.7 Build images

docker-compose build

Validate image was built

$ docker images | grep invoicesender
invoicesender             latest          2216494c8a1e   1 minutes ago   636MB
$

1.4.8 Run containers

docker-compose up -d

Validate the containers are running.

docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS         PORTS                                                  NAMES
605f904b7e04   mysql:8             "docker-entrypoint.s…"   13 seconds ago   Up 5 seconds   33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp   invoicesender-db
4be5e578e87d   nginx:1.21-alpine   "/docker-entrypoint.…"   13 seconds ago   Up 6 seconds   0.0.0.0:8081->80/tcp, :::8081->80/tcp                  invoicesender-nginx
f83f7c7594b1   invoicesender       "docker-php-entrypoi…"   13 seconds ago   Up 9 seconds   9000/tcp                                               invoicesender-app

1.5 Running Database Migrations

As the application is running on a docker container, we must run particular docker command in order to connect to all the services, like the database from the App.

docker exec invoicesender-app php artisan migrate

1.6 Accessing the Web App

Go to http://localhost:8081

If everything got configured correctly, you will see the Laravel Wellcome Page