Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar...

10
Raspberry Pi LAMP Web Server Tutorial By Simar Singh Introduction Raspberry Pi LAMP Web server, is a lightweight server which can host a simple website or store files on a cloud that can be accessed easily on the internet. In the most basic setup, a web server responds to requests on the network to display web pages. There are several reasons to using a RPi web server, - There is no monthly hosting fees. - Raspberry Pi is a small computer which requires little power to run it, making it very cost effective. - The setup is relatively simple and can be done with basic computer knowledge. In this tutorial, I will be showing you how to setup a basic Raspberry Pi LAMP Web server. LAMP consists of four software layers - 1. Linux - is the operating system on which the stack works. All other components are built on top of this layer. In our case, the Linux distro is Raspbian. 2. Apache - is the most popular web server which uses HyperText Transfer Protocol (HTTP) to display files from the server onto the user’s browser. 3. MySQL - maintains databases that can be queried to build a robust website. 4. PHP - is scripting layer used to maintain the website. Requirements - Raspberry Pi 3 kit - MicroSD card - Private internet access Initial Setup of Raspberry Pi 3 Before you can start working on the web server project, it is important to get familiar with your Raspberry Pi and also perform a basic setup. Here is the diagram of the Raspberry Pi 3 with labels to help you identify the slots. 1

Transcript of Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar...

Page 1: Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar Singh Introduction Raspberry Pi LAMP Web server, is a lightweight server which can

Raspberry Pi LAMP Web Server Tutorial By Simar Singh

Introduction Raspberry Pi LAMP Web server, is a lightweight server which can host a simple website or store files on a cloud that can be accessed easily on the internet. In the most basic setup, a web server responds to requests on the network to display web pages. There are several reasons to using a RPi web server,

- There is no monthly hosting fees. - Raspberry Pi is a small computer which requires little power to run it, making it

very cost effective. - The setup is relatively simple and can be done with basic computer knowledge.

In this tutorial, I will be showing you how to setup a basic Raspberry Pi LAMP Web server. LAMP consists of four software layers -

1. Linux - is the operating system on which the stack works. All other components are built on top of this layer. In our case, the Linux distro is Raspbian.

2. Apache - is the most popular web server which uses HyperText Transfer Protocol (HTTP) to display files from the server onto the user’s browser.

3. MySQL - maintains databases that can be queried to build a robust website. 4. PHP - is scripting layer used to maintain the website.

Requirements

- Raspberry Pi 3 kit - MicroSD card - Private internet access

Initial Setup of Raspberry Pi 3 Before you can start working on the web server project, it is important to get familiar with your Raspberry Pi and also perform a basic setup. Here is the diagram of the Raspberry Pi 3 with labels to help you identify the slots.

1

Page 2: Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar Singh Introduction Raspberry Pi LAMP Web server, is a lightweight server which can

1. Follow the diagram to connect all the components to the Raspberry Pi 3.

The Raspberry Pi 3 kits typically comes with a microSD card. If not, please buy a 8GB microSD and install “Raspbian Stretch with desktop and recommended software” from the website - https://www.raspberrypi.org/downloads/raspbian/ PLEASE NOTE: You will have to format the microSD card before copying the Raspbian software.

2. When you switch on the Raspberry Pi 3 for the first time, the welcome screen will

come up and guide you through the setup process. 3. Set the Country, Language, and Timezone. 4. Set a secure password for the Raspberry Pi. 5. Connect to your network. 6. The Raspberry Pi will check if there are any new updates available and install them. 7. Reboot and get familiar with your Raspberry Pi 3. Now you are ready to install the

LAMP server.

2

Page 3: Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar Singh Introduction Raspberry Pi LAMP Web server, is a lightweight server which can

Instructions for setting up LAMP server 1. Before installing the LAMP server, you will have to update to the latest version of

Raspbian by running the following commands.

sudo apt-get update sudo apt-get upgrade -y

2. Now install the Apache2 packages. Apache 2 is the web server layer that uses

HyperText Transfer Protocol (HTTP) to display web pages on the internet.

sudo apt-get install apache2

3. Run the following commands.

3

Page 4: Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar Singh Introduction Raspberry Pi LAMP Web server, is a lightweight server which can

sudo a2enmod rewrite sudo service apache2 restart

The first command activates the module if not yet activated. Restart Apache for all changes to take effect.

4. The .htaccess file configures the website. You have to ensure that the .htaccess file

overrides the standard website settings. Open the configuration file.

sudo nano /etc/apache2/apache2.conf

Change the AllowOverride None to AllowOverride All in the <Directory /var/www/>. Then restart Apache.

5. Now find the IP address of your Raspberry Pi by running the following command.

ifconfig

4

Page 5: Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar Singh Introduction Raspberry Pi LAMP Web server, is a lightweight server which can

The inet IP address in wlan0 is the IP address of your Raspberry Pi if it is connected to a wireless network. In the above screen the IP address is 192.168.1.228

6. Go to any local computer on your network and enter the IP address in the URL of

your browser - you should see the Apache2 Debian Default Page.

5

Page 6: Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar Singh Introduction Raspberry Pi LAMP Web server, is a lightweight server which can

7. Now install PHP. PHP is a widely used scripting language that can be embedded into HTML.

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

6

Page 7: Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar Singh Introduction Raspberry Pi LAMP Web server, is a lightweight server which can

8. To test if the PHP is working create a new PHP file, index.php

cd /var/www/html sudo nano index.php

Enter the following code into your new PHP file:

<?php echo "Hello World"; ?>

9. Delete the default Apache page and restart Apache.

sudo rm index.html sudo service apache2 restart

7

Page 8: Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar Singh Introduction Raspberry Pi LAMP Web server, is a lightweight server which can

Go to your local computer and refresh the URL page. The following page should be displayed.

10.Now install MySQL and restart Apache.

sudo apt-get install mysql-server php-mysql -y sudo service apache2 restart

11.To maintain MySQL database, it is recommended to install phpMyAdmin.

phpMyAdmin is a GUI tool that allows users to maintain SQL databases on a web server.

sudo apt-get install phpmyadmin -y

While installing this you will need to answer the following questions

8

Page 9: Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar Singh Introduction Raspberry Pi LAMP Web server, is a lightweight server which can

Choose [*]apache2 Configure database for phpmyadmin with dbconfig-common? – Select Yes phpMyAdmin application password - choose a secure password

12.Go to your local computer and add /phpmyadmin/ to the IP address in the URL. In

our example - http://192.168.1.228/phpmyadmin/. The following page should be displayed,

Enter the Username as “phpmyadmin” and the password that you entered while installing phpmyadmin.

13.By following the steps above, you should be able to setup a local web server. You

can access this web server on your local network. To connect from outside the network, you will have to setup port forwarding. You have to configure the router to allow incoming traffic through a specific port and be sent to Raspberry Pi. HTTP is by default on port 80. This port has to be opened for your web application to work from external request.

9

Page 10: Raspberry Pi LAMP Web Server Tutorial setup, a …...Raspberry Pi LAMP Web Server Tutorial By Simar Singh Introduction Raspberry Pi LAMP Web server, is a lightweight server which can

The steps for port forwarding vary depending upon on your ISP and the brand of your router. The main idea is to forward the external requests from ports 80 to be forwarded to Raspberry Pi. Here’s an example of how your router configuration may look like:

In the above example, Raspberry Pi’s IP address is 192.168.1.16. All incoming traffic through ports 80 will be forwarded to Raspberry Pi. To test if the port forwarding worked, find your router’s external IP address. You can find this in your router’s manual or by simply typing “what’s my ip address” into Google. Now from an outside network, enter the external IP address in the browser. You should see the php page that you had step in step 8. Congratulations! You now have a basic LAMP web server.

Acknowledgments:

I would like to thank the Institute for Security, Technology and Society (ISTS) at Dartmouth College for providing me the opportunity to attend the fully funded Gencyber Advanced Program (Summer 2018). This summer program was designed to build on the basic foundations of cybersecurity and helped me to further my knowledge and interest in this exciting field. This Raspberry Pi project has been developed using the Raspberry Pi 3 kit generously provided by ISTS to each participant at the end of the Advanced program.

Also, special thanks to Professor Goldstein for leading us through a fast-paced, exciting program and keeping us totally engaged with challenging hands-on labs and exercises.

10