Deploying a Static JS/TS Site on Apache2 (VPS)

1. Prerequisites

/var/www/your_project
TypeHost (subdomain)Value (IP)
AsubdomainYOUR_VPS_IP

For example, if your desired URL is:

https://subdomain.example.com

then Host should be subdomain and the domain is example.com.


2. Build the Project

SSH into the VPS:

ssh user@YOUR_VPS_IP

Go to the project folder:

cd /var/www/your_project

Install dependencies (first time or after package.json changes):

npm install

Build the production files:

npm run build

This creates the static files in:

/var/www/your_project/dist

Ensure Apache (user www-data) can read the files:

sudo chown -R www-data:www-data /var/www/your_project
sudo find /var/www/your_project -type d -exec chmod 755 {} \;
sudo find /var/www/your_project -type f -exec chmod 644 {} \;

4. Create the Apache Virtual Host

Create a new Apache config file:

sudo nano /etc/apache2/sites-available/subdomain.example.com.conf

Replace subdomain.example.com with your actual subdomain and domain.

Put the following content inside:

<VirtualHost *:80>
    ServerName subdomain.example.com

    DocumentRoot /var/www/your_project/dist

    <Directory /var/www/your_project/dist>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/subdomain_error.log
    CustomLog ${APACHE_LOG_DIR}/subdomain_access.log combined
</VirtualHost>

Adjust:

Save and exit.


5. Enable the Site and Reload Apache

Enable the new virtual host:

sudo a2ensite subdomain.example.com.conf

(Optional) Disable the default site if you don’t want it:

sudo a2dissite 000-default.conf

Check Apache configuration syntax:

sudo apache2ctl configtest

If it shows Syntax OK, reload Apache:

sudo systemctl reload apache2

6. Test the Deployment

Open a browser and visit your subdomain, for example:

http://subdomain.example.com/

You should see your deployed site.

If you get an error:


Install Certbot and the Apache plugin:

sudo apt update
sudo apt install certbot python3-certbot-apache

Run Certbot (replace with your real subdomain):

sudo certbot --apache -d subdomain.example.com

Follow the prompts. Certbot will:

After that, your site will be available at:

https://subdomain.example.com/

8. Updating the Site (New Version Deploy)

Whenever you change the project and want to deploy a new version:

  1. SSH into the VPS:

    ssh user@YOUR_VPS_IP
    cd /var/www/your_project
    
  2. Pull latest code (if using Git):

    git pull
    
  3. Install dependencies if needed:

    npm install
    
  4. Rebuild:

    npm run build
    
  5. (Optional) Fix permissions again:

    sudo chown -R www-data:www-data /var/www/your_project
    
  6. Reload Apache (usually not required for static files, but safe):

    sudo systemctl reload apache2
    

Your new version is now live.


9. Quick Summary (Cheat Sheet)

# On VPS
cd /var/www/your_project
npm install
npm run build

sudo nano /etc/apache2/sites-available/subdomain.example.com.conf
sudo a2ensite subdomain.example.com.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

# Optional: HTTPS
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d subdomain.example.com
aria

© 2025 Aria

Instagram 𝕏 GitHub