Create your own LAMP(Linux, Apache, MySQL, PHP) web server on a Raspberry Pi 4b

Genki Ganko
7 min readAug 7, 2023

--

Picture yourself wandering through the vast digital landscape of the internet, clicking through websites that effortlessly deliver the information you seek. Ever wondered what magic lies behind these virtual marvels? Enter the LAMP web server, a powerful combination of open-source software that fuels countless websites with its dynamic prowess. LAMP stands for Linux, Apache, MySQL, and PHP/Perl/Python, each playing a crucial role in this harmonious quartet. Linux, the sturdy operating system, provides a robust foundation, while Apache, the trusty web server, diligently handles the incoming requests, serving up web pages like a seasoned maestro. Together with MySQL, the database management system that securely stores and retrieves data, and PHP/Perl/Python, the versatile scripting languages that bring life to web applications, LAMP forms an unstoppable alliance. It’s a captivating symphony of technologies working hand in hand, shaping the internet as we know it.

In this article, you will be able to create your own LAMP Ubuntu web server within your reach, moreover, on a single board computer, Raspberry Pi 4b.

WHAT YOU NEED

  1. Raspberry Pi — Raspberry Pi 3b, 4b, Pico, 400 or whichever you have
  2. At least 32GB microSD card
  3. Raspberry Pi Power Supply (5V 2.5A)
  4. Ubuntu Server 22.04.2 LTS ISO (link for site below)

Creating Ubuntu image on your microSD card

  1. Download the Raspberry Pi Imager from here. Double click on the installer and install as suggested.

https://www.raspberrypi.com/software/

2. From the Ubuntu website, download the ISO/.xz file. Depending on your preferences, choose either 64bit or 32bit. I suggest you using 64bit architecture because it is backwards-compatible to 32bit software that you want to install.

3. Open up your Raspberry Pi Imager that you just installed.

+

4. Plug in your microSD card to your computer. You will need a microSD card reader or USB-microSD adapter for it. From the Raspberry Pi Imager GUI, choose the operating system, which is Ubuntu Server, and the storage, which will be your SD card. Navigate to ‘Other General-purpose OS’, and choose Ubuntu. If you want to manually choose the Ubuntu installer that you just downloaded, click on the ‘Use Custom’ menu below. After the operating system and storage has been chosen, click on the ‘Write’ and click OK to erase the card.

Note: You might need to reformat your SD card to FAT32. So before you do the steps above, please format your SD card first.

5. When installation is finished, insert your microSD card to your Raspberry Pi. It should be on the back on the computer board.

6. Now power up your Raspberry Pi by connecting the USB-C power supply and your wired network. Note that it will take long for it to initially boot up since it is currently installing after you put on the power. On my case, I directly connected my Raspberry Pi ethernet port to my TP-LINK Archer AX83 wireless router. There are a few ways to remote to your Raspberry Pi but the easiest would be to SSH to it.

Installing Apache

Apache2, often referred to simply as Apache, is a widely-used open-source web server software. It is the second major version of the Apache HTTP Server, developed and maintained by the Apache Software Foundation. Apache2 is known for its robustness, security, and flexibility, making it one of the most popular choices for hosting websites and serving web content on the internet.

  1. Since this is the first installation, you need to fully update your Linux systems. To do that, enter the command below:

sudo apt update && sudo apt upgrade -y

2. Next is to install Apache2. It is the most widely used web server software so in this case, we will use this one. Enter the command below to install the Apache2.

sudo apt install apache2 -y

3. To test your Apache2 installation, go to /var/ww/html directory and you will be able to see a file named index.html. At this point, you are now able to see the Apache web page from your local browser by typing in the IP address of the Raspberry Pi on your browser.

cd /var/www/html

ls -l

cat index.html

Installing PHP

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“.

  1. To install, enter the command below.

sudo apt install php -y

2. You can remove the index.html file and create a PHP file and name it index.php

sudo rm index.html

sudo nano index.php

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

<?php echo “This is a test PHP file. Hello World!”; ?>

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

5. Restart Apache2 service.

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

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

sudo rm index.php

Installing MySQL (MariaDB)

MySQL is an open-source relational database management system (RDBMS) that allows you to store, manage, and retrieve data efficiently. It is one of the most popular and widely used database systems in the world. Developed by MySQL AB, which was later acquired by Oracle Corporation, MySQL is renowned for its reliability, performance, and ease of use.

  1. Install the MySQL Server (MariaDB Server) and PHP-MySQL packages by entering the commands below.

sudo apt install mariadb-server php-mysql -y

sudo service apache2 restart

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

sudo mysql_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

3. When installation is done, you will see a messsage that says ‘Thanks for using MariaDB’.

Installing phpMyAdmin

phpMyAdmin is a free and open-source web application written in PHP that provides a graphical user interface (GUI) for managing and administering MySQL databases. It allows users to interact with MySQL databases through a web browser, making it easier to perform various database-related tasks without the need for complex command-line interactions.

  1. Run the command below to install the phpMyAdmin software.

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

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

sudo phpenmod mysqli

sudo service apache2 restart

3. When you go to your RPi IP address followed by /phpmyadmin (in my case http://192.168.0.86/phpmyadmin), you’ll probably see the “Not Found” error page in your browser. If this happens, you need to move your phpMyAdmin folder to /var/www/html folder. Do this by running the command below. Note that this will note move the folder, but creating a link from the original path.

sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

4. Now when you list the contents of that path, you should be able to see the file from your current directory.

ls /var/www/html

5. Reload your web page and you should be able to see the login interface of the phpMyAdmin. Enter username as ‘root’ and the password that you just created for the database.

NOTE!! When you experience a login error from the phpMyAdmin login, you will need to create a new user to login. Enter these commands below to create a new user and the new user will be ‘admin’.

sudo mysql — user=root — password

> create user admin@localhost identified by ‘your_password’;

> grant all privileges on *.* to admin@localhost;

> FLUSH PRIVILEGES;

> exit;

And that’s it! You have finally installed your LAMP web server in your hands. Enjoy doing your projects!!

You can get your cheap Raspberry Pi 4B here.

--

--

Genki Ganko

IT Infrastructure | Cybersecurity | Former Chef | Jesus Christ follower