问题描述
我想在Apache服务器上部署Angular 2应用程序。我已经阅读了各种指南,例如,但是它们都不起作用。我在服务器上安装了 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 server.
简而言之,这就是我的工作:
In 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在
RewriteBase /
RewriteRule ^ index\.html $-[L]
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}! -d
RewriteRule。 /index.html [L]
< / IfModule>
但只有我的主页域.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服务
开发它的系统。我缺少什么?
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服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!