build2
Got it! You want to deploy a static Astro site (the dist folder) on an EC2 instance running Nginx. I’ll give you a clear, step-by-step guide.
1️⃣ Prerequisites
Section titled “1️⃣ Prerequisites”- EC2 instance with Ubuntu (or similar Linux)
- Nginx installed
- Your Astro project is already built:
npm run build# This creates a `dist/` directory with your static files- You have SSH access to your EC2 instance.
2️⃣ Upload your dist/ folder to EC2
Section titled “2️⃣ Upload your dist/ folder to EC2”You can use scp or rsync:
Using scp:
scp -i /path/to/your-key.pem -r dist/* ubuntu@your-ec2-ip:/var/www/test1//var/www/test1/will be your site root.- Make sure the directory exists:
sudo mkdir -p /var/www/test1- Then set proper ownership and permissions:
sudo chown -R www-data:www-data /var/www/test1sudo chmod -R 755 /var/www/test1Using rsync:
Create the folder with sudo, then give ubuntu write access
sudo mkdir -p /var/www/as1sudo chown -R www-data:www-data /var/www/as1sudo chmod -R 755 /var/www/as1#sudo chown -R ubuntu:ubuntu /var/www/as1rsync -avz -e "ssh -i ~/.ssh/web1.pem" ./dist/ ubuntu@op1.evolo.fr:/var/www/as1/ --rsync-path="sudo rsync"3️⃣ Configure Nginx
Section titled “3️⃣ Configure Nginx”Edit (or create) a server block for your site:
sudo nano /etc/nginx/sites-available/test1Add the following configuration:
server { listen 80; server_name your-domain.com; # replace with your domain or public IP
root /var/www/test1; index index.html;
location / { try_files $uri /index.html; }
# Optional: serve static assets efficiently location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?|ttf|eot)$ { expires 1y; access_log off; add_header Cache-Control "public"; }}try_files $uri /index.html;ensures SPA routing works if you use client-side routing.
4️⃣ Enable the site and test Nginx
Section titled “4️⃣ Enable the site and test Nginx”sudo ln -s /etc/nginx/sites-available/test1 /etc/nginx/sites-enabled/sudo nginx -t # test configurationsudo systemctl reload nginx5️⃣ Access your site
Section titled “5️⃣ Access your site”- Open
http://your-ec2-ip(or your domain if DNS is set) in a browser. - You should see your Astro site served by Nginx.
✅ Optional Enhancements
Section titled “✅ Optional Enhancements”- Enable HTTPS with Let’s Encrypt:
sudo apt install certbot python3-certbot-nginxsudo certbot --nginx -d as1.evolo.fr- Automatic deployment: Use
rsync+systemdor CI/CD to push updateddistafter every build.
📝 script
Section titled “📝 script”If you want, I can write a ready-to-run one-liner deployment script for Astro → EC2 Nginx that does upload, permissions, and Nginx reload automatically.
Do you want me to do that?
Perfect! Here’s a ready-to-run deployment script for deploying an Astro dist/ folder to an EC2 instance running Nginx. It handles uploading, setting permissions, and reloading Nginx.
astro-deploy.sh
Section titled “astro-deploy.sh”#!/bin/bash
# -------------------------------# CONFIGURATION# -------------------------------KEY_PATH="/path/to/your-key.pem" # Path to your EC2 private keyEC2_USER="ubuntu" # EC2 username (ubuntu for Ubuntu AMI)EC2_IP="YOUR.EC2.IP.ADDRESS" # EC2 public IPREMOTE_DIR="/var/www/test1" # Remote directory for siteLOCAL_DIST="./dist" # Local Astro dist folder
# -------------------------------# 1. Build Astro project# -------------------------------echo "Building Astro project..."npm run build
# -------------------------------# 2. Upload dist folder# -------------------------------echo "Uploading files to EC2..."rsync -avz --delete -e "ssh -i $KEY_PATH" $LOCAL_DIST/ $EC2_USER@$EC2_IP:$REMOTE_DIR/
# -------------------------------# 3. Set ownership & permissions# -------------------------------echo "Setting permissions..."ssh -i $KEY_PATH $EC2_USER@$EC2_IP " sudo chown -R www-data:www-data $REMOTE_DIR sudo chmod -R 755 $REMOTE_DIR"
# -------------------------------# 4. Reload Nginx# -------------------------------echo "Reloading Nginx..."ssh -i $KEY_PATH $EC2_USER@$EC2_IP "sudo systemctl reload nginx"
echo "Deployment complete! 🚀"How to use
Section titled “How to use”- Save as
astro-deploy.shin your Astro project root. - Make it executable:
chmod +x astro-deploy.sh- Run it:
./astro-deploy.sh✅ The script will:
- Build your Astro project.
- Upload all files from
dist/to the EC2/var/www/test1folder. - Set correct ownership and permissions.
- Reload Nginx so changes are live.
If you want, I can also improve it to automatically detect your domain, enable HTTPS with Let’s Encrypt, and handle SPA routing in one go.
Do you want me to do that next?