An iPhone Powered Apache Web Server with PHP
A week or so ago, I went ahead and decided to do my “first” hacking of the iPhone. I went to the JailBreakMe.com website and followed the simple instructions and the Installer.app which is basically a “one touch” utility to install 3rd party apps and Linux ports such as PICO and a terminal program.
I was a bit surprised to find a port of Apache 1.3.37 and PHP 5.2.3 in the repository, so I went ahead, installed them both and with a little configuration using PICO (after installing and logging in through SSH), got a functional webserver with PHP support.
I created a PHPINFO() page which shows the extensions that are activated by default, which are pretty much just the core PHP functions and started thinking what to do with it…
Of course I could use it for impromptu testing of code from anywhere, showing simple mockups offline to clients one-on-one, and other things like that, but it wasn’t enough for me…
I put the thoughts on the backburner for a few days, last night I figured I’d try something new.
Here goes…
Apache/PHP was working so I decided to go ahead and copy a full PHP site with full graphics and a PHPMail script on it.
To get this all over to the phone, I TARred up just the most necessary files, filtering out anything not directly referenced (limited amount of memory on the phone for this type of stuff).
Then I FTP’ed, using NCFTP installed from the Installer.App repository, into the machine that had the TAR, downloaded and unTARred it into Apache’s document folder (/Library/WebServer/Documents).
At that point, once all the files were in the correct place and so forth, I hit the website directly on the phone (http://127.0.0.1) and it came right up and was rather speedy. I then hit it from a workstation on the same WiFi network using the DHCP assigned IP and it came up perfectly.
Ok, so far so good, the website comes up just fine locally and remotely, lets take it a few steps further.
I decided to, from work today, to VPN using PPTP built into the iPhone into my DD-WRT router at home (It has a static IP) and then proceed to port forward port 80 from the external IP to the Internally assigned VPN IP of the iPhone. Then I proceed to add a DNS entry for it…
I also VPNed into the DD-WRT over EDGE and it worked just fine as well. Of course it was MUCH slower, but still worked fine.
It really was this easy to do something that I don’t think has ever been done before with a cellphone.
NOTE: Apache was setup to just start 1 httpd instance and I believe a max of 7, which slowed stuff down a lot with a graphical site, so I set it from 1 to 10.
If anyone has questions, feel free to leave a comment and I’ll reply as soon as I can.
November 15th, 2007 at 10:12 pm
thanks for the tips, got it working on my iPod Touch
any idea why “preg_replace” is not working?? Always returns null string…
$str = “this is a test”;
$str = preg_replace( ‘/is/’, “is not”, $str );
print $str;
November 16th, 2007 at 10:46 am
I haven’t used preg_replace, so I’m not sure. I know at my day job last week, I was trying to use the “gregoriantojd” function and it just WOULD NOT work. After a bit of research, the calendar functions ended up not being compiled into PHP. After a quick recompile, it worked fine. Might be a similar issue.
I know the Apache/PHP for the iPhones is pretty minimal feature-wise.
November 16th, 2007 at 12:26 pm
The regexp functions are pretty basic, however. If I give bad parameters for the function, it complains as it should, implying that the function really is there… just not working. There’s probably a compile time setting that is set differently from all of the other installs of PHP that I’ve tried this on.
November 18th, 2007 at 1:33 pm
hello im using dd-wrt im wondering if u can make howto how to setup vpn on iphone and settings on dd-wrt.. or if u can email the settings so i can setup my
December 11th, 2007 at 12:26 pm
Can you please tell me how you configured PHP in the httpd.conf? i didnĀ“t get it working, always getting Internal Server Error…heres my config from now:
Options ExecCGI
AllowOverride None
ScriptAlias / /opt/iphone/bin/
Action php-script /php
AddHandler php-script .php
December 12th, 2007 at 1:38 am
I stupidly upgraded to 1.1.2 and my phone is wiped… It wasn’t that hard though, it’s all out there on the net, just google around. I’m sure you found my site with Google, you’ll find a more detailed instructional elsewhere I’m sure…
Thanks for the visit to the site though.
December 13th, 2007 at 5:48 am
Is there any way to run Mysql server on IPHONE?
December 17th, 2007 at 2:02 am
I don’t think MySQL has been ported yet, although It would be nice…
February 27th, 2008 at 11:50 pm
I am trying to set up PHP and Apache on my Ipod Touch. I have the apache working, but I do not know where to set up the PHP. I get a “Method not allowed” error when I submit a form through POST to a PHP page.
March 7th, 2008 at 1:51 am
I installed SSHD onto my phone and SSHed into it and followed instructions posted on other websites…
There is a little configuration that has to be done to get Apache and PHP working together nicely…
Sorry I can’t be of more help, but it’s been a while since I did this and since then I’ve upgraded to 1.1.4 and am no longer jailbroken.
Good luck.
March 23rd, 2008 at 11:53 pm
How do I make my iphone without cell service into a wifi web server?
March 27th, 2008 at 10:04 am
You can Google around about activating it without service, then you only need to JailBreak it (again Google), and install the Apache web server using the installer.app … As stated above, it is good to install the SSH Daemon on it to configure it.
May 6th, 2008 at 11:34 am
Hi
I tried to install Apache and PHP on my iPhone using installer/app but they were not there.
I knew they were supposed to be in network and development but I didn’t find them. Any idea?
June 13th, 2008 at 6:53 am
Thats superb, you can essentially take a portable web server anywhere! Or if you were a chef could have a mysql databse of recipes and carry them around in your pocket!
June 14th, 2008 at 9:09 am
[...] with a computer can run an 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 [...]
June 16th, 2008 at 8:05 pm
I wish there was a light weight MySQL for the iPhone. (I haven’t looked lately) It would definitely open the door for a really cool apps. I believe SQLite is already on the phone, but that’s a big learning curve for those of us who are most used to MySQL. Of course you can just simply write a remote app to access it via EDGE or WiFi.
I have written a small online iPhone app for Real Estate Agents/Brokers that I’m tweaking for public use.