trypost/.forgejo/workflows/setup-ec2.yml
vickytechkey bc3e8c3534
All checks were successful
Setup EC2 Tools / setup-server (push) Successful in 1m8s
Allow storage permissions for both www-data and ubuntu users
2026-09-08 22:09:34 +05:30

154 lines
6.3 KiB
YAML

name: Setup EC2 Tools
on:
workflow_dispatch: # Allows you to trigger this manually from the Forgejo Actions UI
push:
branches:
- main
jobs:
setup-server:
runs-on: ubuntu-latest
container:
image: node:22
steps:
- name: Install LEMP Stack and Tools on EC2
uses: https://github.com/appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.BETA_EC2_HOST }}
username: ${{ secrets.BETA_EC2_USERNAME }}
key: ${{ secrets.BETA_EC2_SSH_KEY }}
script: |
echo "Starting EC2 Server Setup..."
# 1. Update package list
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install -y software-properties-common curl git unzip nginx supervisor redis-server
# 2. Add PHP repository and install PHP 8.4 with required extensions
sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update -y
sudo apt-get install -y php8.4-fpm php8.4-cli php8.4-pgsql php8.4-mbstring php8.4-xml php8.4-curl php8.4-zip php8.4-bcmath php8.4-intl php8.4-redis php8.4-gd
# 3. Install Node.js (required for Vite frontend assets)
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y nodejs
# 4. Install Composer (PHP Package Manager)
if ! command -v composer &> /dev/null; then
echo "Installing Composer..."
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then
>&2 echo 'ERROR: Invalid installer checksum'
rm composer-setup.php
exit 1
fi
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
rm composer-setup.php
fi
# 4. Install Node.js (v20) and NPM for building frontend assets
if ! command -v node &> /dev/null; then
echo "Installing Node.js..."
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
fi
# 5. Create web directory and deploy code
sudo mkdir -p /var/www/trypost
sudo chown -R $USER:$USER /var/www/trypost
# Clone or pull the latest code
if [ ! -d "/var/www/trypost/.git" ]; then
git clone http://16.113.106.152:3000/vignesh/trypost.git /var/www/trypost
else
cd /var/www/trypost && git pull origin main
fi
# Ensure all storage subdirectories exist and have proper permissions BEFORE composer install
cd /var/www/trypost
mkdir -p storage/logs storage/framework/views storage/framework/sessions storage/framework/cache bootstrap/cache
sudo chown -R $USER:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
# Install PHP dependencies
composer install --no-interaction --prefer-dist --optimize-autoloader
# Install Node.js dependencies and build frontend assets
npm install
npm run build
# Setup Laravel environment file and key
if [ ! -f .env ]; then
cp .env.example .env
fi
if grep -q "^APP_KEY=$" .env; then
php artisan key:generate --force
fi
# Configure Database connection
sed -i 's/^DB_HOST=.*/DB_HOST=16.113.106.152/g' .env
sed -i 's/^DB_DATABASE=.*/DB_DATABASE=trypost/g' .env
sed -i 's/^DB_USERNAME=.*/DB_USERNAME=vignesh/g' .env
sed -i 's/^DB_PASSWORD=.*/DB_PASSWORD=vtechnosoft@123A/g' .env
# Ensure web server owns the files after composer finishes writing caches
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 777 storage bootstrap/cache
# Run database migrations
php artisan migrate --force
# Temporarily enable debug mode to see exactly why it is failing!
sed -i 's/APP_DEBUG=false/APP_DEBUG=true/g' .env
sed -i 's/APP_ENV=production/APP_ENV=local/g' .env
php artisan optimize:clear
# 6. Configure Nginx for socialmedia.tradhox.com
echo "Configuring Nginx..."
sudo tee /etc/nginx/sites-available/socialmedia.tradhox.com > /dev/null << 'EOF'
server {
listen 80;
listen [::]:80;
server_name socialmedia.tradhox.com;
root /var/www/trypost/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
EOF
sudo ln -sf /etc/nginx/sites-available/socialmedia.tradhox.com /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo systemctl reload nginx
echo "Setup Complete! Tools installed: Nginx, PHP 8.2, Composer, Node.js, Supervisor, Redis"
# Trigger workflow run