Same-origin policy bypass in local enviroment - php

I'm developing a SPA that runs on Backbone.js locally and setting server up with Grunt for livereload . I did a REST api with PHP for my app which i also run locally. Now i have a problem with cross domain policy since my servers are on different ports. I tried to combine two servers on one port both from apache and from grunt but i'm not sure if it is at all possible. How should i deal with this problem? i would like to develop my app locally and use the livereload features of grunt.

I propose installing nginx to act as reverse proxy. It can serve static files from one directory (aka frontend), and server side generated scripts (aka backend) from other server.
It serves backend, if the request do not corresponds to the file existing in frontend directory.
This is config example for it - https://github.com/vodolaz095/hunt/blob/master/examples/serverConfigsExamples/nginx.conf
It serves static html, css, js files from directory /home/nap/static and backend from the localhost:3000, and both of them are accessible on localhost:80 as the one server.
I hope this is what you need.

So i ended up using grunt-connect-proxy which did just what i needed.

Related

SOS! How do I deploy multiple Laravel apps on a windows IIS server in a LAN

I am setting up an IIS server in readiness to host multiple PHP web apps. These will be used within our local network so no need internet access. I have read a lot of stuff on how to go about it but am getting lost even more. someone please help.
You can refer to this document for detailed steps about deploy php application on IIS.
Here are some main important points:
Install IIS in control pannel(windows) or server manager(windows server).
Configure php. Youcan download it from php official website or Web PI(more recommand). The version over php8 may not work on IIS.
Make the physical path of site points to root folder of application. It will auto generate web.config. Set the permission of root folder to avoid 500 error.
If you have multiples version of php, you can isntall php manager to configure them.

Statically serve internal (apache server) NodeJS api open on a custom port and forward it to the public url

Introduction
I have a university-managed server, that does not give students access to open ports for internet traffic, but students could still open ports (like 4040 for their NodeJS applications for internal access).
Each student's account has a public_html folder (similar to /var/www/), which serves all files in public_html on this URL (using an Apache server) statically (or rendered by PHP):
http://mi-linux.wlv.ac.uk/~studentid/
Problem
However, in my case, I want to expose an API for external testing using postman and adding it to a React application. The problem is, it uses NodeJS and express, and creates its own server on port 4040.
I could access the API by using the curl command from ssh internally like:
studentid#csl-student:~$ curl http://localhost:4040
{"message": "Hey! The backend is working. Explore routes from the code"}
Now, since students cannot allow open posts for HTTP traffic, one could simply not access my NodeJS API from outside the server (which I have to).
Things I looked up
I searched extensively on topics like how to statically serve a nodejs application, to how to forward ports from 8080 (https port) to 4040 (my NodeJS port), but most of them require sudo access, and some of them simply don't work.
Solution I propose
I think I still could access my public_html folder, that is statically served, and could render PHP. I know that I could create a index.html (or index.php) file that would fetch http://localhost:4040 internally, and simply forward the result, since the API port is open internally, and the index.html file could be served externally.
What the file could do
While loading the file using
http://mi-linux.wlv.ac.uk/~studentid/index.html
The file could load the response from localhost:4040 internally on the server itself (since the API is accessible internally), then send the result along with the status codes and headers.
However, my API has several routes, and I could not manage to hard code each route. There must be a more efficient way of doing this.
What I'm looking for
I would be really thankful if one could direct me to a package already made for the purpose of forwarding static files with responses pre-loaded from an internal API.
Or I could have a php script that could do all the forwarding that I need and make the API public.
PS: I know this should be asked on Server Fault, but since I think this could be done by using a script or something, I asked it here.
A workaround for this would be to host your NodeJs code on a third part platform, say Heroku.
And create a PHP script that acts as a middleware proxy and does curl requests to your Heroku-Hosted-NodeJs API endpoint.
Are the Apache's plugins mod_rewrite and mod_proxy enabled on the server?
If so, maybe you could use ProxyReverse with mod_rewrite's P flag inside a .htaccess file as described in this post:
RewriteEngine On
RewriteRule ^/node/(.*)$
http://localhost:4040/$1 [L,P]
ProxyPassReverse /node http://localhost:4040
(I can't test it right now, so my answer is purely speculative, sorry.)
Another option, in case that the server conf cannot be modified, could be use mod_headers and Location, but I'm not sure if it will be transparent:
Header edit Location ^http://internal\.example\.com/(.*) http://example.com/$1

Single PHP page hosting

I am developing APIs in node js and have hosted the application on amazon EC2. In a third party API it is required to host a php page on our server and give its public url back to them. Is there another way to host it apart from a LAMP setup? Anything apart from EC2 is also fine but please take into consideration that this is a single page having some logic and nothing else will be there in PHP.
I work with node and php. I would recommend NGINX. It's configuration files are really simple and it's much lighter-weight than apache.
You can simply create a redirect/rewrite directive in NGINX that will pass your php page to the node server instead and be done. For this usecase, you wouldn't actually even need to install or configure the PHP backend.
http://nginx.org/en/docs/beginners_guide.html#proxy
You might also check AWS domain management tools (AWS Route 53). There may be a way to directly rewrite the incoming PHP request to go to your node app instead without installing any webserver on your EC2.
Yes, there are other web-servers available apart from apache: For instance Nginx plus or Lighttpd, both are fine and lightweight alternatives to host your PHP files on EC2.
Though, it's not clear to me why don't you like the LAMP setup. Maybe Apache+PHP, without MySQL, would be enough for you?

Run PHP code on local MacOS Jekyll site, port 4000

I've build a Jekyll website on my localhost (MacOS Mavericks). The website is served at http://localhost:4000/website/ and everything regarding Jekyll is running just fine.
However, I now want to have a contact form in PHP that allows me to receive emails. I placed a contact.php file in the website/ folder and have the form POST to that file. On my remote web server, this is working perfectly. However, on the localhost, the PHP isn't parsed, and plain text is displayed on contact.php. However, PHP is parsed perfectly on localhost/contact.php.
How do I get my localhost (Apache? PHP?) to process PHP files on my local Mac http://localhost:4000/ (without breaking my Jekyll website that listens on the same :4000 port)?
You can't use the same port. The port determines the application endpoint that will handle the request on the IP address. The Jekyll server (WEBrick library) uses port 4000 as a default.
The typical way to handle this problem, is to use a "web service" to add dynamic functionality. For instance, the jekyll docs suggest using something like FormKeep, or SimpleForm.
What you're asking is to setup a "web service" yourself. To do this it would need to be on another port or another IP address. The "service" will simply act as an endpoint to accept and process your form post. In this case you could setup a webserver using Apache/PHP on a different port than Jekyll -- such as the standard port 80 -- then write a PHP script (e.g., webform.php) that in combination with the static form is setup to respond and process your form.
Note: It is possible to configure both Jekyll and Apache to respond to requests on port 4000. However, both applications (aka servers) can't be running at the same time. The ip:port combination determines which application an internet request is sent to.
I realize the post is old but this may help someone...
The answer by Mike Stewart is excellent and describes what needs to be done to accomplish the goal.
To add to that answer, here are the specifics of how I do this type of development on a Mac.
Configure CORS in Apache
Run the Jekyll site on default port 4000
Run MAMP stack on default port 8888
Code goes in MAMP's htdocs folder (htdocs/your_project)
PHP resides in a separete "php" or other folder inside the "your_project" folder
Jekyll watches the "your_project" folder and compiles to _site as normal
The CORS issues you'll experience can be resolved locally during development several ways. Here is a good resource for enabling CORS on Apache: http://enable-cors.org/server_apache.html
Once you have CORS configured you'll be able to make Ajax calls to the PHP on port 8888.
I'm running the PHP built-in web server alongside the Jekyll server. I opened a second Terminal window and navigated to the _site folder. The command is php -S localhost:8000 (or whatever port you want to use that is not 4000).
Note that I'm using viewing localhost:8000 in the browser, but having the Jekyll server running simultaneously is nice because Jekyll keeps the build updated as I make changes to the source code (refresh required).

GWT frontend (hosted mode) & PHP backend (apache) simultaneously on localhost?

Being a GWT newbie, I want to create GWT frontend and a PHP backend, communicating via JSON.
The GWT Getting started docs (http://code.google.com/intl/sv-SE/webtoolkit/doc/latest/tutorial/JSONphp.html) suggests
compiling the GWT frontend, and
moving it to an Apache/IIS server
Is there a way to avoid this roundtrip?
Could Jetty and a localhost Apache be set up to run simultaneously so that GWT frontend development (hosted mode) could be done in parallell with PHP backend dev?
Alternatively, could GWT Host mode be setup to use localhost Apache/Tomcat instead of Jetty?
Yes it's possible. You have to do the following:
compile once the gwt project and copy it to the php server (you can compile directly to the php server -war option
run dev mode with the -noserver option (this way you won't use the embedded jetty server)
make sure your php project loads the appropriate gwt host file
you should have the apache/IIS server running while in dev mode.
set the appropriate url in the gwt run configuration (if you use eclipse) to load the page with the hosted file on your apache server
Browsers pages (javascript) are normally only allowed to communicate to their origin servers. There are ways around it, but require changing your html pages, which makes no sense since you only need this for developments.
A better solution would be to just copy necessary files to your PHP project directory after every GWT compile. Ant can do this and your IDE maybe too.

Categories