How to set up Cron Job on Amazon EC2 Cloud Servers

Here I will show you how to set up a cron job on an Amazon Linux server. We used the basic Linux 64bit ami but these directions should work for any Linux web server

How to set up Cron Job on Amazon EC2 Cloud Servers

We used the basic linux 64bit ami but these directions should work for any linux web server

open a command prompt

type: which php
returns: /usr/bin/php

using the reponse from which php above, add the proper shebang to your php script like so:

#!/usr/bin/php -q

the  -q parameter makes it so you don’t get an email every time cron runs.

continuing from command prompt:

type: crontab -e

then use vim editor to add the following line to crontab to run  your php script every minute:

* * * * * /usr/bin/php /home/ec2-user/crontest.php

or every hour:

0 * * * * …

the five cron positions are as follows:

minute         0-59
hour           0-23
day of month   0-31
month          1-12
day of week    0-7 (sun = 0 or 7)

start a php file named crontest.php and enter the following contents:

#!/usr/bin/php -q
//script to test cron on adam’s cloud server:
$message = “If ur reading this then the cron job worked!!!!nn”;

$myFile = “testFile.txt”;
$fh = fopen($myFile, ‘w’) or die(“can’t open file”);

fwrite($fh, $message);
$stringData = “Yay for cloud cron! script was home/ec2-user/crontest.phpn”;
fwrite($fh, $stringData);

start another php file named testFile.php. It will just be a blank txt file with no content at all.

Then upload crontest.php and testFile.php to /home/ec2-user and change permissions of both to 755 or 777 if 755 doesn’t work but 777 can cause a security issue.

Important note: it seems that if you change the regular crontab using crontab -e call that they files have to be in the user’s directory that you are in when making the call which in this case is user ec2-user

To get crontab-e to work, use following comand to switch users and try again:

su ec2-user

to do:
try using sudo crontab -e and puttiing cron files in differnt folders to see if it will work..

How to Access Amazon EC2 Server with FileZilla

I have posted this tutorial on how to access an EC2 Amazon web server with FileZilla because several people seem to have trouble doing so and the process is really simple if you are shown how to do it properly. Follow the below steps closely and you will have access to your Amazon EC2 server using FileZilla in no time.

Connecting to Amazon server in EC2 with FileZilla FTP Client

  1. when an Amazon cloud server is created, the creator is given a security key which is in the format of a .pem file. They then will have to create a .ppk file from the .pem file. Luckily FileZilla will do this for you when you import the .pem so just know that it is doing so and you will be okay.
  2. Open FileZilla and go to “Edit” in the tool bar. Click on “Edit”, then “settings”. After that the settings window will open. In the left of the settings window, click on “sftp”. Then, in the right of the settings window, click on the “add key file…” button and select and import the .ppk or .pem file you were given.
  3. Click “Okay” to close the settings window.
  4. In the main menu of FileZilla, click on “FIle”, then “Site manager” to open the site manager dialog. Click the “new site” button to add a site and give your new connection a name you can remember.
  5. In the” host” field, enter the ip address of the Amazon EC2 you want to connect to. you can use the short or long form of the IP whichever you have access to. It will look something like one of the or
  6. In the “protocol” field, select “SFTP -ssh transfer protocol” from the drop down.
  7. For the “login type”, select “Normal”
  8. For “User” enter the username for your type of instance. For most of my servers, this is ec2-user, so if you have a Linux AMI like mine, enter “ec2-user”. For Ubuntu servers, enter “ubuntu” and for other servers, find out the username and enter it here.
  9. Do not enter a password, just click the “Connect” button next and you are done and the FTP connection should be made if you did everything correctly.

That’s all there is to it.

Amazon Cloud Server Security Rules

In this tutorial, I will go into detail about how to set up security rules for a Linux AMI in an Amazon EC2 web server instance.

When you are creating your instance, you will click on instances in the main nav and then click to add a new instance. In the step of this process that asks you to create or select a security group, you will want to create a special one according to your preferences in most cases unless one of the default ones happens to cover your needs which I doubt. Below, I will go over the rules for creating one for a Linux server with a MySQL database and later a Linux server with a PostgreSQL database. Both are for web servers of course.

Security rules for Linux MySQL Web server

Ports Protocol Source
21 tcp
22 tcp
80 tcp
443 tcp
49152-65535 tcp
0-65535 udp
-1 icmp

when creating your instance, you will be given the opportunity to enter the above rules in. Simply follow the table and create duplicate rules and your web server with Linux and MySQL will work just fine as mine does.


Security rules for Linux PostgreSQL web server

Ports Protocol Source postgresql
21 tcp
22 tcp
80 tcp
443 tcp
5432 tcp
5434 tcp
5499 tcp
49152-65535 tcp
0-65535 udp
-1 icmp

Regardless of which set up you choose, they are entered the same way when you create the instance from your Amazon Management Console. Most of you will probably be using a MySQL database, but as a web developer, I do have a couple clients that use a PostgreSQL database, so it is relevant information for some people.

 Tips for creating Security Groups for an Amazon EC2 Instance

There are some things you should be aware of when creating your security groups. I included the following tips so that beginning web developers do not face the same problems I did when I first started in the cloud:

  1. Add names to your security groups that are descriptive such as Linux_MySQL or Linux_PostgreSQL for the above two.
  2. when launching a new instance, you can reuse security groups, but it seems as if they do not show up in the manage console….well they do, but they will say “no rules”, but yet they work, so this is something I will follow up on in other posts as I learn more.

Postgresql Rename Table

This is a post on how to rename a table in PostgreSQL from the command line. PostgreSQL is a command line rich database application. It does have GUIs available, but most webmasters find it easier to do most database management tasks from the command line or from the psql command prompt.

Command line command to rename table:

First from a command prompt, type:

psql table_name

You will be given a psql command prompt after using the above command line command. Now enter the following command from the psql command prompt:

ALTER TABLE distributors RENAME TO suppliers;


Remove Duplicate Rows from Postgresql Database

In this short tutorial, I will demonstrate how to remove duplicate rows in a postgresql table.

Remove duplicate rows from command line

Log into your server via ssh or open a terminal window and issue the following commands:

To log into psql use the following command from command prompt:

psql database_name

Then from the psql command prompt enter something like this:

DELETE FROM users_with_dups
WHERE  affid NOT IN (SELECT MAX(affid)
                  FROM   users_with_dups
                  GROUP  BY usernm,
                  HAVING MAX(affid) IS NOT NULL)

Of course, in the above query, you will have to change the table name values(two of them which will be the same) and the id column name as well as the group by column names.

Setting up an Elastic IP with Amazon EC2 Webserver

In this tutorial, I will describe in detail how to set up an elastic IP address and associate it with an EC2 instance from the Amazon Management Console.

What is an elastic IP

An elastic IP is Amazon’s equivelient to a static IP. they are unique mostly in one respect. They are IP addresses that get associated with an individual’s Amazon account. They can therefore be set to point to any one of your web server instances in your Amazon account. this can be handy if you every want to point the IP address to another instance in case one goes down or you simply want to change instances for a domain name.

Steps to creating an elastic ip

  1. login to your amazon account at and make a note of the instance ID that you want to add an elastic IP address to.
  2. From the Amazon EC2 tab and click Elastic IPs.
  3. Click Allocate New Address and choose EC2 from the drop-down list. Click Yes, allocate and make a note of the new IP.
  4. Right-click the newly created Elastic IP and choose Associate or check the box in front of it and click associate address at the top of the page and select the instance you want to attach it to.
  5. Be sure to click yes associate to complete the process.
  6. restart your server. If you need assistance with how to restart an Amazon EC2 instance, read this article.



Flexy Template Engine failed to write to directory

This solution is for you If you see the following error using Flexy Templates:

HTML_Template_Flexy fatal error: HTML_Template_Flexy::failed to write to /dirname/etc…


go into your ftp client and find the lib directory for your Flex engine. The file you want is called You have to find the compileDir and templateDir variables and set them all to the correct path. What worked for me was using the entire path instead of the path from the directory the file was in, so I used something like this for the compileDir:


and something like this for the templateDir:


what you need to use will depend on your directory structure, so figure it out by finding the two respective directories and figuring out their complete paths.

Fix Can Not Write to Directory in Bitnami EC2 Server

Okay, I hope this saves someone some headaches. I have a server on Amazon cloud. It is a Bitnami LAPP stack, but this solution would be same for any Bitnami server. I couldn’t write to files from an application in my website.


Make sure that permissions are set to 777 for testing, then you can tighten them later. TO do this, run the following command from the Linux command line:

sudo chmod -R 777 /dir_path/dir_name

Then the folder you want writable has to be owned by the daemon user. This is where I got stumped for an hour or so. My directory was owned by the bitnami user which I thought was correct until I found a post somewhere online that said to change it to daemon. Run the following command at the Linux command prompt to change the owner of your directory to daemon:

sudo chown -R daemon /dir_path/dir_name

FYI, the -R parameter causes both above commands to go into files and subdirectories and change permissions and ownership, so if that’s now what you want, simply exclude the -R in either or both of the above commands.

PHP MYSQL Update Table

In this brief tutorial, I will show you how to do a MYSQL update using PHP.

First, do your database connection:

$con = mysql_connect(“localhost”,”user”,”passwrd”);
if (!$con)
  die(‘Could not connect: ‘ . mysql_error());

mysql_select_db(“my_db”, $con);

Then do the query:

mysql_query(“UPDATE table_name SET field1 = ‘value1’, field2 = ‘value2’
WHERE field3 = ‘whatever’ AND field4 = ‘whatever'”);


That’s it! pretty simple really. You will learn to remember these your own way as I have after years of using them, but for now, you may reffer back to this tutorial anytime you need to do a Mysql Update with PHP.


In this tutorial, we’ll learn how to do a simple MYSQL INSERT using PHP.


first prepare you variables:

$first = “Ian”;
$last = “Lin”;
$email = “”;

second, include your database connection string:

include ‘DB_connection.php’;

do your query:

mysql_query(“INSERT INTO your_table_name (first,last,email) VALUES(‘$first’,’$last’,’$email’)”) or die(mysql_error());

last, close your connection:


That really should be self-explanatory…

Access Table Data from PSQL in PostgreSQL

In order to access data in a PostgreSQL table to view values stored, follow these detailed instructions.

  1. Open a command prompt to your server. I used Putty.
  2. switch to a database user with appropriate privileges. I switched to postgres user with: su postgres and enter your pass. mine starts with a “g”(hint for me:-))
  3. enter psql with: psql databasename
  4. at the psql command prompt enter: SELECT * FROM tablename;

That will show you all the data in the table on the command line.

How to Stop Google from Following a Link

To keep Google and possibly other search engines from indexing a link in your website, use the rel HTML attribute with the anchor link tag like in the following line of HTML code:

<a href=”” rel=”nofollow”>Jafty Interactive Web Development</a>

The above works, but you also have to make sure the link is not in your site map or  linked to without the nofollow rel value elsewhere or it probably will show up to search engines anyway. If you only mention the link once and use the rel attribute with nofollow, it will work however.

Understanding PHP Include Types

There are a few different ways to include files into your current script with PHP. In this tutorial I will explain how to use them for different situations

include PHP Function

Example usage:

include ‘filename.php’;

this type of include is commonly seen in PHP files that include the contents of the named file into your current script wherever it is called. The include function works with the include_path setting in your server’s php.ini file. If you call a file like in the above example usage, PHP will look in the directory named in the include_path variable first. If the file isn’t found there, it will look in the directory of the file that the include function was called from.

include_once PHP Function

The include_once function works the same as the include function except that it checks to see if the code in the include file has been included already and will not include it again if it has already been called into the script.

require PHP Function

The require function is also almost identical to the include function. The only difference is that it will throw a fatal error if the include is unsuccessful

require_once PHP Function

The require_once function is the same as include_once with the exeption that it throws a fatal error if the include doesn’t work.


There are cases where I like to use include_once. For example, I often have roughly three files for most web pages I develop. I have a main file that contains the main contents of the web page. Then I have a header file named something like header.php that contains the site header. Finally I have a footer file named something like footer.php that contains the site footer. The header and footer files normally get included in every page while the main file is typically different, so the same header and footer PHP files are reused over and over again saving disk space and making the website more efficient. It is also practical because you can edit one file to make changes to every page on the website’s header or footer instead of having to edit each page separately.



How to Clone an EC2 Instance

In this brief tutorial, I will describe how to clone an Amazon ec2 server instance.

  1. Log into your AWS account HERE.
  2. click to view your running instances and place a check by the instance you want to clone.
  3. Click the actions tab above the instance area and select “Create Image”.
  4. In the left navigation area, click “AMIs”.
  5. Right click on the AMI you just created and select “Launch Instance”

Then as you follow the prompts to launch the new instance you can make the necessary changes you may have left out the first time. For example, I found this to be the easiest way to add a new security group or open a port on an existing server.

Starting new server

after you have done the above, you will need to start the server and database like this after logging into the server with putty:

sudo service httpd start

sudo /etc/init.d/mysqld start


If you want to use a security group from another server, the rues may not show up, but it did work when I did it. Don’t know why, at first I thought it had to be in same zone, but changed to same zone and still didn’t show up, but when I tested the cloned instance, it worked, so the security rules must be in place…

Restart HTTP Server from Command Line

How to restart web server from command line

Here is how to restart various servers from Linux command prompt.

Restarting a Linux Bitnami Lappstack apache server

sudo /opt/bitnami/ restart apache

Restart a postgresql database on Bitnami LAPPstack

sudo /opt/bitnami/ restart postgresql

Restarting other Amazon Linux APIs

To start most Ubuntu type servers and other Linux APIs use the following command to restart the server:

sudo service httpd restart

Then the following command to restart the database if you are using MySQL:

sudo service mysqld restart