Can't access my PHP files trough Ajax on the App engine - php

I have deployed a PHP app on the App engine but all my Ajax calls to PHP files seems to be redirected to my index.php file. Sending me the whole index.php's generated HTML as response.
Accessing the file directly also load the index.php (https://[mybucket].appspot.com/includes/engine/checkFollowStateFirestore.php)
I believe it may come from my App.yaml but i can't find a working answer on internet.
Here is my App.yaml file:
runtime: php72
default_expiration: "1s"
handlers:
- url: /css
static_dir: css
- url: /vendor/twbs/bootstrap/dist/css
static_dir: vendor/twbs/bootstrap/dist/css
- url: /js
static_dir: js
- url: /vendor/twbs/bootstrap/dist/js
static_dir: vendor/twbs/bootstrap/dist/js
- url: /assets/images
static_dir: assets/images
I have tried adding:
- url: .*
script: auto
or
- url: /includes/engine/*
script: auto
or
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
Options such as:
- url: /includes/engine/checkFollowStateFirestore.php
script: includes/engine/checkFollowStateFirestore.php
Gives me this error on deployment:
ERROR: (gcloud.app.deploy) INVALID_ARGUMENT: script field for handler '/includes/engine/checkFollowStateFirestore.php' must be set to 'auto' for runtime php72
Using PHP include/require works.
Thanks a lot.

Related

How should I write my app.yaml for my php app deployed to gcp's app engine?

I'm deploying a php app to GCP's app engine and can't make the app.yaml configuration work.
The app has this directory structure:
index.php
/sistema
/js
/images
/css
index.php
login.php
otherpage.php
...
I'm having problems with the pages that are under the 'sistemas' folder. And also would like to access those pages without the '.php' in the url.
I've tried with many handlers, but none of them worked as I wanted.
runtime: php55
threadsafe: yes
api_version: 1
handlers:
- url: /sistema/css/*
static_dir: sistema/css
- url: /sistema/js/*
static_dir: sistema/js
- url: /sistema/fonts/*
static_dir: sistema/fonts
- url: /sistema/images/*
static_dir: sistema/images
# Serve php scripts.
- url: /(.+\.php)$
script: \1
- url: /
script: index.php

configuring app.yaml for Google AppEngine to serve ajax requests for PHP

I've a simple folder structure,
main
|__core
|__start.php
|__bootstrap.php
js
|__file.js
css
|__file.css
index.php
Now, this file.js makes xhr request to start.php for which I'm getting a 500 error.
This is my app.yaml:
runtime: php55
api_version: 1
handlers:
- url: /
script: index.php
- url: /js
static_dir: js
- url: /css
static_dir: css
Now I'm not sure if I'm also suppose to do this:
- url: /main/core/.*
script: start.php
Which, by the way, doesn't seem entirely legit to me.

Removing PHP file extension when serving PHP files

I'm trying to remove the file extension in the address from all *.php files in my site. For instance, if a user visits mysite.com/about.php I want the URL to read mysite.com/about.
Here's my app.yaml:
application: mysite
version: 1
runtime: php55
api_version: 1
handlers:
# Static files
- url: /images/*
static_dir: images
- url: /css/*
static_dir: css
- url: /js/*
static_dir: js
# Routing
- url: /services(.*)
script: services.php
- url: /portfolio(.*)
script: portfolio.php
- url: /project(.*)
script: project.php
- url: /about(.*)
script: about.php
- url: /contact(.*)
script: contact.php
- url: /(.*)
script: index.php
How could I achieve this within GAE?
If all you need to do is perform that type of routing, you could use this:
application: mysite
version: 1
runtime: php55
api_version: 1
handlers:
# Static files
- url: /images/*
static_dir: images
- url: /css/*
static_dir: css
- url: /js/*
static_dir: js
# Special case, route requests for the root document
- url: /
script: /index.php
# If the request ends in [something].php, route to that file directly
- url: /(.+\.php)$
script: /\1
# Map all other requests to scripts of the same name
# so that, eg, /thispage (or /thispage/) routes to /thispage.php
- url: /(.*?)/?$
script: /\1.php
NB however that using this method you won't be able to handle 404 errors (although they don't appear to be handled in the app.yaml provided in the question either). Error-handlers in your app.yaml won't work for a 404 because they'll only kick in if they can't match any route you've provided.
So, if you want to handle 404 errors and such, what you should do instead is do the routing from inside a php script, like this:
-url: /(.*)
script: /routes.php
and inside routes.php inspect the $_SERVER['REQUEST_URI'] variable to see what page was requested, and serve the appropriate content accordingly.
edit: thanks to #Avinash Raj for cleaning up the regex Here

Google App Engine .yaml file working locally but not when deployed

My GAE PHP .yaml file works locally but not when deployed. Everything worked fine up until I reorganized the folders my .php scripts were contained in. There is a php folder in the root of the app and the .php further organized into folders.
Current YAML file:
application: raven3mil
version: 1
runtime: php
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /js
static_dir: js
- url: /css
static_dir: css
- url: /images
static_dir: /static/images
# Serve php scripts.
- url: /(.+\.php)$
script: \1
- url: /.*
script: /login.php
error_handlers:
- file: /errors/404.html
File structure example:
+root
+php
+admin
manageUsers.php
+teacher
lessons.php
+student
schedule.php
dashboard.php
+css
index.css
main.css
+js
+jquery
+bootstrap
login.php
I'm not sure why my YAML file works locally but not deployed. Can anyone give any insight?
If your scripts are using content from static_dir's you need to make sure they are available to the application.
You can achieve that by adding application_readable: true to your js, css and images handlers.
You can read more about it here: https://cloud.google.com/appengine/docs/php/config/appconfig

How to get my application to work on appengine

I developed a web application the usual way and hosted on a normal shared server.
These are what I mean by usual
Include my PHP function files (using php include/require)
Use my normal MySQL database with msqli
Upload my files normally to the server using FTP.
For some reasons, I need to move the web app to appengine, I have succeeded in uploading the files but it shows me this error:
500 Server Error
with this error in my log:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 239, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 298, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 84, in LoadObject
obj = __import__(path[0])
ImportError: No module named index
What changes do I need to make to my files to enable my website work well on appengine without issues?
EDIT (Content of app.yaml)
application: gcdc2013-myworkset
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: index.php
- url: /css
static_dir: stylesheets
- url: /images
static_dir: images
libraries:
- name: webapp2
version: "2.5.2"
I tried changing runtime to php but it gave an error which cleared immediately i changed it back to python27
EDIT:
Updated app.yaml file (Project deploys now but shows this error for all PHP files: Could not guess mimetype for excel/excel_reader.php. Using application/octet-stream.
Also the project appears as blank when viewed on the browser):
application: gcdc2013-myworkset
version: 1
runtime: php
api_version: 1
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: index.php
- url: /
script: index.php
- url: /index\.php
script: index.php
- url: /features
script: features/index.php
- url: /about
script: about/index.php
- url: /oauth2callback/?
script: signup.php
- url: (.*)\.[\d]{10}\.(css|js)
static_files: $1.$2
upload: (.*).(.*)
- url: /css
static_dir: css
- url: /js
static_dir: js
Also, how can I import mysql database that I exported from my localhost to the appengine?
Change:
runtime: python27
To:
runtime: php
And remove:
libraries:
- name: webapp2
version: "2.5.2"
See https://developers.google.com/appengine/docs/php/config/appconfig for more information.

Categories