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.
Contents
Requirements
- Raspberry Pi 3 Kit
- 8GB microSD Card for Raspberry Pi OS
Overview of steps
- Install Raspbian OS
- Set a static IP address
- Install the LAMP stack
- Install Apache and PHP5
- Test PHP Processing on your Web Server
- Set correct permissions for your Web Server
- Install MySQL Database
- Creating a MySQL Database User
- Install PHPMyAdmin to manage MySQL databases
- Configure FTP
- 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:
1 |
sudo apt-get install apache2 php5 libapache2-mod-php5 |
Press Y and it will install
Type the following command to restart the web server:
1 |
sudo service apache2 restart |
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:
1 |
cd /var/ww/html |
Now type:
1 |
sudo nano /var/www/html/info.php |
This will open a blank file. We want to put the following text, which is valid PHP code, inside the file:
1 2 3 |
<?php phpinfo(); ?> |
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:
1 |
http://your_server_IP_address/info.php |
The page that you come to should look something like this:
You can delete the file if you wish:
1 |
sudo rm info.php |
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:
1 |
sudo chmod 777 -R /var/www/html |
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:
1 |
sudo apt-get install mysql-server mysql-client php5-mysql |
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.
1 |
sudo mysql -u root -p |
Now run the following command, replacing username with the username of your choice. Also replace password with a secure password of your choice.
1 |
GRANT ALL PRIVILEGES ON mydb.* TO 'username'@'localhost' IDENTIFIED BY 'password'; |
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:
1 |
sudo apt-get install phpmyadmin apache2-utils |
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:
1 |
sudo service apache2 restart |
We need to alter the Apache configuration in order to access PhpMyAdmin. To do this, enter the following command to alter the configuration:
1 |
sudo nano /etc/apache2/apache2.conf |
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:
1 |
Include /etc/phpmyadmin/apache.conf |
Save the file (CTRL + X and enter Y when prompted to save) and Restart Apache2. To restart Apache, enter the following command:
1 |
sudo service apache2 restart |
You can then access phpmyadmin by going to youripaddress/phpmyadmin. The screen should look like this:
After logging in, it will look like this:
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.
My workflow consists of developing my website in Brackets, then transferring my files over to the Raspberry Pi via FileZilla.
Happy coding! Post any questions in the comment section below.