how to install lamp server raspberry pi

How to Setup a Raspberry Pi LAMP Server

Ben by

So you’ve got your hands on a brand new Raspberry Pi and now you want to run a little website on it.

Well you’re in the right place because I’m going to show you how to setup a LAMP stack on your Raspberry Pi.

Install Apache

Apache is one of the most popular web servers around and it’s perfect for our Raspberry Pi.

We can use Apache to serve up static HTML files, or we can use PHP to server dynamic webpages using a CMS (Content Management System).

To install Apache, run the apt-get install command in the terminal:

sudo apt-get install apache2 -y

After a few minutes the install should be complete and we’re ready to test our server.

Testing Your Web Server

By default Apache generates a handy HTML test file and automatically begins hosting it.

On your Raspberry Pi open the web browser and visit:

http://127.0.0.1/

You can also test this from another computer by entering the Raspberry Pi’s IP address:

http://192.168.0.2/

Obviously this IP address will be different for you, so be sure to check your Pi’s IP address.

 

If everything went as planned then you should see Apache’s test page:

apache test page

Congratulations! You’re web server is up and running.

Now to install the rest of our LAMP stack.

Installing PHP

PHP is an scripting language that you can use to generate dynamic pages on your server.

Unlike static HTML files, PHP pages can take user input and show different content, for example a search or login page.

There are plenty of alternative to PHP out there, but for today we’ll be working with PHP.

To install PHP, run the apt-get install command in the terminal:

sudo apt-get install php5 libapache2-mod-php5 -y

Give it a few minutes to do it’s thing and it should be installed.

Testing PHP

We’ve now installed PHP, but we need to test and see if it works.

Open up the file explorer (or terminal) and navigate to:

/var/www/html

Here you should find the default test page named index.html.

You can go ahead and delete this as we won’t be needing it.

Note: index.html will override index.php, so make sure you delete index.html

Create a new file called index.php and paste in the following:

<?php phpinfo(); ?>

Refresh your browser and you should now see a page displaying all of your PHP info.

Installing MySQL

MySQL is a free and popular database server that powers most of the web.

When teamed up with PHP, you can use it to create all sorts of websites and web apps.

To install MySQL, run the apt-get install command in the terminal:

sudo apt-get install mysql-server php5-mysql -y

Once the install finishes, you’ll be asked to enter a root password.

It’s important to remember this as you’ll need to to setup any databases on the server.

Once everything is completed, restart your web server:

sudo service apache2 restart

Setting up a Website

Give yourself a pat on the back because now your Raspberry Pi web server is up and running!

Now it’s all over to you to decide what to do with it.

You can build a basic website in HTML and drop the files into your HTML directory:

/var/www/html

You might decide to use a static site generator like Jekyll to generate your HTML files.

You can also use a CMS like WordPress on your Raspberry Pi to setup a blog!

You may also like