BUILD A WEB SERVER WITH Raspberry Pi

অবুঝ মন
February 6, 2016
How to upgrade to a larger hard drive
March 17, 2016

BUILD A WEB SERVER WITH Raspberry Pi

WHAT YOU WILL DO
Learn to set up a LAMP (Linux, Apache, MySQL, PHP) stack on your Raspberry Pi and configure it to work as a web server. You’ll download and install WordPress and set up a basic website which you can access on any device on the same network as your Pi.

WHAT YOU WILL LEARN
By following this resource and setting up a web server and WordPress website you will learn:
How to install software on your Raspberry Pi
How to install and configure Apache, PHP and MySQL to create a LAMP web server
How to download WordPress and run it as a local website on your Raspberry Pi
How to configure WordPress and make your website accessible to other devices on your local network

WHAT YOU WILL NEED
HARDWARE : There are no additional hardware requirements for this resource beyond the Raspberry Pi and usual peripherals.
SOFTWARE : There are no additional software requirements for this resource beyond what is pre-installed in the current Raspbian image. To make sure your SD card is up-to-date, see the updating Raspbian guide.

SET UP APACHE WEB SERVER
Apache is a popular web server application you can install on the Raspberry Pi to allow it to serve web pages.

On its own, Apache can serve HTML files over HTTP, and with additional modules can serve dynamic web pages using scripting languages such as PHP.

INSTALL APACHE
First install the apache2 package by typing the following command into the terminal:

sudo apt-get install apache2 -y

TEST THE WEB SERVER
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.1.10
(whatever the Pi’s IP address is) from another computer on the network. To find out the Pi’s IP address, type
hostname -I
at the command line (or read more about finding your IP address) in our documentation.

Browse to the default web page, either on the Pi or from another computer on the network, and you should see the following:

This means you have Apache working!
CHANGING THE DEFAULT WEB PAGE
This default web page is just a HTML file on the filesystem. It is located at /var/www/html/index.html.

Note: The directory was /var/www in Raspbian Wheezy but is now /var/www/html in Raspbian Jessie

Navigate to this directory in the Terminal and have a look at what’s inside:

cd /var/www/html
ls -al

This will show you:

total 12
drwxr-xr-x 2 root root 4096 Jan 8 01:29 .
drwxr-xr-x 3 root root 4096 Jan 8 01:28 ..
-rw-r--r-- 1 root root 177 Jan 8 01:29 index.html

This shows that there is one file in /var/www/html/ called index.html. The . refers to the directory itself /var/www/html and the ..refers to the parent directory /www/.

WHAT THE COLUMNS MEAN
 The permissions of the file or directory
 The number of files in the directory (or 1 if it’s a file).
 The user which owns the file or directory
 The group which owns the file or directory
 The file size
 The last modification date & time
As you can see, by default the html directory and index.html file are both owned by the root user. In order to edit the file, you must gain root permissions. Change the owner to your own user with sudo chown pi: index.html before editing.

Try editing this file and refreshing the browser to see the web page change. Press Ctrl + X and hit Enter to save and exit.

INSTALL 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. Other languages are capable of this, but since WordPress is written in PHP, that’s what we need to use this time. PHP is a very popular language on the web; large projects like Facebook and Wikipedia are written in PHP.

Install the PHP and Apache packages with the following command:

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

TEST PHP
Create the file index.php:

sudo nano index.php

Put some PHP content in it:

<?php echo "hello world"; ?>

Now save the file. Next delete index.html because it takes precendence over index.php:

sudo rm index.html

Refresh your browser. You should see “hello world”. This is not dynamic but it is still served by PHP. Try something dynamic, for example:

<?php echo date('Y-m-d H:i:s'); ?>

Or show your PHP info:

<?php phpinfo(); ?>

INSTALL 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.

Install the MySQL Server and PHP-MySQL packages by entering the following command into the terminal:

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

When installing MySQL you will be asked for a root password. You’ll need to remember this to allow your website to access the database.

Leave a Reply

Your email address will not be published. Required fields are marked *