Some links in this post may be affiliate links. We may get paid if you buy something or take an action after clicking one of these, but without addictional costs for you compared to direct buying.
Raspberry pi LAMP server featured image

LAMP server on Raspberry PI

Check my RPI articles in Best Raspberry PI projects article or peppe8o.com home page. Or subscribe my newsletter (top right in this page) to be notified when new projects are available! Also interested to start 3D printing with a cheap budget? Visit my cheap 3D printers list
5
(1)

Raspberry PI boards can perform very well to provide complete web server functions with very small budgets. They also have very low power consumption, so many people can start a web page with affordable costs.

LAMP are servers composed by following elements (each one composing LAMP acronym):

  • Linux – Operating System – to manage hardware integration and general software operations
  • Apache – Web Server Application – to expose html pages
  • MySQL – Database – to store records and data which need to be managed with apposite structures
  • Php – Server-side Scripting – to create dynamic pages

Beside installing LAMP server to publish a website, you need to set also some networking configurations.

The very first thing to configure is assuring hat your Raspberry PI will acquire always the same IP address on every boot. This can be achieved by setting a static IP address on Raspberry PI and configuring your router to leave the same IP address associated to RPI Mac Address (this part depends on your router model).

If you want to publish your web page on internet, you also need configure your router port-forwarding. You must forward external ports 80 (for http) and/or 443 (for https) to Raspberry PI. You should also use a domain, being also able to start with a free No-IP domain (please refer to configure No-IP DUC guide for last part).

In this tutorial, I’m going to show you how to install a LAMP server in your Raspberry PI. I’m going to use a cheap Raspberry PI Zero W, but this guide will apply to all Raspberry PI boards.

What We Need

As usual, I suggest adding from now to your favourite ecommerce shopping chart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from shopping chart. So, hardware will be only:

Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon raspberry pi Zero W box
Amazon Micro SD box
Amazon Raspberry PI Power Supply box

Step-by-Step Procedure

Install Operating System – Linux

We’ll use a light OS without desktop environment (headless), so having more power for services instead of wasting CPU and RAM on not needed desktop gui. For this preparation part, please refer to install Raspberry PI OS Lite. If you want anymore a Desktop environment to use your RPI as Personal Computer together with webserver services, then you can install Raspberry PI OS Desktop, proceding with next steps from internal terminal.

Make your operating system up-to-date:

sudo apt update -y && sudo apt upgrade -y

Install Web Server – Apache

Apache is a fast HTTP server providing advanced functionalities to expose web pages. It is the most spread software used on web to publish pages. It is available from all package repositories, so making simple its installation. From terminal, issue following command:

sudo apt install apache2 -y

Once installation is finished, you can check that Apache is working by using your web browser with URL equal to “http://” followed by your Raspberry PI’s IP address. Following picture shows expected result (192.168.1.77 is my RPI IP address):

Apache debian default page

Install PhP

Also Php is so spread that it is available from all package repositories. To proceed with its installation, use following from terminal:

sudo apt install php -y

To check if installation finished correctly, you can issue following command from terminal (with result):

pi@raspberrypi:~ $ php -v
PHP 7.3.19-1~deb10u1 (cli) (built: Jul 5 2020 06:46:45) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.19, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.19-1~deb10u1, Copyright (c) 1999-2018, by Zend Technologies

You can also test php and get system info by creating a simple php page in your web server. From terminal, create test.php in /var/www/html/ folder with following command:

sudo nano /var/www/html/test.php 

Add following line:

<?php phpinfo(); ?>

Save and exit. Back to your browser, append “/test.php” to Raspberry PI’s IP address, getting following result:

php info page

Install Database – MariaDB instead of MySQL

From database side, I suggest to use MariaDB instead of MySQL. MariaDB is a fork of MySQL, resulting in a lighter software (which better fits Raspberry PI resources) and keeping same MySQL commands valid. We’ll install together with php connector, so that MariaDB and Php will be already able to communicate. From terminal:

sudo apt install mariadb-server php-mysql -y

Once finished, a common best practice for databases is securing it. MariaDB gives you a procedure to set main configurations. From terminal:

sudo mysql_secure_installation

Following questions will be asked (followed by my suggested answers):

  • Enter current password for root (enter for none):
    • root password in fresh installation is empty, so simply press ENTER key or use your root password
  • Set root password? [Y/n]
    • I suggest to answer Y (yes)
  • New password:
    • type your new root password
  • Re-enter new password:
    • type again your new root password
  • Remove anonymous users? [Y/n]
    • I suggest to answer Y (yes)
  • Disallow root login remotely? [Y/n]
    • I suggest to answer Y (yes)
  • Remove test database and access to it? [Y/n]
    • I suggest to answer Y (yes)
  • Reload privilege tables now? [Y/n]
    • Answer yes to make your answers effective

Your LAMP server is now ready!

Simplify Database management – Phpmyadmin

A useful tool to manage database in LAMP server is phpMyAdmin. It can be installed with following terminal command:

sudo apt install phpmyadmin -y

In phpmyadmin setup screens I suggest the following:

  • select apache (mandatory) with space and press Ok
  • select Yes to configure database for phpmyadmin with dbconfig-common
  • insert your favourite phpmyadmin password and press Ok
  • insert again your phpmyadmin password to confirm and press Ok

Grant phpmyadmin user DB privileges to manage DBs:

We’ll connect to MariaDB with root user (defult password is one before set) to grant permissions (remember to use semicolumns at the end of each command row as showed below):

sudo mysql -uroot -p
grant all privileges on *.* to 'phpmyadmin'@'localhost';
flush privileges;
quit

From here, use your web browser to reach phpMyAdmin login page by using Raspberry PI’s IP address followed by “/phpmyadmin/”:

phpmyadmin login page

Use “phpmyadmin” as username and the password you just set during phpmyadmin installation. You will reach phpMyAdmin home page:

phpmyadmin home page

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Comment

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy