Build a Web Server

Turn your Raspberry Pi into an Apache web server and serve up websites. Use it on your own network to build test websites, or get a static IP address to deliver your website to the Internet, directly from your Raspberry Pi.

APACHE

Apache is an incredibly popular web application that runs more than half the servers on earth. Most websites you visit are delivered to you using Apache and it’s free to install and very easy to set up.

STEP 1

Open a terminal window, click LXTerminal in the Launch Bar, and enter sudo apt-get update. This will ensure that apt-get is using the latest versions of all its software. While it’s not vital to do this, it’s a good habit to get into from now on before installing any new software.

STEP 2

Now lets install Apache. Enter sudo apt-get install apache2 apache2-doc apache2-utils. You will be asked if you want to continue; enter y and press return. This installs the latest version of Apache 2, the Apache 2 documentation and a selection of utilities that will be handy as you work on your server.

STEP 3

Apache will now be running from your Raspberry Pi and you can test it by connecting from another computer. First you need your Raspberry Pi’s IP address: enter ifconfig to get it. Now enter that IP number into a browser on your network and you should see:
It works!
This is the default web page for this server. The web server software is running but no content has been added, yet.

STEP 4

Before heading any further we’re going to add some additional functions to our server. PHP is a popular scripting language used to power many advanced websites; enter sudo apt-get
install libapache2-mod-php5 php5 php-pear php5-xcache.
Enter y and press return to install it.

STEP 5

We’re also going to install MySQL server. This is a popular open source database that’s used in by many online websites. Enter sudo apt-get install php5-mysql to get the PHP extension for MySQL and follow this up with: sudo apt-get install mysql-server mysql-client.

STEP 6

You’ll be asked to enter a password for the MySQL root user. Enter a password and press return. You’ll be asked to re-enter the same password again. When that is finished restart the Raspberry Pi using sudo shutdown -r now. When your Raspberry Pi restarts you’ll be ready to begin hosting your website.

CREATING YOUR WEBSITE

Now that you have Apache set up on your Raspberry Pi, it’s time to make something of it. Adjust the HTML and CSS files to build a basic website or use PHP to script a website.

STEP 1

The It Works! web page is an indext.html file located on the file system. You can find it in /var/www/. Enter ls -l /var/www/ to see the index file. Notice that its owner is root not pi. Unlike most documents you work on, this one lives outside your home directory. Enter cat /var/www/index.html to view the contents of the file.

STEP 2

You’ll need to use sudo to edit the file. Enter sudo nano /var/www/index.html to view its contents. If you know HTML you can change the contents of this web page to whatever you want. Any edits you make can be checked by entering the IP address of your Raspberry Pi into a web server.

STEP 3

Alternatively you can open the Epiphany Web Browser inside the desktop and enter http://localhost. This will display the contents of the index.html file inside your web browser. You can create something more dynamic using PHP. Enter sudo rm /var/ww/index.html to delete the index file and nano /var/www/index.php to create a PHP file in its place.

STEP 4

Enter this code into the website:

<h1>The date and time</h1>
<?php echo date(‘Y-m-d H:i:s’);
Refresh the web browser or point it again to
http://localhost 
and you’ll see the current date and time listed dynamically created in your web browser. Codecademy (www.codecademy.com) has good HTML and PHP courses if you want to learn more.

More information