Server Admin

Website Server Administration

Convert Unix Time Online Website

Overview

After discovering exactly what timestamps are and how much they can simplify storing dates and time, I often find myself using unix timestamps more often than not. If you are new to timestamps, take a look at this article and see what timestamps are and how they are useful.

Unix timestamps are great and I can’t live without them but the issue with timestamps is that a timestamp is simply a 10 digit number (e.g. 1307405403).

Convert Unix Time Online Website

I have created a website that allows me to quickly convert a unix timestamp online to an easy to read date as well as being able to convert a date back into a timestamp. It has helped me out tremendously which is why I have now made this tool available for the rest of the community to use and I hope that it makes other people’s life easier.

There are also article on that site which demonstrate Real World Usage Examples Of Timestamps as well as giving you Interesting Facts About Timestamps.

Read more…

Online DNS Dig Website

Overview

For those people who find themselves playing around with DNS setups from time to time will more than likely have worked with or at least heard of the command `DIG`.

For those who don’t know what DIG is or think of it as the necessary first step to hiding a body, will be surprised to know that DIG is a command that is used to directly query a DNS server to find out specific information about a domain name or sub domain. This can be useful when you are trying to setup a new domain or simply check if a sub domain already exists.

Further to this, you can check many types of records such as mail records (used in creating a mail server), CNAME records (points one domain name to another domain name) and most commonly is the A record (domain pointing to an IP address).

Online DNS Dig Website

Now there are times when I don’t have access to my Linux machine or perhaps to lazy to try remember 32 bit password i seem to create and couldn’t get access to the normal dig tool. Here is what lead me to create a website that would allow me to easily perform a dns look up and execute a web based dns dig online. Simply enter the domain you are querying, the type of record to look up and optionally enter the dns server and off you go.

Check Out The Online DNS Dig Tool

You too can use the site by clicking here. Please note that this site is still in it’s early release and may perform unexpectedly.

Read more…

Export Proxy Username Password Linux

Overview

If you are wondering how you can tell your Linux applications to use a proxy, then you have found the right article and as you will find out, it is surprisingly simple. After you set the proxy in linux, any applications such as wget, lynx and ftp will then be redirected through the proxy.
There are two settings you can make. One for web (http_proxy) and the other is ftp (ftp_proxy). We do this by setting each one in our environment variable.

Setup Proxy in Linux

Here is the most basic setup below. It will set an environment variable called http_proxy and ftp_proxy which will cause all your applications to redirect through the proxy.

export http_proxy='http://example.com'
export ftp_proxy='ftp://example.com'

Export Proxy Username Password Linux

Here we set the username and password for the proxy by passing it before the domain and seperate with a colon.

export http_proxy='http://user:password@example.com'
export ftp_proxy='ftp://user:password@example.com'

Set A Different Port To Use With The Proxy

Your proxy can run on a different port, so to select an alternate port, you can pass the port number after the domain. Below we select 8080 as the port for our proxy.

export http_proxy='http://example.com:8080'
export ftp_proxy='http://example.com:8080'

Make Linux Proxy Export Permanent

Something to be aware of, is that setting this environment variable only makes it available until you log out. If you would like to make the export proxy setting permanent, then add any of the lines above to your .bashrc or .profile file in your home directory which is found under /home/yourfolder/.bashrc or /home/yourfolder/.profile or ~/.bashrc or ~/.profile (since ~ is just a shortcut to your home directory). Now each time you login, it will automatically set this environment variable for you but not any other users.

Set Proxy For All Users

In order to setup the proxy for all users on your Linux system, you need to set the export the environment variable to all users when they login by adding a line to /etc/bash.bashrc. Now when any user logs in, they will automatically have the proxy setup for them.

MySQL REPLACE – Replacing text in MySQL

Overview

MySQL replace is a mysql function that allows you to replace one bit of text with another piece of text during a MySQL query. The best way to understand how a MySQL REPLACE Function works is to see it work as you will see in the example below.
The MySQL replace function works similar to how the PHP str_replace function works however the parameters are in different order.

MySQL REPLACE

The function is called mysql replace() and takes 3 parameters which are:

  1. the mysql column name
  2. the text you want to be replace
  3. the text to replace the existing text with

For example, the function will look something like this:

REPLACE(column_name, 'replace_this', 'with_this_text');

Example 1 – SELECT and REPLACE

In this example, any results that have John as their first name will instead return Johnathon as their firstname and any other names will not be affected.

SELECT REPLACE(`firstname`,'John','Johnathon') AS firstname,lastname,age FROM userdatabase;

 

Example 2 – UPDATE and REPLACE

This will update and switch one bit of text with another. Here we are going to change some html and replace <i> tags with <em> tags.

UPDATE wp_posts SET post_content = REPLACE(post_content,'<i','<em');

Now all the italic html tags would have ben replace with emphasized tags.

Running PHP Script From Cron

Overview

Cron is a job based scheduler used in Unix type operating systems. It allows you to schedule tasks to be run at given intervals such as scheduling a php script to run weekly, every second day, hourly, every 3 minutes, weekends, and so on.
In this article, I will show how easy it is in running a php script from cron. It will also show you how you can run a php script via a url using wget.

Running PHP Script From Cron

I will show you 3 ways on running php script from cron which are:

  1. Running php script via command line in cron
  2. Running php script via php5-cgi in cron
  3. Running php script remotely via website url in cron

Running a php script usually requires the php script to have execute permissions set. Running PHP script from cron is usually a matter of specifying the file and as long as the script has execute permissions (i.e. chmod +x) then all should be good. Here are three ways I have found work very well.

Running PHP Script On Command Line From Cron

In order for the script to run correctly you should add the following to your php script. The informs the bash what type of script it is executing.

#!/usr/bin/php
<?php

//.. your code follows here

Next we add the entry into cron.

# you could output to a file by changing /dev/null to /some/file/to/save.log
*/10 * * * * /home/user/scripts/updatesomething.php > /dev/null

Note we didn’t need to execute the script via php (/usr/bin/php /path/to/script.php) since we added the header to the php script.

Running php script via php5-cgi in cron

To execute the php script from cron you need to have php5-cgi installed. You can install this on Debian use “apt-get install php5-cgi” or the equivalent command for your distribution of linux.

# PHP5-CGI: execute the php script every 2 minutes and send output to /dev/null
# you could output to a file by changing /dev/null to /some/file/to/save.txt
*/2* * * * php5-cgi -q /home/user/scripts/parselogs.php > /dev/null

Running php script via website url in cron

And lastly we will use wget from the cron to download a website url and thus, execute the url that the php script is located at. We do this in the cron to allow the url to be downloaded at set times. To use wget, you need to install it wget using “apt-get install wget” or the equivalent command for your distribution of linux.

# wget will visit the url but won't save the file. send it to /dev/null
# this executes the script as if you visited the link in your browser
0 * * * * wget -O /dev/null http://www.example.com/some_cron_script.php

And there we go, your php script will be run by cron on time, every time.

Protect Folder With Htpasswd In Apache

Overview

Need a folder secured on your website with a username and password prompt? You may have some important documents you need to share with selected people. You could have restricted downloads you need to give to certain people. In any of these cases, you can secure a folder with one or multiple usernames and passwords.

Secure Folder With Htpasswd

Secure Folder With Htpasswd


I will show you how to lock down a folder using apache .htpasswd and .htaccess. Once a folder is password protected with htpasswd and htaccess, your website visitors will see a dialog box appear similar to the one below and they will need to enter a username and password before they can access the contents or downloads of that locked folder.

Protect Folder With Htpasswd In Apache

Below is showing what the end result will look like and what this article will help you achieve.

Step 1 – Create the folder and set the permissions on it

If the folder you want secured doesn’t exist, go ahead and create it. Then set the correct permissions and owner for the folder. I am using www-data as our folder owner as this is the owner that apache website runs as.

cd /var/www/html
mkdir secured_folder
chmod 744 secured_folder
chown www-data.www-data secured_folder
cd secured_folder

Step 2 – Create your .htaccess file

Change to the ‘secured_folder’ and create an .htaccess file inside their with the contents below. This will add the username and password security to the folder.

AuthUserFile /var/www/.htpasswd
AuthGroupFile /dev/null
AuthName "My protected files"
AuthType Basic

<Limit GET>
require valid-user
</Limit>

Step 3 – Create the username and password for .htpasswd file

Now change to the folder you want your .htpasswd is to be stored in. We specified /var/www/.htpasswd as you can see the above for ‘AuthUserFile’. Then use the htpasswd function in your command line to create the username and password for the folder you want to secure. It is recommended that you leave this file outside of the actual website folder.

cd /var/www/
htpasswd -c .htpasswd username1

You will be asked to enter a password for ‘username1′.

Step 4 – Adding more users to .htpasswd file (optional)

If you would like to add multiple users to the same .htpasswd file then don’t add the ‘-c’ option.

cd /var/www
htpasswd .htpasswd username2
	you will be asked to enter a password for 'username2'
htpasswd .htpasswd username3
	you will be asked to enter a password for 'username3'

Now when you go to visit your page, it will prompt you to enter a username and password before you can access the page.

Execute Logrotate Command Manually

Overview

One day while perusing around one of my Debian Linux servers I noticed that the disk was extremely full. It isn’t a server I access all the time so the disk space being filled up had gone unnoticed. After taking a look at what could be using up all the disk space, I realised that my log files had grown to be absolutely massive with some log files being a few GB’s in size.

logrotate.d Wasn’t Running

The cause of the issue was that logrotate had not been running for quite some time. I fixed logrotate but I couldn’t afford to wait for the logrotate to automatically run on it’s next schedule. I began to panic and needed to quickly rotate the logs to regain precious space before the server and websites came crashing down.

Execute Logrotate Command Manually

Since most of my disk space was consumed by apache, I decided to execute logrotate command manually in linux and pass it the config of the logrotate apache . To do so, I used the following command:

logrotate -vf /etc/logrotate.d/apache2.conf

The flags ‘-vf’ passed to the command are as follows:

  • -v verbose shows more information. useful to try detect any errors there may be with logrotate
  • -f force the rotation to occur even if it is not necessarily needed

And /etc/logrotate.d/apache2.conf is the location of my config file of for the apache2 logrotate. The content of my config file are as follows

/var/log/apache2/*_log {
	weekly
	missingok
	rotate 52
	compress
	delaycompress
	notifempty
	create 640 root adm
	sharedscripts
	postrotate
		if [ -f /var/run/apache2.pid ]; then
			/etc/init.d/apache2 restart > /dev/null
		fi
	endscript
}

Once I ran logrotate in linux manually, all my apache2 log files got rotated, compressed with gzip and recycled leaving me with heaps more free disk space.

Perl Ping Script To Ping Remote Server Or Website

Overivew

Below is a perl script which will allow you to ping a remote server. The article is a follow up article to my php ping script article. This article is similar in that we will also be pinging a remote website to see it if up and running, however this time we will be using perl’s Net module.

Perl Ping Script To Ping Remote Server Or Website

Here is a surprisingly simple perl code snippet to perform a perl ping request to check if a remote server or website is up. It creates a new Net::Ping object and then sends pings the server. We can optionally specify a port to see if a specific port is responding on the server which can be very useful if we want to know if a service is still up and running such as MySQL or Apache.

Perl Ping Specific Port

By using the perl ping script to ping a specific port, we can test wether a service on the server is up and running. For example, port 3306 would test for MySQL, 80 and 443 would test the webserver and 21 would test FTP.

A Perl Ping Script can be very useful and it can be used to make sure a website or server is up and can make our lives easier if we are monitoring multiple servers.

#!/usr/bin/perl -w

use Net::Ping;

# Host can be either an IP or domain name
my $host = "www.google.com";
#optionally specify a timeout in seconds (Defaults to 5 if not set)
my $timeout = 10;

# Create a new ping object
$p = Net::Ping->new("icmp");

# Optionally specify a port number (Defaults to echo port is not used)
$p->port_number("80");

# perform the ping
if( $p->ping($host, $timeout) )
{
        print "Host ".$host." is aliven";
}
else
{
        print "Warning: ".$host." appears to be down or icmp packets are blocked by their servern";
}

# close our ping handle
$p->close();

Conclusion

In the above example, we ping port 80. This will test if the website is active. As stated above, changing from port 80 to 3306 for example, would then test for an active MySQL service running.

How To Setup IP Address in Linux

Overview

In this guide, you will be shown how to setup the IP address of a computer or server running Linux. Before we know which way to configure your network card, you need to know if you want to use a static or dynamic IP address.

Setup IP Address

Setup IP Address

Static IP vs DCHP IP

Whats the difference? Static is an IP address that yet manually chose and set for your computer. It won’t change until you decide to update it. DHCP is an IP address that is given to your computer by a DHCP server (usually a router or another server). The DHCP assigned IP address is given to you from a pool (or range) of IP addresses and can change depending on which ones are available.

How To Setup IP Address in Linux

Depending on your network configuration, you will need to either use a static IP address which is commonly used on servers on corporate networks. A static IP address is manually entered in and doesn’t change.
You could also use a dynamic IP address which can be assigned to your computer via a DHCP service. DHCP is more common in home networks as it can be setup automatically and doesn’t require the user to know how to change an IP address. DHCP is a service that runs on your network that will automatically setup your network IP address. Before DHCP can assign your computer a IP address, you need to setup your network interface to use DHCP

Editing the Network Interface

All your IP address information be it dynamic or static is stored under your network interface file which can be found under the path /etc/network/interface. To edit this file you may need root or sudo access. To edit the file enter the following:

sudo vi /etc/network/interfaces

Inside this file you will notice a loop back device. Ignore this part as we want to focus on the eth0 interface – or interface 0. If you have multiple network interfaces, you may see eth0, eth1, etc.

Setup DHCP for Network Interface

Find the section that has iface eth0 (or whichever interface you need – eth1, eth2, etc). and change it to match below. That’s the only configuration you. Now you need to restart your network interface to bring in the changes (shown below).

allow-hotplug eth0
iface eth0 inet dhcp

Setup Static IP for Network Interface

If you need a static ip address, then edit your interface file to be similar to below. Note that your ip, netmask and other items will need to match your own network range.

# The primary network interface
allow-hotplug eth0
iface eth0 inet static
address 192.168.0.20
netmask 255.255.255.0
network 192.168.0.1
broadcast 192.168.0.255
gateway 192.168.0.1
dns-nameservers 192.168.0.2

Note that network and broadcast are optional entries and if omitted will be automatically detected.

Restart Networking Interfaces

In order for the above changes to come into effect, you need to restart your network interface. You can do this by running the following command:

/etc/init.d/networking restart

Sometimes you will find your interface doesn’t come online when restarting so entering in `ifup` will bring it back up where eth0 is the network interface you configured.

ifup eth0

And inversely, if you wanted to bring the network interface down, you would type in:

ifdown eth0

Caution! Typing in ifdown will take the network interface offline and drop your network connection so don’t running this command remotely!

Add New Zone To Bind DNS Server

Overview

Welcome to the second of my DNS articles. It is more of a prequel to my first article in which I explained the steps in Creating A DNS Entry For A SubDomain.

This article assumes you have already installed bind and it is all running. If not, then you will need to setup bind first.

What is a DNS Zone?

My previously article explained what a zone is so I won’t go into to much detail here, but essentially a zone is the term used to describe a config file which contains a specific domain/subdomain collection in your DNS server. Bear in mind that a DNS server has multiple zones (and thus domains) associated to it. Each domain generally has its own zone file however in some situations, you can have multiple domains per zone file. For example, mydomain.com and mydomain.net and mydomain.org could all be in the same zone file as long as they all need to point to the same ip address.

Create the DNS zone file

The zone file we will create for this example will be for the domain example.com. The zone file will tell the DNS server which IP the domain should point to as well as configuring expiry and refresh times for the domain. Generally, the naming format for a zone file is db.example.com.conf where the domain in this case is example.com. Now to create the zone file and add the contents shown in the Sample DNS Domain Zone File

sudo vi /etc/bind/db.example.com.conf

Sample DNS Domain Zone File

In this sample DNS domain zone file, there are a couple changes you need to make to ensure it works in your situation. Firstly, change ns1.exampledns.com and ns2.exampledns.com with your own ns1 and ns2 dns servers. Secondly you can change example.com to the domain you are adding.

Optionally leave out $ORIGIN if you are adding multiple domains to a single zone file. The @ symbol simply means the current zone which is great for when you have multiple domains to a single zone file.

; Zone file for example.com
$TTL    3600
$ORIGIN example.com
@       IN      SOA     ns1.exampledns.com.    root.example.com. (
                     2012033101         ; Serial
                           3600         ; Refresh
                           1800         ; Retry
                         604800         ; Expire
                          43200 )       ; Negative Cache TTL

        IN      NS      ns1.exampledns.com.
        IN      NS      ns2.exampledns.com.

@       IN      A       192.168.0.2
www     IN      A       192.168.0.2

Add New Zone To Bind DNS Server

Above we created the zone config file. Now we need to add the zone file to our dns config file which tells our DNS server that it should control and serve requests for this new domain zone.

Open your DNS config file and add a zone in for your domain. I will be using example.com in my example. Here the file is located under /etc/bind/named.conf.local however depending on the way your DNS server was configured, it may be under /etc/bind/named.conf.options or rarely even /etc/bind/named.conf

sudo vi /etc/bind/named.conf.local

Add a zone to your DNS config file

At the end of the file add an entry similar to the following

zone "example.com" {
	type master;
	file "/etc/bind/db.example.com.conf";
};

As you will see, the type of zone is a master zone. This means that it is the primary holder for dns information regarding this domain.

Restarting Bind DNS Server

Once you have added your new zone to your bind dns server, you need to restart the bind daemon in order to reload the new config file. You can do so by issuing the following command.

Debian / Ubuntu Linux

Restart bind dns server on Debian and Ubuntu Linux.

service bind9 start

Alternate method to restart bind dns server on Debian and Ubuntu Linux.

/etc/init.d/bind9 restart

Redhat / Fedora Linux

Restart bind dns server on Redhat and Fedora Linux.

service named restart

Alternate method to restart bind dns server on Redhat and Fedora Linux.

/etc/init.d/named restart

Note: you may need to use sudo to run these commands if your account doesn’t have enough privileges.