Raspberry Pi Web Server: 5 Simple steps to make your own web server
From our STAR Maker: Alok Naushad.
Everyone fancies having a website or server of their own on the internet. May it be for blogging or just for storing files, having your own web server in the internet is something that’s very powerful. In this DIY tutorial, I am going to show how to set up a Web Server in your home by just using a Raspberry Pi. Using this, you could create your own personal Raspberry Pi Web Server which helps you to access your files from any part of the world using the internet. And no need to worry about the DDNS Client, you can get it for free from no-ip.com. Here, we will be using Apache, PHP and MySQL to build this Raspberry Pi Web Server. So let’s get started!!
What are the stuff required to do this project?
- Raspberry Pi 2/B+
- Wifi router with access to internet
- Wifi dongle for Raspberry Pi or LAN cable
How does it work?
In this project we will be first setting up your Raspberry Pi as a local server by installing Apache, PHP and MySQL and test it. After doing this, this local server would be converted into a web server through port forwarding technique from your router. After performing port forwarding, your Raspberry Pi will be accessible through the internet as your router will point to it. Check out this block diagram for the detailed step by step process to build your Raspberry Pi web server:
Lets start by first configuring your raspberry pi and installing Apache, PHP, MySQL and phpMyAdmin.
STEP 1: Installing Apache, PHP and MySQL for your Raspberry Pi web server
Before we start, make sure your Raspberry Pi is up to date and if not update it by running the below command in your terminal:
sudo apt-get update
Next, we will start installing all the packages necessary for our raspberry pi web server. But before that, perform the command below. This helps you to get into the root of our system. So from now on, we do not need to use ‘sudo’ command all the time..
Now lets install ‘Apache’ the main component of the project. For that, use the following command:
apt-get install apache2 apache2-doc apache2-utils
After this, it is time for PHP to get into our pi:
apt-get install libapache2-mod-php5 php5 php-pear php5-xcache
These won’t take long. Now we should install the package for database connectivity (PHP->SQL):
apt-get install php5-mysql
Now we can use install the MySQL server which stores everything, our database for the raspberry pi web server:
apt-get install mysql-server mysql-client
During this installation, you will be asked for a password for the server. Enter a password that you will remember. That’s it! You have completed the first step! Now reboot the pi.
STEP 2: Testing the installed packages on our Raspberry Pi
Type ‘localhost’ or ‘127.0.0.1’ in the pi’s web browser. If you have ssh’d into it, type the pi’s ip address in your browser. Now you should see a page saying ‘It works!’. If you see it, the server is set up correctly or else you should repeat with the commands listed above and check for errors.
You can edit the web page by changing the index.html file in the ‘/var/www’ directory. You can put anything into that folder and you can see it in your local area network when pi’s IP address is entered. This is a good tutorial for learning about html and web page editing: html tutorial.
Now, for testing mysql on your raspberry pi, open up LXTerminal and type as follows:
sudo mysql -uroot -p
This should ask you for the password you entered before during the installation, enter it. If you have entered the correct password you should get a screen like this.
Here, using MySQL control your databases. A more easier way to do that is down below.
STEP 3: Installing phpMyAdmin for your Raspberry Pi web server
phpMyAdmin is a free and open source tool written in PHP intended to handle the administration of MySQL or MariaDB with the use of a web browser. This makes it easier to control the database. So, to install it for your raspberry pi web server, you can use the following commands. Open the terminal and let’s start with:
sudo apt-get install phpmyadmin
This will start installing the packages. You will be asked which Web Server is to be used. Choose apache2.
Next, we will need to configure the database phpmyAdmin should use. For that you should select Yes when asked:
After this, you will be asked for an administrative password. Type in the password of your MySQL database you installed earlier. Next, you will be asked for a custom password for phpMyAdmin. I typed in the same password of MySQL, as it will be easier to remember. If you wish, you can have another password also.
Now, we need to combine apache with phpMyAdmin. For that, you should edit the apache2.conf file. For that, type the following in the terminal:
sudo nano /etc/apache2/apache2.conf
At the end of this file (navigate using Ctrl+V), add a line:
Now restart the server using:
sudo /etc/init.d/apache2 restart
Now, check out the link in your web browser: <pi’s ip address>/phpmyadmin or localhost/phpmyadmin in your pi. You should see the page below.
As the username, you can type in ‘root’ and as the password, you can type in the password you gave for the MySQL Server. If that doesn’t work, just try with the phpMyAdmin password. If you can login successfully, Voila! That’s done.
Now, you can have your own site by customising the index.html or by inserting customised html file and renaming it as index.html in the www folder. You can put images, videos or whatever you want in the www folder and access it in your Local Area Network(LAN). Now that you also have a database of your own and you can maintain the site with it. So we can call it your ‘Server’, a Raspberry Pi web server.
Now, we are going to make our local server available to the whole world! Follow the steps below to do it:
STEP 4: Give your Raspberry Pi a static IP address
Now, you wont be able to access your server from another network. You can only access your server within your Local Network. What we are going to do now is that we are going to make our server available worldwide, to the Internet! Sounds interesting right? Let’s start connecting our raspberry pi web server to the internet.
The Modem in your house gives a number to all devices connected to it to understand the devices. This number is called Internet Protocol (IP) address. It is something like this xxx.xxx.x.x . There are 2 types of them: Internal IP Address and External IP Address. What I told above was about Internal IP address: a number given from the modem to the connected devices to locate it. There is a number given to your modem by the Internet Service Provider (ISP) to locate computers and systems. This number is called as an External IP Address.
We are going to talk about the Internal one. What our modem gives us is a random number. Mostly, it starts like 192.168.1.xxx or 192.168.0.xxx or 192.168.xxx.xxx. The random number is called by a name : DHCP. The hostname is also similar to the IP address. It is a label given to devices connected to a network to identify them. The IP Address of the pi is set to DHCP. We need to change that into a static IP address. For that we need to edit a file in our pi. Before that we need to gather some information about our network. For that, type in the following commands. Have a paper and pen with you before that!
Perform the command:
Note down the highlighted line in the above picture.
inet address: 192.168.1.35
Now, we need the gateway and destination address. For that, type in the following command:
Take a note of the highlighted parts:
Now let’s edit the network interfaces. It is located in /etc/network/interfaces.
sudo nano /etc/network/interfaces
In this file, you can see something like this:
The highlighted part, which is iface eth0 inet dhcp should be changed to: iface eth0 inet static
Under that line, add the information we gathered earlier in the below format.
Note: Change the below written numbers with yours. I have written mine.
What are these numbers?
address: It is the inet address, you got from ifconfig command. You can give any number starting with 192.168.1.xxx. The xxx can be replaced with any number. It is better you have it between 2 and 100. Choosing a higher number is recommended since when you have a lower number it may result in clash of IP addresses. Two devices might have the same address. That is the reason I gave it a higher number.
Netmask: It is the Mask address in the ifconfig command.
Network: It is the Destination address you got from the netstat -nr command.
Broadcast: It is the Bcast address you got from the ifconfig command.
Gateway: It is the gateway address you got from the netstat -nr command.
Finally, it looks like this in my file.
Now, press Ctrl+X to save it and press y. Then remove the existing leashes using this command in your terminal:
sudo rm /var/lib/dhcp/*
After this you should reboot your pi and check the ifconfig command. Remember, you can’t ssh with your old ip address now, check ssh ing with the new one. In mine, ifconfig looks like this.
If inet address shows your new IP Address, congrats! You got a static ip address.
STEP 5: Setting up Port Forwarding and DDNS to your Raspberry Pi web server
Now, we are going to do something out of our Raspberry Pi. We are going to play with our Router or Modem. We need the gateway address you wrote down earlier. You can do this procedure in your computer or pi. I recommend doing it on your PC. For that, open up your browser and type in the gateway number in the URL column and press Enter.
You will see either a login screen or a box coming up asking for your username and password. Sometimes, it might be there written on your modem or else, you should try guessing. Don’t worry, I will give you some options:
If these doesn’t work, put it blank and click login, try it. Or call the customer care of your modem’s company.
After logging in to your modem, you can see a page similar to this. This is what I see in my D-Link DSL-2750U Modem. You should also see something similar to this.
In this page, you can see a tab called Advanced. You should now click that and select Port Fowarding.
After that you should click ‘Add’ in the Port Fowarding Menu and type in as shown:
Select the Service as ‘Web Service (HTTP)’ and type in the Server IP Address which is the IP Address of your Pi. The External and Internal Port start and stop should be of 80. After that click Apply. Now,we have fowarded the 80 port, which is of our Raspberry Pi Web Server to the 80 Port of your Router.
Now what we have to do is, setup the DDNS which stands for Dynamic DNS (Domain Name Setup). We are using a site called No-IP for this. It helps us to get a URL for our router, which has forwarded the port 80. So lets create a URL for our Raspberry Pi Web Server!
First, we need to sign up for an account in www.no-ip.com. This service is for free. You can either select a subdomain now or select one later. There are 4 subdomain options. You can also choose a domain by paying. But in this DIY tutorial, I am going to show you to get a subdomain URL for your Raspberry Pi web server.
What Dynamic DNS does is simple. I told earlier that our ISP (Internet Service Provider) gives us an External IP address, that is random. No-IP.com changes our IP Address into a URL. We also configure this in our Router which will be shown soon. So, whenever our modem gets a new IP, it sends it to No-IP.com by which they connect the new IP Address and our static URL for our Raspberry Pi web server. The URL never changes but the IP does.
After account creation, you should activate your account using the e-mail sent by No-IP.com. You can sign in to your account after the activation of the same. You can see your new URL in the submenu Hostnames under Dynamic DNS Menu.
You can check your URL by clicking on it. As your Raspberry Pi Web Server is just set up, you can see the It Works! Page.
Our Next step is to configure the router for the No-IP.com’s Dynamic DNS. For that, you should again go to your Router’s page and login. After logging in, you should select the Advanced Tab Again and in that you should find an option Dynamic DNS.
If it is not in the Advanced Tab in your Router you should find it somewhere else. All the new Routers have this menu. If you don’t find it still, try updating you Router’s Firmware.
Click Add in the Dynamic DNS Menu of your Router. Fill out the details.
Click Apply and There you go! Everything is done. You have your own Raspberry Pi Web Server. You can access it with just one URL. Accessible to the whole World! All the best with your Server! You can make amazing websites easily using the templates available on the internet.
NaySo Technologies is my future company. That is why I gave the name naysopi for my website. Don’t forget to check out my website running on my very own Raspberry Pi web server: naysopi.ddns.net
Any problems? Please put it in the comments! Also try Googling.
-Submitted by Alok Naushad
I am Alok N, a 14 year old maker from Kalamassery, Kerala. I love working with Raspberry Pi and have done many projects with it. I love emerging technologies and I am basically an Android Developer. I am interested in Computer Programming, Robotics, App Developing.
I Love DIYHacking.com and this is my favourite site. I try most of the projects posted here.