Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am running apache2 as a web server. I have a local file, example.php, which I need to see as rendered by apache. The easiest way to do this would be to move or link the file to the folder apache exposes as part of my site, and then I could get the page with curl or wget.

However, this is not possible in my situation - I cannot write to the directory apache is looking in, which means that I can't access it by the usual port 80 method. Is there a way I can call apache directly, giving it a php file to generate the result of?

I'm looking for a command like $ apache ./example.php > output.html, is this possible?

I'm asking about apache, because although I only really need it to run the php, the php-cli is not usable in this cases - apache php and cli php have access to different areas of memory, and I need the results to match the ones returned through apache.

share|improve this question
Sorry can you explain again why wget or curl aren't options? –  Oli Apr 18 '13 at 16:07
@Oli Because the file I want rendered is not in the folders that apache looks in for webpages. e.g. if http://localhost/ shows the contents of /var/www, but my file is in /home/name/stuff, there's no way to get to it. Unless there is, in which case tell me please! –  Benubird Apr 18 '13 at 16:09

1 Answer 1

Can you just add it to Apache as a new virtualhost?

The simplest way to get something to behave like a website is to treat it like a website. If you could just host this as a local website (eg only on the interface) and wget it, that would probably be the cheapest and cleanest way to deal with this.

Run it as a script with the php command

You can run php example.php > output.html and that should work for most simple scripts.

Note that this will use the php-cli configuration rather than mod_php, php-fpm, php-cgi, etc configurations. That might be important if you have a funny setup.

Edit: I saw this question yesterday that might be of use. It's aimed at wordpress but the same logic should apply for most things where you need to fake a real request. You set up a load of PHP's $_SERVER variables so your script (whatever that might be) can execute correctly… And then include your script.

Use the PHP built-in server (requires PHP 5.4+)

If you cd into your directory and run php -S localhost:9000 you start a local server on port 9000 that will host your website. This is only for testing things like this and should never be exposed to the internet.

This allows you to do a traditional wget lookup:

wget -O output.html http://localhost:9000/example.php

From my previous (now deleted) answer, if you find yourself needing to spoof the domain name, wget will let you pass the host in. This comes in handy if the PHP is doing lookups on server variables.

wget -O output.html --header "Host: example.com" http://localhost:9000/example.php
share|improve this answer
First example doesn't work for me (why I'm asking the question), and the other one looks good except that php -S won't run - man php doesn't show a -S flag either, are you sure that command is right? –  Benubird Apr 18 '13 at 16:30
Certain, but it's quite a recent feature. I'm running Quantal and so my PHP version is 5.4.6. Precise uses 5.3.10 which I think might be too old. Older versions of Ubuntu will have even older versions of PHP. Confirmed: Built-in server landed in 5.4.0. If you have root on this machine, you can upgrade via PPA. –  Oli Apr 18 '13 at 16:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.