trypost/.forgejo/workflows/setup-ec2.yml
vickytechkey c899d7db13
All checks were successful
Setup EC2 Tools / setup-server (push) Successful in 2m40s
chore: add missing AWS CDK and project dependencies to node_modules
2026-09-10 22:50:11 +05:30

196 lines
8.7 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 APP_URL, Filesystem, and Database connection
sed -i 's|^APP_URL=.*|APP_URL=https://socialmedia.tradhox.com|g' .env
sed -i 's|^FILESYSTEM_DISK=.*|FILESYSTEM_DISK=s3|g' .env
sed -i 's|^AWS_DEFAULT_REGION=.*|AWS_DEFAULT_REGION=ap-south-2|g' .env
sed -i 's|^AWS_BUCKET=.*|AWS_BUCKET=trypostmedia|g' .env
sed -i 's|^AWS_URL=.*|AWS_URL=https://d28qhqo5myc2wf.cloudfront.net|g' .env
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
# Configure Facebook OAuth
sed -i 's|^FACEBOOK_CLIENT_ID=.*|FACEBOOK_CLIENT_ID=2105308916859392|g' .env
sed -i 's|^FACEBOOK_CLIENT_SECRET=.*|FACEBOOK_CLIENT_SECRET=79b51ed72514663f3629aa33610490f9|g' .env
sed -i 's|^FACEBOOK_CLIENT_REDIRECT=.*|FACEBOOK_CLIENT_REDIRECT=\"https://socialmedia.tradhox.com/accounts/facebook/callback\"|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 and link storage
php artisan migrate --force
php artisan storage:link || true
# 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..."
if [ -f /etc/letsencrypt/live/socialmedia.tradhox.com/fullchain.pem ]; then
sudo tee /etc/nginx/sites-available/socialmedia.tradhox.com > /dev/null << 'EOF'
server {
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;
}
listen [::]:443 ssl;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/socialmedia.tradhox.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/socialmedia.tradhox.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
listen 80;
listen [::]:80;
server_name socialmedia.tradhox.com;
return 301 https://$host$request_uri;
}
EOF
fi
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
# 7. Configure Supervisor for Horizon Queue Worker
echo "Configuring Supervisor for Laravel Horizon..."
sed -e 's/^[[:space:]]*//' << 'EOF' | sudo tee /etc/supervisor/conf.d/trypost-horizon.conf > /dev/null
[program:trypost-horizon]
process_name=%(program_name)s
command=php /var/www/trypost/artisan horizon
autostart=true
autorestart=true
user=ubuntu
redirect_stderr=true
stdout_logfile=/var/www/trypost/storage/logs/horizon.log
stopwaitsecs=3600
EOF
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl restart trypost-horizon || sudo supervisorctl start trypost-horizon
# 8. Configure Cron for Laravel Scheduler
(crontab -l 2>/dev/null | grep -v 'schedule:run'; echo "* * * * * cd /var/www/trypost && php artisan schedule:run >> /dev/null 2>&1") | crontab -
echo "Setup Complete! Tools installed: Nginx, PHP 8.4, Composer, Node.js, Supervisor, Redis, Horizon"