Raspberry Pi Web Server: 5 Simple steps to make your own web server

From our STAR Maker: Alok Naushad.

Raspberry Pi Web Server

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?

  1. Raspberry Pi 2/B+
  2. Wifi router with access to internet
  3. 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:

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..

sudo bash

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.

Raspberry Pi Web Server

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.

Raspberry Pi Web Server

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.

Raspberry Pi Web Server

Next, we will need to configure the database phpmyAdmin should use. For that you should select Yes when asked:

Raspberry Pi Web Server

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:

Include /etc/phpmyadmin/apache.conf

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.

Raspberry Pi Web Server

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: ifconfig

Raspberry Pi Web Server

Note down the highlighted line in the above picture.

inet address: 192.168.1.35
Bcast: 192.168.1.255
Mask: 255.255.255.0

Now, we need the gateway and destination address. For that, type in the following command:
netstat -nr

Raspberry Pi Web Server

Take a note of the highlighted parts: 

Destination: 192.168.1.0
Gateway: 192.168.1.1 

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:

Raspberry Pi Web Server

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.

address 192.168.1.72
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1

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.

Raspberry Pi Web Server

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.

Raspberry Pi Web Server

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:

Username Password
admin admin
admin password
username password

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.

Raspberry Pi Web Server

In this page, you can see a tab called Advanced. You should now click that and select Port Fowarding.

Raspberry Pi Web Server

After that you should click ‘Add’ in the Port Fowarding Menu and type in as shown:

Raspberry Pi Web Server

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!

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.

Raspberry Pi Web Server

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.

Raspberry Pi Web Server

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.

Raspberry Pi Web Server

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

Raspberry Pi Web Server

Any problems? Please put it in the comments! Also try Googling.

Alok Naushad

Alok Naushad – STAR Maker at DIY Hacking

 

-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.

Recommended Posts
Showing 30 comments
  • toto40530
    Reply

    Really good job man !!

  • Steph
    Reply

    You did a wonderful job here- best I’ve seen :) Keep up the great work.

  • Peter Dodd
    Reply

    Thanks Great Article, performed your steps and everything is up and running. Basic page so far

  • abhishek srivastava
    Reply

    I have tried as suggested by u, but stuck at a point! please check and suggest for possible solutions:
    I have connected pi with ethernet cable to access pi using ssh “address:10.42.0.33”
    To get IP address I have connected wifi dongle to pi and made static wlan0 ip “192.168.1.72”
    I have Binatone DT 845W ADSL2+ model where I have used virtual server to forward port ip address “192.168.1.72”. Settings saved but I cannot access the same from outside network using my pi ip address.Address works in local network. What mistake I have done ?

  • Decrecondiz
    Reply

    Hi, installed my php admin and added it to the apache2.conf file, went to restart the server and it fails every time I get told to look into some files but I don’t know where they are.

    • Decrecondiz
      Reply

      Can’t access the website anymore either

      • Alok N
        Reply

        HI!
        can you send your apache2.conf file…?

  • programmer123454321
    Reply

    Just a note: your website is down again.

    • Alok N
      Reply

      Yeah.. Sorry for it!… It has been changed to a different URL: naysonet.ddns.net .. n It might not be on all the time.. My parents don’t allow me to do so…! Still, worth a shot…

  • Jacob
    Reply

    Hi Alok,
    I followed your instructions and had no problems, but when I try and link a CSS document to my page, it doesn’t load on my page. If I add the code using the tags, it does show up. The CSS file is located in the same directory as the HTML file. Here is my link code:
    What am I doing wrong?

    • Jacob
      Reply

      UPDATE: I fixed it! The permissions on my CSS file made it so the html file couldn’t access it.

  • Sundar Rajan
    Reply

    Hi Alok,
    It was a pleasant surprise to see such an excellent article coming from a 14 year old. I wish you all the very best for a bright future.

    I just followed your instructions and got everything going without any hitch. Thanks.

    Since I have a static IP provided by my service provider, I didn’t have to use DDNS.

    Now let me come my tricky situation. Hope you can think of a way to get over the issue. My router is already configured to do the port forwarding for my CCTV system. I temporarily changed the ip address to that of RPi (in place of CCTV’s IP address) to test things out. Now, does this mean I have to settle for one of the two? Is there a way to access both my CCTV and RPi?

    Regards,
    Sundar Rajan

    • Alok
      Reply

      Hi Mr.Sundar,
      Thanks for using my DIY! About what you asked- You can use your CCTV System and your Server together.. Happy news! but it’s just that they cannot be on the same port. An HTTP Server uses port 80. If your CCTV system does not use Port 80, it’s ok…you can have the Server at Port 80. If you want your Server at the first place, you can change the port of your CCTV System.. If you want your CCTV at the first place, you can change your Server port… Please check out this link to do so

      https://docs.google.com/document/d/1u2mpIkQeZPZ1CWIb59b0iXXIgjHZIaOa70vw6vS_XEI/edit?usp=sharing

    • Jacob
      Reply

      *using the tags

      • Jacob
        Reply

        *style tags. If I add style in angle brackets it won’t show up.

  • joe
    Reply

    My router is special and only allows us to use dyndns.org as a dns service. So Im screwed for domain names

    • Alok
      Reply

      Can u give the model no. of your router??

      • ghosh
        Reply

        Same for me as well.
        Cisco DPC3848V DOCSIS 3.0 Gateway

  • Frey
    Reply

    i have problem apache2 doesnt work on my web server
    here my problem :
    pi@raspberrypi:~ $ systemctl status apache2.service
    ● apache2.service – LSB: Apache2 web server
    Loaded: loaded (/etc/init.d/apache2)
    Drop-In: /lib/systemd/system/apache2.service.d
    └─forking.conf
    Active: failed (Result: exit-code) since Thu 2016-10-20 17:00:35 WIB; 46s ago
    Process: 26343 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
    Process: 26298 ExecReload=/etc/init.d/apache2 reload (code=exited, status=1/FAILURE)
    Process: 26400 ExecStart=/etc/init.d/apache2 start (code=exited, status=1/FAILURE)
    pi@raspberrypi:~ $

  • Gonza
    Reply

    Hi! Thanks for the guide! I have my RPi running a web server now.

    • Alok
      Reply

      You r welcome!!

  • Amit
    Reply

    Hi Alok,
    When i tried installing PHP i.e when i executed this command : apt-get install libapache2-mod-php5 php5 php-pear php5-xcache
    i got the following error :
    E: Package ‘libapache2-mod-php5’ has no installation candidate
    E: Package ‘php5’ has no installation candidate
    E: Unable to locate package php5-xcache
    Please HELP!!!
    Thanks

    • Alok N
      Reply

      Hi Amit,
      Can u reply which version of raspbian are you using?

      • Alok
        Reply

        U can use the command uname -a send me the whole result

  • Andrea
    Reply

    Hi

    I was wondering. Will I still be able to access the internet with my pi if my pi is not connected to the modem/ethernet?

    • Amit
      Reply

      hi Andrea,
      You will be able to access the internet if you’re Raspberry Pi is connected via Wi-Fi or else you wont

  • Jack
    Reply

    Great guide! On a related note, your website (naysopi.ddns.net) is down.

    • Alok N
      Reply

      Sorry! Actually it is… I am working on a new project. So, I have kept my server off. It will come back soon.

Leave a Comment

46 − = 39

Start typing and press Enter to search