Install Apache, MySQL, PHP (LAMP) on Raspberry Pi

To host any website ( WordPress Blog ) you require to Setup LAMP environment (Linux , Apache, MySQL and PHP). In previous article I have explained How to setup and install Raspbian on Raspberry Pi. So in this article you will learn how to install and setup Apache, MySQL and PHP on your Raspberry Pi.

Before starting installation process . You required to update your raspberry pi. To update raspberry pi run following command on raspberry pi terminal. This process may take some time to finish.

sudo apt update && sudo apt upgrade -y

Install Apache

Apache is most popular web-server on Linux. Now a days more than 70% of websites hosted on Apache. To install Apache run following command on raspberry pi terminal.

sudo apt install apache2 -y

After finish of above process verify your Apache installation by opening http://localhost/ on web browser. You will see default Apache page like this one

default apache page

Optionally you can enable rewrite module. Because this module is required for .htaccess URL rewriting. To enable this module run following command.

sudo a2enmod rewrite
sudo systemctl restart apache2
enable rewrite module apache

Install PHP

PHP is one of the most popular server side scripting language. More than 70% website use PHP. This WordPress blog is also created by PHP language. To install PHP on Apache web-server run this command

sudo apt install php libapache2-mod-php -y

After finishing of above process restart Apache server with following command

sudo systemctl restart apache2

To check whether PHP in working on your web-server. Create an PHP page in Apache document root (/var/www/html) with following command. This page will display PHP information on browser using phpinfo() function.

echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/test.php

Then open http://localhost/test.php in web browser. If you see PHP info page like this then you have successfully install PHP on your server

PHP information page

Install MySQL (Mariadb)

MySQL was created by a Swedish company, MySQL AB which currently own by Oracle Corporation. It is one of the most popular database server. Websites like Wikipedia and CMS like WordPress uses MySQL as database server. In recent version of Raspbian MySQL is replaced with MariaDB which is fork of the MySQL and 100% backward compatible with MySQL. To install MariaDB server and its PHP module run following command

sudo apt install mariadb-server mariadb-client php-mysql -y
sudo systemctl restart apache2

Now create admin user with password to manage database. Use this command ( replace Password with your password).

sudo mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'Password';"

Then login to Mysql console to check every thing goes well.

mysql -u admin -p

and enter your mysql password for admin user

MySQL (MariaDB) Login console

To create database run this command in MySQL console

CREATE DATABASE test;

Now check if PHP is able to connect with MySQL by creating mysql.php file in /var/www/html. To create mysql.php file use nano command

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

and put this code in file

<?php
$link = mysqli_connect("localhost", "admin", "ramlal", "test");
if($link)
{
    echo 'Connection to MySQL is successful';
}
else
{
    echo 'Unable to Connect with MySQL';
}
?>

Then press CTRL+O to save file and CTRL+X to exit from nano. Now open http://localhost/mysql.php in browser. If you see Connection to MySQL is successful then you have successfully install LAMP on Raspberry Pi.

Tagged : / /

Liked It? Get Free updates in your Email

Delivered by feedburner

2 thoughts on “Install Apache, MySQL, PHP (LAMP) on Raspberry Pi

  1. ” Now create admin user with password to manage database. Use this command ( replace Password with your password).

    1
    sudo mysql -e “GRANT ALL PRIVILEGES ON *.* TO ‘admin’@’localhost’ IDENTIFIED BY ‘Password’;”
    Then login to Mysql console to check every thing goes well.

    1
    mysql -u admin -p ”

    It wont accept my password

Leave a Reply

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