将 Angular 2 部署到 Apache 服务器 [英] Deploy Angular 2 to Apache Server
问题描述
我想在 Apache 服务器上部署一个 Angular 2 应用程序.我已经阅读了各种指南,例如 this 和 this 但它们都不起作用.我在服务器上安装了 npm
和 ng
.
I want to deploy an Angular 2 application on an Apache server. I've read various guides like this and this but none of them is working. I have npm
and ng
installed on the server.
简而言之,这就是我所做的:
In a nutshell, here's what I did:
- 在我的服务器上克隆了完整的项目存储库.
- 使用
npm install
安装依赖项. - 使用
ng build --prod
命令并创建了一个dist
目录. - 将
apache
根目录更改为/var/www/html/dist
目录. - 启用
mod_rewrite
,重新启动apache
并将此.htaccess
添加到我的dist
目录中.
- Cloned complete project repository on my server.
- Installed dependencies using
npm install
. - Used
ng build --prod
command and it created adist
directory. - Changed
apache
root to/var/www/html/dist
directory. - Enabled
mod_rewrite
, restartedapache
and added this.htaccess
in mydist
directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
但只有我的主页 domain.com
有效,其他页面如 domain.com/login
、domain.com/register
等.抛出 404 错误.甚至 domain.com/index.html/login
也不起作用.
But only my home page domain.com
works, other pages like domain.com/login
, domain.com/register
etc. throw 404 error. Even domain.com/index.html/login
doesn't work.
该应用程序在我使用 ng serve
开发它的本地系统上运行良好.我错过了什么?
The application works fine on my local system where I'm developing it using ng serve
. What am i missing?
推荐答案
看来我的 /etc/apache2/sites-enabled/000-default.conf
文件中缺少这个.添加此并重新启动apache
后,网站运行良好.
It appears i was missing this in my /etc/apache2/sites-enabled/000-default.conf
file. After adding this and restarting apache
, website runs fine.
<Directory "/var/www/html/dist">
AllowOverride All
</Directory>
这篇关于将 Angular 2 部署到 Apache 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!