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.

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 phpmyadminOK
  • Configure database for phpmyadmin with dbconfig-commonYes
  • 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.

Source: https://randomnerdtutorials.com/raspberry-pi-apache-mysql-php-lamp-server/

 

Leave a Reply

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