The Raspberry Pi LAMP

You clicked on this post and came to this page because you thought of a lamp that actually lights up made from a raspberry pi. Sure, a Raspberry Pi home lighting system sounds fun and is on my list of projects to make, that’s not what we mean by LAMP. What we mean by LAMP is a Linux Apache MySQL PHP server. We are going to teach/tell you how to make a Raspberry Pi web server that is fully functional.

Having your own website is every technology enthusiast’s goal, whether it be for learning how to work in HTML, CSS, PHP; or to have a little project website as a hobby. Most people get stumped when it comes to affording a servers hosting price from a hosting company, or they just don’t want their work being hosted on aWhy third party server. a Raspberry Pi? Well, it’s a small 30-50 dollar, credit card sized, computer that runs ARM Linux. The price isn’t the only reason either, what if you are thinking about moving from Windows to Linux? Then not only are you going to have your own web server, but you’ll have a Linux computer off to the side that you can use to learn Linux and get a feel for it.

Enough rambling, lets get onto the project shall we?

What you will need

  • Raspberry Pi 3
  • Ethernet Cable
  • 8GB Micro SD Card
  • Internet Connection
  • A separate computer or laptop
  • NOOBS

The Project

First you want to unzip the NOOBS zip file and put it on your Micro SD Card. When you put the card into the Raspberry Pi and turn it on, you’ll see a screen and need to connect to your wifi (if you used NOOBS lite) and select the Raspbian image to install. Once installed you’ll want to go the the menu and start the Raspi Config program and make sure you have SSH turned on and autologin turned off, then run sudo apt-get update and sudo apt-get dist-upgrade. Once that is done you will want to run passwd and change the pi password and do the same for root, this is to better secure your Raspberry Pi, in the event that you port forward to allow the public to access your website.  Just remember to open a terminal and run ifconfig to get the local IP of your Raspberry Pi, mine is 192.168.0.50.

 

In this post I am going to use the default password of raspberry to simplify things. On a computer you will need to download PuTTY if on windows for ssh, if you are on Linux, you should be fine. What you want to do in a Linux terminal is ‘ssh pi@192.168.0.50’ and log in, if you are using PuTTY you will just have to enter the IP address and leave the port blank or use port 22.

Apache

Now that you have a shell or at least should anyways, you will need to run sudo apt-get install apache2, to get apache2 installed.

By default, Apache puts a test HTML file in the web folder. This default web page is served when you browse to http://localhost/ on the Pi itself, or http://192.168.0.50(whatever the Pi’s IP address is) from another computer on the network. To edit this page just go to /var/www/html/index.html.

PHP

PHP is a preprocessor; it’s code that runs when the server receives a request for a web page. It runs, works out what needs to be shown on the page, then sends that page to the browser. Unlike static HTML, PHP can show different content under different circumstances. To install PHP you’ll need to run sudo apt-get install php7.0 libapache2-mod-php7.0 php7.0-mysql. To see if PHP is working, make an index.php file to put in /var/www/html directory with the contents of <?php phpinfo(); ?> and make sure to delete or remove index.html. If you see the code instead of your PHP info, then you need to restart apache2: sudo service apache2 restart.

MySQL

MySQL (pronounced My Sequel or My S-Q-L) is a popular database engine. Like PHP, its overwhelming presence on web servers enhanced its popularity. This is why projects like WordPress use it, and why those projects are so popular. To install MySQL simply run sudo apt-get install mysql-server to install it, but you may have noticed php7.0-mysql isn’t here, why? If you remember, in the PHP section we already installed it there, to keep everything organized by item or category. After installing MySQL you will need to set up a root password by running sudo mysql_secure_installation and then restart apache2: sudo service apache2 restart.

What’s next?

Make a website for your house as a hobby or make it available to the public by port forwarding, ultimately the choice is yours. Just remember, however, that there is a lot more stuff you can do with a Raspberry Pi then just use it as a web server.