Archives for February 2016

USB LAMP Web Server

servericon

Your Personal http Server

If you would  like to have your own personal web server, running industry standard software, all from a boot-able USB stick, read on…

One of the most useful tools for home automation and web development is a host server. And the most widely used server is of course, Apache.

This article marks the start of a 5-post series that provides step-by-step instructions for setting up your own USB memory stick based http server. We’ll start with the basic LAMP stack in the first three articles, and then add-on some extras to extend the functionality.

What’s LAMP?

LAMP covers the essential ingredients of a web server:

  1. Linux – The operating system
  2. Apache – http server
  3. mySQL – database
  4. PHP – server-side scripting language

Here are the topics planned for this series:

Update: Bonus Topics

Part 1 – Setting up the Linux Apache Server

When finished with part one, you will have the LA (Linux and Apache) LAMP components up and running.

Let’s get to it…

Prerequisites

  • Blank USB Memory stick (4GB minimum)
  • PC with USB port that can be dedicated for the server

While I cannot claim to be a Linux expert, I do have some experience working with the single user Puppy Linux distribution. Being a familiar setting, that is what was selected for this exercise.

But hold on, before you start there is one more selection to make….

You see, there are several versions of Puppy Linux to pick from. My first attempt at this used the most current version, called Slacko Puppy 6.3. However….sparing you the details, I ran into some insurmountable obstacles to completing a successful installation of Apache server with Slacko Puppy.

So I moved on to a Puppy Linux version that works with the Apache Server.

The version that worked is called Precise Puppy 5.7.1

Installing the Linux Operating System

As with many Linux distributions, the installation is really quite simple. Just download the ISO image and a USB installer. That’s it. Here is where you can get both:

Now for the installation. Just install your USB stick and start the installer program. The start up screen will provide 3 easy-to-follow instruction steps.

  1. Select the distribution (Select “Precise Puppy”)
  2. Select the iso file (from the folder that has the downloaded ISO image)
  3. Select the USB stick drive letter.

UUSBinstaller

Then click on the “Create” button to start the installation.

Once the installer is finished, you can boot any PC to the newly installed Puppy Linux simply by putting this memory stick in the PC’s USB port and rebooting (Boot to USB first must be selected in the bios first, of course).

After the computer has booted to Linux and you have completed the self-guided first boot process, you need to perform one additional step before installing Apache.

NOTE: It is recommended that a wired internet connection be used for this and all processes during installation.

First, you need to update the package database. This is a very simply process. Just launch the Puppy Package Manager (Menu->Setup->Puppy Package Manager) and click on the “Configure package manager”. A window will appear with an “Update now” button.

updatenow

Click that button and press enter each time the yellow window prompts you. There will quite a few “Enter”s required to complete this step.

Once complete, exit the Puppy Package Manager. If this is still your first Linux session using the memory stick, it is also recommended that you restart the computer (from the bottom MENU option). The restart will create a save file that will be updated every time you exit Linux. This file contains all the changes you have made to the originally installed Linux distribution.

Adding Apache Server

Now let’s get Apache.

Launch the Puppy Package Manager again. In the search window (Find), enter “apache2”.

findapache

Select “apache2_2.2.22” from the listed results.

selectapache

A pop-up windows will appear as shown below:

examinedepenancies

Click the “Examine dependencies” button.

Click “Download-and-install selected packages” in the window below.

downloadinstall

Another window will appear. Click “Download packages” in this window.

downloadnow

Manual Apache Server Installation Steps

Once the download is complete, we are ready to make the necessary adjustments in order for the server to properly start. We will be adding a new user and add links to the files and directories the Apache server is expecting.

Open a terminal by clicking on the desktop icon “console”.

In the console window, enter the following lines. End each line with the <Enter> key. The entries are shown in red text:

# adduser www-data
adduser: /home/wwwdata: No such file or directory
Changing password for wwwdata
New password:
Retype password:
Password for wwwdata changed by root

Note that <enter> is pressed with no entry for the password.

Now lets continue with the next lines:

# touch /etc/apache2/httpd.conf

# ln -s /etc/apache2/mods-available/auth* /etc/apache2/mods-enabled/

# ln -s /etc/apache2/sites-available/default /etc/apache2/sites-enabled/

# ln -s /etc/apache2/mods-available/alias* /etc/apache2/mods-enabled/

# ln -s /etc/apache2/mods-available/ldap* /etc/apache2/mods-enabled/

# chown www-data:www-data /var/www

# touch index.html

# chown www-data:www-data /var/www/index.html

# ln -s /etc/apache2/mods-available/dir.* /etc/apache2/mods-enabled/

Server Test Page

Lets put the typical “hello world” message in our index.html file so we know it is served properly.

The file is in the /var/www directory, which is the apache server root folder. Open that file and fill it with the following text:

 
 
  1. &lt;html&gt;
  2.     &lt;body&gt;
  3.         &lt;h1&gt;Hello World!&lt;/h1&gt;
  4.     &lt;/body&gt;
  5. &lt;/html&gt;

Now there are only two more things needed to complete the server installation:

1. Set the IP
2. Set the Listening Port

Setting the IP

From the terminal, enter (a wired network cable should be installed):

# /sbin/ifconfig eth0

The computer will respond with several lines. The second line should contain the ip. My second line began with:

inet addr:192.168.0.110

This IP value needs to be added to the hosts file. It is at /etc/hosts.

The first line should be:

127.0.0.0 localhost <your computer id>

Change it to (using your IP value):

192.168.0.110 localhost <your computer id>

You can now save and close the hosts file.

Setting the Listen Port

Now lets finish this off by setting the server listening port. If you want to leave the server listening on the default port (80), you can skip this step.

Open the file: /etc/apache2/ports/ports.conf

Near the top of the file, you will see the following 2 lines:

NameVirtualHost  *:80

Listen 80

Change “80” in these lines to whatever port you want the server to listen on.

Save and close this file.

Now open the file: /etc/apache2/sites-enabled/default

The top line should be:

<VirtualHost *:80>

Change “80” in this line to the same port number in the first file.

Save and close this file.

Testing the Apache Server

First, start the server by entering the following in a console window:

/etc/init.d/apache2 restart

The console should respond with:

startapache

Now, using a web browser from a different computer on your network, enter (replacing 80 with the port number configured in the previous step):

192.168.0.110:80

The browser response should be (my listen port was 9777):

HelloWorld

Congratulations! Your Apache Server is now functional.

This is an enormous first step. You can now serve html files from this server, both within your local network and globally from anywhere on the planet!

In Closing

This is not the end, it is only the beginning of your own host server. I will share my setup as the components are added. Part 2, adding PHP to the server will be coming soon.

I hope you find this information useful…

Loading

Share This:
FacebooktwitterredditpinterestlinkedintumblrFacebooktwitterredditpinterestlinkedintumblr

Your Own MQTT Broker

mqtt

Like many folks, I too started out using the public MQTT broker at test.mosquitto.org. It’s a great way to get started – simple, easy to get working, and FREE! But it does not take long to realize it is unsecured. Anyone can listen in on your topics and there are no logon credentials required or offered as an option.

So I got to searching for a better broker…

One with security. And all the options available with the MQTT standard. Things like:

  • Security Authentication (passwords,certificates)
  • Simultaneous websockets and mqtt (tcp) listeners
  • Persistent Messages

But I did not want to pay for the service. The obvious solution was to host your own broker, either on a host server, or on your local network with broadband access via a router.

Since my host server does not permit continuously running scripts or programs, was limited to a local network solution. But with a broadband connection, it would be on-line and accessible anywhere.

After some research, the most promising options included:

  • PC Broker with Windows OS
  • PC Broker with Linux OS
  • Flash Driver Linux Distro Broker
  • Raspberry Pi2 Broker
  • Android Device Broker
  • Embedded micro-controller

I have read numerous comments about poor (slow) performance using a Raspberry Pi, and since I do not own one, that option was ruled out. For the same reason, I thought about hosting a broker on the trusty ESP8266 but decided against it, at least for now.

And while it would be great to use an old Android phone as an MTTQ broker, the path to get there was a bit murkier than using a Linux hosted server.  It can be done, but few have gone this path. That is, there is little in the way of guidance so this would require significant development.

Windows? Maybe with Windows Server OS running. But that’s not what I got. No.

So looking around at my inventory, I decided to use an unused Window 7 netbook. But, rather than overwriting the hard-drive, a USB flash drive installation was done.

Linux running an MQTT broker when booting to the flash drive.

Windows 7 with the flash drive removed.

While there were a few challenges along the way, it turned out to be a great solution. It has been running continuously now for over a week – flawlessly.

Here is how to set it up…

Linux Installation

Looking for a small, clean Linux distribution, I selected Puppy Linux.  The choice was easy to make, since it had already been setup and running. This post provides step-by-step instructions to configure your flash drive. Follow all the instructions as you will need the development environment to build the MQTT application.

Building the MQTT Broker Application

First thing needed is a copy of the Mosquitto 1.4.7 broker. You can get it here. Then, with the flash drive in a Windows PC, copy the unzipped contents of the folder org.eclipse.mosquitto-1.4.7 to the flash drive in a new folder in the path:

<flash drive>/MyPrograms/mosquitto

You can now install the flash drive in your target computer and reboot. It should start in Puppy Linux.

puppy-start

Before we can build the application, a couple of steps are needed.

  • Install Mosquitto Package
  • Install libwebsockets
  • Edit build configuration file

Let’s go…

Installing the Mosquitto Package

  1. From the Desktop, click on the blue “install” icon.
  2. Click on the “Install Applications” tab.
  3. Click on the Puppy Package Manager icon.
  4. Enter “mqtt” into the search box and hit the “Enter” key.
  5. Click on the mosquitto_0.15 Package.
  6. Click “Install” (Upper right of windows).

Installing the libwebsockets Library

  1. From the Desktop, click on the blue “install” icon.
  2. Click on the “Install Applications” tab.
  3. Click on the Puppy Package Manager icon.
  4. Enter “libwebsockets” into the search box and hit the “Enter” key.
  5. Click on the libwebsockets3_1.2.2.1 Package.
  6. Click on the libwebsockets-dev_1.2.2.1 Package.
  7. Click “Install” (Upper right of windows).

Edit build configuration file

  1. From the Desktop, click on the green “edit” icon.
  2. Click Open, then under “Places”, click “File System”.
  3. Under “Name”, click “mnt”. Then click open.
  4. Under “Name”, click “home”. Then click open.
  5. Under “Name”, click “MyPrograms”. Then click open.
  6. Under “Name”, click “mosquitto”. Then click open.
  7. Under “Name”, click “config.mk”. Then click open.
  8. Scroll down to “WITH_WEBSOCKETS:=no and change to “yes”
  9. Save the file and exit.

Building the broker application

We are now ready to build the application. This is really simple. First, open the console window by clicking on the “console” icon from the desktop. Now switch to the directory that contains the mosquitto source code by entering:

cd /mnt/home/myprograms/mosquitto/src

now build the application by entering:

make

Configuring the MQTT Broker Application

Edit mosquitto run-time configuration file

  1. From the Desktop, click on the green “edit” icon.
  2. Click Open, then under “Places”, click “File System”.
  3. Under “Name”, click “mnt”. Then click open.
  4. Under “Name”, click “home”. Then click open.
  5. Under “Name”, click “MyPrograms”. Then click open.
  6. Under “Name”, click “mosquitto”. Then click open.
  7. Under “Name”, click “mosquitto.conf”. Then click open.
  8. From the edit menu, select”Save as” and save this file to the src folder. The full file path should now be /mnt/home/myprograms/mosquitto/src/mosquitto.conf.
  9. Scroll down or search for “#allow_anonymous true”. Change this to “allow_anonymous false”. Remember to delete the # so this is not commented out. This will force the broker to require usernames and passwords to connect.
  10. Scroll down or search for “#user mosquitto”. Change this to “user nobody”. Remember to delete the # so this is not commented out.Puppy linux does not have a user named “mosquitto” but it does have one named “nobody”. Since Puppy Linux is a single user distribution, it does not allow you to add users.
  11. Scroll down or search for “#password_file”. Change this to “password_file pw.txt”. Remember to delete the # so this is not commented out.
  12. Scroll down or search for the text “#protocol mqtt”. Just after this line, add the following 4 new lines:
    1. listener 11883
    2. protocol mqtt
    3. listener 18080
    4. protocol websockets
  13. Save and exit the file

What step 9 does is configure the broker with two listeners, one with standard mqtt (tcp) protocol and one with websockets.

While any port can be used, a one was added to the standard port numbers so our broker is not in conflict with the “well-known” mqtt ports. This could be important in the complicated event that your client is connected to two different brokers at the same time. In this case, the ports can only be open once. This eliminates potential conflicts.

Creating a password file

Using the file editor (edit icon from the desktop), save a blank file in the src folder:

/mnt/home/myprograms/mosquitto/src/pw.txt

Add a few username/password entries in this file in the format, for example:

username:password
user2:password2

Save the file. Also make a copy of this file for off-line storage.

Now run the password utility. This will change the plain text passwords in the file pw.txt to a hashed value. Run the password utility from the console:

cd /mnt/home/myprograms/mosquitto/src
./mosquitto_passwd -U pw.txt

If you open pw.txt, you will find the plain text passwords have been replaced with a hashed value.

Opening the Linux Firewall

network-firewall-icon

Are we ready to run the broker yet? Almost. But there is one more thing needed in order to access the broker from another device. We need to configure Linux to allow external connections.

Open the file /etc/hosts.allow

It should contain one line:

ALL:LOCAL

Change this to:

ALL:ALL

Save and exit the file.

If you want a more restrictive environment, it is suggested that you research configuration settings for the Linux hosts.allow file. For the purpose of this example, we are opening the MQTT Broker to anyone with proper username/password credentials.

Starting the Broker

Now to start the broker, just go to the src folder and enter the following:

cd /mnt/home/myprograms/mosquitto/src
./mosquitto -c mosquitto.conf

The startup console should display:

start_mosq

The warning occurs because ipv6 is not supported. But this is of no concern for the typical ipv4 addresses. While the application was initially build in the ../src directory, it can be moved and executed from any location of your choosing. just remember to also include the password file utility, the config file and the password file. These are the files needed to run the application:

mosquitto
mosquitto_passwd
mosquitto.conf
pw.txt

Testing the Broker

You probably have your own devices to connect to and test the broker. With the configuration of this broker, you will need to include a username and password when connecting. I like to use the Google Chrome MQTTlens extension and the Android MyMQTT App when making quick MQTT connection checks. I’ve provided additional details for using these MQTT tools in this post.

Port Forwarding and DDNS 

icon-port-forwarding

While you can access this MQTT  broker from any device on your LAN subnet, world-wide broadband access will require configuring your router to forward MQTT port requests to the device running the broker. This is called port forwarding. Please refer to this post for more information about configuring port forwarding and DDNS.

In Closing

This broker setup provides you with all the features of the MQTT specification. One of the best features is the ability to operate a mixed protocol system. This provides an connection to devices using either mqtt (tcp) and websocket protocol together. And you can enable any level of security needed, from simple passwords to security certificates. And since it is running locally, you are in full control.

Next up. I am planning to update my MQTT App inventor template application to support the basic security credential provided by this broker.

I hope you find this information useful…

 

Loading

Share This:
FacebooktwitterredditpinterestlinkedintumblrFacebooktwitterredditpinterestlinkedintumblr

Press Ctrl+C to copy the following code.
"