git clone https://github.com/eisax/hms.git
cd hms- Run
composer install - Run
php artisan key:generate - Setup .env file
- Run migration
php artisan migrate - Run seeder
php artisan db:seed - Run seeder
php artisan serve
This README provides a comprehensive guide to deploying the HMS application using Docker containers. The steps cover installation, container creation, running migrations, and application setup.
-
Install Docker:
- Download Docker from the official website.
- Follow the installation instructions for your operating system.
-
Verify Docker Installation:
docker --version
git clone https://github.com/eisax/hms.git
cd hmsCommand:
docker-compose up -d dbDetails:
- The
dbservice is defined in thedocker-compose.yamlfile. - Configuration:
- Image:
mysql:8.0 - Environment Variables:
MYSQL_ROOT_PASSWORD=rootMYSQL_DATABASE=hmsdb
- Image:
- The database will run on port
3306.
Command:
docker psLook for the mysql-docker container in the list of running containers.
Command:
docker-compose build appThis will create a container for the Laravel application based on the Dockerfile.
Run the MySQL container first, then execute the migrations:
Command:
docker exec -it mysql-docker bashWithin the MySQL container, use Tinker or Artisan:
php artisan migrate
php artisan db:seedExit the MySQL container:
exitCommand:
docker-compose up -d appThe Laravel application will now be running in the hms-app-1 container.
-
Check Running Containers:
docker ps
Ensure both
mysql-dockerandhms-app-1are running. -
Access the Application: Open your browser and navigate to:
http://localhost:8000
-
Push Images to Amazon ECR:
- Tag and push your Docker images to an Amazon Elastic Container Registry (ECR) repository.
-
Create AWS ECS Cluster:
- Define services for
mysql-dockerandhms-app-1.
- Define services for
-
Deploy Containers:
- Use the task definition in AWS ECS to start both containers.
-
Configure Application Load Balancer:
- Route traffic to the
hms-app-1container.
- Route traffic to the
-
Access Application:
- Visit the public DNS or URL of the load balancer.
- Use
.envfor application configuration. - Ensure security groups and networking are properly configured for AWS.
- Check container logs:
docker logs <container-name>
- Rebuild containers if changes are made:
docker-compose build
- Log in to AWS Management Console.
- Navigate to EC2 and launch an instance.
- Choose an Amazon Machine Image (AMI), such as Amazon Linux or Ubuntu.
- Select an instance type (e.g., t2.micro for testing).
- Configure instance details and security group rules (open port 22 for SSH, and port 80 for HTTP).
After launching the EC2 instance, SSH into it and install:
- PHP
- Composer
- MySQL or any database of your choice
- Web server (Apache or Nginx)
Example (for Ubuntu):
sudo apt update
sudo apt install php-cli php-mbstring php-xml unzip curl nginx mysql-server composer -y- Allow HTTP (port 80) and SSH (port 22).
- Add HTTPS (port 443) if using SSL.
Set up a database for the Laravel application and note the credentials.
- Update the database credentials and any other environment-specific variables in your .env file.
Ensure all dependencies are installed locally using Composer:
composer install --optimize-autoloader --no-dev php artisan config:cache
php artisan route:cache- Push your Laravel project to a GitHub repository.
Create a .github/workflows/deploy.yml file in your repository with the following content:
name: Deploy Laravel to AWS
on:
push:
branches:
- main # Trigger workflow on push to the 'main' branch
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1 # Update PHP version as needed
extensions: mbstring, xml, pdo_mysql
ini-values: post_max_size=256M, upload_max_filesize=256M
coverage: none
- name: Install Composer Dependencies
run: composer install --no-dev --prefer-dist --no-progress
- name: Deploy to EC2
env:
PRIVATE_KEY: ${{ secrets.EC2_PRIVATE_KEY }}
HOST: ${{ secrets.EC2_HOST }}
USER: ${{ secrets.EC2_USER }}
run: |
ssh -i "$PRIVATE_KEY" -o StrictHostKeyChecking=no $USER@$HOST << 'EOF'
cd /var/www/html
git pull origin main
composer install --no-dev --prefer-dist --no-progress
php artisan migrate --force
php artisan config:cache
php artisan route:cache
sudo systemctl restart nginx
EOFGo to your repository on GitHub:
- Navigate to Settings > Secrets and variables > Actions.
- Add the following secrets:
- EC2_PRIVATE_KEY: The private key of your EC2 instance.
- EC2_HOST: The public IP or hostname of your EC2 instance.
- EC2_USER: The SSH username (e.g., ec2-user for Amazon Linux or ubuntu for Ubuntu).
- Add the public key of the GitHub Action runner to the ~/.ssh/authorized_keys of the EC2 user.
SSH into the EC2 instance and clone your Laravel project into /var/www/html or your chosen directory:
cd /var/www/html
git clone <repository-url> .
composer install
php artisan migrate --forceEnsure Laravel has the correct file permissions:
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cacheSet up Nginx or Apache to serve your Laravel application.
Example (Nginx):
server {
listen 80;
server_name your_domain_or_ip;
root /var/www/html/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}Restart Nginx:
sudo systemctl restart nginx- Push changes to your GitHub repository.
- Verify that the CI/CD pipeline deploys the application to the EC2 instance.
This setup ensures automatic deployment of your Laravel app to AWS EC2 whenever you push changes to the specified branch on GitHub. For scaling or additional AWS services like RDS or S3, modify the setup accordingly.
Josphat Ndhlovu