Is it possible to use ng-include without web server? - angularjs

I'm running an AngularJS app which merely include a file
<div ng-include src="'sample.html'"></div>
That works when run under web server. But if the web app is run from merely double clicking from file explorer (i.e. not from web server), it doesn't include the file
Does ng-include works outside of web server? I checked the Network status on Chrome, it says OPTIONS on Method, and Load cancelled on Status

One workaround is to inline your templates:
<script type="text/ng-template" id="sample.html">
<div>This is my sample template</div>
</script>
Fiddle.
This puts your template into Angular's template cache. When an ng-include directive is processed, Angular checks the cache first.

The problem is because of file:// protocol. Mostly all browsers disallow XHR requests when file is been served from file://. That is why AJAX requests are failing. And ng-include use them to download files.
You can launch Chrome with --allow-file-access-from-files parameter to disable checks for file://.
FireFox should load files if they are in same folder (check this for more info).
But personally prefer to start simplest NodeJS script to serve files, that could be found at angular-seed project. It require just node binary and gives a feeling of a normal web server.

I had a similar problem and I solved it by running a simple nodejs server to serve static files - http-server. Make sure you have nodejs installed, then:
Install:
$ sudo npm install http-server -g
Then from your project directory:
$ http-server
Now you can access your files from:
localhost:8080/whatever-file-you-have-in-your-directory.html

You can use Grunt along with the grunt-html plug to precompile your templates along with your Angular code.

Using virtual Host on your xampp is the best solution , and give custom name like yourProject.dev
update httpd-vhosts file in C:\xampp\apache\conf\extra\httpd-vhosts
<VirtualHost *:80>
ServerName yourProject.dev
ServerAlias www.yourProject.dev yourProject.com www.yourProject.com
DocumentRoot "C:\Users\customFolder/yourProject"
<Directory "C:\Users\customFolder/yourProject">
DirectoryIndex index.html index.php
ServerSignature Off
Options Indexes FollowSymLinks IncludesNoExec
AllowOverride All
Allow from all
Require all granted
</Directory>
</VirtualHost>
then update your hosts file to 127.0.0.1 yourProject.dev
in Windows : c:\Windows\System32\Drivers\etc\hosts
in Mac : /private/etc/hosts
don't forget to rest your xampp/wampp

Related

Using Create-React-App with Apache VirtualHost

I am trying to develop a project for a company. This company has an API that only allows requests from two sources: Their own host, and xyz.localhost.
At first, I developed the project with jQuery only (loaded in via CDN). With an Apache VirtualHost setup, this worked -- I could access the API.
Now, I want to refactor the project and use React with it. I used create-react-app to create a react directory.
The problem is: I can get create-react-app to use xyz.localhost, but I am still getting the CORS error message in Chrome:
Access to fetch at 'http://api.thatcompany.com/search?search=a'
from origin 'http://xyz.localhost:3000' has been blocked by CORS policy
Has anybody any ideas how to make it work?
Thank you in advance.
/private/etc/apache2/httpd.conf:
# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf
Include /private/etc/apache2/vhosts/*.conf
/private/etc/hosts:
127.0.0.1 xyz.localhost
255.255.255.255 broadcasthost
::1 xyz.localhost
/private/etc/apache2/vhosts/xyz.localhost.conf:
<VirtualHost *:80>
DocumentRoot "/Users/MYUSERNAME/dev_projects/MY-REACT-APP/public"
ServerName xyz.localhost
<Directory "/Users/MYUSERNAME/dev_projects/MY-REACT-APP/public">
AllowOverride All
Require all granted
</Directory>
<filesMatch "\.(html|htm|js|css)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>
</VirtualHost>
My create-react-app start script:
"scripts": { "start": "HOST=xyz.localhost react-scripts start" }
I found a workaround.
I simply pointed the VirtualHost to the build folder of Create React App, this worked and served me the static files.
Editing the code was pretty tiresome, as I had to run npm run Build after every update.
Luckily, I found the npm watch module. With that, create react App would rebuild on every save. I still have to wait a second between save and refreshing the browser, and it’s absolutely not comparable to using create react app on its own, but it was a good enough solution!

Running development version of create-react-app in Apache

I've looked around for this everywhere and there is tonnes of documentation on how to DEPLOY apps created with create-react-app to apache which is fairly straight forward, however there's barely anything on how to run your development environment on apache.
I'm working on an app that's going to be deployed on an apache server and so I want to mimic the environment during development but can't figure out how to set it up correctly. Is it even possible?
I have the following conf file set up for my site - but because the index.html file doesn't directly include the JS required for React to do its magic, I just get an empty page with the <div id="root"></div> holder.
Conf:
<VirtualHost *:80>
ServerName warehouse.local.com
DocumentRoot /var/www/warehouse-local/public
ErrorLog /var/log/apache2/warehouse.local.com-error_log
CustomLog /var/log/apache2/warehouse.local.com-access_log common
<Directory /var/www/warehouse.local.com>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Anyone got any advice on how to achieve what I'm after?
emmm,why you want to run dev version in Apache?
I think ,I will need Apache just when I deploy my prduction version of react app.

How does one change nagios default url to custom url?

I am running Nagios Core 4.0.8 in my environment and I am looking to find a way to change the default url from https://example.com/nagios to https://example.com. Is there a way one can do that?
You will need to edit the cgi.cfg file for Nagios.
vim /usr/local/nagios/etc/cgi.cfg
Change url_html_path=/nagios to url_html_path=/
Edit nagios.conf:
Change ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin" to
ScriptAlias /cgi-bin "/usr/local/nagios/sbin"
Comment out the line Alias /nagios "/usr/local/nagios/share" and then add below
DocumentRoot /usr/local/nagios/share
Add the following at top of the configuration:
<VirtualHost *:80>
ServerName status.example.com
and add </VirtualHost> at the bottom.
Edit your /usr/local/nagios/share/config.inc.php file:
Change $cfg['cgi_base_url']='/nagios/cgi-bin'; to $cfg['cgi_base_url']='/cgi-bin';
Restart Apache and Nagios
If you are using Apache to serve your Nagios page you can do different things. Apache configuration files are usually located in /etc/apache2/.
Probably your DocumentRoot is set to /var/www, so you can create a file named "index.html" and place this code inside it to redirect to /nagios URL:
<META HTTP-EQUIV="Refresh" Content="0; URL=/nagios">
You can also edit your nagios apache config (probably /etc/nagios/apache2.conf...) or apache config /etc/apache2/*.conf and add:
RedirectMatch ^/$ /nagios
If you don't like these methods there are more, just think of it as using Apache to redirect, not like a Nagios thing.

Apache localhost authentication

I am trying to setup up basic authentication to a folder on my localhost running apache. Currently the app runs fine without authentication. I have setup a virtual host so I can access my application through dev.myapp.com
The code I add to my .htaccess file to force authentication is:
<Directory "/Users/myusername/Sites/dev.myapp.com">
AuthType Basic
AuthName Test
AuthBasicProvider file
AuthUserFile /etc/apache_users
Require valid-user
</Directory>
I have created a user.
When I type dev.myapp.com into the browser I get an internal server error. I am fairly new to apache. A point in the right direction would be appreciated.
Thanks.
First off, using .htaccess is a bit slower and requires that you have set the AllowOverride directive accordingly. It is recommended that you instead use httpd.conf to establish basic authentication. The Apache documentation explains all of this so check out this link http://httpd.apache.org/docs/2.2/howto/auth.html.
Regardless, I think I see the error. Your <Directory> tag looks a little odd. I understand the name of your site is dev.my.app.com but is that the actual name of the folder where the site dev.myapp.com points to on your server? Your httpd.conf file should have an entry like this:
<VirtualHost *>
DocumentRoot "document/root/path"
Other directives here
</VirtualHost>
The DocumentRoot is where Apache directs all incoming web traffic. If you are trying to establish authentication for your entire site, the value of DocumentRoot is most likely what you would want in your Directory tag ... making it <Directory /document/root/path>.
To locate httpd.conf look in in /etc/apache2/. Make sure to restart your server after you change the file (sudo /etc/init.d/apache2 restart). Hope that helps, please update if you haven't already resolved the problem.

How to install cakephp on localhost?

I wanna to know how to install cakephp on localhost?
Please explain me.
I usually make an apache and mysql installation on a linuxbox. I can use windows too, however I do not recommend it ;)
So, I usually make a new entry into the /etc/hosts file to make a sitename available to cakephp.
127.0.0.1 localhost caketest.local
next step to copy all cakephp files into a subdirectory inside /home/myusername/public_html/caketest
app
cake
index.php
plugins
README
vendors
.htaccess
then I set up the site to apache (not neccessary),
<VirtualHost *:80>
DocumentRoot "/home/myusername/public_html/caketest"
ServerName caketest.local
# This should be omitted in the production environment
SetEnv APPLICATION_ENV development
<Directory "/home/myusername/public_html/caketest">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
restart apache. you also need to edit the .htaccess files and place a RewriteBase directive with hte path to the actual directory, e.g.
RewriteBase /~myusername/caketest
create a database, set the db connection in cake config files and that's all.
you can point your browser to http://caketest.local
if you do not want a test site url you can skip hosts, and apache vhost creation, but the url to use should be http:/localhost/~myusername/caketest
another important thing is to enable userdir modul in apache, and also check if using php is enabled in userdirs too.
If you're on windows, get WAMP. Install it, then download CakePHP in C:\wamp\www\. Extract CakePHP in that folder so you have a folder kind of like this: C:\wamp\www\cakephp\. Now you can access the installation by going to localhost/cakephp/.
An old article of mine but still quite relevant:
Installing CakePHP
Assuming you have a *AMP setup (Apache+MySQL+PHP), just copy the files to your htdocs folder. On Ubuntu, it's /var/www, so you would create /var/www/myApp and copy the whole Cake structure into there, ending up with something like:
/var/www/myApp/app/
/var/www/myApp/cake/
/var/www/myApp/vendors/
/var/www/myApp/index.php
/var/www/myApp/.htaccess
Then you can access your app by the url: http://localhost/myApp

Resources