+5 votes
259 views
in Setting by (242k points)
reopened
How to use a Raspberry Pi web server

1 Answer

+3 votes
by (1.6m points)
edited
 
Best answer

What is the purpose of the Raspberry Pi as a web server?
Requirements to convert Raspberry Pi into a web server
Create permanent access to a server under the same IP address
How to create a Raspberry Pi web server with LAMP
Raspberry web server application fields
Web server security
Apache configuration
Configure PHP
Configure MySQL
Configure phpMyAdmin

image

How to use a Raspberry Pi web server

The Raspberry Pi single board computer can be configured in just a few steps so that you can call it your own Raspberry pi web server. Even though its performance is lower than the commercial variants offered on the market, using Raspberry Pi as a private web server is very useful in some areas, such as when you want to analyze and host your web page in a test environment or manage your files. on your own and make these available in the cloud. Our tutorial provides you with the information you need to configure your own web server on this small computer by means of a free LAMP software pack..

What is the purpose of the Raspberry Pi as a web server?

If you want to use Raspberry Pi as a web server, you must be aware that the hardware has certain limitations. The computer offers worse performance compared to what you can get from a regular web provider . For hosting complex web pages (for example online stores or pages with dynamic content), Raspberry Pi also has certain limitations, as well as for web pages that have many visitors. The low bandwidth of the Internet connection is another obstacle in the use of Raspberry Pi as a web server.

However, the Raspberry PI web server is suitable in some fields of application: it can be used as a local server with an Internet connection or used in a corporate network. With it you can test web pages that are online or host complete web pages of small size that have a reduced number of visits. Likewise, you can also manage your own cloud or configure programs for home automation (light control, heating, etc.)..

Another advantage of a Raspberry Pi as a web server is the reduction in current costs that it entails, since it needs both an Internet connection and electricity, but this does not exceed, in general, 5 W. In this way, the expense for continuous operation of the web server will be practically negligible. In addition, the acquisition costs of the required components are minimal.

Requirements to convert Raspberry Pi into a web server

The information above refers to one of the many Raspberry Pi application variants as a web server. For this, it is not only necessary to have this small-sized computer, but also:

  • an SD card with Raspbian operating system
  • Internet connection, either via a network cable (recommended) or via WLAN
  • power supply with micro USB cable

Since a web server works in? Headless? (without display or input devices) and represents the variant with the greatest energy savings , the ideal would be to configure remote access to the server through the SSH protocol: with the help of an SSH client (such as PuTTY, WinSCP for Windows or with OpenSSH for Unix operating systems), you can comfortably install the web server on a computer, smartphone or any other device and make the necessary changes. If you decide on administration via SSH, enter the IPv4 address of the Raspberry Pi in your SSH client to connect them to each other. If you don't know what the IP address of your Raspberry is, you can access it by entering the command hostname -I (or ifconfig ) in the terminal. In case you are using the Raspberry Pi without a display and without input devices, you can find the IP address by looking for the router in your web browser (usually by typing "http://192.168.0.1")..

If you want to use the web server for a private cloud or for other types of applications that require more storage space, it will be necessary to expand the storage capacity . This is easily accomplished on the Raspberry Pi and is accomplished via a USB stick or external hard drive.

Create permanent access to a server under the same IP address

The servers not only have to be permanently online so that users can access them at all times, but they also have to be available under the same IP address. As a general rule, the Internet connection of a home server is provided with a single dynamic IP address . This varies continuously (every 24 hours) and prevents the server from always being accessible from the same IP address. A static IP address would help in this case, although not all ISPs provide them (and if they do, they are subject to additional monthly costs).

There are also other possibilities to host a server at the same address, for example by means of dynamic DNS (DDNS) , which can be both free and paid. In this case, a domain name has to be registered and linked to a computer or router. By means of specific software, the current IP address of your Internet access is assigned to a domain name and, by means of this, the server remains continuously accessible under the same name. Another option is to visit your router manufacturer's website for more information on how to configure dynamic DNS on the device itself.

How to create a Raspberry Pi web server with LAMP

With the following tutorial you can create a Raspberry Pi web server with LAMP stack . LAMP is the acronym for a software pack consisting of the following elements: Linux operating system (this tutorial refers to the Raspbian system already present on the device), an Apache web server, a MySQL database and the scripting language PHP. Besides the LAMP pack, phpMyAdmin is also installed .

Log into the console (terminal) of your Raspberry Pi. Before starting to configure the LAMP software pack, the first thing you should do is update all the packs already installed by means of these two commands:

  sudo apt-get update  
  sudo apt-get upgrade  

Now you can start installing each of the LAMP elements. The tutorial guides you through a process divided into phases marking and explaining each of the intermediate stages. In this sense, not only are some important questions explained, but also the cycles with which the operating capacity of the corresponding installation is checked are shown.

Apache configuration

Installation of the Apache Servers HTTP server is quick. This is done with a single command and the same applies to the other three LAMP components of the Raspberry Pi web server. For Apache 2 enter the following command in the terminal:

  sudo apt-get install apache2  

To check if the installation was successful, type the IP address of the Raspberry Pi in the address bar of your browser . If Apache 2 has been installed successfully, a default page with the message? It works!? Will appear in the browser. This web page in HTML format can be found on Raspbian Jessie in the /var/www/html/index.html directory and in the old version of Raspbian called Wheezy under the /var/www/index.html directory . The page can be reviewed in the directory and other types of web pages can be included in it:

  sudo nano /var/www/index.html  

or

  sudo nano /var/www/html/index.html  

If you have made changes, you will have the possibility to see them after reloading the web page in the browser.

Configure PHP

To make the web server not only capable of processing HTML, CSS or JavaScript files, but also PHP, install PHP5 :

  sudo apt-get install php5 libapache2-mod-php5  

Now you can generate a PHP file under / var / www . As a test, install the phpinfo.php file :

  sudo nano phpinfo.php  

Make the following corrections in the editor:

  <?php phpinfo(); ?>  

If you entered /infophp.php to get the IP address of the Raspberry Pi in the browser ("http: //192.168.XX/phpinfo.php"), the corresponding page should appear. This takes place when PHP has been installed on your Raspberry Pi. 

Configure MySQL

You can use MySQL to create the web server database . With the command

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

you can install everything you need for it. You will be asked to determine the root password for MySQL , but, once this is done and once the database configuration is finished, complete the installation definitively by restarting MySQL (command: sudo / etc / init.d / mysql restart ) or the Raspberry Pi computer (command: sudo reboot ).

Configure phpMyAdmin

You can use phpMyAdmin to manage MySQL. With the help of this free software you can easily manage the database through a graphical interface in the browser. To carry out its installation, run the following command:

  sudo apt-get install phpmyadmin  

In the resulting screen select Apache 2 as the web server. Next, you are asked if the phpMyAdmin administrative database needs to be installed, to which you answer yes. Now you must choose a password (which can be the same as for MySQL) and the installation is complete.

The last step is to link phpMyAdmin with the Apache web server . This is done with the following command, on which the apache2.conf configuration file can be edited with the nano editor:

  sudo nano /etc/apache2/apache2.conf  

Place the cursor at the end of the configuration file (or repeatedly press the key combination? Ctrl? +? V?) And enter a new line in the file: 

  Include /etc/phpmyadmin/apache.conf  

Save changes made using the key combination? Ctrl? +? Or? and close the config file with? Ctrl? +? X ?. Next, update the Apache web server by including a new command in the console: 

  /etc/init.d/apache2 restart  

After completing the above steps, not only has the web server been fully configured, but its database can now be managed through phpMyAdmin on the web server. To do this, enter the IP address of the Raspberry Pi in the address bar followed by / phpmyadmin ("http: //192.168.XX/phpmyadmin") and log in to the phpMyAdmin page with the username? Root? and with the previously set MySQL password. As soon as you have logged in, you can include data sets in phpMyAdmin, create or delete databases and tables, and use other types of functions.

Raspberry web server application fields

Your web server is already configured and ready to use, so now you have the possibility to create and host a web page, equipping your server with HTML and PHP pages. You can place each of the pages directly on the Raspberry Pi under the / var / www (Raspbian Wheezy) or / var / www / html (Raspbian Jessie) directories. Another option is to create the pages on an external computer with a web editor of your choice and later transfer them to the web server using SFTP client software, which is achieved using programs such as FileZilla, WinSCP, PuTTY or OpenSSH.

Apache web server settings can be made using the .htaccess configuration file . In this case, it is recommended create a separate 404 error page for your web page or redirect the web page to another domain. If you want to know the 10 best tricks for .htaccess files, click here. Using Raspberry Pi as a web server gives you the opportunity to easily create web pages and learn how to manage and test how a web server works.

Along with hosting web pages, the server is also suitable for many other projects. In this way, it is also possible to use the Raspberry Pi web server as a private multimedia and data server. Likewise, with the free software ownCloud you can also configure and manage a private cloud in it. The file hosting program allows you, among other factors, to deposit files in the cloud and access and synchronize them. The list of projects that can be carried out with a Raspberry Pi web server is extensive and, if you have your own web server, you have the possibility of carrying out your own project at your fingertips.

Web server security

The security of the web server is an aspect that you cannot forget. In particular, when a server is permanently connected to the Internet, it is likely to be attacked sooner or later by malicious people. Therefore, it is necessary that it has the most recent updates and that you are not responsible for the web hosting. It is recommended that sensitive data, specifically, be hosted on a web server only when there is certainty of the steps to be followed to do so. 


...