Tutorial – Install Apache, PHP and MySQL on Raspberry Pi

The fundamental services required to turn your Raspberry Pi into a web server consist of Apache (the web server itself), PHP (scripting language) and MySQL (database server). When installed on a Linux based system, the collective term for these is LAMP.

There are other secondary services which aren’t quite as important at this stage to provide FTP and outbound mail, but that’ll be covered in a separate tutorial.

First things first, I’m assuming you’ve got Raspbian installed and followed my previous tutorial – Prepare your Raspberry Pi to become a web server.

Step 1 – Make sure you’re up-to-date

Ensure you’re running the latest system software. To check for updates and install them enter the following command:

sudo apt-get update

Step 2 – Change your hostname

Just to miminise the chances of any hickups we’re going to change the hostname from the default ‘raspberrypi’. From the terminal, enter:

sudo nano /etc/hostname

Nano editor will launch. Replace ‘raspberrypi’ with the domain from which your Raspberry Pi will run from. It’s not essential to change the hostname, but it is something I have done.

Once you’ve changed the hostname press CTRL + X to exit nano, and save when prompted. Now restart your Raspberry Pi to take to the new hostname:

sudo reboot

Once restarted, find out the FQDN to ensure your RPi has the correct hostname. A FQDN is a Fully Qualified Domain Name. To do this enter the following command once your RPi has restarted and you’ve logged back in with SSH:

hostname --fqd

If the hostname you entered is returned, congratulations! Go stick the kettle on and have a brew before moving onto the next part.

Step 3 – Install Apache

Here’s where the fun begins. We’re going to start by installing Apache and some other packages. To do this its begin with entering:

sudo bash

This means we’re not having to type sudo each time we run a command. When you’ve done this, enter the following:

apt-get install apache2 apache2-doc apache2-utils

This shouldn’t take long. Once we’ve done that we’re going to install a few support packages including PHP. Once complete, enter the following command:

apt-get install libapache2-mod-php5 php5 php-pear php5-xcache

This too shouldn’t take too long. Follow up with installing the support package for database connectivity:

apt-get install php5-mysql

Now we’re going to install MySQL server. Do this by entering the following command:

apt-get install mysql-server mysql-client

As part of the installation, you’ll be asked to set a root password. Enter a password and then confirm it when prompted in the blue screen.

That’s pretty much it! Reboot your device, you’re ready to start hosting from your Raspberry Pi! In the next few tutorials, I’ll guide you through installing PhpMyAdmin, FTP server and Postfix. Feel free to leave your comments below.


Web Developer living in Manchester, working for Studio Skylab (http://www.studioskylab.com). Views and thoughts are my own.

You may also like...

65 Responses

  1. Gio says:

    thx for tutorial

  2. Joe says:

    Straight forward tutorial. tnx.

  3. Dave Nash says:

    There’s a slight typo in the hostname section: It should read “hostname –fqdn”.

    Cheers for the tutorial, keep up the good work!

  4. Dave Nash says:

    Oops: *–fqdn

  5. shio says:

    hi I have problem with MySql intallation ,when i install it i get this erorr . Anyone knows how to resolve this problem

    Unable to set password for the MySQL “root” user

    │ An error occurred while setting the password for the MySQL
    │ administrative user. This may have happened because the account already
    │ has a password, or because of a communication problem with the MySQL
    │ server.

    │ You should check the account’s password after the package installation.

    │ Please read the /usr/share/doc/mysql-server-5.5/README.Debian file for
    │ more information.

  6. Dave B says:

    Good tutorial, Thanks. I found that I also had to edit /etc/hosts when I changed my hostname otherwise hostname -f returned: hostname: Name or service not known.

  7. Mihai Beffa says:

    Trying to follow your instructions. The first stumbling block is apache. Command
    apt-get install apache2 apache2-doc apache2-utils
    You might want to run ‘apt-get -f install’ to correct these:
    and here I get a bunch of unmet dependencies.
    “apt-get -f install” seems to be running clean, no error messages, but it does not solve the problem.

  8. Ken says:

    I get an error after installing MySQL and the server tries to start. When I try to run mysqld manually, I get:
    InnoDB: mmap(135987200 bytes) failed; errno 12
    130322 14:10:39 InnoDB: Completed initialization of buffer pool
    130322 14:10:39 InnoDB: Fatal error: cannot allocate memory for the buffer pool
    130322 14:10:39 [ERROR] Plugin ‘InnoDB’ init function returned error.
    130322 14:10:39 [ERROR] Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed.
    130322 14:10:39 [ERROR] Unknown/unsupported storage engine: InnoDB
    130322 14:10:39 [ERROR] Aborting

    • Ken says:

      InnoDB buffer pool size was 128M, which I though would be ok, since the pi runs with 512M, but I shrunk it down to 64M and mysqld ran fine:

      mysqld –innodb-buffer-pool-size=67108864

  9. Alexander says:

    Thanks for the great tutorial. i’ve got my Raspberry up and running now as a LAMP server 🙂

  10. David R says:

    When will you be uploading your ftp and WordPress install tutorial?

    I’ve installed vsftpd on my Pi but can’t seem to find the var/www on the pi.

    • Ste W says:

      Hi there. Sorry for the late reply. I’m looking at this at the moment, I’m probably going to run a tutorial on installing WordPress without FTP first, followed by a FTP installation tutorial. Try ‘/var/www’ instead of just ‘var/www’, that way it will look for the directory from the root.

  11. Paul Roberts says:

    Thank you so much for your help and easy to follow tutorials. You are a star.

    When I restarted apache I got a error – Could not reliably determine the server’s fully qualified domain name, using 192.xxx.x.xx for ServerName

    This didn’t stop apache from working but it did bug me and I thought it might lead to problems later. I found the solution here http://tinyurl.com/3umfdat

    Simple 2 steps

    1. Edit apache2.conf to include “Include httpd.conf” at the end of the file

    sudo nano /etc/apache2/apache2.conf

    add :

    #Include the httpd.conf
    Include httpd.conf

    and then save the file

    2. Edit httpd.conf (this may be blank by default) to specify localhost as the ServerName

    sudo nano /etc/apache2/httpd.conf

    add :
    ServerName localhost

    and then save the file

    When then restarting apache the error is no longer returned and all looks healthy!

    sudo /etc/init.d/apache2 restart

    Thanks again.

  12. Wolf says:

    Thx. Short but precise.

  13. kernalzero says:

    Thought I would share this. If you add the following you can enable SSH2 which is more secure than FTP if you plan on running a wordpress site later on with your pi. FTP is so 1980s.

    apt-get install libssh2-php
    /etc/init.d/apache2 restart

    source: http://snowulf.com/2010/06/29/wordpress-enabling-sshsftp-updates/

    Looking forward to your writeup on installing wordpress!

  14. I just like the helpful info you supply on your articles. I will bookmark your blog and check once more here frequently. I am quite certain I will be told lots of new stuff proper here! Good luck for the following!

  15. Alf says:

    I wish to place the images displayed in my web server, doghouse.dyndns-web.com/views on a flash drive at /mnt/2GB_USB_flash/images/ rather than on /var/www/views. This to limit multiple writes to the SD card.
    Please tell me if this can be done and if so how is it achieve ?

  16. Paul says:

    Working my way through this and just noticing some things…

    Step one: sudo apt-get update will only check for updates but not install them as your directions state…it should be:
    [code] sudo apt-get update && sudo apt-get dist-upgrade[/code]

    Step three: you open a new bash shell, but raspberry pi defaults you to bash, you can verify what shell you are in by typing:
    [code] ps -ef | grep $$ | grep -v ps | grep -v grep [/code]
    (the grep -v portions will remove erroneous entries)

    Other than that good stuff and thanks for helping folks out!

  17. Paul says:

    In regards to my previous post I just noticed on one of your other tutorials you stated you use sudo bash to access root permissions, an alternative would be sudo -i, it does the same thing with less typing.

  18. Mike says:

    I followed your guide, but php files doesnt work. Just show blank pages. But phpmyadmin works great.

    Any ideas?

  19. trent says:


    I completed the hostname step, but in chrome I cannot load my web server using my hostname. I can only access it by its ip.

    Any reason why?

    • Kakahuete says:

      In order to use the hostname you need a DNS on your LAN. If not the hostname will only be usable from the Raspberry.
      If you like, you can edit the hosts file in all the computers of your LAN to use that name instead of the IP adress.

  20. Michael McShane says:

    Nice work. Keep up the good efforts.

  21. John says:

    Thanks, very useful

    (hostname –fqd works like hostname –fqdn)

  22. Kakahuete says:

    Instead of using `sudo bash` which runs another bash proccess, you can simply run `sudo -s` which logs in with root permissions.
    BTW, great tutorial!

  23. works like a charm! thanxAlot!

  24. Hayden says:

    Very handy tutorial thanks!
    I too am a web developer, lived in North Wales (Llandudno) and moved to Cheshire (Chester)

  25. johngriswold says:

    I too got stung by the hostname & hosts issue, but reading the comments fixed all that. Thanks for a great tutorial.. Now on to phpmyadmin!

  26. Chaz Worm says:

    thx for the tutorial. spot on and to the point. i’m migrating an old linux box web server to a pi i have thumbtacked to the wall beside my bosses head. this will be an interesting project.

  27. rpdesign2013 says:

    hi there great tutorial but i have a simple problem. if i type in i’ll get the welcome index page all good but when i type in mysite.local [not the real name used here for descriptive purpose] i get the search engine instead of the index page i want. I have jumped ahead and have intst installed phpmyadmin and again works with but not with mysite.local/phpmyadmin.

  28. Robert Petet says:

    I have answered my own question in that for the rasberrys browser you have to type http:// in front of the mysite.local address. Yes i set up virtual hosts using the apache/ports file 🙂

  29. Samuel says:

    Thank for the good tutorial, I did everything that’s in it, but still the PHP commands are not functioning.

    Garage Door

    the PY file is working fine when operating it manually, sudo python garagedoor2.py
    but not when its inserted in the php section with its path.

  30. Joe says:

    Any ideas why after installing Apache 2.2 and doing a restart of the Raspi 2 Model B, the network is no longer connect. Found out that I have to run dhcpcd -f –configure so the network connection comes back up? Do not know why the install breaks the network connections. Idea’s appreciated. thanks.

  31. localse3 says:

    how do we update apache, php and mysql? the version are slightly out dated.

  32. sean says:

    When i attempt to run get-apt install apache2, i receive a long string of errors referencing the raspbian mirrordirector site. Any idea what i may be doing wrong? I would really like to build a bare-bones version of my website and host on my pi for in-home testing etc.


    • Ste W says:

      It sounds like a connection issue, are you able to ping google.com from your raspberry pi? If that doesn’t work I’d say there’s an issue with it seeing the outside world

  33. Bart says:

    Where can I get a decent LAMP installation? I’m setting up my raspberry pi2 offline, so I can’t download anything to the raspberry pi2.

  34. Bart says:

    Where can I get a offline installer LAMP for my raspberry?

  35. Bart says:

    Where can I get a lamp installer for my Raspberry? I’m working offline so I need to install it via an USB stick

  36. Sascha says:

    Thank you for this great tutorial. It worked perfectly.

  37. Matias Miano says:

    Hi, one simple question… After doing all the steps, where should I host a web site?

  1. 6th April 2013

    […] You could make your own $35 Apache web server. Another set of instructions is here. […]

  2. 10th April 2013

    […] your Raspberry Pi, and prepare it ready to become a live functioning web server. Be sure to follow Installing Apache, PHP and MySQL on your Raspberry Pi once you’re […]

  3. 16th May 2013

    […] In this tutorial, I’m going to talk you through installing PhpMyAdmin on your Raspberry Pi powered web server. I’m assuming you’ve got Raspbian installed, and you’ve followed my tutorial: install Apache, PHP and MySQL on Raspberry Pi. […]

  4. 29th June 2013

    […] There’s no point in me going step by step on the Install Apache, PHP and MySQL as this guy https://twitter.com/ste_tweets does it perfectly here http://www.raspipress.com/2012/09/tutorial-install-apache-php-and-mysql-on-raspberry-pi/ […]

  5. 11th October 2013

    […] doncs he agafat una Raspbian, hi he instal·lat Apache2, PHP5 i MySQL i he col·locat una mediawiki.  Ara l’estic omplint d’informació referent a projectes […]

  6. 27th January 2014

    […] getting all of WordPress’ dependencies set up I followed Dingleberry Pi’s great set of instructions (though these are Mac OS-oriented). Other useful links are below, relevant to configuring virtual […]

  7. 21st June 2014
  8. 4th July 2014
  9. 1st August 2014
  10. 18th January 2015

    […] your Raspberry Pi, and prepare it ready to become a live functioning web server. Be sure to follow Installing Apache, PHP and MySQL on your Raspberry Pi once you’re […]

  11. 25th March 2015

    […] Tutorial – Install Apache, PHP and MySQL on Raspberry Pi […]

  12. 21st May 2015

    […] When prompted, choose Yes. Next you’ll be asked for an administrative password, this is the root password that was set during the MySQL installation in the previous tutorial. […]

  13. 29th August 2015

    […] to Apache, installing it is relatively easy, you just […]

  14. 29th January 2016

    […] will find on the raspipress.com full details about installation and advanced Raspberry apache […]

  15. 27th May 2016

    […] MIRRORED Source: https://www.stewright.me/2012/09/tutorial-install-apache-php-and-mysql-on-raspberry-pi/ […]

  16. 19th September 2016

    […] Tutorial – Install Apache, PHP and … – The fundamental services required to turn your Raspberry Pi into a web server consist of Apache (the web server itself), PHP (scripting language) and MySQL … […]

Leave a Reply