The Rasp Pi serves up a tempting web server

Restart Apache

You now have what is commonly called a LAMP stack (i.e., Linux, Apache, MySQL, and PHP). As you spend time working with the LAMP stack, you will become very familiar with the command to restart the web server:

# /etc/init.d/apache2 restart

Once Apache is restarted, you can type your local network IP address (i.e., http://<IP address>) in the browser from any computer on the local network.

To find your IP address, use the ifconfig command. In Listing 1, look for eth0 to find the desired IP (here, If you are using your Raspberry Pi with a high-definition TV as a monitor, just type http://localhost. Your browser should show a simple web page that indicates the web server is working (Figure 2). The file that is displayed is called index.html, which resides in the /var/www folder.

Listing 1


root@raspberrypi:/home/pi# ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:ec:e8:11
          inet addr:  Bcast:  Mask:
          RX packets:4510454 errors:0 dropped:8795 overruns:0 frame:0
          TX packets:3272907 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:510930023 (487.2 MiB)  TX bytes:744465642 (709.9 MiB)

Now that your server works locally, it is time to set everything up so your domain name will point to this tiny Raspberry Pi.

Hosting the Website

To view your website from your local cafe or from half way across the world, you need to point your domain name to a specific set of DNS servers. Then, those DNS servers will need to set your domain name and a matching IP address. Free services like No-IP [4] allow you to do this, or some web hosts allow you to alter DNS entries.

Please note that updates to a DNS or changing DNS servers can take a day or so to occur. If you do not take the time factors into consideration, you could drive yourself crazy troubleshooting why your website cannot be viewed at a particular instance in time. Nevertheless, you can always make the odd check with proxy servers to see if your DNS has propagated and is viewable from another location.

Once the IP address and domain name information have been entered, you can set up your local network to show the world your website. Although the DNS has propagated and is working, your site will not be found until you allow port forwarding. To fix this, you must access your router and set the local IP of the Raspberry Pi to allow port forwarding on port 80 (Figure 3).

Figure 3: Port forwarding on port 80.

Now, you should be able to open a browser, type in your domain name, and see the same page you saw on your localhost when you typed in the Rasp Pi's local IP address. Of course, your IP will be different now.

Once you have the port forwarding set to port 80 for your local IP address, you can configure Apache and set up a virtual host for your domain. To begin, open the Apache configuration file for editing:

# cd /etc/apache2
# nano apache2.conf

Now, go to the bottom of the file and look for Include conf.d/. You will want to add the first two lines shown below; and, if you installed phpMyAdmin, you want to add all three of the following lines:

Include sites-enabled/
Include conf.d/*.conf
Include /etc/phpmyadmin/apache.conf

The major change is the addition of Include conf.d/*.conf, which allows you to add entries for each domain name you will be hosting. In this case, the domain name is Obviously, your domain name will be something different.

To add an entry and make a config file for a new domain name, enter

# cd /etc/apache2/conf.d
# nano

then add the VirtualHost code in Listing 2. Once that's done, save the file and restart the Apache server (/etc/init.d/apache2 restart).

Listing 2

Setting Up a Virtual Host

<VirtualHost *:80>
        ServerAlias *
        DocumentRoot /var/www/
        <Directory "/var/www/">
                Order allow,deny
                Allow from all

Note that the document root for the website is /var/www/ All files for the website need to be there. Now, you can take a deep breath and type your domain name into the browser; you should see the index.html or index.php file (in the /var/www/ folder) in the browser. At this point, you can actually make a website.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content