Sie sind auf Seite 1von 5

Ben

Walters

About Me

Blog

Setting Up a Raspberry Pi Web Server


FEBRUARY 12, 2016

2016
Ben
Ever sinceit's
introduction
early2012,
the Raspberry Pi has proven itself to be an extremely capable little machine. For less than
$30, you Walters
get a credit card-sized computer capable of automating home systems, powering robots, or even serving as a basic
desktop computer.
This tutorial however, focuses specifically on getting your Raspberry Pi set up to run as your very own web server. In addition, we'll
cover how to set up Dynamic DNS records, so you can access your sites/files even when you're away from your home network

13 Comments

Ben Walters - Blog

Recommend

Share

Login

Sort by Best

Join the discussion

without having to remember an always-changing IP address.


"That's cool and all...but what am I going to do with my own web server?"

MassivFotze

24 days ago

http://blog.peter-r.co.uk no longer loads up (404/403). Where do I go now to download the necessary files?

Great question! The quick answer is: Whatever you want! To be more specific, you can:

Ben Walters

Set up your own, private Dropbox-style cloud storage for your personal files/videos/images
Create a site that interfaces with your home security cameras and check them remotely
Host your own low-traffic webpages
Etc.
Beyond the web server-specific functions, a Pi with dynamic DNS set up can be used to:
Host your own Minecraft server (tutorial)
Run your own Git server (tutorial)
And much more!
Disclaimer: The Raspberry Pi is great as a lightweight web server for personal use and experimenting. However, if you are
interested in hosting a heavily trafficked site like a blog, I'd highly recommend hosting your content on a third-party web host. In
addition, most ISP's aren't particularly interested in letting their customers host their own web servers, and a self-hosted web server
with a lot of external requests can raise some flags and even slow down your home network connection.

Reply Share

Mod


Chris Pro

Reply Share

4 months ago

Ben, to reiterate what everyone else has said, great tutorial! I have one issue that I am not really clear on. I have two
name servers from cloudflare and they have been pending for two days now. You mention in the blog that "These will
need to be entered in your domain record in order to complete the transfer process" Being a complete noob I am
unsure where I need to enter these? Is it somewhere in the namecheap site? Appreciate any help here.

Reply Share

Ben Walters

Mod

I'm glad the tutorial was helpful to you! As far as entering the nameserver records in Namecheap is
concerned, I've updated the post to include more detailed instructions on that step.
Basically there should be a "Nameservers" section under your domain's settings in Namecheap that you
enter the Cloudflare-generated nameservers. Feel free to take a look and let me know if you have any more
questions!

Step One: Set up your Pi

The first step in setting up any new Raspberry Pi is to format an SD card for installing the Raspbian OS on. If you purchased/your
Pi came with a pre-formatted NOOBS SD card, you can skip these first few steps as your SD card already has Raspbian ready to
go.

Reply Share

Chris Pro > Ben Walters

For this step, you can choose to interact with your Pi directly using a USB mouse, keyboard, and monitor or SSH in via another
computer on your network (my preferred method).
NOTE: This would be a perfect opportunity to set a static IP address for your web server, so you can always access it at the same
local address. This can be done via your router's web interface.

Once you know the LAN IP address assigned to your new server, enter the following command in the terminal on your other
computer, making sure to replace the IP address accordingly:
ssh pi@192.168.0.106
You'll be asked for your root password (the one you set during the installation process, otherwise is "raspberry" by default).
OPTIONAL: If using the default password, you can change it by entering the following command after logging in with the default
credentials:
passwd

> Chris Pro 4 months ago

Reply Share

5 months ago

Hi Ben, thanks for the run-through. Near the conclusion, after testing
the configuration I did not receive a "Success." I got "Warning: unable
to determine IP address." Is there somewhere I have to manually enter
my Pi's IP address (other than ssh)?
Am I supposed to enter the API codes somewhere?

Reply Share

maddaddam > maddaddam

5 months ago

I figured our where I left out the API and think I figured out where the IP gets
entered at Cloudflare. But still no IP address in the test.
I did get this comment at the top when testing:
"WARNING: skipping host: web-dyndns: 'zone=' is an invalid fully qualified host name."

... I tried different forms for that value in /etc/ddclient.conf but couldn't get rid of the warning. Can you tell me
what form it should be in?

Reply Share

Ben Walters

To SSH, you'll need to determine the local IP address assigned to your Pi, which can easily be done by consulting your router's web
interface (http://192.168.0.1 for Netgear routers) and inspecting "Attached Devices". Look for "raspberrypi" in the description
column.

Mod

Great, I'm glad to hear! And thanks! :)


maddaddam

LAMP stands for Linux, Apache, MySQL, and PHP. It is a term commonly used to refer to a software stack for development and
deployment of web applications and web sites on servers.

4 months ago

Reply Share

Ben Walters

Once you've completed the Raspbian installation process, you can go ahead and plug your SD card into your Pi along with your
HDMI, Ethernet and USB mouse/keyboard. Fiinally, add some USB power to boot it up for the first time!

Step Two: Setting Up LAMP

Ha! I was close but this narrowed it down perfectly. Again, a big thanks for taking the time to post an
amazing tutorial!

For those of you with brand new, unformatted cards, you'll want to follow raspberrypi.org's excellent guide to getting Raspbian
installed on your SD card.

NOTE: If your first Raspberry Pi boot completes without any prompts, it will eventually end you on the GUI desktop. To ensure SSH
is enabled from here, click "Menu" in the top left and navigate to "Preferences > Raspberry Pi Configuration". In the window that
opens, switch to the "Interfaces" tab and make sure "SSH" is set to "enabled".

> Chris Pro 4 months ago

Hey Chris!

With all that out of the way, let's get started!

Next go ahead and complete the Raspbian installation, making sure to enable SSH in the configuration process (great video
tutorial available here).

> MassivFotze 23 days ago

Thanks for the heads up! I've updated the post with a new source for the ddclient update. Please let me know
if you have any further issues!

Mod

> maddaddam 5 months ago

Hi maddaddam! Thanks for your reply.


I'm sorry to hear you're having some troubles - it sounds like the issue lies in how you're entering your
domain name in the '/etc/ddclient.conf' file. If your domain name is "testdomain.com", you'd enter
exactly the following (including the backslashes and commas):
use=web, web=dyndns
protocol=cloudflare, \
server=www.cloudflare.com, \
zone=testdomain.com, \
login=cloudflarelogin@gmail.com, \
password=apikey1234abcd \
testdomain.com
Regarding the "...unable to determine IP address" error, it may be related to the fixing the last line
above (your domain name). Give those fixes a try and feel free to comment back if you have any more
questions!
1

Reply Share

maddaddam > Ben Walters

Now that you're connected to the Pi, you'll want to update your Raspbian package list and install any new versions as necessary:

Reply Share

Mod

> maddaddam 5 months ago

In my experience, those are the hardest errors to fix and the most annoying! But I'm
glad to hear you were able to get it working.

Next, install Apache:

You're very welcome! If you continue to have issues, feel free to share. I'd be happy to
help!

sudo apt-get install apache2 -y

Are you a developer? Try out the HTML to PDF API

Ben Walters

sudo apt-get upgrade

open in browser PRO version

5 months ago

Thanks again for the tutorial. It's been a good learning experience, and hopefully I can make
use of it once it's up.

sudo apt-get update

Once the Apache installation process has finished, you can go ahead and check to make sure your server is working by navigating

Thanks. In reading your reply and checking my ddclient.conf I noticed I had mis-typed
"use=web, web-dyndns". So it seems the dozens of checks and changes and web searches
and hours spent were unnecessary as the error just came down to a single typo. Haha.
Anyway, it's all working good now. The site still isn't live, however, I think the issues now are
with cloudflare and namecheap. If you're interested, I'll keep you updated.

OPTIONAL: By default, the full Raspbian image includes a number of extra features/libraries that most users won't need. You can
run a script (follow the instructions here) to optimize and remove these unecessary features and speed up the boot time of your Pi.
Note, this may take a while to run, so go grab a snack!

1
Barnaby Johnson Fry

Reply Share

5 months ago

pdfcrowd.com

to your Pi's IP address in a web browser (i.e. http://192.168.0.106).


If everything is configured correctly, you should see a page displaying the words "It works!".

Thanks for the tutorial! I'm interested in setting up my own Raspberry Pi Webserver, and I was glad to see your blog
post. My question is in regard to the Dynamic DNS Settings. You have provided directions for your DNS provider,
Name Cheap. I'm not using them, and I'm confused about how to proceed, unless I were to transfer my domains
over to their service, which I would rather not do. (FWIW, I'm a customer of namesilo.com). Do you have any advice
for setting up DynDNS?

Reply Share

Ben Walters

Mod

> Barnaby Johnson Fry 5 months ago

Hi Barnaby! I'm so glad my post has been helpful! As far as setting up DDNS with Namesilo, I don't have any
personal experience with their services. However, some quick searching suggests it's very possible.
It sounds like they have an API which will allow you to update your DNS records (overview here:
https://techjourney.net/update....
In addition, there's some documentation regarding the API specifically here: http://www.forkrobotics.com/20...
The API documentation I linked above is for Windows Powershell, but I think the information might be helpful,
nonetheless.
Feel free to post back your experience! I'm looking into switching my DNS provider in the future (heard good
things about Cloudfare.com).

If nothing shows up, try restarting the Apache service:


sudo service apache2 restart
The next step in the LAMP setup is to install MySQL, which is a powerful open-source Database Management System used to
manage/save application data:
sudo apt-get install mysql-server mysql-client php5-mysql
You will be prompted with several windows asking for a default "root" password and confirmation. Feel free to enter whatever
password you like, but be sure to take note of it for future use.
Once the MySQL installation is complete, it can be activated using the following command:
sudo mysql_install_db
You'll be asked for the root password you set during the installation process (you can change it here if you'd like), followed by a
number of configuration questions. In most cases, you can simply answer "y" to all of these and proceed through the rest of the
process.
Now that MySQL is installed and configured, we'll install PHP:
sudo apt-get install php5 libapache2-mod-php5 -y
Once the PHP installation is finished, you're ready to configure Apache to serve your pages, bypassing your ISP's restrictions on
port 80.

Step Three: Configure Apache


Now that all parts of our LAMP stack are present, let's set up Apache to listen on port 5050 instead of the default 80.
This is done to bypass many ISP's blocking port 80 to prevent their customers from hosting their own web servers (shhh....) unless
they upgrade to a business-tier service.
The first step is to modify the Apache configuration file:
sudo nano /etc/apache2/ports.conf
Change the following lines:
Listen 80
To:
Listen 5050
Exit the nano editor and save your changes by pressing 'Ctrl + X', 'y', and finally 'Enter'.
Restart Apache again to apply the changes:
sudo service apache2 reload
Confirm that your changes are working by navigating to your Pi's IP address in your browser again, only this time append ":5050" to
the URL (i.e. http://192.168.0.106:5050).

Step 4: Dynamic DNS


The next step is to set up Dynamic DNS for a domain name of your choosing. By default, your ISP assigns your web server a
random, public IP address (ex. 11.222.33.444). Unfortunately, in most cases they will frequently reassign this IP address, making it
very difficult to maintain a reliable connection to your web server. Thankfully Dynamic DNS allows us to bypass this issue
altogether by having your new web server regularly report and update it's own public IP address in your domain name's DNS record!
There are many excellent DNS providers, but for the sake of this tutorial, I'll be following the steps I took to configure my DNS for
cloudflare.com. I recently switched to Cloudflare for my DNS services since they offer more features and DDoS protection than my

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

previous provider. As a bonus, they offer all this in their free tier of service!
First though, if you don't already own a domain name you'd like to use, you'll need to purchase one. I recommend Namecheap. I've
been using their services for years and have had a great experience. However, feel free to use whomever you'd like!
Once you've got a domain name ready to use, go ahead and create/login to your Cloudfare account. Next, enter your new domain
name into the search field and let Cloudfare pull your domain records:

When the search is complete, click "Continue", confirm that all of your DNS records were successfully pulled and click "Continue"
again.
Next, you will be prompted to select what level of service you want to use. You can go ahead and select the "Free" tier:

You should be redirected to your Cloudflare dashboard and see something like the following:

There will be two nameserver records displayed. These will need to be entered in your domain record in order to complete the
transfer process.
To do this, simply log into your Namecheap account, navigate to your dashboard, and click "Domain List" on the left-side menu.
Click "Manage" on the domain for which you are transferring DNS control to Cloudflare, and enter the aforementioned nameserver
records under the "Nameservers" section:

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

After updating your domain record with the Cloudflare-provided nameservers, it should only take an hour or so to complete the
transfer (they say up to 48, but that's a generous estimate). Once it's done, you'll see the following on your Cloudflare dashboard:

Awesome! Your domain name record and DNS provider should now be configured and ready to accept DNS updates from your web
server.

Step 5: DDNS Configuration on Apache


First, we'll need to install 'ddclient', a DDNS client for Apache on your web server. Run the following command:
sudo apt-get install ddclient
During the setup process, you'll be asked for some details. Feel free to enter whatever you'd like here, as we'll be changing most of
it in a second anyways.
Now before we go any further, we need to patch our ddclient installation. By default, the Raspbian repository doesn't include an
updated version of ddclient. Fortunately, we can patch our install with the following commands (credit link):
sudo apt-get install curl sendmail libjson-any-perl libio-socket-ssl-perl
curl -o ddclient-cf-patch-3.8.1.patch https://googledrive.com/host/0Bxw4R9RJ0ozIZGw1aWw3VlpZ
ZkE
sudo patch /usr/sbin/ddclient < ddclient-cf-patch-3.8.1.patch
Next, we're going to update our 'ddclient.conf' to communicate with our new DNS profile on Cloudflare, but first need to get our
Cloudflare API key.
This can be done by logging in to your account, going to your settings (dropdown in the top right), and scrolling down to the "API
Key" section:

Once you have your API key copied down, go ahead and enter the following command to open your ddclient configuration file for
editing:
sudo nano /etc/ddclient.conf
You'll need to change to the following:
use=web, web=dyndns
protocol=cloudflare, \
server=www.cloudflare.com, \
zone=yourdomainnamehere, \
login=yourcloudflareloginemail, \
password=yourcloudflareapikey \
yourdomainnamehere
Exit the nano editor and save your changes by pressing 'Ctrl + X', 'y', and finally 'Enter'.
To test our configuration, and set the initial values, run:
sudo ddclient -daemon=0 -debug -verbose -noquiet
You should see a list of debug commands followed by a "SUCCESS" line reporting that your server's current IP was set on your DNS
record.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Let's quick recap what we've accomplished.


First, we installed all of the necessary web server components on your Raspberry Pi.
Second we configured our Apache web server to listen for requests on port 5050 instead of the default 80.
Third, we set up Dynamic DNS on our newly registered domain name with Cloudflare.
Finally, we set up DDNS on Apache to regularly update our Cloudflare DNS record with our web server's randomly assigned
public IP address.
NOTE: It may take a few minutes to a few hours for your DNS record to update and correctly resolve requests to your domain name
to the public IP of your web server, so be patient.
And that's it! Congratulations - you now have a fully functional Raspberry Pi web server!
Please feel free to share any questions/comments you may have below!

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com