Just a few days ago I had the need to show a web application I was working on to some family members so that they could give me their point of view on development and tell me, or rather guide me, where I should continue. To do this, the truth is that I did not want to have to buy space from a company, or a web address, or anything like that. With this in mind, my only 'salvation'It was to set up my own server with what little I had at home and that is where the aid comes into play Raspberry Pi.
If you have ever worked developing a web application, surely you will know all those free programs like LAMP, an acronym for Linux Apache MySQL and PHP, that is, a program that just by installing it on your Linux computer you can run dynamic HTML web pages since it installs everything you need for this. Of course we also have other versions for Windows, in this case WAMP and even XAMP for other operating systems.
Table of Contents
How to configure a Raspberry Pi to be able to use it as a web server.
In the case of our Raspberry Pi we need to install the LAMP version so that you can host any type of web application, web page ... or whatever you need. For this, before continuing you will need a Rasbperry Pi, as expected, a SD memory card 4 GB minimum capacity, a power adapter compatible with the MicroB connector of the Raspberry Pi, a connection cable Ethernet, monitor HDMI compliant and a HDMI cable, for an keyboard or even a mouse although this is not necessary.
Before continuing, the first thing we have to do is prepare our Raspberry Pi. In case you have never done it before, tell yourself that you need the same boot from an SD card which should contain a disk image of the operating system you want to run.
One option for this is to buy an SD card that is already prepared to install and boot our Raspberry Pi or leave it completely empty and install everything necessary ourselves. In my specific case, I opted for this last option. To prepare the card we need the image of the operating system, I opted for Raspbian "Wheezy". Once I had the ISO I used the program Win32 Disk Image.
Once we have our SD card with everything we need, we just have to insert it into our Rasberry Pi and, before even starting, install all the peripherals that we are going to use, that is, have the screen, keyboard or mouse connected in case you want to use it.
When everything is ready, we just turn on our Rasbperry Pi and we will see how the system fully automatically lists all the information about the operating system and the elements that we have connected. Once all this work is done you will see the window raspi-config where you must make the following changes:
- Expand the root partition so that all the space on the SD card can be used.
- Set a time zone.
- Enable the SSH server, this is in Advanced Options.
- Deactivate the startup on the desktop, since all the configuration will be done from the terminal.
- Update the Raspberry Pi, this option is found within Advanced Options.
- Restart your Raspberry Pi, for that we just have to write Sudo reboot.
Preparing the SSH connection to connect remotely to the Raspberry Pi
At this point it remains to start configure SSH. This is so that you can work with your Raspberry Pi from another computer, that is, you can connect remotely and upload new files or change the configuration.
Once your Raspberry Pi has been configured in previous steps, the system will ask you for a username and password, because it is the first time we start it, it will have the default users, if you have not changed them, they will be pi and as password r.
At this point you have to take into account how Linux itself works since, even though you are writing the password, no characters of any kind are displayed, do not worry since the text is being written.
Just when we log into the system we have to write:
Thanks to this command we will be able to know the IP address that our controller has. Within the extensive output we will have to look for the line "inet addr”We can find a number similar to this: 192.168.1.1. I say similar since surely the last 1 is a completely different number. This number in full, in the case of the example 192.168.1.1, we will have to copy it since we will need it to access via SSH from another computer.
At this point we will have to download an SSH client, in my case I have opted for Putty, well known in practically all environments. The IP address that we copy now we will have to copy it in Putty in the countryside "Host name (or IP address)”. Just below you will be asked for a username and password that will be the same that we use to access our Raspberry Pi, that is, pi y r.
Once we have had access to the system, it is not a bad idea to change the password and finally update the entire system. For that we execute the following commands by pressing enter after typing each one:
sudo passwd pi sudo apt-get update sudo apt-get upgrade
We finally install LAMP in the system
Finally we get to the point of installing LAMP and for that we execute the following command:
sudo apt-get install apache2 php5 libapache2-mod-php5
Once the command is executed, the system will ask you if you want to continue, you just have to type y and hit enter to continue. As a detail, tell you that this installation process may take a few minutes. In case you may have some kind of error during the installation, in my case there were none, run the following commands:
sudo groupadd www-data sudo usermod -g www-data www-data
and restart Apache with the command:
sudo service apache2 restart
As a final check, you just have to go to any computer you have at home, start a browser and put in the address bar the IP of your Raspberry Pi where you should be able to see a screen that says It works!, this means that the installation has been successful and Apache is up and running.
It's time to install the database
We install what is necessary to access our own database
To have access to our own database we just have to install MySQL and for that we execute the following command:
sudo apt-get install mysql-server mysql-client php5-mysql
Again it will ask us if we want to continue with the installation and for that we will only have to register y and hit enter.
We install FTP on our Raspberry Pi
In this step we will install an FTP to be able to send files from any computer to our Raspberry Pi and from the Raspberry Pi itself to a computer where we may need them. This is a process as simple as executing a few commands such as:
sudo chown -R pi /var/www
The next command to run would be:
sudo apt-get install vsftpd
Once the whole process has finished we have to edit the vsftpd.config file and for that we just have to write:
sudo nano /etc/vsftpd.conf
When the file editor opens, we will have to change the following lines:
anonymous_enable = YES happens to be anonymous_enable = NO
uncomment local_enable = YES
uncomment write_enable = YES
at this point you have to go to the end of the file and add force_dot_files = YES
As a detail, tell you that to uncomment the previous lines, you just have to remove the # sign in front of them. Once the previous steps have been carried out, press ctrl + X e y to save all modified data. The next thing is to restart the FTP service again with the following command:
sudo service vsftpd restart
With these steps our web server would already be working perfectly waiting to receive the files from our web application to be able to view them directly from the browser.