There are a few instalable ways available in order to make your Raspberry Pi to act as a web server. Two of the most popular, than make a perfect fit for the Pi, are the Apache and the Lighttpd. I am going to show both of those and I will begin with my preferred one, the Apache.
Before we begin do the usual update process:
sudo apt-get update
sudo apt-get upgrade
and to be 100% that everything are working correctly do a reboot after that:
Now its time to load the Apache server into the RPi. Open a terminal connection with your Pi and type:
sudo apt-get install apache2 php5 libapache2-mod-php5
this will install the Apache HTTP server and the PHP5 extension. If you want just to test the HTTP server functionality with simple HTML pages then you don’t need the PHP extension and just use the sudo apt-get install apache2. But for a some next tutorials this extension is critical. So, act accordingly by your own needs!
After you hit enter, the RPi will download and install all the necessary packages and dependencies for the web server. This takes a little bit of time but there is nothing to worry about it! When the installation process finishes, open a web browser like Chrome, Firefox, Safari, Opera etc (do not use Internet Explorer – it works OK but there are a huge set of other problems with this one) and type the IP address of your RPi.
You should now see a “It works!” page with a few other lines! If this is the case then everything went OK and you are ready to go! Your Apache web server is working properly! If this is not the case (and you have a result like the photo below), then check your board’s IP address, try re-installing Apache, rebooting your RPi or check if you have set a strange rule on your rooter.
And now its time to check the PHP extension for normal operation! The “It works!” is just showing the good functionality of the web server only. To check the PHP we must change a little bit the HTML code of the page the Apache is loading. To access this page type:
to make a list of all available files/softwares in this directory. You should have only one file named “index.html”. You can rename the extension of this file or delete it and later on you can create the .php file. I choose to delete the original file:
sudo rm index.html
but in case you want to rename it type:
sudo mv index.html index.htmlll
Now this directory (if you delete the original file) should be empty. Pay attention to this. If you have two .html files in the same folder the web server will have “strange” functionality. You can define witch file to load by editing the configuration file but this another story.
Create a new file named index.php:
sudo nano index.php
The nano editor will load and inside the file copy paste just the:
Press Ctrl-X, then Y and finally Enter. And you are done! Reload again your web browser page and you will have a long PHP info page showing that the PHP extension is working perfectly!
Now we are ready to do the web pages that we want! You can install more libraries/extensions to work in hand with the Apache wed server but this is due to your needs and projects you are working for.
And before the end, its time to change the ownership of the /var/www folder because you are going to have problems if you start transferring files into this directory. The problems are oriented due to the fact that this directory is not owned by the user pi. You can confirm this by typing:
ls -l /var/www
You’ll see that only root (the super user in other words) is owning the www directory. To change the ownership type:
sudo chown -R pi /var/www