This post is one of a list of tutorials on the Raspberry PI, click here to access the summary!
After creating your SD card, and After booting for the first time your Raspberry Pi, there is a strong possibility that you want to use as a web server.
Install web server on Raspberry Pi, why?
But why would Install web server on Raspberry Pi, rather than to appeal to specialized service providers in the web hosting?
First, from an economic point of view, be aware that web hosting services are not free and and bill their clients every month / year. Unlike Raspberry which just requires a connection.
Moreover, choosing the Raspberry, you can change your service plan (eg, disk size, hosting Database, etc.), which is generally not the case with specialized hosts which often sell shared hosting with low capacity configuration.
However to support more users, you better use a Raspberry Pi 2, or the Raspberry Pi with 1 GB of RAM, rather than the type of Raspberry B + (512 MB RAM).
The question now posed is, How to install a web server on your Raspberry Pi?
Apache server installation with Raspbian
Apache, what is it?
First, we will install Apache, which is the web server itself.
When talking about web server, we often think of the machine, but it also includes software that allows the machine to analyze a user requests (in the form http), and return the file for the request (or an error if the file is not found, or malformed request).
In the context of Apache, we are talking about the software.
At present, Apache is the most used web server, with about 60% market share. Apache even has its own license, used by many other projects. In addition, the massive use of Apache (now the standard web servers), coupled with its high popularity, bringing a great abundance of documentation, courses, and other books on its use, from installation till the security.
Whether for the Raspberry Pi and Raspbian, or for a more general machine Apache is a safe choice, and skills you acquire on the subject will be always useful.
Before installing the web server on Raspberry Pi, make sure that you have a good updated machine. To do this we must have administrator rights, That’s mean being logged in as root or via sudo.
sudo aptitude update sudo aptitude upgrade
Once the Raspberry Pi updated, we will install the Apache server.
sudo aptitude install apache2
Check if Apache works
Once installation is complete, we can do a test that prove if Apache is working correctly by visiting the address of the Raspberry.
For this, we must try to reach the Raspberry from the port 80 (the port is not yet open from outside, it will do so from the Raspberry itself).
Do not worry, it’s very easy. Just open the web browser of Raspberry (by default it’s Midori under Raspbian), and go to the address “http://127.0.0.1”. You should then get a page with a message like “It works! “.
If you do not have a graphical interface on your Raspbian, or use SSH to connect to your Raspberry, you can use the following command:
wget -O verif_apache.html http://127.0.0.1
This command will save the HTML code of the page in the “verif_apache.html” file in the current directory.
So you just have to read the file with the command:
wget -O verif_apache.html http://127.0.0.1
If you see marked in a place in the code, “It works!” That’s mean Apache is running.
Apache uses the “/ var / www” as the root for your site. This means that when you call your Raspberry on port 80 (http), Apache looks for the file in “/ var / www”.
For example, if you call the address “http://127.0.0.1/example”, Apache will look for the “example” file in the “/ var / www” directory.
To add new files, sites, etc., so you should add to this directory.
However, you may want to quickly allow interactions between the site and the user. For example enable the user to register, etc. For this, you will need PHP.
PHP installation on Raspberry
What is PHP?
First, you must know that PHP is an interpreted language. And as in the case of servers, the acronym PHP can have several meanings. In fact, when we speak of PHP, we can speak about either language or interpreter.
Here, when we speak of PHP install, it means that we will install the interpreter, to use the language.
PHP (this time we speak about the language) is mainly used to make a dynamic site, that is to say that the user sends information to the server, which sends back the results changed according to this info. Conversely, a static site does not adapt to the information provided by a user. It is saved as a file once and for all, and still deliver the same content.
PHP is free, and maintained by the Foundation PHP and Zend company and various other companies (it should be noted that Zend is also the author of the famous Zend PHP framework, highly used and recognized in the world of business).
This is one of the most used programming languages, and it is even more used for the Web programming, with about 79% market share.
Again, all the skills that you can acquire about the language or the installation and configuration of the interpreter, will be always useful. We highly advise you to learn PHP, which is really a wonderful language.
Once again we will appeal to the administrator to install php5 with the command line:
sudo aptitude install php5
Check that PHP works
To know if PHP is working, it’s not very complicated, and the method is relatively close to that used for Apache.
You will first remove the “index.html” file in the “/ var / www” directory.
sudo rm /var/www/index.html
Then create a ‘index.php’ file in that directory with the command line
echo "<?php phpinfo(); ?>" > /var/www/index.php
From there, the operation is the same as for the verification of Apache. You try to access your page, and you should have a result close to this file (if you do not have an interface, use the same method as before, and look for the words “PHP Version”).
A MySQL database for your server
A DBMS is what? Why MySQL?
Now that we have set up PHP5, you will probably want to store information for use in your sites. For this, the most commonly used databases.
We will set up a DBMS (Database Management System), namely MySQL.
MySQL is a free database, powerful, massively used (about 56% market share of free DBMS). Again, so MySQL is a key development, whatever the branch, you absolutely must learn it and master it.
To do this, we will install mysql-server and php5-mysql (which will serve as a link between php and mysql)
sudo aptitude install mysql-server php5-mysql
During the mysql-server installation, we are asked a password for the MySQL administrator account (root), careful to remember it because it will be used later.
Check that MySQL is working properly
To check the operation of MySQL, this time we only use the command line. For that, we’ll just connect ourselves with the command:
mysql --user=root --password=yourpassword
You should then have a message like
“Welcome to MySQL! ”
So now you have a web server connected to PHP5 and MySQL. Is all you need. So you can stop here.
PHPMyAdmin easily manage databases
However, you may want a somewThe installation of PHPMyAdmin is not so obligatory. We will do here an installation without special security settings.
Installing PHPMyAdmin is very simple via the package manager, use the following command:hat simpler intThe installation of PHPMyAdmin is not so obligatory. We will do here an installation without special security settings.
Installing PHPMyAdmin is very simple via the package manager, use the following command:erface for managing your databases than just MySQL console. For this you can install PHPMyAdmin.
PHPMyAdmin, what is it?
PHPMyAdmin is an application developed in PHP, and aims to provide a simplified interface for MySQL.
It allows you for example to see quickly and legibly the contents of your database, or to manipulate it without having to do your MySQL queries by yourself.
How to install PHPMyAdmin
The installation of PHPMyAdmin is not so obligatory. We will do here an installation without special security settings.
Installing PHPMyAdmin is very simple via the package manager, use the following command:
sudo aptitude install phpmyadmin
PHPMyAdmin will ask you several questions about its settings.
So choose an Apache server, then use the default choice for the base (dbconfig-common). For the root password, this is the one you used for MySQL.
PHPMyAdmin Once installed, make sure the “/etc/apache2/apache2.conf” file contains the line.
Check the installation of PHPMyAdmin
To check the installation of PHPMyAdmin, you simply try to access them, using the address of your Raspberry monitoring / phpmyadmin. For example, it will be local “http://127.0.0.1/phpmyadmin”.
Making a server accessible from the web
Your web server is ready. However, you can probably not be accessed from the Internet. Indeed, it would require that your box (Livebox, Freebox, etc.) redirects requests to your Raspberry on the correct ports, etc.