Introduction: Turning Your Raspberry Pi Into a Personal Web Server

Picture of Turning Your Raspberry Pi Into a Personal Web Server

LAMP is an acronym that stands for LinuxApacheMySQLPHP and are components required to run a Dynamic HTML webpage.

This instructable will demonstrate how you can turn your Raspberry Pi into a personal web server. The steps to install all of the components are relatively straight forward. After your personal web server is complete, you can use it to host a custom HTML or PHP resume, or a personal landing page which is what I chose to do. 

Step 1: Materials

Picture of Materials

- Raspberry Pi

- SD memory card (4GB or larger)

- Power adapter compatible with the MicroB connector on the Raspberry pi

- Ethernet patch cable

- HDMI compatible monitor and HDMI cable

- USB Keyboard (no mouse is necessary, you can navigate the entire interface via keyboard. Arrow keys,  Enter, ESC, etc.)

Step 2: Preparation

Picture of Preparation

The Raspberry Pi always needs to boot off of an SD card loaded with an operating system (OS) disk image.

You may buy an SD card that has already been made bootable complete with the Raspberry Pi OS or you may buy a blank card and

There are many OS versions offered for the Raspberry Pi, however, in this example, I have chosen to use Raspbian “Wheezy”.

Download the OS disk image and write it to your memory card. To write the image I used Win32 Disk Imager. Once the image is written to the card, plug the card into the Raspberry Pi's SD card reader and power on the device. (Be sure you have all the proper I/O devices plugged in, i.e., keyboard, Ethernet cable,  HDMI monitor.)

Step 3: Initial Configuration

Picture of Initial Configuration

Keep in mind, if you mess up anything along the way, just reformat your card, reload the disk image and start again. No harm no foul.

Upon powering the Raspberry Pi with the SD card plugged in, you will see the system automatically listing information about the operating system and peripherals. You do not need to know what it all means for this project, but I absolutely wouldn't discourage you from reading it and learning a little bit about your Raspberry Pi for your own benefit!

After the boot information is complete, you will be prompted with the raspi-config window. Here you should make the following changes:

- Expand root partition so all space on the SD card is usable.
- Set timezone.
- Enable SSH server (found in the Advanced Options)
- Disable boot to desktop, we want to configure this program using only the terminal environment.
- Update your Raspberry Pi (found in the Advanced Options)
- Reboot your Raspberry Pi at this time.

Use the following code to reboot your Raspberry Pi:

sudo reboot

* I also do not recommend overclocking your Pi for this project, the default setting works perfectly and overclocking will reduce the overall lifetime of the hardware. In addition, to get to this window any time after the initial configuration, use the following command: sudo raspi-config

Step 4: SSH Set Up

Picture of SSH Set Up

Now you will be able to ssh into your Pi so you can work on it from another computer if you would like. Once the Raspberry Pi reboots, it will prompt you for login information. Where it says "raspberrypi login: " type the default username which is pi and hit enter. Where it says "password: " type the default password which is raspberry

*when typing the password, no characters will be displayed. Don't get freaked out and think your keyboard stopped working. Just type out the password as you normally would and hit enter when you are done. I promise it is receiving your input!

At this point, find the IP address of your raspberry pi with the following command: 

ifconfig

In the second line of the output you will see "inet addr: " followed by a number to the effect of 192.168.1.1 (yours will be slightly different, but you get the idea). Write this number down and bring up your SSH client. For this example I am using an application called Putty as my SSH client. Type the IP address that you wrote down from the ifconfig step into the text field that says "Host Name (or IP address)"

It will prompt you for login information, this will be the same as before. Username is pi and the password is raspberry

Once logged in, it is a good security practice to change the default password. Change your password with the following command:

sudo passwd pi

Run the following commands to ensure your OS is up to date (hit enter after each and wait until one runs to completion before starting another):

sudo apt-get update
sudo apt-get upgrade

Step 5: Installing Apache

Picture of Installing Apache

To install Apache and PHP, use the following command: 

sudo apt-get install apache2 php5 libapache2-mod-php5

You will be prompted if you would like to continue, typefor yes and hit enter to continue. This process may take a few minutes. 

If you get any errors, run the following commands: 

sudo groupadd www-data
sudo usermod -g www-data www-data


Restart Apache with the following command:

sudo service apache2 restart

Now go to your web browser of choice and type the Raspberry Pi's IP address into the URL bar. (if you cannot remember the IP address, use the ifconfig command from earlier in this tutorial. 

You should see a web page that says "It works!" like the picture above:

Step 6: Install MySQL

Picture of Install MySQL

Install MySQL with the following command:

sudo apt-get install mysql-server mysql-client php5-mysql

You will be asked if you want to continue, type y for yes and hit enter to continue. 

During the installation, you will be prompted to set a new password for the MySQL root user.  Type a password of your choice, confirm the password again to continue the installation. 

Step 7: Install FTP

Picture of Install FTP

Install FTP to allow transferring files to and from your Raspberry Pi.

First we have to take ownership of the web root. To do this, use the following command:

sudo chown -R pi /var/www

Next, install vsftpd with the following command: 

sudo apt-get install vsftpd

Now we can begin the configuration process. First have to edit the vsftpd.config file. To edit the file use the following command:

sudo nano /etc/vsftpd.conf

Find and edit the following lines as specified:

Change anonymous_enable=YES to anonymous_enable=NO,
Uncomment local_enable=YES
Uncomment write_enable=YES
then go to the bottom of the file and add force_dot_files=YES

*to uncomment, remove the pound sign (#) in front of the specified line

exit the editing interface by pressing ctrl+X, type y to confirm the changes and press enter to save the file path. 

Restart the FTP service with the following command:

sudo service vsftpd restart

Step 8: Finalization

Picture of Finalization

Change the root password of your Raspberry Pi with the following command:

sudo passwd root

Add the user root as a mod with the following command:

sudo usermod -L root

At this point, your server should be up and running and you should be able to transfer files through FTP by using your servers IP address and connecting using port 21.

I will go into detail about how I added an HTML resume to my Pi when I have another spare moment, but for now you all can at least have all the proper components installed on your machines to start exploring the possibilities on your own!


 

Comments

chase.parks94 (author)2017-10-01

When I get to the mySql part I keep getting this error

daliborc2 (author)2017-06-14

Great tutorial, worked great for me!

Richardpaul01 (author)2017-04-12

OK so far, but how can I store and play a video-clip on my new Pi server ?

JiaGUO made it! (author)2017-03-20

This is exactly what I am looking for. It works perfect for me. I also set up the port forwarding table on my router, so that I am able to access the web using the WAN ip address. By the way, it seems the default index.html was updated, as shown in my screenshot.

LucaA35 (author)2017-03-11

I have a strange problem. Apache and FTP works, I'm able to transfer
a file from another computer and I reach the home apache page via
browser BUT:

1- I put a php file inside www/html (root dir for
apache) and if I go to hostname/1.php it finds the file, but php doesn't
work and only shows a blank page. I tried moving the php file and it
gives me the correct error, so the file is found, but it doesn't work. I
have to say it's a simple php file, already checked in other
configuration.

2- if I make another dir in /var/www, let's say
"test", and copy inside the index.html file, trying to access via
browser the /hostname/test gives me the "not found" error, although the
directory and files are really there.

I tried access, for both problems, in remote and local. Same result.

Thank in advance if you can help.

LarsV22 (author)LucaA352017-03-15

Also did you install all the nececary php plugins?
a good tutorial for this is:

https://www.raspberrypi.org/learning/lamp-web-server-with-wordpress/worksheet/

LarsV22 (author)LucaA352017-03-15

Have you tried to restart apache2?

You can do that by typing this:

sudo service apache2 restart

GAllen1123 made it! (author)2017-03-12

Managed to get it to work! wasn't sure how the file transfer works but i just used file zilla.

XavierL34 (author)2016-12-25

I'm unable to access the index.html file I just replaced in the www folder as it keeps saying its forbidden. Anyone knows why? Thanks!

numberwhun (author)XavierL342017-01-08

I don't know what you have done for setup, but check:

- That the permissions are set correctly

- That /var/www is set as your DocumentRoot

Other than that we would have to know more about your setup to advise you.

KirkF (author)2015-05-31

My Pi runs one website but how do I get it to run others? I put similar Apache 2 code in separate files then in the same file and it won't take the other websites like my old WAMP server did running on a WinXP. Will running LAMP instead of Apache 2 fix this? Thanks.

HarveyF1 (author)KirkF2015-08-10

In the "/var/www" folder, simply create another directory.

' sudo mkdir /var/www/yoursitename '

Now, if your your pi's ip address was (example) 192.168.0.223, open

' http://192.168.0.223/yoursitename ' in your web browser.

if you get a http 403 forbidden error, run the following in SSH

' sudo chown -R pi /var/www/yoursitename '

that should now run two sites. No site limit.

I have two sites set up on my pi.

http://86.11.221.243:81 and http://86.11.221.243:81/exsite

LAMP stands for Linux, Apache, Mysql, PHP

methmuni (author)HarveyF12016-09-30

this didn't work for me it says "404 Not found" please help

ps. this is what i did

i made a folder inside the /var/www folder named "timedata"

and placed a html file named "main.html" but nothing worked

StephanW12 (author)methmuni2016-11-03

You are supposed to name the file index.html index.html or index.php if necessary to find sour site in the browser via "yoururl.abc/timedata". Otherwise you need to use "yoururl.abc/timedata/main.html" in your case.

BR

WingkanChu (author)2016-05-19

Hi! All,

After I clicked "yes" to the "continue prompt", I got a lot of 404 error, such as Erro http://mirrordirector.raspbian.rog/raspbian/ jessie/amin php5-.... How can I fix it? Needless to say, the installation of apache server was not successful. Thanks in advance to answer.

Regards,

Robert.

Rahulp29 (author)2016-02-08

trying

over_load made it! (author)2016-02-02

Wow. Tiny web server!

patrickbullard (author)2016-01-02

Thank you so much. This is the first step of a my first project. I got my Raspberry Pi New Years Eve in the mail. Today is my unbox and first turn on. I am going to wire up my new home heating system to my GPIO's, Write a web page to read and interpret the systems status and write an android app to communicate with the web page. If this all works, I will publish an Instructable on this.

nimrodlehavi (author)2015-12-28

thanks :) very cool

techspec (author)2015-12-13

Thanks for sharing! This was a breeze! If you have the time, perhaps how to set up an internal/home DNS server would be a great add on to this.

JonathanG57 (author)2015-11-29

How to restart web-server after having pi offline for a few weeks. Trying to finish a project for school. Any assistance greatly appreciated.

KalaiyarasanE (author)2015-07-30

I made it

JoeyL1 (author)2015-05-28

The default VSFTP mask should be uncommented and 002 or something like it. I spent an hour trying to figure out why my php pages weren't being displayed.

ratheeshbr (author)2014-10-09

I am a beginner on Raspberry Pi. When I tried to install apache2, its shown that, " Its the newest version. When I tried http://localhost/ on raspberry pi browser it works well. But I cannot connect to Rasppi from another PC using the IP address of the Raspbpi.

Can you please help? Is there anything error?

BlueRobot (author)ratheeshbr2015-05-17

I am not very experienced, but these are common errors you could check:

Is the Pi connected to your network?

Are you using the Ipv4 address? i.e. 192.168.1.11(yours will be different)

Did you update/upgrade before you installed Apache?

Thanks,

BlueRobot

That's because http://localhost points to the. well local host. So it resolves to whatever machine its accessed on. You'll need to find out the IP of the RPi and put that into your browser, and that should get you one it (of course you need to first have the RPi connected to a network as well as the computer you'll access it from)

ofey (author)ratheeshbr2014-10-25

Can you SSH into your pi from another pc?

RubbaBoy (author)2015-03-29

I was wondering, can you add a domain to this, more specifically one from Godaddy? This may be an obvious no, but i don't know anything being 13 xD

damavox. (author)RubbaBoy2015-05-12

Yes but you would have transfer the domain to your home IP address and then portforward.

jacobiscrazy (author)RubbaBoy2015-04-10

yes, whoever you get your domain with will come will do,e with some kind of domain name server that will Atleast allow you to do basics. You will need to set your Pi as a static ip on your local network, make sure your public ip is static, and allow a connection to your Pi through your router on external port 80. Ask or Google if you need any assistance.

Slapshot135 (author)2015-04-27

This probably sounds like a really simple and stupid question but, where do I put a file to upload to the internet?

damavox. (author)Slapshot1352015-05-12

In the www folder

jacobiscrazy (author)2015-04-10

Can anybody comment on how it holds with a bit of traffic?

The Expert Noob (author)2014-09-10

For Future Reference. This code allows one to Chroot or "jail" a person to their home directory keeping them from browsing the whole system.
----------------------------------------------------------------------------------------------
Create Group for sftp users:

sudo groupadd sftponly

----------------------------------------------

Add the following to the end of the /etc/ssh/sshd_config file (sudo nano):

Subsystem sftp internal-sftp

# This section must be placed at the very end of sshd_config

Match Group sftponly

ChrootDirectory %h

ForceCommand internal-sftp

AllowTcpForwarding no

-------------------------------------------------------

-------------Commands to create a new user-------------

----------------------(ex john.)-----------------------

---(replace 'john' with the user you want to create)---

-------------------------------------------------------

sudo mkdir /home/john

sudo useradd -d /home/john -M -N -g users john

sudo passwd john

[enter password]

sudo chown root:root /home/john

cd /home/john

sudo mkdir files

sudo chown john:users /home/john/files

sudo usermod john -g sftponly

sudo usermod john -s /bin/false

sudo service ssh restart

Brilliant. I couldn't figure out why my SFTP connection wasn't working for my custom user, this solved my problem! And helped me lock them into their own directory!

BONUS: To mount a device such as a USB stick to your home directory,
sudo mount -t ext4 /dev/sda1 /home/john/files

This assumes you have an ext4 formatted USB drive.

You can do this by:
sudo mkfs.ext4 /dev/sda1

Assuming the USB drive you want to format is sda1
It will also erase all files off the device
Ext4 will allow you to chown the drive to your user instead of root

(this also is my workaround for the whole kitkat SDcard issue)

terryo1 (author)2015-03-23

I'm using this tutorial to set up a server for my senior design project. I got all of the apache installs done and functioning. I can pull up the ip on my computer which i am using to share my wifi connection with the pi. When I attempt to pull up the same page on my phone, it times out.

Is there something I am missing? Do i need to set a static ip?

both my phone and computer are logged into the same wifi network at school

Mike Hewitt FB (author)2015-03-22

beliveau (author)2015-03-14

To upload an html file, you will need to use an ftp client. I like to use the FileZilla client on my Windows machine. You will then be able to use your pi credentials (not root) to login by ftp. Note that when you connect by ftp you will land on your home directory (/home/pi). To get the files that you transferred via tp published to your web server, you need to ssh into your raspi and then move or copy them over to your webserver root which is /var/www

twright23 (author)2015-01-13

Hi I've completed all of the steps and everything has worked out perfectly. If there is any way I can get more information on how to upload a HTML document to the server that would be great!

kingafro000 (author)2015-01-12

Nice! but its been one year since you made this but still no html.

agulesin (author)2015-01-04

Hi Mark,

I've had my Raspberry for a year or so but never got around to using it! Just trying your tutorial and run across a problem, I wonder if you (or anyone else!) can help!

I've downloaded the SD card image, it's "2014-12-24-wheezy-raspbian" and takes up 3.05Gb on my computer. But when I write it to the SD Card (with Disk Imager), it only takes up 55Mb of space - in fact the SD card's total size shows as 79Mb or something silly like that (it should be 4Gb).

Putting the SD into the Raspberry and booting gives a few lines of text which pass too quickly to read, then a four-colour screen pops up for a second or so. This repeats until I switch it off.

What have I done wrong? Any help greatly appreciated!

Abdullah Eyles,

Ankara, Turkey

agulesin (author)agulesin2015-01-05

OK - Found the answer, sharing for anyone interested:

"Under Windows, all you will be able to see is the /boot partition, which is in the MS-DOS VFAT format. Raspbian uses a different file system format for its system files."

http://raspberrypi.stackexchange.com/questions/432...

agulesin (author)agulesin2015-01-12

Me again: I've discovered the REAL reason for repeated re-booting, the cable between my USB power supply and the Raspberry was too thin - I learned that from the Raspberry Troubleshooting forum.

In short, some micro-USB data cables use thin wires which aren't thick enough to transmit the power the Pi requires; it starts up then the voltage is not enough so it switches off and tries again, ad infinitum. Using a dedicated micro-USB power supply solved this (as most mobile phone chargers are these days).

So far I've got to the stage of installing SQL - thanks again Mark!

lee.sugden.9 (author)2015-01-08

Great tutorial! thank you

ofey (author)2014-10-25

This is great thanks. How can I now create a database and set up WordPress? Thanks, Shane

eltel (author)2014-10-07

Excellent tutorial, thank you.

Dougless (author)2014-09-08

Really inspiring and interesting article! I used it for researching different possibilities before i made my own webserver using the Raspberry Pi, Model B.

Check out the site the Raspberry is hosting at the moment -http://www.kimnyegaardandreasen.dk

cnl2218 (author)2014-05-23

Thanks for the great tutorial! Got me setup quickly and correctly!

Drdray1 (author)2014-05-06

hi great tutorial! I am wondering how do you get to the part in step seven where you changes things from yes to no? I've already gotten to the black window with the options at the bottom

About This Instructable

401,232views

271favorites

License:

Add instructable to: