In this tutorial, I am going to show you 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 allows you to access your files from any part of the world using the internet. no need to worry about the DDNS Client, you can get it for free from no-ip.com. We will be using Apache, PHP, and MySQL to build this Raspberry Pi Web Server. So let’s get started!!
Required Materials
- Raspberry Pi 2/B+
- Wifi router with access to the internet
- Wifi dongle for Raspberry Pi or LAN cable
Resources
Basic Linux Commands for Beginners
How Does it Work?
We will be first setting up your Raspberry Pi as a local server by installing Apache, PHP, and MySQL and testing it. After doing this, this local server can 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.
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 command below 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 in to the root of our system. So from now on, we do not need to use the ‘sudo’ command all the time.
sudo bash
Now let’s 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 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.
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. If it doesn’t, 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 the Pi’s IP address is entered.
Now, for testing myself on your Raspberry Pi, open up LXTerminal and type as follows:
Now, for testing myself 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.
You can now use MySQL control your databases. An easier way to do that is down below.
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. To install it on 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 too.
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.
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 log in successfully, Voila! That’s done.
Now, you can have your own site by customizing the index.html or by inserting a customized 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, 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:
Giving your Raspberry Pi a Static IP Address
Now, you won’t be able to access your server from another network. You can only access your server within your Local Network. We are going to make our server available worldwide to the internet! 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 Addresses and External IP Addresses. What I mentioned above was about the 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 your 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 or 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
Note the highlighted line in the above picture.
inet address: 192.168.1.35Bcast: 192.168.1.255Mask: 255.255.255.0
Now, we need the gateway and destination address. For that, type in the following command:
netstat -nr
Take a note of the highlighted parts:
Destination: 192.168.1.0Gateway: 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:
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 numbers written below with yours. I have written mine.
Note: Change the numbers written below with yours. I have written mine.
address 192.168.1.72netmask 255.255.255.0network 192.168.1.0broadcast 192.168.1.255gateway 192.168.1.1
What are these numbers?
address: It is the inet address, you got from the 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 a lower number may result in a clash of IP addresses, meaning two devices might have the same address.
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.
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 the inet address shows your new IP Address, congrats! You have a static IP address.
Setting up Port Forwarding and DDNS on 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 that you wrote down earlier. You can do this procedure on 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 come up and ask 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, make it blank and click login. Or call the customer care of your modem’s company.
After logging into 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 now the IP Address of your Pi. The External and Internal Port start and stop should be 80. After that, click “Apply”. Now, we have forwarded the 80 port to the 80 Port of your Router.
Now we have to 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 let’s 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 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 mentioned earlier that our ISP (Internet Service Provider) gives us a random External IP address. 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 No-IP.com’s Dynamic DNS. For that, you should again go to your Router’s page and log in. 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.
NaySo Technologies is my future company. That is why I gave the name NaySoPi for my website.
Any problems? Please put them in the comments!
[/vc_column_text][/vc_column][/vc_row]
[/vc_column_text][/vc_column][/vc_row]
Comments
Post a Comment