将 Angular 2 部署到 Apache 服务器 [英] Deploy Angular 2 to Apache Server

查看:40
本文介绍了将 Angular 2 部署到 Apache 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Apache 服务器上部署一个 Angular 2 应用程序.我已经阅读了各种指南,例如 thisthis 但它们都不起作用.我在服务器上安装了 npmng.

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:

  1. 在我的服务器上克隆了完整的项目存储库.
  2. 使用 npm install 安装依赖项.
  3. 使用 ng build --prod 命令并创建了一个 dist 目录.
  4. apache 根目录更改为 /var/www/html/dist 目录.
  5. 启用mod_rewrite,重新启动apache 并将此.htaccess 添加到我的dist 目录中.
  1. Cloned complete project repository on my server.
  2. Installed dependencies using npm install.
  3. Used ng build --prod command and it created a dist directory.
  4. Changed apache root to /var/www/html/dist directory.
  5. Enabled mod_rewrite, restarted apache and added this .htaccess in my dist 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/logindomain.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆