# Deployment
# 部署
When you are ready to deploy your Angular application to a remote server, you have various options for
deployment.
当你准备把 Angular 应用部署到远程服务器上时,有很多关于部署的选项。
{@a dev-deploy}
{@a copy-files}
## Simplest deployment possible
## 最简化的部署方式
For the simplest deployment, build for development and copy the output directory to a web server.
最简化的部署方式就是为开发环境构建,并把其输出复制到 Web 服务器上。
1. Start with the development build:
使用开发环境进行构建
ng build
2. Copy _everything_ within the output folder (`dist/` by default) to a folder on the server.
把输出目录(默认为 `dist/`)下的*每个文件*都复制到到服务器上的某个目录下。
3. Configure the server to redirect requests for missing files to `index.html`.
Learn more about server-side redirects [below](#fallback).
配置服务器,让缺失的文件都重定向到 `index.html` 上。
欲知详情,参见[稍后](#fallback)的服务端重定向部分。
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. For the next steps in deployment, see [Optimize for production](#optimize).
这*不是*生产环境部署。它没有优化过,对最终用户来说也不快。
但是,这足够用来跟管理者、团队成员和其它涉众在内部分享你的进度和想法了。
部署的下一个步骤,参见[为生产环境优化](#optimize)。
{@a deploy-to-github}
## Deploy to GitHub pages
## 发布到 GitHub pages(页面服务)
Another simple way to deploy your Angular app is to use [GitHub Pages](https://help.github.com/articles/what-is-github-pages/).
另一种发布 Angular 应用的简单途径是使用 [GitHub Pages](https://help.github.com/articles/what-is-github-pages/)。
1. You need to [create a GitHub account](https://github.com/join) if you don't have one, and then [create a repository](https://help.github.com/articles/create-a-repo/) for your project.
Make a note of the user name and project name in GitHub.
你需要[创建一个 GitHub 账号](https://github.com/join)(如果没有的话),然后为你的项目[创建一个仓库](https://help.github.com/articles/create-a-repo/)。记下 GitHub 中的用户名和项目名。
1. Build your project using Github project name, with the Angular CLI command [`ng build`](cli/build) and the options shown here:
使用 Angular CLI 命令 [`ng build`](cli/build) 来构建这个 GitHub 项目,选项如下:
ng build --prod --output-path docs --base-href
1. When the build is complete, make a copy of `docs/index.html` and name it `docs/404.html`.
当构建完成时,把 `docs/index.html` 复制为 `docs/404.html`。
1. Commit your changes and push.
提交你的更改,并推送。
1. On the GitHub project page, configure it to [publish from the docs folder](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).
在 GitHub 的项目页中,把该项目配置为[从 docs 目录下发布](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)。
You can see your deployed page at `https://.github.io//`.
你可以到 `https://.github.io//` 中查看部署好的页面。
ng build --watch
1. Start the web server in terminal B:
在终端 B 中启动 Web 服务器:
lite-server --baseDir="dist"
The default browser opens to the appropriate URL.
默认的浏览器会打开相应的 URL。
* [Lite-Server](https://github.com/johnpapa/lite-server): the default dev server installed with the
[Quickstart repo](https://github.com/angular/quickstart) is pre-configured to fallback to `index.html`.
[Lite-Server](https://github.com/johnpapa/lite-server)是["快速上手"仓库](https://github.com/angular/quickstart)中安装的默认开发服务器,它被预先配置为回退到 `index.html`。
* [Webpack-Dev-Server](https://github.com/webpack/webpack-dev-server): setup the
`historyApiFallback` entry in the dev server options as follows:
[Webpack-Dev-Server](https://github.com/webpack/webpack-dev-server)在开发服务器的配置中设置了 `historyApiFallback`,代码如下:
historyApiFallback: {
disableDotRule: true,
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml']
}
#### Production servers
#### 生产服务器
* [Apache](https://httpd.apache.org/): add a
[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/):
[Apache](https://httpd.apache.org/):在 `.htaccess` 文件中添加一个[重写规则](http://httpd.apache.org/docs/current/mod/mod_rewrite.html),
代码如下([出处](https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/)):
RewriteEngine On
# 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]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
* [Nginx](http://nginx.org/): use `try_files`, as described in
[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`:
[NGinx](http://nginx.org/):使用 `try_files` 指向 `index.html`,详细描述见[Web 应用的前端控制器模式](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#front-controller-pattern-web-apps)。
try_files $uri $uri/ /index.html;
* [IIS](https://www.iis.net/): add a rewrite rule to `web.config`, similar to the one shown
[here](http://stackoverflow.com/a/26152011/2116927):
[IIS](https://www.iis.net/):往 `web.config` 中添加一条重写规则,类似于[这里](http://stackoverflow.com/a/26152011/2116927):
<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>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
* [GitHub Pages](https://pages.github.com/): you can't
[directly configure](https://github.com/isaacs/github/issues/408)
the GitHub Pages server, but you can add a 404 page.
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.
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
[create a `.nojekyll` file](https://www.bennadel.com/blog/3181-including-node-modules-and-vendors-folders-in-your-github-pages-site.htm)
[GitHub 页面服务](https://pages.github.com/):你没办法[直接配置](https://github.com/isaacs/github/issues/408) Github 的页面服务,但可以添加一个 404 页,只要把 `index.html` 复制到 `404.html` 就可以了。
它仍然会给出一个 404 响应,但是浏览器将会正确处理该页,并正常加载该应用。
使用[在主分支的 `docs/` 下启动服务](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)
并[创建一个 `.nojekyll` 文件](https://www.bennadel.com/blog/3181-including-node-modules-and-vendors-folders-in-your-github-pages-site.htm)也是一个好办法。
* [Firebase hosting](https://firebase.google.com/docs/hosting/): add a
[rewrite rule](https://firebase.google.com/docs/hosting/url-redirects-rewrites#section-rewrites).
[Firebase 主机服务](https://firebase.google.com/docs/hosting/):添加一条[重写规则](https://firebase.google.com/docs/hosting/url-redirects-rewrites#section-rewrites)。
"rewrites": [ {
"source": "**",
"destination": "/index.html"
} ]
{@a cors}
### Requesting services from a different server (CORS)
### 请求来自另一个服务器的服务(CORS)
Angular developers may encounter a
cross-origin resource sharing error when making a service request (typically a data service request)
to a server other than the application's own host server.
Browsers forbid such requests unless the server permits them explicitly.
Angular 开发者在向与该应用的宿主服务器不同域的服务器发起请求时,可能会遇到一种跨域资源共享(CORS)错误。
浏览器会阻止该请求,除非得到那台服务器的明确许可。
There isn't anything the client application can do about these errors.
The server must be configured to accept the application's requests.
Read about how to enable CORS for specific servers at
enable-cors.org.
客户端应用对这种错误无能为力。
服务器必须配置成可以接受来自该应用的请求。
要了解如何对特定的服务器开启 CORS,参见enable-cors.org。
{@a optimize}
## Optimize for production
## 为生产环境优化
Although deploying directly from the development environment works,
you can generate an optimized build with additional CLI command line flags,
starting with `--prod`.
虽然也可以直接用开发环境部署,但也可以使用其它的 CLI 命令行标志来生成优化过的构建成果,我们先从 `--prod` 开始讲。
### Build with _--prod_
### 使用*--prod*构建
ng build --prod
The `--prod` _meta-flag_ engages the following optimization features.
`--prod` 标志具有如下优化特性。
* [Ahead-of-Time (AOT) Compilation](guide/aot-compiler): pre-compiles Angular component templates.
[预先(AOT)编译](guide/aot-compiler):预编译 Angular 的组件模板。
* [Production mode](#enable-prod-mode): deploys the production environment which enables _production mode_.
[生产模式](#enable-prod-mode):部署到启用了*生产模式*的生产环境。
* Bundling: concatenates your many application and library files into a few bundles.
打包:把你的多个应用于库文件拼接到少量包(bundle)中。
* Minification: removes excess whitespace, comments, and optional tokens.
最小化:删除多余的空格、注释和可选令牌。
* Uglification: rewrites code to use short, cryptic variable and function names.
混淆/丑化:重写代码,使用简短的、不容易理解的变量名和函数名。
* Dead code elimination: removes unreferenced modules and much unused code.
消除死代码:删除未引用过的模块和很多未用到的代码。
The remaining [copy deployment steps](#copy-files) are the same as before.
其余的[复制等部署步骤](#copy-files)步骤和以前的一样。
See [`ng build`](cli/build) for more about CLI build options and what they do.
要了解关于 CLI 构建选项及其作用的更多知识,参见 [`ng build`](cli/build)。
{@a enable-prod-mode}
### Enable production mode
### 启用生产模式
Angular apps run in development mode by default, as you can see by the following message on the browser
console:
Angular 应用默认运行在开发模式下,你可以在浏览器的控制台中看到如下信息:
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
Switching to _production mode_ can make it run faster by disabling development specific checks such as the dual change detection cycles.
切换到*生产模式*可以通过禁用开发阶段特有的检查(比如双重变更检测周期)来让它运行得更快。
Building for production (or appending the `--environment=prod` flag) enables _production mode_
Look at the CLI-generated `main.ts` to see how this works.
为生产环境构建(添加 `--environment=prod` 标识)可以启用*生产模式*。
阅读 CLI 生成的 `main.ts` 以了解它的工作原理。
{@a lazy-loading}
### Lazy loading
### 惰性加载
You can dramatically reduce launch time by only loading the application modules that
absolutely must be present when the app starts.
通过只加载应用启动时绝对必须的那些模块,你可以戏剧性的缩短应用启动时间。
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.
可以配置 Angular 的路由器,来推迟所有其它模块(及其相关代码)的加载时机,方法有[一直等到应用启动完毕](guide/router#preloading "Preloading"),或者当用到时才按需[*惰性加载*](guide/router#asynchronous-routing "Lazy loading")。
Chrome DevTools Network Performance page is a good place to start learning about measuring performance.
Chrome DevTools 的网络和性能页是你开始学习如何测量性能的好地方。
The [WebPageTest](https://www.webpagetest.org/) tool is another good choice
that can also help verify that your deployment was successful.
[WebPageTest](https://www.webpagetest.org/)工具是另一个不错的选择,它还能帮你验证这次部署是否成功。
{@a inspect-bundle}
### Inspect the bundles
### 检查发布包
The source-map-explorer
tool is a great way to inspect the generated JavaScript bundles after a production build.
source-map-explorer 工具可以帮你在生产环境构建之后探查 JavaScript 包。
Install `source-map-explorer`:
安装 `source-map-explorer`:
npm install source-map-explorer --save-dev
Build your app for production _including the source maps_
为生产环境构建应用,包括源码映射表(source map)
ng build --prod --source-map
List the generated bundles in the `dist/` folder.
在 `dist/` 目录下列出生成的包。
ls dist/*.bundle.js
Run the explorer to generate a graphical representation of one of the bundles.
The following example displays the graph for the _main_ bundle.
运行浏览器来生成其中一个包的图形化表示。
下面的例子展示了 `main` 包的图表。
node_modules/.bin/source-map-explorer dist/main.*.bundle.js
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.
`source-map-explorer` 会分析与包一期生成的 source map,并画出所有依赖的地图,精确展示哪些类包含在哪个包中。
Here's the output for the _main_ bundle of the QuickStart.
下面是 "快速上手" 应用中 `main` 包的输出。
{@a base-tag}
## The `base` tag
## `base` 标签
The HTML [_<base href="..."/>_](/guide/router)
specifies a base path for resolving relative URLs to assets such as images, scripts, and style sheets.
For example, given the ``, the browser resolves a URL such as `some/place/foo.jpg`
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.
HTML 的 [_<base href="..."/>_](/guide/router) 标签指定了用于解析静态文件(如图片、脚本和样式表)相对地址的基地址。
比如,对于 ``,浏览器就会把 `some/place/foo.jpg` 这样的 URL 解析成到 `my/app/some/place/foo.jpg` 的请求。
在导航期间,Angular 路由器使用 *base href* 作为到组件模板文件和模块文件的基地址。
See also the [*APP_BASE_HREF*](api/common/APP_BASE_HREF "API: APP_BASE_HREF") alternative.
另一种方式参见 [*APP_BASE_HREF*](api/common/APP_BASE_HREF "API: APP_BASE_HREF")。
In development, you typically start the server in the folder that holds `index.html`.
That's the root folder and you'd add `` near the top of `index.html` because `/` is the root of the app.
在开发期间,你通常会在存有 `index.html` 的目录下启动开发服务器。
那就是根目录,你要在 `index.html` 的顶部附近添加 ``,因为 `/` 就是该应用的根路径。
But on the shared or production server, you might serve the app from a subfolder.
For example, when the URL to load the app is something like `http://www.mysite.com/my/app/`,
the subfolder is `my/app/` and you should add `` to the server version of the `index.html`.
但是在共享或生产服务器上,你可能会在子目录下启动服务器。
比如,当前应用的加载地址可能类似于 `http://www.mysite.com/my/app/`,这里的子目录就是 `my/app/`。所以你就要往服务端版本的 `index.html` 中添加 ``。
When the `base` tag is mis-configured, the app fails to load and the browser console displays `404 - Not Found` errors
for the missing files. Look at where it _tried_ to find those files and adjust the base tag appropriately.
这里如果不配置 `base` 标签,应用就会失败,并在浏览器的控制台中为缺失的文件显示一个 `404 - Not Found` 错误。看看它*试图*从哪里去查找那些文件,并据此调整 base 标签。
## Building and serving for deployment
## 为部署而构建和启动服务器
When you are designing and developing applications, you typically use `ng serve` to build your app for fast, local, iterative development.
When you are ready to deploy, however, you must use the `ng build` command to build the app and deploy the build artifacts elsewhere.
在设计和开发应用程序时,通常使用 `ng serve` 来构建应用,已进行快速的、本地的、迭代式的开发。
不过,当准备部署时,你必须使用 `ng build` 命令来构建应用,并在别处部署构建成果。
Both `ng build` and `ng serve` clear the output folder before they build the project, but only the `ng build` command writes the generated build artifacts to the output folder.
在构建之前,`ng build` 和 `ng serve` 都会清空输出目录,但是只有 `ng build` 命令才会把生成的构建成果写入到输出目录下。
The output folder is `dist/` by default.
To output to a different folder, change the `outputPath` in `angular.json`.
输出目录默认为 `dist/`。
要想输出到其它目录,请修改 `angular.json` 中的 `outputPath`。
The `ng serve` command builds, watches, and serves the application from local memory, using a local development server.
When you have deployed your app to another server, however, you might still want to serve the app so that you can continue to see changes that you make in it.
You can do this by adding the `--watch` option to the `ng build` command.
`ng serve` 命令会构建、监视并使用本地开发服务器从内存中提供网站服务。
但是,当你将应用部署到其它服务器时,你可能希望仍然能持续看到你对该应用所做的修改。这时候,你可以为 `ng build` 命令添加 `--watch` 选项来做到这一点。
```
ng build --watch
```
Like the `ng serve` command, this regenerates output files when source files change.
像 `ng serve` 命令一样,当源码文件发生变化时,它会重新生成输出文件。
For complete details of the CLI commands, see the [CLI command reference](cli).
要了解 CLI 命令的详细信息,参见 [CLI 命令参考手册](cli)。