Lightweight webserver with php and database


17 posts
by Freemann » Wed Jun 27, 2012 8:48 pm
Hi,

I'm playing with the Pi for a few days and now looking for a (ultra) lightweight webserver with php and database support.

I found a couple of possible options;
Webserver (with php support):
Nginx
tHttpd
lighthttpd
or/and ....

Database:
Mysql,
SQLite,
or/and ....

With the above options, its possible to make a dozen of "end products".

Could anybody (dis)advise a combination and could anybody help me with the installation of that option?

Thanx!
Posts: 10
Joined: Wed Jun 27, 2012 8:20 pm
by DaveCummings » Wed Jun 27, 2012 10:31 pm
I am running apache 2 and MYSQL on mine all seems fine
Posts: 19
Joined: Tue May 29, 2012 8:26 pm
Location: Liverpool UK
by Freemann » Thu Jun 28, 2012 5:24 am
DaveCummings wrote:I am running apache 2 and MYSQL on mine all seems fine

And how is it preforming?

I had a LAMP installation on the raspberry, with a mysql (innodb) table (200.000 records, multiple indexes). The responsiveness of the Pi was dramatic, with 1 page refresh the mysql proc spiked for about 1 sec. and the apache proc for half a second.

My AsRock Ion Atom 330 (under clocked to 1ghz), ubuntu 12.04, (LAMP) serves this page with without any pain.

I want to install Nginx, php, sqlite on the raspberry. Anybody how could help?
Posts: 10
Joined: Wed Jun 27, 2012 8:20 pm
by trickierstinky » Thu Jun 28, 2012 11:31 am
I'm using Lighttpd with sqlite db.

This is the one setup I found to work the best with very little effort.

I tried Lighttpd with mysql, but I found using something like mediawiki with mysql just made the whole experience far too slow to access from outside my network.

I then switched mediawiki to use sqlite and if I'm honest I am not going to go back to mysql as it has now become really fast.
Posts: 17
Joined: Sat May 26, 2012 2:47 pm
by RaTTuS » Thu Jun 28, 2012 11:39 am
it think you will be better off running the RPi from an usb drive [see proper HD not SD card or usb flash device] 2.5" powered one will do wonders for your speed
http://www.catb.org/esr/faqs/smart-questions.html <- ask smart Questions
"That's not right, the badgers have moved the goalposts."
1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX - Prosliver FTW
User avatar
Posts: 5386
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
by Freemann » Thu Jun 28, 2012 12:46 pm
RaTTuS wrote:it think you will be better off running the RPi from an usb drive [see proper HD not SD card or usb flash device] 2.5" powered one will do wonders for your speed


I have a 32Gb class10 SD-card, would it be a big difference to use a USB-hdd-drive?
Posts: 10
Joined: Wed Jun 27, 2012 8:20 pm
by MoonTEC » Mon Jul 02, 2012 4:05 pm
Freemann wrote:I want to install Nginx, php, sqlite on the raspberry. Anybody how could help?

In ~5 minutes (Tested on Raspbian, fresh install after apt-get update && apt-get dist-upgrade and rpi-update, headless over ssh)
Code: Select all
# apt-get install nginx
# /etc/init.d/nginx start

Test in browser http://YouRaspberryPiAddress/index.html
If everything is OK, should see "Welcome to nginx!"
Code: Select all
# apt-get install php5-fpm
# apt-get install php5-sqlite
# nano /etc/nginx/sites-available/default

Uncomment line: listen 80; ## listen for ipv4...

Add index.php to line with index, to look like:
index index.php index.html index.htm

Modify line with server_name: server_name _ ## default vhost to catch all requests
or add yours, like server_name http://www.test.com

Add or uncomment lines:
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

If default document root left unchanged, generate phpinfo php script and reload nginx.
Code: Select all
# echo "<?php phpinfo(); ?>" > /usr/share/nginx/www/phpinfo.php
# /etc/init.d/nginx reload

Test in browser how it works http://YouRaspberryPiAddress/phpinfo.php

pdo_sqlite
PDO Driver for SQLite 3.x enabled
SQLite Library 3.7.13

sqlite3
SQLite3 support enabled
SQLite3 module version 0.7
SQLite Library 3.7.13

Tested default nginx config with httperf from other computer over wifi. CPU load from 4% at idle, to 100% at full load, RAM usage from 17MB at idle, to 28MB at full load.
Code: Select all
root@aspire-one:/home/me# httperf --server 192.168.2.108 --uri /phpinfo.php --num-con 5000 --num-call 10 --rate 200 --timeout 5
httperf --timeout=5 --client=0/1 --server=192.168.2.108 --port=80 --uri=/phpinfo.php --rate=200 --send-buffer=4096 --recv-buffer=16384 --num-conns=5000 --num-calls=10
Maximum connect burst length: 1

Total: connections 4361 requests 8759 replies 8185 test-duration 46.883 s

Connection rate: 93.0 conn/s (10.8 ms/conn, <=1022 concurrent connections)
Connection time [ms]: min 562.4 avg 10302.3 max 28861.2 median 9591.5 stddev 5539.7
Connection time [ms]: connect 998.8
Connection length [replies/conn]: 8.369

Request rate: 186.8 req/s (5.4 ms/req)
Request size [B]: 77.0

Reply rate [replies/s]: min 41.8 avg 180.0 max 275.0 stddev 86.3 (9 samples)
Reply time [ms]: response 929.5 transfer 18.5
Reply size [B]: header 159.0 content 5852.0 footer 0.0 (total 6011.0)
Reply status: 1xx=0 2xx=1043 3xx=0 4xx=0 5xx=7142

CPU time [s]: user 1.33 system 45.48 (user 2.8% system 97.0% total 99.8%)
Net I/O: 1039.0 KB/s (8.5*10^6 bps)

Errors: total 4290 client-timo 3651 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 639 addrunavail 0 ftab-full 0 other 0

Securing, customising depends on You.
User avatar
Posts: 28
Joined: Sat Aug 20, 2011 8:20 am
Location: Lithuania
by RaTTuS » Tue Jul 03, 2012 7:35 am
Freemann wrote:
RaTTuS wrote:it think you will be better off running the RPi from an usb drive [see proper HD not SD card or usb flash device] 2.5" powered one will do wonders for your speed


I have a 32Gb class10 SD-card, would it be a big difference to use a USB-hdd-drive?

yes - random access on a proper HD is loads faster than an SD card
http://www.catb.org/esr/faqs/smart-questions.html <- ask smart Questions
"That's not right, the badgers have moved the goalposts."
1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX - Prosliver FTW
User avatar
Posts: 5386
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
by neil2909 » Sat Jul 07, 2012 9:35 am
What would cause the following.

reloading nginx configuration: nginx : [emerg] "fastcgi_pass" directive is duplicated in /etc/nginx/sites-enabled/default:69
nginx configuration file /etc/nginx/nginx.conf test faild

after running the nginx reload command.

Apart from that all the other bits went well.

edit< if i look at the web page phpinfo.php explorer wants to open it like a file ? >

Sorry to ask questions but i hoping to get it runing and then try and read up as to how to move on with PHP
Neil
Posts: 26
Joined: Sun Jul 01, 2012 7:23 am
by Tadas Sasnauskas » Sat Jul 07, 2012 12:15 pm
RaTTuS wrote:it think you will be better off running the RPi from an usb drive [see proper HD not SD card or usb flash device] 2.5" powered one will do wonders for your speed


Why not best of both? Use sd card for root file system and mount /home & /var on usb hdd.
Posts: 6
Joined: Sat Jul 07, 2012 12:10 pm
by neil2909 » Sat Jul 07, 2012 8:55 pm
Well, I should have looked at and around line 69 and i would have found the problem.

Seems to run now missed the two fastcgi_pass commands and the comment to comment one out.

Posting does work, it made we re-vist the problem. :D
Posts: 26
Joined: Sun Jul 01, 2012 7:23 am
by Jim Manley » Mon Dec 10, 2012 6:45 am
For those who may not be aware and like to use PHP for processing CGI requests, as of version 5.4, PHP now has a built-in minimal web server. Simply type php -S localhost:8000 (note that's an uppercase -S, not lowercase) in a dedicated terminal window - don't background it with an ampersand (&) as there are status/error messages sent to the terminal that may be useful to see. The :8000 port number can alternatively be any non-reserved high port number in the range 49152–65535 if :8000 is in use by a conflicting service on your system (e.g., iRDMI (Intel Remote Desktop Management Interface - officially assigned by IANA), SHOUTcast (unofficial, unassigned use), or FreemakeVideoCapture service (a part of Freemake Video Downloader - unofficial, unassigned use).

The current directory where the server is launched from becomes the web-root document directory (from where web files are served, e.g., .htm/.html/.php files). This was developed solely as an easy way to test and debug PHP scripts by executing them locally and displaying activity in a browser that's pointed at localhost:8000/whatever.php - it should NOT be used as a production web server due to its minimal security mechanisms. If you have MySQL installed and php.ini configured to work with MySQL, PHP will be able to access the DBMS using the same connection and DB SQL query syntax as with any Apache/nginx/thttpd/Lighttpd/etc., httpd (web) server. This could be very useful on a 256 MB Pi board, as MySQL alone can occupy upward of 70 MB of RAM (make sure to minimize the RAM split dedicated to the GPU if you don't need it when running this kind of service).

Hope this is useful to someone.
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!
User avatar
Posts: 1357
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, USA
by mxlplyk » Wed Dec 12, 2012 10:10 am
Why not best of both? Use sd card for root file system and mount /home & /var on usb hdd.


the /home and /var, is that where the read and write always happens?
causing the SD card to wear-out easily?

But, How exactly do you do that?
Posts: 24
Joined: Tue Jul 31, 2012 1:33 am
by hayesey » Wed Dec 12, 2012 11:04 am
I would go for lighttpd and sqlite. I've used this combination on various embedded/low power devices and find it works very well (most smartphones use sqlite, inc the iPhone). One thing to be aware of with sqlite though is that it doesn't handle concurrent writes (a write locks the *whole* database) so if you are going to be doing a lot of writes to the DB you might start to run into problems with other things trying to read. Multiple reads are fine though, it's very good in that respect for a lightweight database system.

So if you are going to be building a website that writes to the database when people are accessing it and you will have more than a handful of concurrent accesses then you might want to consider using mysql.
User avatar
Posts: 77
Joined: Mon Nov 28, 2011 12:46 pm
Location: Manchester, England
by mxlplyk » Thu Dec 13, 2012 7:32 am
Isn't it that having a Database entails a lot of read/write instances on the RasPi's SD card? And that would easily wear it down?
How would you go about avoiding that?

It was mentioned in one of the posts earlier that, the boot partition can be left on the SD card, and then the /home and /var can be moved to maybe a hard drive or SSD?

But can anyone here post some instructions on how to do that, coz I plan to plan the same thing.

Thanks!
Posts: 24
Joined: Tue Jul 31, 2012 1:33 am
by zoyo » Sat Dec 15, 2012 10:06 am
You may consider Hiawatha as the webserver. Rock solid, secure and light on resources.
Step-by-step tutorial is available at:
http://www.hiawatha-webserver.org/forum/topic/1214
Posts: 5
Joined: Tue Jul 24, 2012 9:15 pm
by permaband » Fri Dec 21, 2012 1:24 am
Tried em all and stress tested my pi over the last few months. After all the fuss I ended up back with apache2 and mysql. Ease of use/config and flexibility just beats wasting hundreds of hrs on trying to learn the byzantine ways of other options. Ultimately everything else was slower and more time consuming to setup and optimize than tried and trusted AMP. :D

One you've optimized your install by turning off as much logging as possible and dumping what remains on a ramdisk (I use ramlog) and tweaked php.ini, apache conf and mysql for sensible memory and process limits it all hums along just fine tbh. My pi stats: http://www.grognard.net/stats.php
My website grognard.net is currently being home hosted and although its early days in terms of bolting on dem php modules, its been solid enough these last few months. :D

Interestingly (altho completely subjective) I found sqllite and lightttpd actually slower when running common apps. Even with a php accelerator!
Posts: 62
Joined: Wed Aug 01, 2012 2:14 pm
Location: uk