如何为Angular 2配置Apache [英] How to configure apache for angular 2
问题描述
我需要为angular2配置虚拟主机.我已经尝试过这篇文章
I need to configure virtual host for angular2. I have tried following this article
https://www.packtpub.com/mapt/book/Web+Development/9781783983582/2/ch02lvl1sec15/Configuring+Apache+for+Angular
因此,我需要像这样设置虚拟主机
According to this i need to setup virtual host like this
<VirtualHost *:80>
ServerName my-app
DocumentRoot /path/to/app
<Directory /path/to/app>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html
# to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
谁能告诉我应用程序的路径应该是什么,因为我的应用程序在默认的2号角端口4200上运行. 还有其他方法吗?
Can anyone tell me what should be the path to app as my app is running on default angular 2 port that is 4200. Is there any other way to do it.
推荐答案
angular-cli构建
在本地开发环境中,在项目根目录中运行ng build --prod
.
On your local development environment run ng build --prod
in your project root.
这将创建一个名为dist
的文件夹,您希望将所有文件和文件夹从dist
内放置到服务器上的Apache根目录中.
This will create a folder called dist
, you want to place all the files and folders from within dist
to your Apache root directory on your server.
设置apache服务到index.html的路由.您可以使用两种方法来编辑虚拟主机,也可以在网站根目录中使用.htaccess.
Setup apache to serve routes to index.html. You have two methods you can use, either edit your virtual host or use .htaccess in your website root directory.
选项1:虚拟主机
<VirtualHost *:80>
ServerName my-app
DocumentRoot /path/to/app
<Directory /path/to/app>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html
# to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
选项2:.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html
# to allow html5 state links
RewriteRule ^ index.html [L]
</IfModule>
这篇关于如何为Angular 2配置Apache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!