How to deploy an Angular app to shared hosting
Summary
Deploy your Angular app to shared hosting in three simple steps: run ng build --prod (or ng build in Angular 12+), modify the base href if needed, and upload the contents of the dist folder to your hosting provider. Your static Angular application will be ready to serve.
Introduction
Shared hosting remains a cost-effective solution for deploying Angular applications, especially for small to medium-sized projects. Unlike complex deployment pipelines, deploying to shared hosting is straightforward when your application doesn't require server-side dependencies. This guide walks you through the complete process using the Angular CLI.
Prerequisites
Before you begin, ensure you have:
Step 1: build your Angular app for production
The Angular CLI provides a powerful build command that prepares your application for production deployment.
For Angular 12 and later
ng build
The production configuration is now the default build target in modern Angular versions.
For Angular 11 and earlier
ng build --prod
What happens during the build?
The production build process:
After a successful build, you'll find a dist folder in your project root containing your production-ready application.
Step 2: Configure the base-href
The base href tells your Angular app where it's located on the server. This is crucial for proper routing and asset loading.
If deploying to root directory
If your app will be accessible at https://yourdomain.com, you can leave the default setting or explicitly set it during build:
ng build --base-href="/"
The index.html file should contain:
<base href="/">
If deploying to a subdirectory
If your app will be in a subdirectory like https://yourdomain.com/myapp/, you have two options:
Option 1: Set during build
ng build --base-href="/myapp/"
Option 2: Manually edit index.html
Open dist/your-app-name/index.html and modify the base tag:
<base href="/myapp/">
Important: Always include the trailing slash in the base href.
Step 3: upload files to shared hosting
- Navigate to your
dist/your-app-namedirectory - Select all files and folders inside this directory
- Upload them to your hosting provider using:
- Place files in the appropriate directory:
Troubleshooting common issues
Routing Issues (404 Errors on Refresh)
Angular uses client-side routing by default. When users refresh the page or access routes directly, the server looks for those files and returns 404 errors.
Solution: Configure your .htaccess file (for Apache servers):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
For subdirectory deployments, update the RewriteBase accordingly:
RewriteBase /myapp/
RewriteRule . /myapp/index.html [L]
API and external resource issues
When your app consumes APIs or external resources:
Assets not loading
If images, fonts, or other assets fail to load:
Blank white page
A blank page typically indicates:
Alternative build optimizations
Further reduce bundle size:
ng build --optimization=true --build-optimizer=true
Generate a build report:
ng build --stats-json
Then analyze with:
npx webpack-bundle-analyzer dist/your-app-name/stats.json
Best practices
Conclusion
Deploying an Angular application to shared hosting is a straightforward process that requires minimal configuration. By following this guide, you can have your Angular app live and accessible in minutes. Remember to test thoroughly, configure routing properly, and optimize your build for the best performance.
The simplicity and cost-effectiveness of shared hosting make it an excellent choice for Angular applications that don't require server-side rendering or complex backend infrastructure.
