Web Development Tutorials for April 2011

Change hostname on Ubuntu

The hostname of an Ubuntu system is usually specified when it is being installed. Especially if you were not the one who installed the system, you may want to change the hostname to something that is more meaningful to you. The importance of the hostname on an Ubuntu system is usually overlooked as it is not seen as something worth changing. The truth is, if you have a properly set hostname, you will be easily able to determine the exact server that you currently have a terminal open on.

The tutorial below shows how to change the hostname on Ubuntu.

Concatenate strings in Bash

To concatenate strings in Bash, we use special characters to delineate the variable from the literal string. String concatenation is useful for joining the values of variables to a string before outputting it or using it to perform some other function. The Bash concatenate tutorial below shows how this not so well known technique works.

If we wanted to concatenate a variable to a string in a Bash script, we would do it as shown below:

LOVE="Tutorial Arena"
OUTPUT="I love ${LOVE} so much!"
echo $OUTPUT

Run shell script from PHP

Running a shell script or command from PHP offers a number of conveniences to an administrator. When properly set up, shell scripts and commands can be run by simply requesting a web page over the Internet. The web server would receive the request and then pass it on to the PHP interpreter. By using special PHP functions, we are then able to make PHP run the shell script or command for us.

Check open ports on Ubuntu Linux

One may want to check open ports on Ubuntu to ensure that there are no services listening that shouldn't be. If we remember, a port is what an application will use to communicate with another application, provide a service, etc. To get an idea of what services are running on a system, we would need to check the open ports on the system.

Use cron to run PHP script

Using a cron job to run a PHP script is very easy if you know how. If we recall, cron is a Unix/Linux utility that is used to perform tasks at specified intervals. By setting up a cron job with the appropriate parameters, we can get our system to execute PHP scripts or any other kind of script.

Create file with dd on Linux

Occasionally, for testing purposes, we will want to create a large file. There is a handy utility installed on most Linux systems called dd which makes the task of creating files of any size very easy. The beauty of dd is that it works very fast and very large files can be created quite quickly. The tutorial below will show how to create a file of any size with dd on Linux.

Apache compiled in modules

Apache is a very well-designed and modular web server. Its power and convenience partly comes from its ability to use various modules, and the ease in which these modules can be changed as necessary. Specific modules can be compiled into the server when it is being built so that a web server suited for a particular task is ready out of the box.

Most times, we are not the ones who have compiled Apache so we have no way to immediately know what modules have been compiled into it. Coming to our rescue is a simple command we can execute to determine Apache's compiled modules.

Check Apache version on Ubuntu linux

To ensure that we have the latest version of the Apache web server running, we need to perform a check on our current version. In the tutorial below, we will demonstrate how easy it is to check our Apache version on Ubuntu or other similar modern flavour of Linux.

To check what version of Apache we have running, we can open a terminal and execute the following command:

apache2 -v

We invoke the apache2 binary which is located at /usr/sbin/apache2 (by default in Ubuntu 10.04 LTS) and tell it that we want to know the current version.

Send Email with attachment from the command line in Ubuntu Linux

Sending emails with attachments from the command line in Ubuntu is easily accomplished using a tool called mutt. Mutt allows us to send emails non-interactively. That is, we can invoke mutt from a shell script and it will perform the specified tasks for us with no further intervention needed on our part.

We can also run mutt from a shell (such as bash) or even as a cron job and have it carry out our desired tasks.