Turning Your Raspberry Pi Into a Personal Web Server

426,361

273

73

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

- 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

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

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

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

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

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

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

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!


 

3 People Made This Project!

Recommendations

  • Plastics Contest

    Plastics Contest
  • Optics Contest

    Optics Contest
  • Make it Glow Contest 2018

    Make it Glow Contest 2018

73 Discussions

0
None
HermanoidTheGreatchase.parks94

Reply 4 months ago

I did as well. I discovered by running "apt list *php*" that php5 is obsolete, and the newest version (php7.0 at the time of writing) is the only one available to install. I just switched to the newer version, and all ran smoothly.

0
None
wadek6

Question 6 months ago

I'm fairly new to website design, I was wondering if you could access this pi server outside of your LAN. That is you can access it anywhere you have an internet connection, not necessarily from the same router

0
None
ChadGN

Question 8 months ago

I read through this article 2 times. I am feeling good about trying
this on my new Rasberry Pi3. First time every setting up a server and I
have a question about this.

1. would this be good for file sharing with VPN?

2. if so should I be aware of any potential issues?

Again I am a noob to networking so this so don't be afraid to layout as much information as

possible. I greatly appreciate any and all help.

0
None
ChadGN

Question 8 months ago

I read through this article 2 times. I am feeling good about trying this on my new Rasberry Pi3. First time every setting up a server and I have a question about this.

1. would this be good for file sharing with VPN?

2. if so should I be aware of any potential issues?

Again I am a noob to networking so this so don't be afraid to layout as much information as

possible. I greatly appreciate any and all help.

0
None
daliborc2

1 year ago

Great tutorial, worked great for me!

0
None
Richardpaul01

1 year ago

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

0
None
LucaA35

1 year ago

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.

2 replies
0
None
LarsV22LucaA35

Reply 1 year ago

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/

0
None
LarsV22LucaA35

Reply 1 year ago

Have you tried to restart apache2?

You can do that by typing this:

sudo service apache2 restart

0
None
XavierL34

1 year ago

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!

Screen Shot 2016-12-26 at 15.36.48.png
1 reply
0
None
numberwhunXavierL34

Reply 1 year ago

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.

0
None
KirkF

3 years ago on Introduction

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.

3 replies
0
None
HarveyF1KirkF

Reply 3 years ago on Introduction

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

0
None
methmuniHarveyF1

Reply 2 years ago

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

0
None
StephanW12methmuni

Reply 2 years ago

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

0
None
WingkanChu

2 years ago

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.

1 reply
0
None
ShaneE4

Reply 2 years ago

The 4 char indentation was removed. : )

0
None
patrickbullard

2 years ago

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.