Raspberry Pi Web Server

 by drcurzon
Hi there,
This is my first Instructable so all criticisms and comments are welcome.
This will show you how to set up a simple wired web server on your Raspberry Pi, with PHP and MySql.

The Raspberry Pi is a good choice for a webserver that will not recieve too much traffic, such as a testing server, or small intranet, as it doesn;t get too hot (so is nice and quiet), and only uses around 5 Watts of power (costing £3.50 a year where I am if it's running 24/7)

In the future I might add how to set up a wireless server.

If you have any problems, post them in the comments, or PM me - I'm happy to help.
 
Remove these adsRemove these ads by Signing Up

Step 1: You will Need

To create a simple web server, you will need:

1. A power supply (Micro USB)
2. A network cable
3. A HDMI cable (or Component cable) as well as a screen and USB keyboard
3. A Raspberry Pi
4. An SD card (2GB or more)
5. An SD card reader for your computer
6. Win32DiskImager (For Windows) or dd (for Unix)
7. The Raspberry Pi Debian Image
8. PuTTY (For Windows) or any other SSH client
9. Around an hour of free time

You can download the software from the following locations:

http://www.softpedia.com/get/CD-DVD-Tools/Data-CD-DVD-Burning/Win32-Disk-Imager.shtml (for Win32DiskImager)
http://downloads.raspberrypi.org/download.php?file=/images/debian/6/debian6-19-04-2012/debian6-19-04-2012.zip (Debian Image)
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html (for PuTTY)
1-40 of 115Next »
dale3h says: Oct 3, 2012. 5:41 PM
Just wanted to share my experience with this Instructable. Everything went smoothly until it was time to connect to MySQL from a remote machine on the same network. So here's how to fix that:

1. Grant access to your remote machine using: GRANT ALL ON *.* TO 'root'@'192.168.1.%' IDENTIFIED BY 'your_password_here'; (I used 192.168.1.% so that any computer on my network can connect to it)
2. Go into the my.cnf file (sudo nano /etc/mysql/my.cnf) file and look for "bind-address" and comment this out (put a # in front of the line)
3. Reload MySQL config (service mysql reload)
4. Restart MySQL server (service mysql restart)

Hope this helps, and thank you for the great Instructable :)
drcurzon (author) in reply to dale3hOct 3, 2012. 11:29 PM
Hi there,
Thanks for posting this to help others :)
Daniel
shawn_the_beast says: May 22, 2013. 11:20 AM
I would like to point out that service was spelled wrong. I just noticed that when copying and pasting the code over ssh. this is not meant as a rude comment. Other than that worked out perfect with me!!!
shawn_the_beast in reply to shawn_the_beastMay 22, 2013. 11:23 AM
In step 9.
cmrodican says: May 7, 2013. 12:06 PM
i am also having this issue, and unplugging/restarting does not work... also, on step 4, i remove the # from "#send host-name "andare.fugue.com", and you say to change "andare.fugue.com" to anything i want, does it have to be with xxx.xxx.xxx, orwhat format should that be in? btw, thanks for this tutorial... it is really helpful. i am just stuck on this one thing. thanks
cmrodican in reply to cmrodicanMay 7, 2013. 12:07 PM
sorry... the issue i am having is that my Hostname does not work and I have to ftp to the ip.address.
cheycomm says: Apr 28, 2013. 4:14 AM
Nice tutorial, I found this very helpful and easy to follow.

I do have 1 question. Why wouldn't you use the SFTP supplied with the SSH protocol as opposed to installing an additional ftp server (vsftp)?
drcurzon (author) in reply to cheycommApr 28, 2013. 11:33 AM
Hi there,
Glad it was useful.
Honestly, I wasn't aware of the program (SFTP) until you mentioned it, I'll have a look into that.
Many thanks, Dan.
deborahb13 says: Apr 11, 2013. 8:43 PM
Thank you SO much for this tutorial, I just got my Raspi today and I am trying to figure out all the tricks :)
ethoma says: Apr 6, 2013. 5:26 AM
So far all is nice but I cant seem to connect to the raspberry with its hostname, only with the IP address. any hint on where the Problem is? the Router sees the Pi and its new host name but the browser cant.

all in all a very good Tutorial
drcurzon (author) in reply to ethomaApr 6, 2013. 8:37 AM
Hi there,
This is usually a router issue, which OS do you use at the moment?
Can you try a "ping" command (or equivalent) with the host name?
What's the output?
Many thanks, Dan.
neer0 says: Mar 29, 2013. 5:21 AM
What Permissions are to be changed while uploading admin using filezilla??
thecank says: Mar 4, 2013. 10:03 AM
I had two lines like this:
#send host-name "xxxxxxxx"
send host-name = gethostname()
I commented out the second one and changed the host name in quotes to raspberryPi then set it in the command line but I cannot connect to it via Putty (I still have to use the IP address)

Also in step 9, my vsftpd file is blank. Not sure what I'm doing wrong.
karnuvap says: Mar 3, 2013. 1:08 AM
Hi there - I'm afraid I am stuck on this a little bit.

I get as far as adding the # in the /etc/passwd file. However when I try to add pi as a mod it throws up the error that the user pi does not exist. Assuming this was the desired outcome, I try the last few steps and try to login as pi however it says access denied no matter how many passwords I try to enter for pi (the password for root, my chosen pi password and the default one - neither work).

This only seems logical, as I had just commented out the user pi in the previous file, surely?

Ignoring this set back, since it appears to be an optional step, I successfully connect to the FTP server but my FTP directory looks much different to the one in your screenshot despite being the same directory (/home/pi).

Any ideas? Thanks.
drcurzon (author) in reply to karnuvapMar 3, 2013. 6:10 AM
Hi there,
I think the distribution has changed since I wrote this.
Try leaving the user "pi" in as normal (Leave out the #).
Many thanks, Dan.
rbli63 says: Feb 27, 2013. 2:37 PM
Wouldn't it be better to use an FTP client that supports a user-settable default upload path (like for example Filezilla)?

You say:"At the moment you will need to change the directory every time you login by FTP", then some magic, concluded by:
usermod -d /var/www pi

I'm a Linux n00b, so please correct me if I'm wrong. The usermode -d changes the default home path for user Pi, right? That is, not only when logging in via FTP, but also when logging in via SSH, or anything else, right?

With Filezilla (and probaby many others) this is not necessary. But more importantly, I wonder whether or not the usermod is a good idea at all. I had the webserver up & running, no problem. But then I installed some more stuff, which then of course, landed in /var/www. Which looks a bit messy to me. It may be prone to hacking, I don't know.

Any learned comments?
drcurzon (author) in reply to rbli63Feb 27, 2013. 2:38 PM
Hi there.
Thanks for the comments.
As I understand it vsftp is a "thin" server, as in it doesn't use that much in the way of resources.
The better approach for vsftp is to use a dedicated FTP user and set that user's home directory to var/www.

Other than that, a server like FileZilla would be a good idea, I was just trying to use as little of the raspberry pi's few resources as possible.

As far as security, I don't believe that there would be any issues due to the user's home directory, however it is a good question.

Many thanks, Dan.
hamcircuit says: Feb 17, 2013. 12:03 PM
I tried adding that and I still just get a blank page still.
drcurzon (author) in reply to hamcircuitFeb 17, 2013. 12:33 PM
try:
sudo apt-get install phpmyadmin

WAIT FOR IT TO INSTALL

nano /etc/apache2/apache2.conf

AT THE END OF THAT FILE TYPE

#Include phpmyadmin
Include /etc/phpmyadmin/apache.conf

SAVE THE FILE THEN

service apache2 restart

then navigate to:
http://HOSTNAME HERE/phpmyadmin

Let me know how it goes, Dan.
hamcircuit says: Feb 17, 2013. 10:18 AM
Here is the contents of the index.php file. I can delete later so it does not take up so much space on page.

/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* forms frameset
*
* or common.inc.php
* @package PhpMyAdmin
*/

/**
* Gets core libraries and defines some variables
*/
require_once './libraries/common.inc.php';

// free the session file, for the other frames to be loaded
session_write_close();

// Gets the host name
if (empty($HTTP_HOST)) {
if (PMA_getenv('HTTP_HOST')) {
$HTTP_HOST = PMA_getenv('HTTP_HOST');
} else {
$HTTP_HOST = '';
}
}


// purge querywindow history
$cfgRelation = PMA_getRelationsParam();
if ($GLOBALS['cfg']['QueryHistoryDB'] && $cfgRelation['historywork']) {
PMA_purgeHistory($GLOBALS['cfg']['Server']['user']);
}
unset($cfgRelation);


/**
* pass variables to child pages
*/
$drops = array('lang', 'server', 'collation_connection',
'db', 'table');

foreach ($drops as $each_drop) {
if (array_key_exists($each_drop, $_GET)) {
unset($_GET[$each_drop]);
}
}
unset($drops, $each_drop);

if (! strlen($GLOBALS['db'])) {
$main_target = $GLOBALS['cfg']['DefaultTabServer'];
} elseif (! strlen($GLOBALS['table'])) {
$_GET['db'] = $GLOBALS['db'];
$main_target = $GLOBALS['cfg']['DefaultTabDatabase'];
} else {
$_GET['db'] = $GLOBALS['db'];
$_GET['table'] = $GLOBALS['table'];
$main_target = ! empty($GLOBALS['goto']) ? $GLOBALS['goto'] : $GLOBALS['cfg']['DefaultTabTable'];
}

$url_query = PMA_generate_common_url($_GET);

if (isset($GLOBALS['target']) && is_string($GLOBALS['target']) && !empty($GLOBALS['target']) && in_array($GLOBALS['target'], $goto_whitelist)) {
$main_target = $GLOBALS['target'];
}

$main_target .= $url_query;

$lang_iso_code = $GLOBALS['available_languages'][$GLOBALS['lang']][1];


// start output
require './libraries/header_http.inc.php';
?>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
xml:lang=""
lang=""
dir="">



phpMyAdmin <?php echo PMA_VERSION; ?> - <br /> <?php echo htmlspecialchars($HTTP_HOST); ?>



echo PMA_includeJS('jquery/jquery-1.6.2.js');
echo PMA_includeJS('update-location.js');
echo PMA_includeJS('common.js');
?>


drcurzon (author) in reply to hamcircuitFeb 17, 2013. 11:44 AM
Hi there,
Can you add the following to the top of the page:

echo "test";
die();

and try that - you should see "test" on screen when you go to the page.
Thanks, Dan.
hamcircuit says: Feb 16, 2013. 11:15 AM
Permissions are 775. I am typing in 192.168.1.110/admin
do not see a admin.php?
Thanks
drcurzon (author) in reply to hamcircuitFeb 16, 2013. 11:31 AM
Sorry, meant index.php (admin/index.php)
hamcircuit says: Feb 6, 2013. 10:10 AM
Hello, I have install all but have run into a problem. When I type the pi.ip/admin all I get is a blank page in the window. I have change all the permissions so that I can get in. Not sure were I need to go to from here. Thanks
hamcircuit in reply to hamcircuitFeb 16, 2013. 10:38 AM
Can anyone help me with this problem?
hamcircuit in reply to hamcircuitFeb 16, 2013. 10:58 AM
i am able to open the website at just the ip address but when I try to bring up the php it just bring to a blank page. I have changed all permissions to 775. I am using PHP 3.5.7 and wheeze. Hope this will bring some info.
drcurzon (author) in reply to hamcircuitFeb 16, 2013. 10:47 AM
Hi there.
What are the permissions of the page?
What is the URL you are typing in? (including page)
What is the content of admin.php?

Many thanks, Dan.
jansonap says: Feb 9, 2013. 7:11 AM
For me, when i set permissions to "var/www" from the command line it did not apply them recursively to all sub folders and files. Easiest way to do this is if you have FileZilla opened right click on the "www" folder and click "File Attributes". Type 755 into the numeric value if not there already then check "recursive into subdirectories". Click "OK". Now try and access the admin page again.
spafruit1 says: Feb 8, 2013. 8:26 AM
At stap 7 Type in "sudo apt-get update" i get always a connection timed out at 69%. Any tips?
spafruit1 says: Feb 8, 2013. 3:03 AM
At stap 9 you say :
Now restart the FTP server with "sudo servce vsftpd restart"
but i think it must be:
Now restart the FTP server with "sudo serivce vsftpd restart"
drcurzon (author) in reply to spafruit1Feb 8, 2013. 7:42 AM
Hi there.
Thanks for that, yeah it should be "service".
Many thanks, Dan.
Robbanz says: Feb 6, 2013. 12:52 PM
Well something with "reply" is bugging out...

@Dan thanks for your answer.
I just reainstalled Everything and now it works (must have missed something after all)...

Now is my next problem that i can't portforward port 1433 and 3306 for MySql and MSSQL... but that's Thomsons fault NEVER buy a TG789VN...
Robbanz says: Feb 3, 2013. 2:20 PM
Hi, i have followed all step without any errors and i have port 80 opened in my router but when trying to log in to http://raspberrypi/admin/phpMyAdmin

i get below error
Forbidden
You don't have permission to access /admin/phpMyAdmin on this server.

Does any one have any idea about what can be wrong?
drcurzon (author) in reply to RobbanzFeb 3, 2013. 11:23 PM
Hi there.
What have you set the permissions for the files as?
Can you access any other page you've got on the pi?
Many thanks, Dan.
jstemm says: Jan 30, 2013. 8:23 PM
I believe there is a missing (perhaps extra?) step when commenting out the "pi" line in /etc/passwd. What you may chose to do is copy that line, such that you have the line repeated twice. Then comment out one of the lines.

'usermod -d /var/www pi' changes the user's home directory from the default (/home/pi) to the directory /var/www. This will alter the line in the /etc/passwd file. Copying the original line and commenting it out essentially keeps a "backup" of what the line used to say.

That's what I'm assuming anyway, and is what I did. When you FTP using your username and password, you are directed to /var/www immediately. Hope that all makes sense.
prusux says: Jan 29, 2013. 3:04 AM
Same here. After adding "#" to the pi line under /etc/passwd there is no possibility to start a new session as pi user. maybe there is a different line needed as this pi user is just uncommented and does not exist for the system.. sooo after searching the web there is a "solution" in the youtube (lol) http://www.youtube.com/watch?v=zXxWncOxAWk

Anyway, i do not think that this is a popper solution. Please correct this manual :) Thanks!

munderwood4 says: Jan 22, 2013. 7:32 PM
I'm following along fine, but I'm kinda of stumped on this step: "usermod -d /var/www pi"
After commenting out the /etc/passwd line, the above command doesn't find the username pi. I also can't create a new ssh session as pi. Did I miss something?
the_number says: Jan 20, 2013. 12:21 PM
Cant get the reply-function to work, anyways:
The answer to my problem is here: http://forums.debian.net/viewtopic.php?f=30&t=50502
You need to change the ownership of the folder to "www-data". I have no idea what means, I´m still new to to linux, but that solves it.
drcurzon (author) in reply to the_numberJan 20, 2013. 11:18 PM
Hi there.
In case you still need help (looks like you beat me to it :) ) to change the owner of a directory the command is "chown".
For example "chown www-data /home/pi/apache".
Hope this helps, if not then let me know :)

Thanks, Dan.
the_number says: Jan 20, 2013. 11:02 AM
First of all, thank you for your in depth tutorial!

After successfully setting up apache in the default configuration, I wanted to change the root directory to "/home/pi/apache". Changing the respective configurations in the 000-default file I get a "403 - Permission denied" error.

I have tried everything I could find on google - I changed both the "DocumentRoot = PATH" and "", the later once with quotation marks and once without. I have also done "chmod -R 755 PATH" for the directory itself and all it´s parents.
Here is a good summery of what I tried: http://www.cyberciti.biz/faq/apache-403-forbidden-error-and-solution/

However none of this had any effect. I have also read that something called "selinux" might interfere, but from what I understand it is not installed on raspbian.

Does anyone have successfully changed their DocumentRoot? Any ideas on what is causing the issue?
1-40 of 115Next »
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!