Setting Up LAMP Server on Mint

Uncategorized

This is just a quick addendum to my previous post on how to set up a LAMP Server.

I’m now using Linux Mint, and I followed those instructions only to encounter a mild but persistent irritation. For some reason, the default behaviour in Mint is to read your website starting at /var/www/html/[whatever], rather than just at /var/www/[whatever] like a normal person.

To fix this, go to /etc/apache2/sites-available and change the DocumentRoot line in all files to the appropriate directory. I changed all the files because I don’t know the difference between the two… and it worked so whatever.

Now my website is up and running again! Huzzah!

Working Around Dynamic IPs

Uncategorized

It took me a while, but I think I’ve finally figured out how to automatically update the IP address of your domain name.

The script I recommend at the end of the page will have your domain password in it! You should protect dat noise. See my post on basic passwords for Apache servers.

Recap

Let’s review the context. I set up a website server on my home desktop, and then I set up a script that emails me when my IP changes. I had to do this because I have a normal internet subscription, so my ISP might change my IP address occasionally.

That was fine because I didn’t have a domain name. So if I wanted to access my website from the Internet, I checked my emails to see what my home desktop’s IP address was most recently, and I plugged that number into my browser.

Along Came an Angel

Thereafter, an amazing human being bought me my very own domain name, absentstills.com. The domain name is operated by a company called eNom. I went to the eNom configuration page and set it so that http://www.absentstills.com, abc.absentstills.com, and wwx.absentstills.com all point to my IP address.

Wonderful! Now typing in absentstills.com from anywhere in the world brought you to my website.

The problem still remained, however, that anytime my ISP changed my IP address, I would have to go manually re-configure the settings at eNom’s website to point to my new IP address. We can do better than that.

Dynamic DNS IP Updating is a PITA

What should be a really simple task is actually surprisingly annoying.

In order to make this work, the company hosting your domain name has to provide this service. Luckily, eNom does. The idea is that you submit commands to a website called http://dynamic.name-services.com/ in the extension of the URL. For instance, if you enter into your browser the URL

https://dynamic.name-services.com/interface.asp?command=setdnshost&zone=your-domain-name.com&domainpassword=my-domain-password&address=192.0.123.56

then you are asking eNom to ‘setdnshost’ of ‘your-domain-name.com’ to ‘192.0.123.56’. After this command is processed, your-domain-name.com will point to whatever is at the IP address 192.0.123.56.

Sounds easy enough. In fact, Sean Schertell over at DataFly.Net already concocted a python script to do this. It’s pretty easy to use, and his post describes the instructions quite well.

The only caveat I have is to change the ip_check_url to http://ifconfig.me/ip, since the one he uses doesn’t seem to work anymore (the post is a few years old). Since ifconfig can be intermittent in responding to your requests, I put the update request into a while-try-except loop:

    # Compare our recently saved IP to our current real IP
    recent_ip = read_file(ip_text_file)

    # Keep trying until ifconfig works
    current_ip = ""
    while True:
        try:
            current_ip = read_url(ip_check_url)
#            print "%s" % current_ip
            break
        except:
            print "IfConfig Failed, Trying Again"

So that’s it, right? What’s so hard about all this?

Well, when I set up the script and ran it, nothing really worked. Except it did. Let me explain.

It seems that the issue lies in delays between your script running, eNom receiving the message, eNom actually updating your IP address, and your updated IP address propagating through the Internet.

So let’s say you try to debug the program. You manually set you IP address incorrectly at access.enom.com and try to reset it using your script. The script tells you that the command was submitted correctly, but your eNom configuration page still shows the incorrect IP address.

This is because eNom hasn’t processed your request yet, even though it has received it successfully. Of course, if you don’t know this, then you’ll spend the next half hour debugging and trying to fix your script, or you’ll give up and reset the IP manually. And then nothing will work and you will get frustrated. And then, eventually, the original command will get processed, and everything will work for no apparent reason.

So to save you this trouble, here is my modified version of Sean’s script. If I notice that it isn’t working as expected, I will update the code here. Don’t try and debug it yourself unless you have a lot of patience, or you know something I don’t.

#!/usr/local/bin/python

##############################################
# update_enom.py by: Sean Schertell, DataFly.Net
# Modified by Martin Magill, 17/01/2015
# ------------------------
# A simple python script to update your dynamic DNS IP for domains registered with Enom.
# 
# Requirements:
# - You have a domain registered with Enom, nameservers are set to Enom's (true by default), and you've set a domain password
# - Your client machine runs python
# - You know how to configure a cron job to periodically run this script (every 5 mins recommended)
#
# Cron example to run every 5 minutes, and log output messages:
# */5 * * * * python /var/www/update_ip.py >> /var/www/ip_log.txt
# Access your cron jobs by sudo crontab -e, and then add that line to the end of the file.

##############################################
# Configure
##############################################

ip_check_url = 'http://ifconfig.me/ip'   # URL which returns current IP as text only
ip_text_file = '/var/www/protected/ip.txt'         # Text file to store recent ip file
domain       = 'www.your-domain-name.com'        # Enom registered domain to be altered
password     = 'your-password'           # Domain password

##############################################

import urllib2, os

def read_url(url):
    return urllib2.urlopen(url).read()

def read_file(path):
    return open(path, 'r').read()

def parse_enom_response(enom_response):
    enom_response_dict = {}
    for param in  enom_response.split('\n'):
        if '=' in param:
            try:
                key, val = param.split('=')
                enom_response_dict[key] = val.strip()
            except: pass
    return enom_response_dict

def save_new_ip(current_ip):
    return open(ip_text_file, 'w').write(current_ip)

def update_enom():    
    # First, ensure that the ip_text_file exists
    if not os.path.exists(ip_text_file):
        open(ip_text_file, 'w').close() 

    # Compare our recently saved IP to our current real IP
    recent_ip = read_file(ip_text_file)

    # Keep trying until ifconfig works
    current_ip = ""
    while True:
        try:
            current_ip = read_url(ip_check_url)
#            print "%s" % current_ip
            break
        except:
            print "IfConfig Failed, Trying Again"

    # Do they match?
    if recent_ip == current_ip:
        print "URLs match"
        return # IP address has not changed since last update

    # No match, so let's try to update Enom
    settings = {'domain': domain, 'password': password, 'current_ip': current_ip}
    enom_update_url = 'https://dynamic.name-services.com/interface.asp?command=setdnshost&zone=%(domain)s&domainpassword=%(password)s&address=%(current_ip)s' % settings
#    print "%s" % enom_update_url
    enom_response = read_url(enom_update_url)

    # Any errors?
    response_vals = parse_enom_response(enom_response)

    if not response_vals['ErrCount'] == '0':    
        raise Exception('*** FAILED TO UPDATE! Here is the response from Enom:\n' + enom_response)

    # Okay then, lets save the new ip
    save_new_ip(current_ip)
    return    
##############################################

update_enom()

Passwords For Your Apache Server

Uncategorized

I just realized that it might be important to password protect some stuff on your website. Here’s how to do it.

Let’s assume that your website fils are stored in /var/www/. So create a sub-directory called e.g. protected.

In /var/www/protected/, create a file called .htpasswd. This will store the username and password require to access files in this folder from the Internet. The file should contain lines formatted like:

username:password

You may want to encrypt your passwords before putting them in. This means that if somebody hacks your folder, they will see some gibberish password that is hard to reverse-engineer into what you actually need to type into the password box. To do this, use a utility like the one at the bottom of this page. Note that you are trusting that they aren’t screwing you over and recording your password when they encrypt it for you…

Almost done. Now you need to tell your server to use this.

As per this link, you need to open your Apache configuration file with e.g.

sudo nano /etc/apache2/apache2.conf

and then add to the end of the file a block as follows:

<Directory "/var/www/protected">
  AuthType Basic
  AuthName "Authentication Required"
  AuthUserFile "/var/www/.htpasswd"
  Require valid-user

  Order allow,deny
  Allow from all
</Directory>

That should just about do it. To force the changes into effect, restart your server:

sudo service apache2 reload

Now try navigating to the protected folder from your browser. You should encounter a pop-up asking you for your credentials.

How to Email Yourself Your IP Address

Absinthe, Programming, Website Development

I’ve used the instructions here to set up a script that emails my desktop’s IP address to me.

Their instructions are five years old, so I had to make some additions:

  • In Gmail, you need to enable Access to Less Secure Apps.
  • I had to add ‘touch ip.txt’ to their bash script right before ‘read ip1 < ip.txt’, otherwise the script just hung at that line forever.
  • ifconfig.me sometimes returns nothing for IP. This makes the routine think your IP has changed (into nothing). To avoid this, I put the call to ifconfig.me inside a while loop that tries to call again until it gets a response.

I need to email myself my IP because, otherwise, my ISP might change my IP address (a la dynamic IP), and then I wouldn’t know where to find my website anymore.

Since now I will always be able to find my home IP, I can always access my website from anywhere in the world.

My script:

#!/bin/sh

SUBJ=”some subject”
EMAIL=”your_email@gmail.com”

ip1=””
ip2=””

touch ip.txt
read ip1 < ip.txt

while [ “$ip2” = “” ]
do
ip2=$(wget -qO- ifconfig.me/ip)
done

if [ “$ip1” = “$ip2” ]
then
exit
else
echo “$ip2” > ip.txt
echo “$ip2” | mail -s $SUBJ $EMAIL
exit
fi

How to Set up an Apache Server on Linux

Programming, Website Development

I’m going to set up Apache on my desktop. It’s going to be a pain in the ass. I might as well document the experience here.


What is Apache?

Apache is a web server program. Following the butler-and-house metaphor from my post on website ownership, Apache is the hollow shell of the butler. When you write a program to control the butler (your website), you will store them on the Apache server.

If I understand correctly, installing and configuring Apache on my computer will allow me to use my desktop as a website server. I will still need to buy a domain name in order to make that website accessible from the Internet.

Getting Apache

The first step should be downloading and installing Apache. According to Andy Kahn, this is as simple as going to http://www.apache.org and downloading the source distribution file.

Andy’s instructions are about 12 years old, and it looks like the Apache website has evolved significantly since his heyday. The homepage is an extremely noisy mess of ‘Latest News’es and ‘Latest Activity’es and what-have-you-es.

Hidden away in the upper-right-hand corner is a promising button called Download. Eureka!

Not quite. Rather than the big shiny ‘Download’ button I was hoping to find, the Download page is a list of mirrors. Annoying… but at the top of the page they list a recommended mirror, so let’s click on that.

Uh oh. That takes you to a huge list of directories. The page explains that these “contain current software releases from the Apache Software Foundation projects.” All of them do? How many versions of Apache do you need?

This is getting too painful. Trying a different approach…

Getting Apache, Take 2

So instead of trying to be smart about things, I’ve decided to ask Google to do my thinking for me. Googling “how to set up ubuntu desktop as a web server” led me to a very promising guide.

Following their instructions exactly… works! It’s nice when things work. I ignored the section on phpMyAdmin because I want to learn to use MySQL.

In Summary

I now have a working Apache server on my desktop. Stay tuned to see what I end up doing with it…


Addendum

I found some more useful instructions. This describes some security measures that might be worth employing, as well as how to give you user account permission to write into the website without sudo-ing in every time.

I’m now running a website that is accessible over the Internet. Cool.

The only problem now is that I still don’t have a domain name. You can still find my website online, though, but only if you know that my IP address is 192.0.247.83 (and only as long as the remains my IP address – ISPs change your address periodically).

I’ll work on setting up some work-around for this. If I can manage it, then I’ll have a working and useful website for free!

Top Programming Languages

Programming, Website Development

We were shown this neat list in class today. It ranks programming languages by their popularity. If you’re wondering what languages to add to your resume, this might be a good place to start.

C is still king, which I was glad to learn. Java is in close second, which is all the more incentive for me to finally get around to learning it. JavaScript was chosen “Programming Language of the Year” for 2014, since it showed the most positive growth last year.