2017-05-26 15:28:06 -04:00
# Deployment
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
This page describes techniques for deploying your Angular application to a remote server.
2017-02-22 13:09:39 -05:00
{@a dev-deploy}
2017-11-03 14:10:53 -04:00
{@a copy-files}
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
## Simplest deployment possible
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
For the simplest deployment, build for development and copy the output directory to a web server.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
1. Start with the development build
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
< code-example language = "none" class = "code-shell" >
ng build
< / code-example >
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
2. Copy _everything_ within the output folder (`dist/` by default) to a folder on the server.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
3. If you copy the files into a server _sub-folder_ , append the build flag, `--base-href` and set the `<base href>` appropriately.< br >< br >
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
For example, if the `index.html` is on the server at `/my/app/index.html` , set the _base href_ to
`<base href="/my/app/">` like this.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
< code-example language = "none" class = "code-shell" >
ng build --base-href=/my/app/
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
You'll see that the `<base href>` is set properly in the generated `dist/index.html` .< br >< br >
If you copy to the server's root directory, omit this step and leave the `<base href>` alone.< br >< br >
Learn more about the role of `<base href>` [below ](guide/deployment#base-tag ).
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
4. Configure the server to redirect requests for missing files to `index.html` .
Learn more about server-side redirects [below ](guide/deployment#fallback ).
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
This is _not_ a production deployment. It's not optimized and it won't be fast for users.
It might be good enough for sharing your progress and ideas internally with managers, teammates, and other stakeholders.
2017-02-22 13:09:39 -05:00
{@a optimize}
## Optimize for production
2017-11-03 14:10:53 -04:00
Although deploying directly from the development environment works,
you can generate an optimized build with additional CLI command line flags,
starting with `--prod` .
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
### Build with _--prod_
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
< code-example language = "none" class = "code-shell" >
ng build --prod
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
The `--prod` _meta-flag_ engages the following optimization features.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
* [Ahead-of-Time (AOT) Compilation ](guide/aot-compiler ): pre-compiles Angular component templates.
* [Production mode ](#enable-prod-mode ): deploys the production environment which enables _production mode_ .
* Bundling: concatenates your many application and library files into a few bundles.
2017-03-31 19:57:13 -04:00
* Minification: removes excess whitespace, comments, and optional tokens.
* Uglification: rewrites code to use short, cryptic variable and function names.
2017-11-03 14:10:53 -04:00
* Dead code elimination: removes unreferenced modules and much unused code.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
The remaining [copy deployment steps ](#copy-files ) are the same as before.
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
You may further reduce bundle sizes by adding the `build-optimizer` flag.
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
< code-example language = "none" class = "code-shell" >
ng build --prod --build-optimizer
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
See the [CLI Documentation ](https://github.com/angular/angular-cli/wiki/build )
for details about available build options and what they do.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
{@a enable-prod-mode}
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
### Enable production mode
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
Angular apps run in development mode by default, as you can see by the following message on the browser
console:
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
< code-example format = "nocode" >
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
Switching to _production mode_ can make it run faster by disabling development specific checks such as the dual change detection cycles.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
Building for production (or appending the `--environment=prod` flag) enables _production mode_
Look at the CLI-generated `main.ts` to see how this works.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
{@a lazy-loading}
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
### Lazy loading
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
You can dramatically reduce launch time by only loading the application modules that
absolutely must be present when the app starts.
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
Configure the Angular Router to defer loading of all other modules (and their associated code), either by
[waiting until the app has launched ](guide/router#preloading "Preloading" )
or by [_lazy loading_ ](guide/router#asynchronous-routing "Lazy loading" )
them on demand.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
#### Don't eagerly import something from a lazy loaded module
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
It's a common mistake.
You've arranged to lazy load a module.
But you unintentionally import it, with a JavaScript `import` statement,
in a file that's eagerly loaded when the app starts, a file such as the root `AppModule` .
If you do that, the module will be loaded immediately.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
The bundling configuration must take lazy loading into consideration.
Because lazy loaded modules aren't imported in JavaScript (as just noted), bundlers exclude them by default.
Bundlers don't know about the router configuration and won't create separate bundles for lazy loaded modules.
You have to create these bundles manually.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
The CLI runs the
[Angular Ahead-of-Time Webpack Plugin ](https://github.com/angular/angular-cli/tree/master/packages/%40ngtools/webpack )
which automatically recognizes lazy loaded `NgModules` and creates separate bundles for them.
2017-02-22 13:09:39 -05:00
{@a measure}
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
### Measure performance
2017-02-22 13:09:39 -05:00
You can make better decisions about what to optimize and how when you have a clear and accurate understanding of
what's making the application slow.
2017-03-31 07:23:16 -04:00
The cause may not be what you think it is.
2017-02-22 13:09:39 -05:00
You can waste a lot of time and money optimizing something that has no tangible benefit or even makes the app slower.
You should measure the app's actual behavior when running in the environments that are important to you.
2017-03-31 07:23:16 -04:00
The
2017-04-24 14:23:45 -04:00
< a href = "https://developers.google.com/web/tools/chrome-devtools/network-performance/understanding-resource-timing" title = "Chrome DevTools Network Performance" >
2017-03-31 07:23:16 -04:00
Chrome DevTools Network Performance page< / a > is a good place to start learning about measuring performance.
2017-02-22 13:09:39 -05:00
2017-03-31 07:23:16 -04:00
The [WebPageTest ](https://www.webpagetest.org/ ) tool is another good choice
2017-02-22 13:09:39 -05:00
that can also help verify that your deployment was successful.
2017-11-03 14:10:53 -04:00
{@a inspect-bundle}
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
### Inspect the bundles
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
The < a href = "https://github.com/danvk/source-map-explorer/blob/master/README.md" > source-map-explorer< / a >
tool is a great way to inspect the generated JavaScript bundles after a production build.
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
Install `source-map-explorer` :
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
< code-example language = "none" class = "code-shell" >
npm install source-map-explorer --save-dev
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
Build your app for production _including the source maps_
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
< code-example language = "none" class = "code-shell" >
2018-06-19 06:59:02 -04:00
ng build --prod --source-map
2017-11-03 14:10:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
List the generated bundles in the `dist/` folder.
< code-example language = "none" class = "code-shell" >
ls dist/*.bundle.js
< / code-example >
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
Run the explorer to generate a graphical representation of one of the bundles.
The following example displays the graph for the _main_ bundle.
< code-example language = "none" class = "code-shell" >
node_modules/.bin/source-map-explorer dist/main.*.bundle.js
< / code-example >
The `source-map-explorer` analyzes the source map generated with the bundle and draws a map of all dependencies,
showing exactly which classes are included in the bundle.
Here's the output for the _main_ bundle of the QuickStart.
< figure >
< img src = "generated/images/guide/cli-quickstart/quickstart-sourcemap-explorer.png" alt = "quickstart sourcemap explorer" >
< / figure >
{@a base-tag}
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
## The `base` tag
2017-02-22 13:09:39 -05:00
2017-04-21 13:10:19 -04:00
The HTML [_<base href="..."/>_ ](/guide/router )
2017-02-22 13:09:39 -05:00
specifies a base path for resolving relative URLs to assets such as images, scripts, and style sheets.
2017-03-31 07:23:16 -04:00
For example, given the `<base href="/my/app/">` , the browser resolves a URL such as `some/place/foo.jpg`
2017-02-22 13:09:39 -05:00
into a server request for `my/app/some/place/foo.jpg` .
During navigation, the Angular router uses the _base href_ as the base path to component, template, and module files.
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-04-30 16:10:32 -04:00
See also the [*APP_BASE_HREF* ](api/common/APP_BASE_HREF "API: APP_BASE_HREF" ) alternative.
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 07:23:16 -04:00
In development, you typically start the server in the folder that holds `index.html` .
2017-02-22 13:09:39 -05:00
That's the root folder and you'd add `<base href="/">` near the top of `index.html` because `/` is the root of the app.
But on the shared or production server, you might serve the app from a subfolder.
2017-03-31 07:23:16 -04:00
For example, when the URL to load the app is something like `http://www.mysite.com/my/app/` ,
2017-02-22 13:09:39 -05:00
the subfolder is `my/app/` and you should add `<base href="/my/app/">` to the server version of the `index.html` .
2017-11-03 14:10:53 -04:00
When the `base` tag is mis-configured, the app fails to load and the browser console displays `404 - Not Found` errors
2017-02-22 13:09:39 -05:00
for the missing files. Look at where it _tried_ to find those files and adjust the base tag appropriately.
2017-11-03 14:10:53 -04:00
## _build_ vs. _serve_
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
You'll probably prefer `ng build` for deployments.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
The **ng build** command is intended for building the app and deploying the build artifacts elsewhere.
The **ng serve** command is intended for fast, local, iterative development.
2017-03-30 15:04:18 -04:00
2017-11-03 14:10:53 -04:00
Both `ng build` and `ng serve` **clear the output folder** before they build the project.
The `ng build` command writes generated build artifacts to the output folder.
The `ng serve` command does not.
It serves build artifacts from memory instead for a faster development experience.
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
< div class = "l-sub-section" >
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
The output folder is `dist/` by default.
2018-04-30 08:23:24 -04:00
To output to a different folder, change the `outputPath` in `angular.json` .
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
The `ng serve` command builds, watches, and serves the application from a local CLI development server.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
The `ng build` command generates output files just once and does not serve them.
The `ng build --watch` command will regenerate output files when source files change.
This `--watch` flag is useful if you're building during development and
are automatically re-deploying changes to another server.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
See the [CLI `build` topic ](https://github.com/angular/angular-cli/wiki/build ) for more details and options.
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
< hr >
2017-02-22 13:09:39 -05:00
{@a server-configuration}
## Server configuration
This section covers changes you may have make to the server or to files deployed to the server.
{@a fallback}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Routed apps must fallback to `index.html`
Angular apps are perfect candidates for serving with a simple static HTML server.
You don't need a server-side engine to dynamically compose application pages because
Angular does that on the client-side.
2017-03-31 07:23:16 -04:00
If the app uses the Angular router, you must configure the server
2017-02-22 13:09:39 -05:00
to return the application's host page (`index.html`) when asked for a file that it does not have.
{@a deep-link}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
A routed application should support "deep links".
A _deep link_ is a URL that specifies a path to a component inside the app.
For example, `http://www.mysite.com/heroes/42` is a _deep link_ to the hero detail page
that displays the hero with `id: 42` .
There is no issue when the user navigates to that URL from within a running client.
The Angular router interprets the URL and routes to that page and hero.
2017-03-31 07:23:16 -04:00
But clicking a link in an email, entering it in the browser address bar,
2017-02-22 13:09:39 -05:00
or merely refreshing the browser while on the hero detail page —
all of these actions are handled by the browser itself, _outside_ the running application.
The browser makes a direct request to the server for that URL, bypassing the router.
A static server routinely returns `index.html` when it receives a request for `http://www.mysite.com/` .
2017-03-31 07:23:16 -04:00
But it rejects `http://www.mysite.com/heroes/42` and returns a `404 - Not Found` error *unless* it is
2017-02-22 13:09:39 -05:00
configured to return `index.html` instead.
#### Fallback configuration examples
There is no single configuration that works for every server.
2017-03-31 07:23:16 -04:00
The following sections describe configurations for some of the most popular servers.
2017-02-22 13:09:39 -05:00
The list is by no means exhaustive, but should provide you with a good starting point.
#### Development servers
2017-03-31 19:57:13 -04:00
* [Lite-Server ](https://github.com/johnpapa/lite-server ): the default dev server installed with the
2017-02-22 13:09:39 -05:00
[Quickstart repo ](https://github.com/angular/quickstart ) is pre-configured to fallback to `index.html` .
2017-11-03 14:10:53 -04:00
2017-03-31 19:57:13 -04:00
* [Webpack-Dev-Server ](https://github.com/webpack/webpack-dev-server ): setup the
2017-02-22 13:09:39 -05:00
`historyApiFallback` entry in the dev server options as follows:
2017-11-03 14:10:53 -04:00
< code-example >
historyApiFallback: {
disableDotRule: true,
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml']
}
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
#### Production servers
2017-03-31 19:57:13 -04:00
* [Apache ](https://httpd.apache.org/ ): add a
2017-11-03 14:10:53 -04:00
[rewrite rule ](http://httpd.apache.org/docs/current/mod/mod_rewrite.html ) to the `.htaccess` file as shown
(https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/):
2017-03-31 19:57:13 -04:00
2017-11-03 14:10:53 -04:00
< code-example format = "." >
RewriteEngine On
& #35 If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
2017-02-22 13:09:39 -05:00
2017-11-03 14:10:53 -04:00
& #35 If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
< / code-example >
2017-03-31 19:57:13 -04:00
* [NGinx ](http://nginx.org/ ): use `try_files` , as described in
2017-02-22 13:09:39 -05:00
[Front Controller Pattern Web Apps ](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#front-controller-pattern-web-apps ),
modified to serve `index.html` :
2017-11-03 14:10:53 -04:00
< code-example format = "." >
try_files $uri $uri/ /index.html;
< / code-example >
2017-03-31 19:57:13 -04:00
* [IIS ](https://www.iis.net/ ): add a rewrite rule to `web.config` , similar to the one shown
2017-02-22 13:09:39 -05:00
[here ](http://stackoverflow.com/a/26152011/2116927 ):
2017-03-30 15:04:18 -04:00
2017-11-03 14:10:53 -04:00
< code-example format = '.' >
< system.webServer>
< rewrite>
< rules>
< rule name="Angular Routes" stopProcessing="true">
< match url=".*" />
< conditions logicalGrouping="MatchAll">
< add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
< add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
< /conditions>
2018-02-06 15:31:21 -05:00
< action type="Rewrite" url="/index.html" />
2017-11-03 14:10:53 -04:00
< /rule>
< /rules>
< /rewrite>
< /system.webServer>
< / code-example >
2017-03-31 19:57:13 -04:00
* [GitHub Pages ](https://pages.github.com/ ): you can't
2017-03-31 07:23:16 -04:00
[directly configure ](https://github.com/isaacs/github/issues/408 )
the GitHub Pages server, but you can add a 404 page.
2017-02-22 13:09:39 -05:00
Copy `index.html` into `404.html` .
It will still be served as the 404 response, but the browser will process that page and load the app properly.
2017-03-31 07:23:16 -04:00
It's also a good idea to
[serve from `docs/` on master ](https://help.github.com/articles/configuring-a-publishing-source-for-github-pages/#publishing-your-github-pages-site-from-a-docs-folder-on-your-master-branch )
and to
2017-02-22 13:09:39 -05:00
[create a `.nojekyll` file ](https://www.bennadel.com/blog/3181-including-node-modules-and-vendors-folders-in-your-github-pages-site.htm )
2017-11-03 14:10:53 -04:00
2017-03-31 19:57:13 -04:00
* [Firebase hosting ](https://firebase.google.com/docs/hosting/ ): add a
2017-02-22 13:09:39 -05:00
[rewrite rule ](https://firebase.google.com/docs/hosting/url-redirects-rewrites#section-rewrites ).
2017-11-03 14:10:53 -04:00
< code-example format = "." >
"rewrites": [ {
"source": "**",
"destination": "/index.html"
} ]
2017-03-30 15:04:18 -04:00
2017-11-03 14:10:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
{@a cors}
### Requesting services from a different server (CORS)
Angular developers may encounter a
2017-04-24 14:23:45 -04:00
< a href = "https://en.wikipedia.org/wiki/Cross-origin_resource_sharing" title = "Cross-origin resource sharing" >
2018-01-05 04:36:47 -05:00
< i > cross-origin resource sharing< / i > < / a > error when making a service request (typically a data service request)
2017-02-22 13:09:39 -05:00
to a server other than the application's own host server.
Browsers forbid such requests unless the server permits them explicitly.
There isn't anything the client application can do about these errors.
2017-03-31 07:23:16 -04:00
The server must be configured to accept the application's requests.
2017-02-22 13:09:39 -05:00
Read about how to enable CORS for specific servers at
2017-04-24 14:23:45 -04:00
< a href = "http://enable-cors.org/server.html" title = "Enabling CORS server" > enable-cors.org< / a > .