Raspberry Pi: Install Apache + MySQL + PHP (LAMP Server)

In this guide, you’ll learn how to install a LAMP (Linux, Apache, MySQL, PHP) server on a Raspberry Pi. LAMP is a software bundle that is used for web development. The Raspberry Pi will have Raspbian OS installed and you’ll use phpMyAdmin to easily manage your database through a web interface.

Install on Raspberry Pi Apache + PHP + MySQL + phpMyAdmin LAMP Server

Prerequisites

Before continuing with this tutorial:

If you like home automation and you want to build a complete home automation system, I recommend downloading my home automation course.

After having your Raspberry Pi board prepared with Raspbian OS, you can continue with this tutorial.

You can either run the next commands on a Raspberry Pi set as a desktop computer or using an SSH connection.

Updating and Upgrading

Before starting the installation procedure, open a Terminal window and run the following commands to update your Pi:

pi@raspberrypi:~ $ sudo apt update && sudo apt upgrade -y

Install Apache2 on Raspberry Pi

Apache2 is the most widely used web server software. Briefly, a web server is the software that handles requests to access a web page. Then, depending on the page you have requested, the server will generate the document to serve you (.html, .php, etc).

Apache2 on Raspberry Pi LAMP Server overview

To install Apache2 on your Raspberry Pi, run the next command:

pi@raspberrypi:~ $ sudo apt install apache2 -y
Raspberry Pi Install Apache2 LAMP Server

That’s it! Apache is now installed. To test your installation, change to the /var/www/html directory and list the files:

pi@raspberrypi:~ $ cd /var/www/html
pi@raspberrypi:/var/www/html $ ls -al
index.html

You should have an index.html file in that folder. To open that page in your browser, you need to know the Raspberry Pi IP address. Use:

pi@raspberrypi:/var/www/html $ hostname -I
Raspberry Pi change directory RPi IP Address

In my case, the Raspberry Pi IP address is 192.168.1.86. If you open your RPi IP address in any browser in your local network, a similar web page should load (http://192.168.1.86):

Raspberry Pi Apache2 Installed

Install PHP on Raspberry Pi

PHP is a server side scripting language. PHP (Hypertext Preprocessor) is used to develop dynamic web applications. A PHP file contains <?php … ?> tags and ends with the extension “.php“.

To install PHP on Raspberry Pi, run:

pi@raspberrypi:/var/www/html $ sudo apt install php -y

You can remove the index.html and create a PHP script to test the installation:

pi@raspberrypi:/var/www/html $ sudo rm index.html
pi@raspberrypi:/var/www/html $ sudo nano index.php

In your index.php file add the following code to echo the “hello world” message:

<?php echo "hello world"; ?>
Raspberry Pi Create PHP Test File Hello World

To save your file: press Ctrl+X, followed by y, and press Enter to exit.

Finally, restart Apache2:

pi@raspberrypi:/var/www/html $ sudo service apache2 restart

To test if Apache2 is serving .php files, open the Raspberry Pi IP address and it should display the “hello world” message from the index.php script created earlier.

Raspberry Pi test PHP File Hello World message web browser

If everything is working, you can remove index.php file from the /var/www/html directory:

pi@raspberrypi:/var/www/html $ sudo rm index.php

Install MySQL (MariaDB Server) on Raspberry Pi

MySQL (often pronounced My SQL) is a popular open source relational database.

Install the MySQL Server (MariaDB Server) and PHP-MySQL packages by entering the following command:

pi@raspberrypi:/var/www/html $ sudo apt install mariadb-server php-mysql -y
pi@raspberrypi:/var/www/html $ sudo service apache2 restart

After installing MySQL (MariaDB Server), it’s recommend to run this command to secure your MySQL installation:

pi@raspberrypi:/var/www/html $ sudo mysql_secure_installation

This should appear in your Terminal window:

Raspberry Pi MySQL Database MariaDB Secure Installation
  • You will be asked Enter current password for root (type a secure password): press Enter
  • Type in Y and press Enter to Set root password
  • Type in a password at the New password: prompt, and press Enter. Important: remember this root password, as you will need it later
  • Type in Y to Remove anonymous users
  • Type in Y to Disallow root login remotely
  • Type in Y to Remove test database and access to it
  • Type in Y to Reload privilege tables now

When the installation is completed, you’ll see the message: “Thanks for using MariaDB!”.

Raspberry Pi MySQL Database-MariaDB Final Secure Installation

If you experience any error login into phpMyAdmin, you might need to create a new user to login. Those commands will create a new user with name (admin) and password (your_password).

pi@raspberrypi:/var/www/html $ sudo mysql --user=root --password
> create user admin@localhost identified by 'your_password';
> grant all privileges on *.* to admin@localhost;
> FLUSH PRIVILEGES;
> exit;

Install phpMyAdmin on Raspberry Pi

phpMyAdmin is a free software tool written in PHP, intended to handle the administration of MySQL using a web interface.

To install phpMyAdmin on a Raspberry Pi, type the following command into the terminal:

pi@raspberrypi:/var/www/html $ sudo apt install phpmyadmin -y

PHPMyAdmin installation program will ask you few questions. We’ll use the dbconfig-common.

  • Select Apache2 when prompted and press the Enter key
  • Configuring phpmyadmin? OK
  • Configure database for phpmyadmin with dbconfig-common? Yes
  • Type your password and press OK
Raspberry Pi install phpMyAdmin

Enable the PHP MySQLi extension and restart Apache2 for changes to take effect:

pi@raspberrypi:/var/www/html $ sudo phpenmod mysqli
pi@raspberrypi:/var/www/html $ sudo service apache2 restart

When you go to your RPi IP address followed by /phpmyadmin (in my case http://192.168.1.86/phpmyadmin), you’ll probably see the “Not Found” error page in your browser:

Failed to open phpMyAdmin Raspberry Pi

If that’s the case, you’ll have to move the phpmyadmin folder to /var/www/html, run the next command:

pi@raspberrypi:/var/www/html $ sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

Now, if you list the files, it should return the phpmyadmin folder:

pi@raspberrypi:/var/www/html $ ls
phpmyadmin
Raspberry Pi move phpMyAdmin to /var/www/html phpMyAdmin folder

Reload your web page (http://192.168.1.86/phpmyadmin), your should see the login page for phpMyAdmin web interface::

Raspberry Pi Open phpMyAdmin Login Page

Enter your defined username (it should be Username = root) and the password you defined during the installation.

Press the Go button to login. A new page loads:

Raspberry Pi Open phpMyAdmin Logged in Page

That’s it! Your Raspberry Pi board is prepared with a LAMP server: Apache2, MySQL, PHP. We’ve also decided to include phpMyAdmin in this installation for an easier database management through a web interface.

Optional Step (but recommended)

To manage your web pages, you should change the permissions for your /var/www/html/ folder. To do this, run the following commands:

pi@raspberrypi:~ $ ls -lh /var/www/
pi@raspberrypi:~ $ sudo chown -R pi:www-data /var/www/html/
pi@raspberrypi:~ $ sudo chmod -R 770 /var/www/html/
pi@raspberrypi:~ $ ls -lh /var/www/

After running these commands, you’ll see something as follows:

Raspberry-Pi change var www html folder

Wrapping Up

We hope you found this guide useful! Your Raspberry Pi has a LAMP server with phpMyAdmin that allows you to build interesting IoT projects like these:

Learn more about Home Automation with the Raspberry Pi: Build a Home Automation System for $100

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

48 thoughts on “Raspberry Pi: Install Apache + MySQL + PHP (LAMP Server)”

  1. Hi, all but completed this, however, I seem to have failed at the last hurdle. I cannot log in to phpMyAdmin with user Root, but I can log on as admin. However, having done so, I don’t have privileges to create a new database. Can you please advise…

    Reply
    • Andrew, had the same problem.
      Could you issue the following commnd in mysql:
      SELECT user,authentication_string,plugin,host FROM mysql.user;
      does that show an output in which there is an authentication plugin used for ‘root’?

      If so, do the following:
      ALTER USER root@localhost IDENTIFIED WITH mysql_native_password;
      SET PASSWORD=PASSWORD(‘yourpassword’);
      FLUSH PRIVILEGES;
      That allowed me to log in as root

      Reply
      • No, I don’t show a : “authentication plugin”, only “unix_socket”.

        #mysql
        MariaDB [(none)]> SELECT user,authentication_string,plugin,host FROM mysql.user;

        Output:

        | user | authentication_string | plugin | host |

        | root | | unix_socket | localhost |
        | phpmyadmin | | localhost |

        Reply
    • To solve the problem, that you can not connect with root at phpmyadmin try the following:

      $sudo mysql -u root

      [(none)]> use mysql;
      [mysql]> update user set plugin=” where User=’root’;
      [mysql]> flush privileges;
      [mysql]> \q

      Make shure after plugin= there are two seperated ‘ (apostophes) not one double-one!

      Reply
  2. By the way, looking at other guides, they suggest logging in with user phpmyadmin – whilst this login does work, it still doesn’t have privileges…

    Reply
    • If you guys are having the same problem as Andrew try running the following commands

      sudo mysql -p -u root
      GRANT ALL PRIVILEGES ON . TO ‘root’@localhost IDENTIFIED BY ‘password’;
      FLUSH PRIVILEGES;

      then try logging in again and it should work
      Best of luck

      Reply
  3. I can log in with user as phpmyadmin and password that i set previously.
    What do you meant by still doesn’t have privileges. How to check and confirm the privileges ?

    Reply
  4. I’ve now sorted this 🙂

    In answer to Ong – even though I could log into phpMyAdmin, it wouldn’t allow me to create a database – I basically had read only rights. There was a red warning cross symbol saying No Privileges.

    On installing onto a new Pi 3A+ I realised I’d not launched the mysql server using “sudo mysql” and then entered the following code:
    pi@raspberrypi:/var/www/html $ sudo mysql –user=root –password
    > create user andrew@localhost identified by ‘your_password’;
    > grant all privileges on *.* to andrew@localhost;
    > FLUSH PRIVILEGES;
    > exit;

    This then allowed me (logged in as andrew) to create the database and complete the project

    Reply
    • Hi Andrew, I think I have the same problem as you. can you help me plzz…even if I could login to phpMyAdmin, it wouldn’t allow me to create a database – I basically had read-only rights. There was a red warning cross symbol saying No Privileges.
      i don’t know what’s my problem even i follow all the steps … plzz i need a heelp and thanku

      Reply
  5. Can’t log in with either root or admin.
    With root:
    #1698 – Access denied for user ‘root’@’localhost’
    ! mysqli_real_connect(): (HY000/1698): Access denied for user ‘root’@’localhost’

    With admin:
    #1045 – Access denied for user ‘admin’@’localhost’ (using password: YES)
    !mysqli_real_connect(): (HY000/1045): Access denied for user ‘admin’@’localhost’ (using password: YES)

    Otherwise, fantastic job!!

    Reply
  6. Disclaimer:
    Totally new to installing and using db’s

    Darn, I was getting all the for mentioned errors. And I implemented several of the suggested solutions, but, I was still getting the errors. So, I decided to do the old reboot trick and it worked. Wish I could say what exactly fixed it.

    After rebooting, from the browser page, tried to login again and it failed when I used root@localhost and my password. I dropped the “@localhost”, which I had tried prior to the reboot, off the user name and only used “root” and the password. And up came the phpmyadmin home page. BTW, I never was able to login using “admin”

    one thing I did figure out was, to verify if you are using the correct root password, you can re-issue the cmd:
    “sudo mysql_secure_installation”.
    If you just hit ENTER, you will get an error if the root password has been set somehow.
    You can then type the password which you “think” is the correct one, if it is, the script continues and you can ^C’d out of it.
    then I rebooted, tried to log

    Reply
  7. Best thing about a raspberry pi is you can do this tutorial over and over again :). I almost have this memorized. Thank you Ed for your work you put into this. I have become a little big less stressed in the shell!

    Reply
  8. Very good tutorial. Successful outcome. Previously i have found this install a long process. But you accurately specified ability level in the prerequisites which I am now familiar with.

    In the I have fallen foul at the password phase when installing MYSQL. I have added extra notes to go with yours in my note book:
    You wrote: “at the New password: prompt, and press Enter.”
    I Wrote” (Important: Plan ahead, Practice write User name and password pair before entering, YOU ONLY ONE GO AT TYPING THIS PASSWORD – DOES NOT ASK FOR A RETYPE, SO BE SURE WHEN PRESSING KEYS)

    Reply
  9. I’m lost, tried everything I could think of, searched the web and still have an issue 🙁
    In my browser window I see this;
    Index of /
    [ICO] Name Last modified Size Description
    [ ] esp-data.php 2021-04-12 16:04 2.3K
    [DIR] phpmyadmin/ 2017-01-23 13:20 –
    [ ] post-esp-data.php 2021-04-11 14:11 2.1K
    Apache/2.4.38 (Raspbian) Server at 192.168.1.86 Port 80

    Click on phpmyadmin, enter username (admin) and password… loads with no problem

    Click on esp-data.php and get this: Connection has failed: Access denied for user ‘admin’@’localhost’ (using password: YES)

    Click on post-esp-data.php and get this: No data posted with HTTP POST.

    Running an ESP8266, serial window getting this: httpRequestData: api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=25.76&value2=56.35&value3=1014.36
    HTTP Response code: 200

    Where did the wheels come off the bus?

    Reply
  10. Everything worked exactly like stated in the tutorial, even the errors, until “Reload your web page (http://192.168._._/phpmyadmin), your should see the login page for phpMyAdmin web interface”. But at that point I didn’t get the phpMyAdmin web interface; instead yet another I got another 404 Not Found page.

    Something’s broken. I have no idea at the moment.

    Reply
  11. All was working well according to the tutorial, I could access .phpmyadmin and all, but when I followed the last step, “Optional Step (but recommended)”, the commands

    pi@raspberrypi:~ $ ls -lh /var/www/
    pi@raspberrypi:~ $ sudo chown -R pi:www-data /var/www/html/
    pi@raspberrypi:~ $ sudo chmod -R 770 /var/www/html/
    pi@raspberrypi:~ $ ls -lh /var/www/

    returned that pi:www-data wasn’t a valid user. I then tried to change the exact line to my username, caiomvital, and ran the “ls -lh /var/www/”. It returned me

    “total 4,0K
    drwxrwx— 2 caiomvital root 4,0K jan 15 12:22 html”

    after these commands, when I tried to access my ip “192..168.0.4” again, it returned me the 403 Forbidden,
    “You don’t have permission to access this resource”
    “Apache/2.4.38 (Raspbian) Server at 192.168.0.4 Port 80”

    How can I solve this?

    By the way, thank you very much for the tutorial!

    Reply
      • Replace pi in the command: sudo chown -R pi:www-data /var/www/html/ to the username you used when setting up your raspberry pi. pi is the default which is why it was used in this tutorial. If you have multiple users on the pi you will need to give the correct user permission.

        Reply
  12. I got some messages in phpmyadmin page
    Deprecation Notice in ./../../php/Twig/Loader/FilesystemLoader.php#40
    realpath(): Passing null to parameter #1 ($path) of type string is deprecated

    Backtrace

    FilesystemLoader.php#40: realpath(NULL)
    ./libraries/classes/Template.php#59: Twig\Loader\FilesystemLoader->__construct(string ‘/usr/share/phpmyadmin//templates/’)
    ./libraries/classes/Theme.php#103: PhpMyAdmin\Template->__construct()
    ./libraries/classes/Theme.php#174: PhpMyAdmin\Theme->__construct()
    ./libraries/classes/ThemeManager.php#306: PhpMyAdmin\Theme::load(
    string ‘./themes/pmahomme’,
    string ‘/usr/share/phpmyadmin/./themes/pmahomme/’,
    )
    ./libraries/classes/ThemeManager.php#89: PhpMyAdmin\ThemeManager->loadThemes()
    ./libraries/classes/ThemeManager.php#129: PhpMyAdmin\ThemeManager->__construct()
    ./libraries/classes/ThemeManager.php#397: PhpMyAdmin\ThemeManager::getInstance()
    ./libraries/common.inc.php#315: PhpMyAdmin\ThemeManager::initializeTheme()
    ./index.php#23: require_once(./libraries/common.inc.php)

    Reply
  13. If you cant login follow this steps, they worked for me:

    computingforgeeks.com/how-to-solve-error-1524-hy000-plugin-unix_socket-is-not-loaded-mysql-error-on-debian-ubuntu/

    Reply
  14. Forbidden
    You don’t have permission to access this resource.

    Apache/2.4.54 (Debian) Server at 192.168.43.219 Port 80
    Whenever i try to login into php my admin i got these in my browser

    Reply
  15. Hello, I’m looking to buy a raspberry pi pico with wireless wifi rp2040 microcontroller (https://www.aliexpress.com/item/1005002074614015.html?spm=a2g0o.productlist.0.0.1eb166bcgDKMac&algo_pvid=5a47d939-fb11-4df8-a871- 0ca51637d713&algo_exp_id=5a47d939-fb11-4df8-a871-0ca51637d713-22&pdp_ext_f=%7B%22sku_id%22%3A%2212000019018843727%22%7D&pdp_npi=2%40dis%21BRL%2185.58%2177.04%21%21%21%21%21%4021031a5516649859190373352e788a %2112000019018843727%21sea&curPageLogUid=h29Hmfx9iTGJ) to use in website projects, but I have verified that it has micro python support. Could you tell me if this module would be compatible?

    Thanks,

    Eliseu

    Reply
  16. Hi, instead of seeing the phpMyAdmin web interface, I get an “403 Forbidden” error. Does anyone of you have an idea on how to fix it or on what did I do wrong?
    Thanks!

    Reply
  17. Hi, instead of seeing the phpMyAdmin web interface, I get an “Forbidden
    You don’t have permission to access this resource.” error.
    Does anyone have an idea on how to fix it or on what did I do wrong?
    Thanks!

    Reply
  18. Hi I followed all of the above for a R pi 5 and all appeared to go well until I installed phpmyadmin.
    I followed the scripts as above but when I tried to access from my browser I just get a blank page with no comments, errors or phpmyadmin.
    Please help!
    Any debug suggestions?
    Can I uninstall phpmyadmin and try again?
    Best wishes, Ian

    Reply
  19. All sorted now.
    Reinstalled everything from scratch and got the phpmyadmin login page but still could not login.
    Hours of research and finally got the clues here.
    https://forums.raspberrypi.com/viewtopic.php?t=222100
    $ sudo mysql;
    MariaDB [mysql]> select * from mysql.user;
    No admin user!!
    MariaDB [mysql]> CREATE USER ‘admin’@’localhost’ IDENTIFIED BY ‘password’;
    MariaDB [mysql]> GRANT ALL PRIVILEGES ON . to ‘admin’@’localhost’ WITH GRANT OPTION;
    All sorted. Ian

    Reply
  20. Hey, almost everything worked perfectly for me, except that I get an error whenever I want to open the priveleges tab on my database.

    It says:
    #1267 – Illegal mix of collations (utf8mb4_general_ci,COERCIBLE) and (utf8mb4_unicode_ci,COERCIBLE) for operation ‘<>’

    And it would’nt go away :c

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.