WebFaction
Community site: login faq

Hi

I am looking for a way to do this

User visits a URL Request is sent to Webfaction application which is just a shell script (or something very lightweight) If file exists, serve it from server if not, run php script and generate image

Essentially I'm trying to only introduce PHP in needed situations

What would be the best way around this?

asked 02 May '14, 02:19

mcmaxx
11147
accept rate: 0%


Here you go :)

#!/bin/bash

# assumptions:
# - a single query string parameter for the image name
# - images are stored in a subdirectory named 'images'
# - you're using php 5.5
# - your PHP script is named create_image.php

# this gets the image name
filearg="`echo $QUERY_STRING | cut -d= -f2`"

# this gets the extension and sets the correct content type
extension="${filearg##*.}"
echo "Content-Type: image/$extension"
echo ""

# make the path to the image
image="images/$filearg"

# check if the image exists on that path and create it if it
# does not exist
if [ ! -e "$image" ]; then
    /usr/local/bin/php55 create_image.php $image
fi

# serve the image
cat $image
permanent link

answered 02 May '14, 22:15

seanf
12.2k42136
accept rate: 37%

Hi

Thanks for the reply!

To have this script run by a URL I've done the following :

Created a custom app and website for it Created a script (script.sh) and placed it in the folder

But after reading http://docs.webfaction.com/xmlrpc-api/tutorial.html some steps seem a bit different, is there another tutorial for having the script run when accessed via url?

(08 May '14, 01:09) mcmaxx

A 'custom app listening on port' application is not what you need here.

Instead, create a Static/CGI/PHP5.5 app, then put the script in the app directory. Name the script "script.cgi" instead of "script.sh", and set the permissions on the sript to 711.

Then, add the app to a website via our control panel

I'm not sure why you're looking at our API documentation for this. The docs for setting up websites are available at:

Hope that helps!

(08 May '14, 20:32) seanf

Hi

I created the app, file and set the permissions, but accessing the url

[myapp].[mysite].webfactional.com simply lists all the files in the directory rather than running the .cgi

Also, shouldn't the .cgi be .sh if you're running /bin/bash?

Thanks for your patience!

(10 May '14, 20:33) mcmaxx

Are you certain that the application type you chose supports running those files as cgi?

You should choose a Static/CGI/PHP application for this

http://docs.webfaction.com/software/static.html?highlight=cgi#static-cgi-php

If you need to change how a file is handled then the following information is pertinent.

http://docs.webfaction.com/software/static.html?highlight=cgi#changing-the-handler-for-files

(10 May '14, 20:51) aaront ♦♦

Hi Aaron

Thanks for the input, you let me on the right track

I've followed the instructions here: http://kb.siteground.com/how_to_run_simple_shell_scripts_directly_from_a_browser/

And in combination with what was provided above I believe the script works well

Thanks :)

(10 May '14, 20:58) mcmaxx
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×262
×16
×12

question asked: 02 May '14, 02:19

question was seen: 3,031 times

last updated: 10 May '14, 21:23

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2020 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM