Home > General, Mac > iPod Touch – Apache and PHP 5.2.3

iPod Touch – Apache and PHP 5.2.3

October 12th, 2007

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

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!

No TweetBacks yet. (Be the first to Tweet this post)

 
Creative Commons License This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.0 Belgium License.

 

Related posts:

  1. iPod Touch 1.1.1 opened up
  2. Flex Builder 2 – Ant support
  3. Flash Lite running on the iPhone
  4. iPhone App Reviews – AIR application
  5. The Adobe/Apple Relationship


 

General, Mac

  1. pekkis
    October 15th, 2007 at 21:34 | #1

    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… :(

  2. D
    October 17th, 2007 at 13:45 | #2

    Wicked! Exactly what I’ve been looking for! The iPod Touch and the iPhone must be the world’s sexiest portable web servers!

  3. October 19th, 2007 at 23:53 | #3

    Thnx a lot! PHP on my iPhone is great. Now I’m working on an Liveblog for my Fon.

  4. Xypho
    October 22nd, 2007 at 17:41 | #4

    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! :)

  5. October 23rd, 2007 at 18:07 | #5

    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.

  6. October 31st, 2007 at 00:26 | #6

    Peter – thanks for the article, worked flawlessly on my touch.
    Do you know how to get Perl working as well?

    Thanks,
    Ryan

  7. jj
    November 6th, 2007 at 22:43 | #7

    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…

  8. Sam
    November 7th, 2007 at 00:19 | #8

    Great Info!
    Thanks

    I am looking to configure ruby on ipHone Web Server as well please help.

    thanks

  9. November 13th, 2007 at 16:54 | #9

    Great Info!
    Thanks~!!!

  10. November 13th, 2007 at 17:01 | #10

    @jj: You only need an symbolic link from ~/Media… to /Library/WebServer/Documents/etc. Thats all.

  11. jj
    November 14th, 2007 at 05:58 | #11

    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…

  12. Leonardo
    November 15th, 2007 at 23:57 | #12

    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.

  13. Leonardo
    November 17th, 2007 at 04:57 | #13

    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!

  14. November 17th, 2007 at 17:02 | #14

    @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?

  15. Leonardo
    November 17th, 2007 at 19:26 | #15

    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.

  16. November 18th, 2007 at 01:54 | #16

    @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″;
    }

  17. Leonardo
    November 18th, 2007 at 18:36 | #17

    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.

  18. November 18th, 2007 at 19:29 | #18

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

  19. Leonardo
    November 18th, 2007 at 22:01 | #19

    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.

  20. mls
    November 19th, 2007 at 00:05 | #20

    There is rather huge problem with PHP on iPhone/iPod touch – bug in preg_replace function, which always returns empty string…

  21. Fransjo
    November 21st, 2007 at 02:24 | #21

    Hello

    I’ve got Apache and PHP working now, but where is php.ini located on the ipod Touch?

  22. November 21st, 2007 at 02:30 | #22

    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?

  23. fransjo
    November 21st, 2007 at 02:33 | #23

    hmmm ok … so like … you got one php.ini for me to start with hahaha?

    “Do you use an reverse tunnel or dyndns” eh?

  24. November 21st, 2007 at 02:59 | #24

    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/

  25. fransjo
    November 21st, 2007 at 03:04 | #25

    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

  26. Leonardo
    November 21st, 2007 at 03:51 | #26

    Fransjo, perhaps you should do some kind of tutorial to have the script you mention, it could really be useful :-) .
    Cheers.

  27. Illsley
    November 21st, 2007 at 23:20 | #27

    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?

  28. November 25th, 2007 at 03:24 | #28

    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?

  29. wrs
    December 1st, 2007 at 09:35 | #29

    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…

  30. wrs
    December 1st, 2007 at 09:51 | #30

    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.

  31. Emile
    December 1st, 2007 at 10:10 | #31

    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

  32. cgomez
    January 2nd, 2008 at 03:54 | #32

    Hi, may I ask you to write the example of what the scriptalias for perl should be?

    I’d really appreciate it.

  33. Herve5
    January 11th, 2008 at 23:08 | #33

    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!

  34. January 12th, 2008 at 02:42 | #34

    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.

  35. Herve5
    January 12th, 2008 at 13:33 | #35

    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é

  36. Herve5
    January 13th, 2008 at 12:20 | #36

    … and yes it works :D

    (I just moved /opt/iphone and created the associated link)

    Thanks again!
    Hervé

  37. dd
    February 1st, 2008 at 11:22 | #37

    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

  38. hexcode
    February 12th, 2008 at 06:51 | #38

    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

  39. February 12th, 2008 at 14:16 | #39

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

  40. Vincent
    February 13th, 2008 at 16:40 | #40

    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! :o 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?

  41. Joe
    March 6th, 2008 at 00:07 | #41

    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 !

  42. March 6th, 2008 at 00:14 | #42

    Hi Joe,

    Can you try looking for the folder in /private/etc/ ?

    Cheers
    Davd

  43. Joe
    March 6th, 2008 at 02:11 | #43

    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 ! :-D

  44. Thomas
    March 11th, 2008 at 12:28 | #44

    That works perfectly.

    Does anyone know how to get SQLite 3 to work?
    SQLite 2 is included, – but I really ned SQLite 3

  45. Ryan
    April 7th, 2008 at 21:32 | #45

    Thank you very much — worked great!

  46. Ali
    April 10th, 2008 at 11:08 | #46

    how to download PFP on iphone plz let me know and from wher i can downalod php

  47. kyle
    July 5th, 2008 at 01:17 | #47

    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?

  48. kyle
    July 5th, 2008 at 01:39 | #48

    never mind

  49. novemberain27
    October 27th, 2008 at 16:13 | #49

    Hi,
    I have ipod touch (2.1) and I have jailbroken it (using quickpwn).
    I want to install apache and php on the device, but i cannot find the packages in the installer (i am using installer 4 beta).
    I have tried to add several other repos in the sources, but havent been able to find the packages in any of them.

    What should I do?
    Can anyone tell me some repo which has those packages, so that i can add it.

    thanks & regards,

  50. yanus
    November 13th, 2008 at 05:06 | #50

    Same issue here. iPhone 3g running 2.1 cannot locate PHP package, anyone know a source where it exists ?

  51. novemberain27
    November 13th, 2008 at 06:37 | #51

    I solved my problem. I couldnt find any repo which has the PHP and apache packages. But, I needed to install them nonetheless (since this was a part of some project work I am doing in my univ).

    So, I downgraded my iPod touch from 2.1 to first 1.1.5 (to 1.1.4 later on) and installed not only apache & php, but right now, I even have MySQL and even Drupal running on my iPod touch.

    I faced lots of difficulties through my way, installing all of them. So, I am willing to help anyone who needs help with installing Apache/php/mysql/drupal on ipod/iphone [the only pre-requisite (atleast as of now) is that a downgrade is necessary (to 1.1.5/1.1.4)]. My email is novemberain27[AT]gmail[DOT]com.

    Regards,

  1. October 24th, 2007 at 18:46 | #1
  2. December 18th, 2007 at 01:19 | #2
  3. December 19th, 2007 at 00:58 | #3
  4. April 23rd, 2008 at 20:41 | #4
  5. June 14th, 2008 at 15:09 | #5
Comments are closed.
"Schöne Harz Ferienwohnung im Herzen von Deutschland. Weitere Ferienwohnungen im Harz finden Sie auf der angegebenen Urlaubsseite.
Falls Sie einen Partner für erfolgreiche Suchmaschinenoptimierung suchen, sind Sie bei Bullz-Eye an der richtigen Adresse."