Run PHP code on local MacOS Jekyll site, port 4000 - php

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).

Related

connect webpack devserver (HMR) with PHP project running with virtual host

Currently, I developing a WordPress plugin with React frontend.
I tried to connect my backend (PHP) running on a virtual host with webpack devserver with hot module replacement (HMR).
Is there any config how to do it.
note: I tried with devserver proxy but cant figure it out.
Thanks in advance.
Getting HMR is always complicated for custom setup like these. Theoretically, this is pretty simple. You simply need to run the webpack-dev-server with the HMR module. On your PHP file, make sure it uses the JS file generated from webpack-dev-server. The code to refresh is bundled in the JS file so it should work.
However, the devil is in the details. You'll need to make sure the communication between your page and webpack-dev-server works without issue. Monitor your websocket network request and make sure they communicate as expected.
An issue you'll probably encounter is security since the websocket connection is expecting a different host. In this case, you can use config from the following answer: I am getting an "Invalid Host header" message when connecting to webpack-dev-server remotely
Another issue could be that the JS file tries to connect to the wrong location. Eg: It thinks that your host is where the websocket connection is. If this is the case, you can use the public setting. See here for documentation: https://webpack.js.org/configuration/dev-server/#devserverpublic
To me this is akin to running with a reverse proxy. You can see this discussion to understand the problem: https://github.com/webpack/webpack-dev-server/issues/804
There might be other problems. You just need to check the websocket connection and make sure they communicate without problems
How you refer to the generated js file is a separate problem. A simple example would be setting the port to 8081 and bundling the js file into bundle.js. Then in your php code, refer to it as http://localhost:8081/bundle.js

Can I have a subdomain of php application on asp.net application

I have an asp.net web application hosted and I would like to add a sub-domain (or sub-directory) and run/host a php application in it.
How would i do this?
A subdomain would be nicer from a technical standpoint:
You could make your DNS route the subdomain.yourdomain.com to a different Server then yourdomain.com
You could add different IIS bindings for subdomain.yourdomain.com and yourdomain.com in IIS.
For a sub-directory you'd have to run php in IIS as well, here is a guide in how to set it up: You can also run an php website in iis.
You could also run php on Apache and dotnet in IIS. The only downside: If you try to run both apps on 1 server, only 1 (IIS or apache) Can listen for http traffic on port 80 (the default port). so you'd have to host 1 on a different port (eg for port 81: yourdomain.com:81) which i wouldn't advice for a production website.
You could make a tiny application which receives every request and forwards the http request to the right application on a different port. This is called a reverse proxy.

Cannot Get Laravel Welcome Page to Show

My server is on DigitalOcean cloud. I am using Ubuntu with Apache web server. My home computer is running Windows 7. I am using putty for terminal.
Followed all of the directions on https://laracasts.com/series/laravel-5-fundamentals/episodes/1. Made it up to 8:40, where it directs you to run php -S localhost:8888 -t public. I run that, then open a web browser and tried the following:
-http://mywebsite.com:8888
-http://www.mywebsite.com:8888
-http://mywebsite.com/learning-laravel-5/public
-http://mywebsite.com/learning-laravel-5/public/index.php
None of the above work.
In Google Chrome, the first two options where I list the port number, I get a page saying This webpage is not available. ERR_CONNECTION_REFUSED. In IE, I just get a page with big font saying "This page can't be displayed."
For the last two options, I just get a completely blank page. In the console, I get this error: Failed to load resource: the server responded with a status of 500 (Internal Server Error).
I'm trying to pick up a web app framework to broaden my php skill set. Can someone help me out? What am I doing wrong/what is the video tutorial missing that I have to do in order to get Laravel up and running?
php -S localhost:8888 -t public is meant for running a site locally, which is what the video is showing.
If you are using a Digital Ocean droplet with Ubuntu and Apache, you will need to configure Apache to use /public as the document root and have Laravel installed in the /var/www directory.
From there you can visit the droplet's IP address (http://XXX.XXX.XXX.XXX) instead of the domain name (unless you have configured the DNS for that domain name). You won't need the port in the URL either, since Apache will be serving it on the default port 80.
This probably is not the answer you want, but here's my advice based on setting up a few VPSs on Digital Ocean. Step back. Spin a new VPS. Keep your old one around, if you want, but start afresh.
Create a new droplet
Setup your SSH and PuTTY and make sure that works
Setup your FTP (if you're using it)
Setup your DNS
Setup your Apache config files. DO has a very good tutorial on this: https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-14-04
Now, work on getting your "Hello world" html page to show when you access your domain www.yourdomain.com, yourdomian.com. Don't fixate on ports at this point, just get a minimal server running. This might help too: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts
Make an saved "image" of this basic working setup so you can spin a new VPS if you need to.
Now that you have a server that loads your domain index page, you can start to install your Laravel.
In a nutshell, I'd advise you to establish a stable working server platform before you try to install and launch a more complex technology like Laravel, or Rails, etc.
Tutorials often make complex technologies seem easy, the 10-minute expert, but there is tremendous complexity masked under the hood of these frameworks. Start simple and build on a server one piece at a time. You have to walk before you run.
I use Laravel often, but my experience with servers is more relevant here. Everything you've said indicates an access problem, and knowing how hosting companies work, they probably have that port blocked (along with all other non-standard ports).
You can test this using PuTTY, open it up and enter the host name of your server. Change the port to 80 and change close on window exit to never, then connect. Enter:
GET / HTTP/1.1
Host: yourwebsite.com
Then press enter twice and the server will process the result. It should show you the HTML of your home page.
Now try it again with port 8888 and see if you can even connect. If you can connect then it's not a port issue, but my guess is you'll get a fatal error Network error: Connection refused, which means the port is closed or blocked via firewall.
Even though DigitalOcean give you complete control over the server, the connection probably still runs through their firewall. It's possible that you have your own firewall, but otherwise if the server runs through their firewall and the port is blocked nothing you can do on the server will open that port.
did you try chmod -R 0777 storage ?

Load a page server-side and deliver to web browser

I'm working on a raspberry pi project where i've set up a media centre with an apache web-interface over my LAN. I've recently installed the deluge-web browser GUI for managing torrents which runs over a different port.
I'm forwarding port 80 so that I can access the media center interface when i'm away from home for management, but would like to get it set up so i can access the deluge-web ui without having to forward its port, so that i can implement a stricter security control using mysql or php with apache.
So what i'm trying to do is set up a frame which is loading the deluge-web ui on the server-side, visible and accessible by the browser.
I'm running apache 2.2.22, php 5.4.39, raspian (debian 7.8).
Is that it all possible?
No. A frame is a client side technology. It can't do anything about permissions or creating network paths to internal servers.
If you want to make a service accessible with added authentication, then you'll need to look at using something like an SSH tunnel or HTTP proxy instead.
If I understand right, what youre looking for is curl. Check Curl Documentation on PHP Site. Basically, it can download any url (page/file/whatever) and then you can use it in your php script, possibly dumping it into an iframe or div.

Same-origin policy bypass in local enviroment

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.

Categories