Skip to content

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.


  • EC2 instance with Ubuntu (or similar Linux)
  • Nginx installed
  • Your Astro project is already built:
Terminal window
npm run build
# This creates a `dist/` directory with your static files
  • You have SSH access to your EC2 instance.

You can use scp or rsync:

Using scp:

Terminal window
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:
Terminal window
sudo mkdir -p /var/www/test1
  • Then set proper ownership and permissions:
Terminal window
sudo chown -R www-data:www-data /var/www/test1
sudo chmod -R 755 /var/www/test1

Using rsync:

Create the folder with sudo, then give ubuntu write access

Terminal window
sudo mkdir -p /var/www/as1
sudo chown -R www-data:www-data /var/www/as1
sudo chmod -R 755 /var/www/as1
#sudo chown -R ubuntu:ubuntu /var/www/as1
Terminal window
rsync -avz -e "ssh -i ~/.ssh/web1.pem" ./dist/ ubuntu@op1.evolo.fr:/var/www/as1/ --rsync-path="sudo rsync"

Edit (or create) a server block for your site:

Terminal window
sudo nano /etc/nginx/sites-available/test1

Add 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.

Terminal window
sudo ln -s /etc/nginx/sites-available/test1 /etc/nginx/sites-enabled/
sudo nginx -t # test configuration
sudo systemctl reload nginx

  • Open http://your-ec2-ip (or your domain if DNS is set) in a browser.
  • You should see your Astro site served by Nginx.

  • Enable HTTPS with Let’s Encrypt:
Terminal window
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d as1.evolo.fr
  • Automatic deployment: Use rsync + systemd or CI/CD to push updated dist after every build.

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.


#!/bin/bash
# -------------------------------
# CONFIGURATION
# -------------------------------
KEY_PATH="/path/to/your-key.pem" # Path to your EC2 private key
EC2_USER="ubuntu" # EC2 username (ubuntu for Ubuntu AMI)
EC2_IP="YOUR.EC2.IP.ADDRESS" # EC2 public IP
REMOTE_DIR="/var/www/test1" # Remote directory for site
LOCAL_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! 🚀"

  1. Save as astro-deploy.sh in your Astro project root.
  2. Make it executable:
Terminal window
chmod +x astro-deploy.sh
  1. Run it:
Terminal window
./astro-deploy.sh

✅ The script will:

  1. Build your Astro project.
  2. Upload all files from dist/ to the EC2 /var/www/test1 folder.
  3. Set correct ownership and permissions.
  4. 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?