Turn your Raspberry Pi into a Web Server

In this tutorial you’ll learn how to make a Raspberry Pi web server by installing LAMP. LAMP stands for Linux Apache MySQL and PHP. The LAMP stack is a popular open source web platform commonly used to run dynamic web sites and servers. It is the platform of choice for development and deployment of web applications which require a solid and reliable foundation.

This is especially useful if you’re interested in Web Development and website creation. You can use your Raspberry Pi web server to host your personal website or application on your home network.

Requirements

Overview of steps

  1. Install Raspbian OS
  2. Set a static IP address
  3. Install the LAMP stack
    1. Install Apache and PHP5
    2. Test PHP Processing on your Web Server
    3. Set correct permissions for your Web Server
    4. Install MySQL Database
    5. Creating a MySQL Database User
    6. Install PHPMyAdmin to manage MySQL databases
  4. Configure FTP
  5. Develop your website / web app

Instructions

1. Install Raspbian OS

First, you must install the Raspbian OS on your Raspberry Pi. Check out the official guide here .

2. Set a static IP address

You’ll want to set a static IP address on your Raspberry Pi to ensure you will always be able to connect to it at the same IP address. To do this, use my guide here .

3. Install the LAMP stack

Open up a terminal on your Raspberry Pi (CTRL+ALT+T) or by connecting to it through SSH .

3a. Install Apache and PHP5

To install Apache and PHP run the following command:

Press Y and it will install

Type the following command to restart the web server:

Now navigate to your Raspberry Pi’s IP address. If you see a page similar to this, it worked!

3b. Test PHP Processing on your Web Server

In order to test that our system is configured properly for PHP, we can create a very basic PHP script.

We will call this script info.php. In order for Apache to find the file and serve it correctly, it must be saved to a very specific directory, which is called the “web root”. This is where your website lives.

This directory is located at /var/www/html/. We can create the file at that location by navigating to the directory:

Now type:

This will open a blank file. We want to put the following text, which is valid PHP code, inside the file:

When you are finished, press CTRL+X then Y then ENTER to save and close the file.

Now we can test whether our web server can correctly display content generated by a PHP script. To try this out, we just have to visit this page in our web browser. You’ll need your server’s public IP address again.

The address you want to visit will be:

The page that you come to should look something like this:

You can delete the file if you wish:

3c. Set correct permissions for your Web Server

In order to be able to read/write your web pages to your default directory you will have to change the director permissions. To accomplish this, execute the following command:

This will assign full permissions 7 for the owner, read/write 6 for the group, and read/write for everyone 6, recursively.

3d. Install MySQL Database

Now we will install MySQL. In a terminal type the following command:

Press Y and it will install

During the installation, you will be prompted to set a new password for the MySQL root user. Type a password of your choice, confirm the password again to continue the installation.

3e. Creating a MySQL Database User

By default PHPMyAdmin will disallow you to login using the root login. Instead you will need to create a new user if you wish to create and access datatables within PHPMyAdmin. To do this first login as root with the password you selected.

Now run the following command, replacing username with the username of your choice. Also replace password with a secure password of your choice.

You can exit by entering quit. Once done you can proceed to installing phpMyAdmin.

3f. Install PHPMyAdmin to manage MySQL databases

The easiest way to install phpMyAdmin is through apt-get:

During the installation, phpMyAdmin will walk you through a basic configuration. Once the process starts up, select Apache2 for the server (press SPACE then TAB then ENTER)

Choose YES when asked about whether to Configure the database for phpmyadmin with dbconfig-common

Enter your MySQL password when prompted

Enter the password that you want to use to log into phpmyadmin

After the installation has completed, restart Apache:

We need to alter the Apache configuration in order to access PhpMyAdmin. To do this, enter the following command to alter the configuration:

The configuration file will load in Nano. Navigate to the bottom of the file (keep pressing CTRL + V to jump page by page until you’re at the bottom of the file) and add the following new line to the file:

Save the file (CTRL + X and enter Y when prompted to save) and Restart Apache2. To restart Apache, enter the following command:

You can then access phpmyadmin by going to youripaddress/phpmyadmin. The screen should look like this:

After logging in, it will look like this:

raspberry pi webserver phpmyadmin

4. Configure FTP

To transfer files back and forth between your Raspberry Pi you’ll want to set up SFTP. This is a secure and easy way to upload your website files. To learn how to set the up, check out my guide here.

5. Develop your website / web app

Time to get started on your website or web application. PHP files will run and you can set up databases.

 By default you will place your web page files at /var/www/html 

My workflow consists of developing my website in Brackets, then transferring my files over to the Raspberry Pi via FileZilla.

raspberry pi web server

Happy coding! Post any questions in the comment section below.

 

(Visited 2,049 times, 1 visits today)
Related Content