Raspberry Pi,  Tutorials,  Website Development

Install LAMP and VSCODE on Raspberry Pi for Web Development

In this tutorial, we will learn how to install LAMP stack (Linux, Apache, Mysql, and PHP) and VS CODE in your Raspberry Pi. You can use this to serve your own web server using pi or you want to use Raspberry Pi as a portable web development machine if you do not have a computer with you. By the end of this tutorial, you will be able to run PHP codes and connect PHP with Mysql.

Install APACHE

First, you will need to install Apache. Open up your terminal and execute the following command.

After the installation, type the command below. You will see the list of file and file permissions

Open Chromium and type localhost. You should be able to see the Apache2 Debian default page.

This means that your Apache is working.

 

Install PHP

To install PHP , run the command below.

After the installation, make sure you are still in var/www/html.

Let’s delete the index.html file

and create a simple php file.

Insert the following code in your index.html file.

Open Chromium and type localhost. If you see the phpinfo page, it means you have successfully install PHP.

 

Install Mysql 

Now let’s install MySql server and PHP-MySql

After that, execute this command to proceed with the installation.

Enter current password for root (enter for none)Press Enter
Enter to Set root password?.Type Y
New password: ; Reenter PasswordType in your new Root password and do not forget it. *Important*
Remove anonymous users.Type Y
Disallow root login remotely.Type Y
Remove test database and access to it.Type Y
Reload privilege tables now.Type in Y

If you are done with the installation, you should be able to see the  “Thanks for using MariaDB!” message.

Now restart your Apache Server

 

Install PHPMyAdmin

PhpMyAdmin is a free software used to handle database administration or the web. It provides a convenient GUI for working with the MySQL database management system. To install, PHPMyAdmin, run the command below.

You will be asked to provide a password for PHPMyAdmin to register with the database server. Enter your desired password and reconfirm if asked.

Now let’s edit our apache2.conf

And insert the following line at the bottom.

Save the file and restart your Apache Server

Now visit localhost/phpmyadmin and you should be able to see the phpmyadmin login page.

 

Access Mysql Console and Create a User

Let’s access the Mysql Console and create a user. The recent update of Mysql doesn’t permit us to use the root account so we will need to add a new user.

Enter your root password created.

Create User

Execute the following commands.

Grant All Privileges

Now go back to “localhost/phpmyadmin” login page and login with your user/user credential.

 

Install VS CODE

Execute the command below to download and add the public gpg keys.

To start installing, execute the curl command below.

Now open up your Code-OSS by clicking the “Raspberry Pi icon -> programming -> Code-Oss”.

If you see a blank black bg after opening your Visual Studio Code, you may need to install an older version of code-oss.

Then hold the code-oss to stop it from updating.

If you need to unhold. (Execute only if you want to upgrade code-oss to a newer version)

Open your VSCode, go to “File -> Open Folder” and browse /var/www/html/

 

Adding Permission for VS Code

You will notice that you won’t be able to add / save a file in /var/www/html/ directory. To add permission execute the chmod command below.

*Important: Please do not run this on a production server as it defeats the purpose of adding security!

 

Testing out PHP and Mysql Connection

Go to PHPMYAdmin and create a new database “test” with the following code.

Go to SQL and Run the insert query below.

Now let’s edit our “index.php” file. Paste the following PDO codes.

Go to your browser and visit “localhost”. You should be able to see the JSON Array.

 

That’s all for this tutorial. Hope you like it. Do not forget to share this tutorial by clicking the social media buttons below 🙂

Spread the love

Leave a Reply

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