Monday, 3 June 2013

Setting up a personal web server on the Raspberry PI

Posted by Gary Newell  |  at  23:26 30 comments

Introduction

This week has been an awful week. I have basically had very limited access to the internet and so this has put me behind on the schedule that I had set for May.

I have therefore decided this week to finally type up the notes I had taken when setting up the Raspberry PI as a personal web server.

Why set up a Web Server on the Raspberry PI?

There are a few reasons why you might set up your Raspberry PI to run as a web server.

If you are a student learning to program then you might find it useful to run the Raspberry PI as a web server so that you can practice your PHP scripting or PERL scripting. PHP is good as it is a modern OO language that incorporates a lot of the principles of other programming languages

You can learn common techniques with PHP such as test driven development. There are also good coding frameworks available such as Codeigniter which will help you learn MVC. Skills learned in PHP can easily be transposed to other languages including C# and Java.

Other more hobbyist programmers might like a web server to run something like Joomla to create a home intranet site.

People who design Wordpress templates might like a personal web server to test out new templates. By using the Raspberry PI as the web server the designer can use other computers to make sure the layout looks good in Internet Explorer, Chrome and Firefox.

Assumptions

This guide assumes you are using the default Raspbian Wheezy operating system.

The web server that is to be used is Lighttpd.

PHP will be installed as the scripting language.

MySQL will be installed as the database server.

Get your IP Address

If you are working directly on the Raspberry PI then you can skip this part but if you are accessing via SSH obtain the IP Address of the Raspberry PI by typing the following into a terminal window on the Raspberry PI.

ifconfig
This will return an IP address in the form of 192.168.1.x where x is the 4th number. (For example 30).

If you follow this guide direct from the PI then you can get away with using localhost instead of the IP address when specified later on in the guide and you can skip the SSH step below.

SSH


If you plan to SSH onto the Raspberry PI from within Windows run Putty.exe (http://www.putty.org/) and type in the IP address into the Host Name box. Now click "Open".

If you plan to SSH onto the Raspberry PI from within Linux open a terminal window and type SSH 192.168.1.x (where x is the last number).

Whether you are running from within Windows or Linux you will be asked for the username and password. You should enter "pi" as the user and the password you set up for the "pi" user when you installed Raspbian.

You should now be logged in via SSH.

Update your repositories

First of all run an apt-get update to make sure everything you need is available to download.

sudo apt-get update

Installing Lighttpd



There are quite a few guides on the internet for showing how to set up a web server. For the Raspberry PI I chose to install Lighttpd as it is light in nature and won't use up too much of the Raspberry PI's resources.

To install Lighttpd run the following command
sudo apt-get install lighttpd

To test whether the web server is installed correctly type the following into the web browser address bar:

http://192.168.1.x
Replace x with the last number of your IP address. If you are running directly on the Raspberry PI then you can just enter the following:
http://localhost
You should see a placeholder page as shown above.

Installing PHP

To install PHP run the following command:

sudo apt-get install php5-cgi























Now you need to add a line to the php.ini file. To do this there is a file called php.ini in /etc/php5/cgi.

Type the following:
sudo nano /etc/php5/cgi/php.ini
Navigate to the bottom of the file and add the following line.
cgi.fix_pathinfo = 1
To save the file press CTRL and O on the keyboard and to exit nano press CTRL and X.

Finally to get fast CGI working type the following onto the command line:
sudo lighty-enable-mod fastcgi
(PLEASE NOTE THIS WAS MISSING FROM THE ORIGINAL VERSION) 

Now type the following:

sudo lighty-enable-mod fastcgi-php 
Now the web server needs to be restarted.
sudo /etc/init.d/lighttpd restart

Create a test PHP script

To test that PHP is installed correctly type the following into the terminal window:
sudo nano /var/www/phpinfo.php
Now within nano enter the following script:
<?php    phpinfo();?>
Press CTRL and O to save the file and CTRL and X to exit nano.


Within a web browser type the following into the address bar:

http://192.168.1.x/phpinfo.php
Replace x with the last number of your IP address. If you are running directly on the Raspberry PI then you can just enter the following:
http://localhost/phpinfo.php

Install MySQL

There are three applications that will be installed for MySQL:


  • MySQL-Server
  • MySQL-Client
  • PHPMyAdmin


MySQL-Server is the actual database server.
MySQL-Client enables you to interact with the server from the command line.
PHPMyAdmin enables you to interact with the server from within a web browser.


 To install all three applications at once type the following into the terminal window:
sudo apt-get install mysql-server mysql-client phpmyadmin

























During the install you will be asked to enter a password for the root user. Do not confuse this with the root user within Linux. This is the root password for the database server. Enter a strong password. Repeat the password when asked to do so.



During the installation of PHPMyAdmin you will be asked which web server is installed. Choose lighttpd.























A message will appear asking whether you want to create a dummy database. As the message states, if you know what you are going to be using the database server for or a database is already configured then you can answer no but if you are just experimenting then you can answer yes.

I recommend answering yes to this. It doesn't do any harm.























You will now be asked to enter the root password and repeat it. Enter the root password you created earlier. (remember this is the database root password, not the linux root password).

When the install has finished restart the server by typing the following into the terminal:

sudo /etc/init.d/lighttpd restart

To test that the database server has been installed correctly open a browser window type the following into the address bar:


http://192.168.1.x/phpmyadmin
Replace x with the last number of your IP address. If you are running directly on the Raspberry PI then you can just enter the following:
http://localhost/phpmyadmin



Enter the username and password to log in. 

Summary

This tutorial has shown how to set up the basic web server with PHP and MySQL

You might now want to try installing Wordpress, Joomla, Codeigniter and Blueprint.

I will be following up this article with details of how to install the aforementioned items later this week. I will also show you how to access the web pages externally and how to set up a host name to replace the IP address.

If you have any problems using this guide or you have tips for other users feel free to add them to the comments section.

Thankyou for reading.


About the Author

Gary Newell started the Everyday Linux User blog in 2010 and has written reviews on dozens of different Linux based operating systems. He has also written a number of tutorials.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

Related posts

30 comments:

  1. When I try the go to 192.168.1.x I just get a 403 - Forbidden error.
    I have checked through all the commands and I can't find where I might have gone wrong.
    Has anyone else had this problem and an suggestions on what I should do?

    ReplyDelete
  2. Which step in the tutorial is giving you the 403 error? Is it when trying to SSH onto the PI or when trying to view a page in a browser?

    If you are logged directly onto the PI (ie not using SSH) then replace the 192.168.1.x with localhost.

    If you are using SSH then the address you put in the browser is the same address you used to SSH onto the PI. (i.e 192.168.1.30, replace 30 with the number you get from running IFConfig)


    ReplyDelete
    Replies
    1. Sorry, I didn't mention where I had got up to.
      It is when I try to go to 192.168.1.x/phpinfo.php
      I think it might be to do with how I edited php.ini but I have tried a few different ways but I still didn't work.

      Delete
    2. We can take this offline if you like and I'll see if I can work out why it isn't working for you. Click the email link at the top of this page and send me an email and I will reply .

      Delete
    3. I have updated the guide. See the highlighted part above

      Delete
    4. Thank you, it now works.

      Delete
    5. Also I then had some problems with phpmyadmin but symlinking it with /var/www gets it working, use
      sudo ln -s /usr/share/phpmyadmin/ /var/www/phpmyadmin
      And then restart lighttpd and mysql.

      Delete
    6. YES! That link is needed (evidently)! Now my install works.

      Gary, please add to the tutorial!

      Thanks. BTW, excellent tutorial, by far the best I have found for the web server install!

      Delete
  3. hi! very nice article! I was doing exactly that half a week ago and happen to follow your guide without knowing. Would you help me set up an public_html folder inside /home/pi/ and some kind of free Cpanel.

    no tutorial explain it easily. I'm still new to Linux. I love tutorial witch explain what is going on!

    Thx a lot again!
    10/10 ;)

    ReplyDelete
  4. Hi,

    Hope you might be able to help. I've followed your instructions to the letter but when installing the MySQL server I get about half the way through and it throws up this error-

    [FAIL] /etc/init.d/mysql: ERROR: The partition with /var/lib/mysql is too full!

    Any ideas how to fix this problem? Once it's gone through the install process it comes up with this -

    An error occurred while installing the database:

    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

    Any help would be appreciated! Cheers

    ReplyDelete
  5. Hello from Paris Gary,
    as an absolute beginner, thank you very much for this step-by-step tutorial.
    I do not know where I got it wrong but I'm having an error message while configuring phpmyadmin, saying " ERROR 1045 (2800) : Access denied for user 'root'@'localhost' (using password: YES ) ".
    I tried " retry " several times, then without knowing how, just came back to the Terminal window :/
    Now I don't even know how to get back to the configuration.
    Thank you for your help and time. Cheers.

    ReplyDelete
  6. How to setup a second website?

    ReplyDelete
  7. This might work for you http://www.cyberciti.biz/tips/howto-lighttpd-web-server-setting-up-virtual-hosting.html

    ReplyDelete
  8. Hi. Great tutorial!

    Everything works, except phpmyadmin. If I enter "http://localhost/phpmyadmin" in my browser, I get the error: 404 - Not Found.

    I tried it, using the IP address, on my PC and it gives the same error. If I enter "phpinfo.php" instead of "phpmyadmin" it displays the page that you indicated.

    Any help would be appreciated!

    ReplyDelete
    Replies
    1. See if this helps http://redmine.lighttpd.net/boards/2/topics/4310

      Delete
  9. Okay, so I've been through this and everything seems to be set up properly. How do I then connect to it to put on the website I want to test? I'm not sure where to go after this...

    ReplyDelete
    Replies
    1. You need to add your web pages to the folder /var/www. It is likely that you won't have permissions to add files to that folder. You can get around this by opening a terminal as root or adding yourself to the group that does have permissions. (I think it is www-data)

      Delete
  10. Hello,

    I would like to monitor a sensor and display and store the data in real time through a web server on the pi. I have some ideas but I'd like to draw upon the collected wisdom in the pi community.
    Step1.
    1 have a php program that gets the data from a sensor
    2 continuously add the data to a file/database
    3 display the graph on a web page

    Step 2 is a database the best thing here or can I just output to a file. If a database is best then which one, is MySQL overkill?

    Step 3 how do I do this most efficiently?

    Any advice before I embark on this would be much appreciated.

    Thank you,

    Sudhir

    ReplyDelete
  11. Hello Gary. As I mentioned in a reply below, congrats for a well done, accurate and professional tutorial. I finally have my web server working now thanks to your work (and have quickly made a copy of my SD!)

    Now to finish up so I can actually start developing my new website. I need your help getting Wordpress installed now, as I am sure many other readers also do. I have chased the wrong rabbit again and again as I've tried various installation tutorials and it has further aged me. But alas, finally I discover you (a highly competent rabbit that can actually run in a straight line!).

    Pleeze provide us with the Wordpress install (or at least refer us to a tutorial which works). Best regards and thanks much in advance!

    ReplyDelete
    Replies
    1. Oops, forgot to check the 'Notify me' box! Hope to hear from you soon!

      Delete
    2. Hi there. I have never installed Wordpress before but I will give it a go. It will have to wait a few days though as I need to reinstall Raspbian on my Raspberry PI. I have been messing around with it so much it is virtually trashed. That is the thing I love about the PI. You can really hammer it and when you are finished refresh and start again.

      Delete
    3. I have got it on the radar and I have fixed my Raspberry PI so that it is running as a web server again. Just got to get around to doing the Wordpress bit.

      Delete
  12. Where comes the username from? During install I never got to choose a username, only password. I followed this guide to install everything but when i try to start phpmyadmin, http://123.123.1.12/phpmyadmin I get a 404 - Not Found.

    ReplyDelete
    Replies
    1. This might help with your phpmyadmin error http://redmine.lighttpd.net/boards/2/topics/4310

      Delete
    2. Thanks for your reply, I managed to get it to work by removing everything and reinstall.

      Delete
  13. 403 on lighttpd problem.
    I put #sudo lighttpd-enable-mod fastcgi fastcgi-php :: It's ok
    (Only #sudo lighttpd-enable-mod fastcgi ;; Not ok

    ReplyDelete
  14. This is very nice and cool post.I was waiting for this type article and I have gained some useful information from this site. Thanks for sharing this information. Keep blogging.
    Wordpress Website Development

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete

Feel free to comment on any of the blog posts. Please try to be constructive.

Offensive messages will be removed as will blatant adverts for misleading products and sites.

Thanks for visiting my blog

    Popular Posts

    Total Pageviews

    Subscribe

    Enter your email address:

    Delivered by FeedBurner

    Popular This Month

    What are other people buying?

    Zorin OS Buy
    PCLinuxOS Buy
    Lubuntu Buy
    Linux Mint Buy
    Simplicity Linux Buy
    Kubuntu Buy
    Kali Buy

    Followers

    Feedburner Followers

Blogger templates. Proudly Powered by Blogger.
back to top Google