iPod Touch - Apache and PHP 5.2.3
12 10 2007When you’ve got Installer.app installed on your iPod Touch you’ll notice that it allows you to install Apache and a number of scripting languages (PHP, Perl, Python, Ruby).
That’s all well and good but getting it to run in on that local Apache install is another thing. Just managed to get it to work with PHP 5.2.3 so just follow along if you’re in the same situation:
1. Edit /etc/profile and update the line setting PATH to read as follows:
PATH="/bin:/sbin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/iphone/bin"
2. Edit /etc/httpd/httpd.conf and add the following to the end of the file:
ScriptAlias /php /opt/iphone/bin
AddType application/x-httpd-php .php
Action application/x-httpd-php "/php/php-cgi"2. Restart Apache by SSH’ing to the iPod Touch and running the following command:
apachectl restart
Now put your PHP files into the webroot (/Library/WebServer/Documents/) and access them by pointing Safari to http://127.0.0.1/<path_to_your_file>.php
I’m yet to try it with Perl and Ruby but that should just be a matter of adding another ScriptAlias to httpd.conf of your local Apache install. Enjoy!






Hi Peter,
I found your blog as I was searhing info to start PHP on iTouch. I did the things you said, but I couldn’t restart Apache with your command. Is it same if I reboot the iTouch?
I can’t get PHP working…
Wicked! Exactly what I’ve been looking for! The iPod Touch and the iPhone must be the world’s sexiest portable web servers!
Thnx a lot! PHP on my iPhone is great. Now I’m working on an Liveblog for my Fon.
Hey thanks for this it really helped, I just installed Apache and PHP on my Ipod touch and it works great. I couldn’t get apache to restart so I did I reboot on the Ipod and that fixed the problem, later on going through the bins I noticed the last line “apachectr restart” should read “apachectl restart”
Thanks again!
Thnx again! I use your introduction to build up my always-on-moblog under http://docpool.org/iphone. There is an addition to your setup: with the reverse ssh-tunnel you can hold your iPhone-server accessible even if you switch your network. It works fine for me in all networks Wifi or EDGE.
[...] euch hier eine Hand voll Tipps zum Aufsetzen eines eigenen iPhone-Webservers. Gerade die verlinkten Tipps zu PHP und dem, auch auf docpool eingesetzten, live-moblog sind gold- und natürlich auch [...]
Peter - thanks for the article, worked flawlessly on my touch.
Do you know how to get Perl working as well?
Thanks,
Ryan
Now if only someone could make a script linking to the ipods photo, and music library, i would make i extremely easy to share, i realize that the music part would be illegal to share, but all my purchased music wouldn’t play without my apple_id anyway so an easy, cool way to use do this would be awesome…
Great Info!
Thanks
I am looking to configure ruby on ipHone Web Server as well please help.
thanks
Great Info!
Thanks~!!!
@jj: You only need an symbolic link from ~/Media… to /Library/WebServer/Documents/etc. Thats all.
except itunes won’t let me sync full size images to the iPhone, instead it uses
ithmb format.
see:
http://forums.ipodlounge.com/showthread.php?p=650968
and iTunes is also not so simple to link as iTunes also saves iTunes Music Library data file (in XML format)….
so a great deal of coding is needed…
Is there any way to install MySQL in the iPod Touch or iPhone? I been searching some kind of clue but i found nothing. Perhaps someone knows how or can think how to install it. Thanks in advance.
Hello. I’m having a problem when i’m not connected to the wireless network and only conected with edge. Perhaps is because i use the next file to get internet with edge:
function FindProxyForURL(url, host)
{
if (isInNet(myIpAddress(), “10.0.0.0″, “255.0.0.0″))
return “PROXY 200.40.246.2:3128″;
else
return “DIRECT”;
}
Please, can anybody help me with this? Thanks!
@Leonardo: You mean, you like to use an automatical proxy configuration (PAC)? What is the problem exacly? Do you have access to a web page if you connected to edge or not?
Thanks for your reply. Yes, that’s my proxy.pac file for browsing internet through the wap service. The thing is that when i’m in the edge network i get the 505 error when i want to access localhost and i think the problem could be because my proxy.pac config.
@Leonardo: Ah! I’m not familiar with the PAC configuration, but I think you make a mistake in the if statement. This script says: if my adress is in local net, then use the proxy, otherwise request the page direct. What you like to have is: if the requested address in the local net, then go there direct, otherwise use the proxy 200.40.246.2:3128.
So, this probably will helps you:
function FindProxyForURL(url, host)
{
if (isInNet(myIpAddress(), “10.0.0.0″, “255.0.0.0″))
return “DIRECT”;
else
return “PROXY 200.40.246.2:3128″;
}
Thanks again for your reply. I don’t know much what my config file means, but the thing is that it already works as i posted earlier. What i thought was that apache isn’t working because of the proxy config.
@Leonordo: O.k. so you dont need more help? If you use an outside proxy, it is impossible to reach the web server local (on your iPod Touch or iPhone) running.
That’s what i wanted to know, it’s impossible then. I thought it could be some extra coding in the config file so i could get to the local web server when i’m in the edge network; my mistake. Thanks for all.
There is rather huge problem with PHP on iPhone/iPod touch - bug in preg_replace function, which always returns empty string…
Hello
I’ve got Apache and PHP working now, but where is php.ini located on the ipod Touch?
There is no php.ini. But you can create one for yourself. In most cases it is more easy to set up the thinks you need in an included php file.
Do you use an reverse tunnel or dyndns, so that we can touch your touch?
hmmm ok … so like … you got one php.ini for me to start with hahaha?
“Do you use an reverse tunnel or dyndns” eh?
Hey dude!
php.net says:
[ When php run as CGI ]
Place your php.ini file in the dir of your cgi’d php binary, in this case /cgi-bin/
hmm … well it turned out I had to place it in
/opt/iphone/lib/
I’ve written a little script to upload files to my iPod Touch and also download it from my iPod and of course delete them. This way I don’t have to open SSH
… Well thnx for helping me ..
greets
Fransjo, perhaps you should do some kind of tutorial to have the script you mention, it could really be useful :-).
Cheers.
I cant put my php files in the Documents folder! They all gets empty with filesize 0 when I upload them to the Touch, any ideas?
Hello Illsley
I’ve spent the last 2 hours trying to overcome this very problem with my iPod Touch. The error occurred whilst using SFTP - I used a few different clients, each with the same error.
I lacked information about the error, since the clients couldn’t report the exact error (sigh)
However, I have since discovered, the problem with uploading files is lack of available disk space on the Touch. I had to uninstall some packages to create room. What I’m not clear about is whether there is indeed a finite amount of space in the File System - perhaps this doesn’t extend to the 16GB? I see two partitions on the device, one 300MB and is full, and the other 15GB.
Does anyone know anything more about available disk space?
Jonathan…
There are two partitions defined…
the 300 mb partition is where the OS lives…
the other 8+ gb is where your media usually goes…
unfortunately, the web root is inside the OS partition by default…
One way to get around this is to move your webroot to the media partition (/private/var),
then create a softlink to it from the original folder.
Something like this:
cd /Library
mv WebServer /private/var
ln -s WebServer /private/var/WebServer
I learned this trick from another forum for creating more room for your third party apps by moving the /Applications folder the same way…
Btw, my UNIX is a bit rusty now, so all you *nix gurus out there feel free to correct as needed…
ok…correction to my last post…
The command for crating the symbolic link is actually as follows:
ln -s /private/var/WebServer WebServer
I had the arguments reversed before…this should be run from the /Library folder.
Fransjo:
I am trying to do the same script to upload, deleted and download files but when uploading files I am getting 405 error: “The requested method POST is not allowed for the URL /upload.php.”
do you have any idea how to overcome this?
Thanks
[...] Peter Elst » iPod Touch - Apache and PHP 5.2.3 When you’ve got Installer.app installed on your iPod Touch you’ll notice that it allows you to install Apache and a number of scripting languages (PHP, Perl, Python, Ruby). (tags: ipod iphone apache) [...]
[...] Peter Elst shows how to get PHP 5.2.3 onto an iTouch. Do at your own risk, but bookmarking anyway. Looks like there’s a chance to get Perl, Python, Ruby, etc in your iTouch as well but would you really WANT to I wonder? Tags: Apache, iTouch, PHP [...]
Hi, may I ask you to write the example of what the scriptalias for perl should be?
I’d really appreciate it.
php doesn’t install?
On my iTouch v1.2, where I already installed dozens of other packages (including Apache), the PHP install doesn’t go up to the end: download is OK, but then installation seems instant, there is no “cleaning up” phase, the iTouch doesn’t lock in the end, and in fact when I look again in installer.app installable packages PHP is still there (and nowhere in the “uninstall” section).
My interpretation is that there is a problem during this very precise installation process here. I suspected a lack of room on the touch (would this be explicitly stated?): I removed a big wedict dictionary, restarted the touch, retried: no way. I can install other packages, but not the php one.
What can I do?
is there some place where I could get php manually? (I fear there are lots of libraries?)
TIA!
I think you have a problem with the space. In 1.1.2 they have limited the first partition. In /opt/iphone there is a lot of stuff if you install perl an php. There are some tipps on the net how to move the /Application folder and the /opt folder to /var/root and link the moved folder to the original locations.
Thank you moblog!
I’m convinced too now -I just checked, thanks to your hint: the installer saturates /opt/iphone with just a 700K file and stops, and at this point the corresponding volume is filled. I’ll try the move you suggest.
btw, I’m quite impressed by your reactivity: this indeed is a sign a mobile blog is strikingly efficient!
Hervé
… and yes it works
(I just moved /opt/iphone and created the associated link)
Thanks again!
Hervé
Hi,
damn I just upgraded to 1.1.3 with the installer and evrything work good so far, but
I havent deinstalled all apps befor upgrading. So I cannot start the apache because the httpd.conf file is missing, and I also cannot deinstall it, alway get this main script error.
How can I reset the installer.app config file to do clean installs?
thx
Ok, so any updates to this mighty fine post for us 1.1.3 users? As i see it /etc/profile isn’t there anymore… so no way to modify that file…
it worked on my 1.1.2 just fine but now my 1.1.3 is just a bit different.
any help would be appreciated, thanks
@hexcode
I’m in the same boat (1.1.3) - I just created a file called “profile” in that location and copied the code from the top of that page into it. Next I restarted the phone for the changes to take affect - php seems to be working now.
Hmm, can’t get it to work on 1.1.3. I’ve did what you said but I just see the PHP code in Safari (as if PHP wasn’t installed). But I did!
The strange this is, is that I had a profile file. I already tried it in 1.1.2 and it worked, but after my (Dev-Team)upgrade…
Any advice on this one?
Hi,
I installed Apache and PHP on my iPod Touch, and modified the HTTP daemon config.
Apache runs well, but it doesn’t seem to recognize the PHP extension.
I also remarked that the alias /icons points to a folder that is not created on my iTouch : /usr/share/httpd/icons
Indeed, I don’t have a httpd folder under share !
I tried to uninstall / reinstall Apache, but still no httpd folder there…
Any ideas ? Or a listing of all the files installed by Apache, so that I can see what else is missing on my iPod…
Thanks !
Hi Joe,
Can you try looking for the folder in /private/etc/ ?
Cheers
Davd
David,
I’ve got an httpd folder in /private/etc , but it only contains 5 config files and no subfolder.
But I just saw that Apache’s error_log (in /private/var/log/httpd) kept increasing since I installed Apache, with the same errors every 10 seconds :
[Thu Mar 6 00:30:04 2008] [alert] httpd: Could not determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName
[Thu Mar 6 00:30:04 2008] [crit] (48)Address already in use: make_sock: could not bind to port 80
So I stopped added a line in httpd.conf for ServerName (127.0.0.1), and the same in resolv.conf, and… now PHP seems to works !
But I still have the second error (port 80 already used)… So I checked the running processes, and I saw there was 3 instances of httpd : one was run by root user, and the 2 others by “nobody” !
I could kill both, but one keeps coming back (with a new PID) whenever I kill it !
According to my iPod’s Sysinfo app, the httpd run by user root uses /bin/sh as shell, but the one run by user “nobody” uses /usr/bin/false !
If I stop Apache with apachectl stop, the httpds keep coming back some seconds later…
There’s something I don’t understand here… It’s been many years since I last used as much Unix commands :-))
And I still don’t understand where’s Apache’s “icons” folder !
That works perfectly.
Does anyone know how to get SQLite 3 to work?
SQLite 2 is included, - but I really ned SQLite 3
Thank you very much — worked great!
how to download PFP on iphone plz let me know and from wher i can downalod php
[...] out on my 8GB iPod Touch. I loaded both Apache and PHP on the iPod Touch, did a few edits (check here) and there you go PHP running on the iPod [...]
[...] apache server and serve up web pages. In fact, anyone with an ipod touch or iphone can do the same! even with php which opens the way for people to serve up dodgy content over your internet connection - [...]
im on an ipod and can’t see all of what i need to put in the PATH. Can someone post a comment with it?
never mind