Introduction: Raspberry Pi Web Server

Picture of Raspberry Pi Web Server

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.

Step 1: You Will Need

Picture of 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)

Step 2: Format SD Card

Picture of Format SD Card

Firstly, you need to install the Debian image onto an SD card (2GB or more).
(The image can be downloaded from http://downloads.raspberrypi.org/download.php?file=/images/debian/6/debian6-19-04-2012/debian6-19-04-2012.zip)

You can do this using the Win23DiskImager tool for Windows (downloaded from http://www.softpedia.com/get/CD-DVD-Tools/Data-CD-DVD-Burning/Win32-Disk-Imager.shtml)

Insert your SD card.
Once you have downloaded the above, Extract the Debian image, and the Imaging tool, then run Win32DiskImager.
Ignore the error that you will see.
Select the Image file (debian6-19-04-2012) and your SD card, then click "Write", it will ask you to confirm, click "Yes". Then enjoy a break, as it usually takes a couple of minutes to write.

Once the write process has finished, you can remove the SD card, then the fun begins.

Step 3: Allow SSH Connections

Picture of Allow SSH Connections

Now plug the HDMI cable into your Raspberry Pi and your monitor, then add the power cable and SD card, as well as the USB Keyboard.

When you first turn on your Raspberry Pi, the boot up might take a while, but be patient. It will also reboot itself once, this is normal.

Login with the following information when prompted:
Username: pi
Password: raspberry

Type in the following without the quote marks:

"sudo -i"
[Return / Enter key]
"cd /boot"
[Return / Enter key]
"mv boot_enable_ssh.rc boot.rc"
[Return / Enter key]
"reboot"
[Return / Enter key]

This will restart your raspberry pi.

Step 4: Change Hostname

Picture of Change Hostname

Now that SSH is enabled, you want to change the hostname so you can connect to it more easily.

Login to your raspberry pi with the username pi and password raspberry.

Type in the following without the quote marks:

"sudo -i"
[Return / Enter key]
"nano /etc/dhcp/dhclient.conf"
[Return / Enter key]

Now look for the line "#send host-name "xxxxxx";" (on mine, xxxxxx is andare.fugue.com), and remove the "#" symbol from in front of it.

Now hold the Ctrl key and press "x" to save the changes. You will be asked to save changes, just type "y" then hit the Return / Enter key.

Then type "hostname xxxxxx" where "xxxxxx" is what you want to call your server (I chose "raspberryPi" to make it easy).
Please Note that you cannot have any spaces in the hostname.

Finally for this step, type in "ifdown eth0" and hit Return / Enter.
Once that has finished, type "ifup eth0" and hit Return / Enter.

Now type "halt" and hit Return / Enter.

Step 5: SSH Login

Picture of SSH Login

Once your Raspberry Pi has halted, you can disconnect it from the monitor as it is no longer needed.

Plug your Raspberry Pi's power connection in, as well as the network cable into your switch, or hub / router.
Wait a minute or so, then run your SSH client. (I used PuTTY).

Type your hostname into the hostname box, and use port 22. (The hostname is the one you set earlier).
Then click "Open".

As this is the first time you have connected to your Raspberry Pi via SSH, you will get a security warning. Click "Yes" to continue.

Now you can login to your Raspberry Pi with the username: pi and password: raspberry.

Since SSH is working, you no longer need to worry about plugging in a monitor, or keyboard, so you Raspberry Pi can be placed anywhere where there are power and network connections available.

Step 6: Change Default Password (IMPORTANT)

Picture of Change Default Password (IMPORTANT)

Ok, so SSH is working, and you have access to your Raspberry Pi, but it's now accessible by anyone on your network.

To change the default password, start an SSH session (step 5), and login, then type "sudo -i", hit Return / Enter, then type "passwd pi" and hit Return / Enter.

Then you can type in your new password, it will ask you to confirm the password.

Step 7: Install Apache With PHP

Picture of Install Apache With PHP

Again, either start an SSH session, or continue if you're already in one.

Type in "sudo apt-get update"
Once this is complete, you may recieve an error, just run the same command again to resolve this.
Then type "sudo apt-get install apache2 php5 libapache2-mod-php5"
It will say "Do you want to continue", just press "y" and hit Return / Enter.
Wait for this to finish (as it may take a while). When it is finished, you may have noticed that there was an error, to solve this, type "sudo groupadd www-data", then "sudo usermod -g www-data www-data".
Now restart apache with "sudo service apache2 restart". This time there should be no errors.

If you now start a web browser on your computer, and type your hostname into the address bar, you will get the apache installed page.

To enable htaccess files you must modify the config files, like so:

Type "sudo nano /etc/apache2/sites-enabled/000-default" and hit Return / Enter.
You'll see a line like the following:

"AllowOverride None" - change this to "AllowOverride ALL". (see image for more detail)
Then hold the Ctrl button and press the "x" key, then press "y" when prompted and hit Return / Enter.
Now restart apache with "sudo service apache2 restart".

Step 8: Install MySQL

Picture of Install MySQL

Again, either start an SSH session, or continue if you're already in one.

Type in "sudo apt-get install mysql-server mysql-client php5-mysql" and hit Return / Enter.
You will be asked "Do you want to continue?", press the "y" key, then Return / Enter.
This will take a while to download and install, but a screen will appear asking you to set "New password for the MySQL "root" user", enter a password, then confirm it.
Again, wait a while, then you're done.

Step 9: Install an FTP Server

Picture of Install an FTP Server

For this I chose to use vsftp, as it's pretty simple to set up.

Again, either start an SSH session, or continue if you're already in one.

Type in "sudo chown -R pi /var/www" <-- IMPORTANT (otherwise you won't be able to upload anything)

Type in "sudo apt-get install vsftpd" and hit Return / Enter
Now type in "sudo nano /etc/vsftpd.conf" and hit Return / Enter

Search through the file and change the following lines:
anonymous_enable=YESChange To anonymous_enable=NO
#local_enable=YESChange To local_enable=YES
#write_enable=YES
Change To write_enable=YES

Also, add a line to the bottom of the file:
force_dot_files=YES

Thenhold the Ctrl key and press "x", then "y", then the Return / Enter key.

Now restart the FTP server with "sudo servce vsftpd restart"

At the moment you will need to change the directory every time you login by FTP, to solve this:

Type:
"sudo -i"
"passwd root"
Type in any password (this is only temporary)
"exit"
"exit"
Start a new SSH session
Username: root
Password (the one you just set)
"nano /etc/passwd"
find the line "pi:x;1000:1000:Raspberry Pi User,,,:home/pi:/bin/bash" and change it to "#pi:x;1000:1000:Raspberry Pi User,,,:home/pi:/bin/bash".
Then hold the Ctrl key and press "x", then release Ctrl and press "y" then hit Return / Enter.
"usermod -d /var/www pi"

Now close the SSH session, and start a new one with username: pi and your password, and type the following:

"sudo -i"
"usermod -L root"
"exit"

Now you are able to connect to your Raspberry Pi with an FTP client, using the following information:
Host: (Hostname you set - I used raspberryPi)
Username: pi
Password: (Password you set previously)
Port: 21

Step 10: Installing PhpMyAdmin

Picture of Installing PhpMyAdmin

Installing phpMyAdmin is a simple task.

1. Go to http://www.phpmyadmin.net
2. Download the zip file
3. Extract it to a folder
4. Upload the extracted files to your server (you may need to set permissions)

For the last point, start your FTP client, then connect to your Raspberry Pi, copy the files across and you're done.

Now navigate to where you installed phpMyAdmin (for me it's http://raspberryPi/admin)
You can login with the information:
Username: root
Password: What you set earlier (in step 8)

Step 11: Overview

Now you have a Raspberry Pi server consuming around 5 Watts (very economical :D)

Installed on your server is:
- SSH
- Apache web server
- PHP5
- MySQL server
- VSFTP FTP server
- phpMyAdmin

For the future, a firewall is recommended. (I may do an instructable on this soon)
I may also add a wireless dongle to make it a wireless server that just requires a power connection (or battery) making a completely portable server.

I hope this serves to help some people, and all criticisms are welcome.
If you have any problems, post them in the comments, or PM me - I'm happy to help.
Daniel.

Comments

dale3h (author)2012-10-03

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

diy_bloke (author)dale3h2014-08-09

is the "GRANT ALL ON *.* TO 'root'@'192.168.1.%' IDENTIFIED BY 'your_password_here';"
a command you type into the terminal? or does it go into some config file?
btw, I can advise anybody using a remote linux server (like the Raspberry) to install 'webmin' as well

PrinsNathan (author)diy_bloke2014-11-11

You get an SQL type console when you type "mysql -p -u root" which accepts SQL code like the GRANT. the rest is all normal console code :) btw, what a coincidence to see you here as well!

diy_bloke (author)PrinsNathan2014-11-11

thanks. :-) small world :-)

well all went great with the server and it still works, just somehow the terminal program doesnt accept my login anymore :-) I know th epassword is right as I still can get in through 'Webmin'

drcurzon (author)dale3h2012-10-03

Hi there,
Thanks for posting this to help others :)
Daniel

puihuenlaw (author)2015-02-09

I don't know why i have this problem. After I add a "#" in "/etc/passwd". I followed your steps and enter the command: "usermod -d /var/www pi". But it said "user 'pi' does not exist. Can anyone help me??

For some weird reason, I found that the line he wants you put a # in front of, doesn't actually exist.

If you're like me, you probably put the hashtag in front of the wrong line.

#pi:x;1000:1000:,,,:home/pi:/bin/bash

This line is weirdly not the same line as the one in the tutorial, though it does resemble it.

The line in the tutorial is this:

pi:x;1000:1000:Raspberry Pi User,,,:home/pi:/bin/bash

What I did, is I actually made a new line and wrote in the line we were supposed to hashtag, then I un-hashtagged the line that was already there when I opened the file.

It looked like this in the end:

pi:x;1000:1000:,,,:home/pi:/bin/bash (Pre-existing line)

#pi:x;1000:1000:Raspberry Pi User,,,:home/pi:/bin/bash (Line I wrote in)


It seemed to fix the issue.

AzizN15 (author)david.krismer.12017-05-08

in order not to change directory every time open /etc/passwd and change the first lineto:

root:x:0:0:root:/home/pi:/bin/bash

It doesn't make scenes because the "#" sign is for disabling the following line without deleting it. If you just put a new line:

pi:x;1000:1000:,,,:home/pi:/bin/bash (Pre-existing line)

#pi:x;1000:1000:Raspberry Pi User,,,:home/pi:/bin/bash (Line YOU wrote in)

The # sign doesn't do anything for you. (It didn't disable the pre-existing line)

I thought this tutorial is outdated so the code is not suitable for Raspberry Pi B+. I just skipped that command and go straight. And it goes right!

puihuenlaw (author)puihuenlaw2015-02-18

In addition, I found a app called "WebIOPi". It is more convenient to use GPIO in the web and over the web.

muralikota (author)2016-12-26

when i type sudo nano /etc/apache2/sites-enabled/000-default
i wont get data which is prewritten , i got an empty editor file.

AzizN15 (author)muralikota2017-05-08

use instaed:

sudo nano /etc/apache2/apache2.conf

HariprakashN (author)muralikota2017-04-08

It is "sudo nano /etc/apache2/sites-enabled/000-default.conf"

JMGSE (author)2016-10-30

I´ve done it precis some it was described. But now I don´t have no chance to come in. :(

TimL18 (author)2015-08-06

This guide need some serious overlooks. WARNING IT WILL RUIN YOUR SSH-setup. Many, if not all experience the same thing. Dumb-blue-eyed as I were I followed it through since it looked thorough enough. Anyone know a get around? -.- Or will I need to get to my raspi physically now and reconfigure it? Gosh why did I not question internet.

arcans (author)TimL182016-02-01

Totaly, this tutorial is really bad. I have to reset everything...

JesseW20 (author)2015-12-06

What OS was this using? Raspbian?

DavidW95 (author)2015-08-27

Thank you for taking the time to create this document. It has been very helpful.

In section #9 the letter i is missing from "service" on this line:

Now restart the FTP server with "sudo servce vsftpd restart"

This should be

Now restart the FTP server with "sudo serivce vsftpd restart"

d00nald (author)2015-05-02

can you use this server rechable external network, and register .com to that?

please let me know, thanks!

epot (author)d00nald2015-06-02

Yes. Take a look at this: http://www.howtogeek.com/66214/how-to-forward-ports-on-your-router/

kartikvr20 (author)2015-06-01

can we use this for surfing on internet

scuéllar sagaón (author)2015-05-14

Good work, thanks

José MaríaS (author)2015-03-23

Hello

I'm just trying to install mysql on my raspberry pi B+ with no success.

When I try to install: sudo apt-get install mysql-server

I've got always a message that mysql failed to start.

I looked around the internet and tryed several "solutions" but nothing worked.

I've installed last raspbian version on my SD.

Can you or anyone help me. Thank very much in advance.

José María Sánchez

Ibiza - Spain

bmccaig (author)2015-01-05

where can i find a version of debian, the link provided does not work?

Gelfling6 (author)bmccaig2015-02-25

You should be able to grab the Raspian image from the Raspberry Pi main website, and make the changes listed here. Though, in this instructable, he suggests a 2GB CF card, I highly recommend an 8GB or higher, as the latest images seem to be larger once they expand. Jump to http://www.raspberrypi.org/downloads/ for the images.

gago.babic.3 (author)2015-02-17

can I ask. i have basic php site. and i want to have password . so only me have acess to site. I am turning lights over internet and right now enybody can do this. can you tell me how can i protect site . I am beginer so....

HowardC5 (author)2015-02-13

Tried several suggestions, aidenv5's recommendation solved the problem. Thanks aidenv5. Here is his recommendation.


To log in again using the user pi you need to add that user to the group pi.

ssh to pi with user root

type sudo useradd -g pi pi

type passwd pi

type new UNIX password/confirm

type exit

type exit

DavidM48 (author)2015-02-08

If you are having issues with running php, just run sudo chmod -R 755 /var/www/

It fixes an issue where php would not run.

XanderA1 (author)2015-01-23

Thanks for this, just what I needed.

DanishR (author)2015-01-17

Yes it works, works perfectly... Was getting few errors in the middle but than i realized i missed few steps.

Anyone interested in adding reset switch to Raspberry Pi

inteldon (author)2015-01-01

Someone needs to update this instructable. So many people are complaining about the # and other minor settings. Please update with corrections.

inteldon (author)2015-01-01

I followed this tutorial. By the end of it I could no longer log into my RPi using remote desktop! It didn't recognize my root account or pi account. When I tried to use the browser I did get the "It works!" page from apache, but when I tried accessing the /phpmyadmin I got banned for inserting the wrong password. I did enter the right password I created when I was setting it up. I tried restarting the RPi but had the same problems again. Please help!

CharlesS5 (author)2014-12-19

I don't think this insctructable is very detailed nor accurate at times. A lot the steps are just given with out any justification especially as the steps proceed toward the end, which can leave you in some tight spots. Commenting out my pi user made me panic before I knew to relogin as root. At times it can be confusing and not clear exactly how to reach a particular checkpoint of a step. For instance change host name should mention the difference between eth0 and wlan0. Also accessing your apache server has to do with you IP and not the entered in hostname. There are also better ways to do things in my opinion such as phpmyadmin 'sudo apt-get install phpmyadmin.' I just think for someone getting into web development this tutorial is nice in that it lays out on the software you need to host your own server but there are simpler better ways that won't leave you guessing why the %$^# you just did that.

cozghes (author)2014-12-04

HI,

everything went well, but when i try to connect the ip on my browser not work.
anyone can help me?
thanks
davidbarcomb (author)2014-11-19

Clear and concise. Great instructable!

winrish01 (author)2014-11-05

mv boot_enable_ssh.rc boot.rc

this command is not working showing no such files or directory

help me out plzz ASAP..

RabidDreds (author)winrish012014-11-09

I had to use "sudo /etc/init.d/ssh start" instead and it worked fine

winrish01 (author)2014-11-05

after all this steps when I open putty and enter host name and press open

it shows "connection refused"

can any one help me out ?//

tiffany.doughty.7 (author)2014-10-28

Im trying to login to putty and it says connection refused...

GalenA1 (author)2014-10-20

How to gain access to root again

do NOT remove the # in front of the pi password

login as pi

type sudo -i (gives root w/o passwd)

type passwd

enter in a NEW ROOT PASSWORD

verify password

exit

oululife (author)2014-10-15

Good writeup.
I did much the same, but used lighttpd as the server. Also couple of recommendations:

If my host machine is running Linux, not Windows, try PACmanager as an alternative to putty. Bit more friendly.
At some point, do a "sudo rpi-update" to make sure your firmware's up.to.date. gets extra goodies to the camera, etc. Needs a reboot afterwards.
Load Filezilla on the host machine! Saves a lot of messing about!
If you want to put the machine on the web, checkout dnsdynamic.com. (I might write an 'instructable' for this, it's fairly easy, but...
Anyway, oululife.dnsdynamic.com has been running this, only interrupted when we had to decorate, for over a year without any downtime.
HTH
Andy

tyler.ben.acevedo (author)2014-09-23

New to using the Pi. When I enter the command "ifdown eth0" I receive an error saying interface eth0 no configured.

Is this also causing me to get the Network Error:Connection refused?

Hello there,

eth0 connection is a connection via Ethernet cable, if you are using a WiFi dongle, type in wlan0 instead.

I recommend typing in "sudo ifdown wlan0 && sudo ifup wlan0" instead as it put both command together so you can reconnect if your doing the commands remotely.

tyler.ben.acevedo (author)2014-09-23

New to using the Pi. When I enter the command "ifdown eth0" I receive an error saying interface eth0 no configured.

Is this also causing me to get the Network Error:Connection refused?

mikak2 (author)2014-09-16

I was able to log in as root after restarting the raspberry. Then I removed the # added for user pi. After that user pi was found again.

LeonardoR2 (author)2014-09-07

Typo after closing nano: "service" instead of "servce"

WouterD made it! (author)2014-09-07

For installing PHPMyAdmin you should follow this tutorial, much easier and it WORKS. Great tutorial though. http://www.raspipress.com/2012/09/tutorial-install...

tony.steinbock (author)2014-09-07

You can use your Banana Pi also as a Webserver / FTP Server:

http://www.bananapi-kaufen.de/ftp-server-installieren/

itsleif (author)2013-11-21

I followed the steps, all works fine, but now I cannot log in any more. After restart the pi user login does not work and the new password does not work either. I also tried the root user with both passwords the old and the new, nothing works.

No I cannot log in my pi any more :( I cannot change the password file either because my mac does not mount the linux partition...

how can login again?

MattC1 (author)itsleif2014-08-22

remove the # infront of pi:x;1000:1000:Raspberry Pi User,,,:home/pi:/bin/bash

About This Instructable

1,185,601views

1,119favorites

License:

Bio: Hi there, I'm an engineering apprentice with a passion for computing.
Add instructable to: