Change timezone on PHP

There are some methods that allows you to change the timezone of the PHP (5.1+) scripts (ie, when the date is not shown due to a different timezone of the server).

The first one is by modifying the php.ini file of the web server (if you are allowed to). We just need to add the following line:

date.timezone = "America/Lima"

Other method is using the ini_set() function directly on your script (at the beginning of it), this way:

ini_set('date.timezone', 'America/Lima');

And the third method consist on the use of the function date_default_timezone_set() (also, directly on your script):

date_default_timezone_set('America/Lima');

The list of the available timezones is here.

Sources: SiteGround, The Electric Toolbox

Debian/Ubuntu’s apt behind a proxy

To allow the Advanced Packaging Tool, or just APT for friends to do its job when the box is behind a proxy, we can use one of the following options.

From the command line (this will only work for the session we are working on):

export http_proxy=http://username:password@server:port

Or if the proxy does not require a username:

export http_proxy=http://server:port

Another option is to add the following line into the file /etc/apt/apt.conf (note the semicolon at the end):

Acquire::http::Proxy "http://username:password@server:port";

Enable or Disable Check Constraints on SQL Server

I found a very handy SQL script on this post, that allows you to enable or disable all constraints on a database. The only problem I had was that my database uses schemas to organize the tables, so the script failed to work.

I made a modification on the script to solve the problem, so here it is:

/************************** DISABLE ALL TABLE CONSTRAINTS **********************************
This script will enable or disable all constraints on all tables within the database
that it is run in.
********************************************************************************************/
 
SET NOCOUNT ON
SET ROWCOUNT 0 
 
DECLARE @COUNT INT
DECLARE @String nvarchar (1000)
DECLARE @ConstraintName VARCHAR(128)
DECLARE @TableName VARCHAR(128)
DECLARE @SchemaName VARCHAR(128)
 
--Find all constraints and their respective tables from the sysobjects table and place into a temp table.
--Primary Key and Unique Constraints via Unique Indexes are not disabled through this command
--You should use the ALTER INDEX...DISABLE command in SQL Server 2005
SELECT
	o.name AS ConstraintName,
	sc.name AS SchemaName,
	object_name(o.parent_obj) AS TableName
INTO #Const_Table
FROM sysobjects o, sys.schemas sc
WHERE xtype IN ('F') AND o.uid = sc.schema_id
 
SELECT @COUNT = COUNT(*) FROM #Const_Table
 
--Setting the rowcount to one allows for one row from the temp table to be picked off at a time.
--Used as an alternative to a cursor.
SET ROWCOUNT 1
 
--Loop until all rows in temp table have been processed.
WHILE @COUNT > 0
BEGIN
	--The rowcount of one ensures that only one tablename and constraint name is picked.
	SELECT @TableName = TableName, @SchemaName = SchemaName, @ConstraintName = ConstraintName
	FROM #Const_Table
 
	--Comment or uncomment the following statements according to your needs
	--Build execution string to enable constraint.
	SET @String = 'ALTER TABLE ['+ @SchemaName + '].['+ @TableName + '] WITH CHECK CHECK CONSTRAINT [' + @ConstraintName +']'
	--Build execution string to disable constraint.
	--SET @String = 'ALTER TABLE ['+ @SchemaName + '].['+ @TableName + '] NOCHECK CONSTRAINT [' + @ConstraintName +']'
 
	--Execute the SQL
	EXEC sp_executesql @string
 
	--Remove this row from the temp table, since it has now been processed.
	DELETE FROM #Const_Table
	WHERE ConstraintName = @ConstraintName AND TableName = @TableName AND SchemaName = @SchemaName
 
	SET @COUNT = @COUNT - 1
END -- Loop
 
DROP TABLE #Const_Table
 
SET ROWCOUNT 0

Count the total number of records on a MySQL database or per table

To count the total number of records on a MySQL database, we could run the following command:

SELECT SUM(TABLE_ROWS) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{tablename}';

If we need to know the number of records per each table, we can run:

SELECT TABLE_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{tablename}';

Source: Stack Overflow

Duplicate a MySQL database on a single line

On this article, it is shown a very convenient way to duplicate a complete MySQL database using a single line of code:

$ mysqladmin create {dest_db_name} -u {username} --password={password} && \
mysqldump -u {username} --password={password} {source_db_name} | \
mysql -u {username} --password={password} {dest_db_name}

Where:

  • {dest_db_name}: Name of the destination database.
  • {source_db_name}: Name of the database to be duplicated.
  • {username}: MySQL username.
  • {password}: MySQL password.

If we need to duplicate it to another server, we would do something like this:

$ mysqladmin create {dest_db_name} -u {dest_username} --password={dest_password} -h {dest_server} && \
mysqldump -u {source_username} --password={source_password} -h {source_server} {source_db_name} | \
mysql -u {dest_username} --password={dest_password} -h {dest_server} {dest_db_name}

Where:

  • {source_db_name}: Name of the source database.
  • {source_server}: Source MySQL server.
  • {source_username}: Source MySQL username.
  • {source_password}: Source MySQL password.
  • {dest_db_name}: Name of the destination database.
  • {dest_server}: Destination MySQL server.
  • {dest_username}: Destination MySQL username.
  • {dest_password}: Destination MySQL password.

Nginx and Munin

To be allowed to show Nginx graphs on the Munin report, it is required to enable nginx status and to listen on:

http://127.0.0.1/nginx_status

To do this, we just need to add the following code on /etc/nginx/sites-enabled/default or into any other site file (like create a new one /etc/nginx/sites-available/status and then make symbolic link to site-enabled directory):

server {
    listen 127.0.0.1;
    server_name localhost;
    location /nginx_status {
        stub_status on;
        access_log   off;
        allow 127.0.0.1;
        deny all;
    }
}

Source: Server Fault

Install VMWare ESXi using a USB flash drive

Today, I decided to try the VMWare ESXi, the free bare metal virtualization product of VMWare, on an old Pentium 4 3.2GHz PC (with 2.5GB of RAM), and run on top of it, some Ubuntu servers.

I was not able to use the latest version (4.x) of the ESXi as it only works on 64-bit boxes. Instead, the version 3.5 does work on 32-bit processors as the one I’m using.

As I didn’t want to burn a CD with the installer, I chose to use an USB stick to make the installation. So, the first thing to do was converting the ISO installer into a USB, using this software. I had a little problem, that I solved in some minutes with the help of Google.

After that, the installation ran without problems (there is a nice ESXi installation guide here) and the server was up and running some minutes later.

Then, I just unplugged the monitor, keyboard and mouse from that PC, as they are not gonna be needed anymore. I installed the VMWare vSphere Client on my main machine to manage the server.

So far, I have installed two Ubuntu Linux servers and they are running with not bad performance, at least, enough for testing and development.

No DEFAULT or UI configuration directive found

When I tried to run the VMWare ESXi installer from a USB stick (after creating the installer on a USB from the ISO), I got the following error:

SYSLINUX 3.85 2010-02-20 CBIOS Copyright (c) 1994-2010 H. Peter Anvin et al
No DEFAULT or UI configuration directive found!
boot:

The solution was pretty simple thanks to this post. I just needed to rename a couple files on the USB as follows:

  • isolinux.bin to syslinux.bin
  • isolinux.cfg to syslinux.cfg

It worked flawless. But if that does not work for you, you may try formatting the USB as FAT instead of FAT32.

Remove duplicated whitespaces on C#

The Trim method on C# only removes whitespaces at the beginning and at the end of a string, but it does not remote duplicated whitespaces inside the string (like the Trim function on VB for Apps), which is very handy sometimes.

One of the ways to do it on C#, is using Regular Expressions. This method will not only remove whitespaces but also tabs and line breaks, and will replace them with one whitespace.

string s1 = "He saw   a cute\tdog.\nThere\n\twas another sentence.";
Regex r = new Regex(@"\s+");
string s2 = r.Replace(s1, @" ");
// The result would be: He saw a cute dog. There was another sentence.

You will need to add the reference to System.Text.RegularExpressions to your namespace.
Source: Dot Net Perls

Concatenate text on NVelocity

There is no operator to concatenate two strings in NVelocity (the .Net version of Velocity) like we can do on other languages like C# or PHP, with the plus (+) operator for the first one and dot (.) for the second one. For example, in C# we would do:

string strHello = "Hello " + "World";

And in PHP:

$hello = "Hello " . "World";

But in NVelocity, what we must do if we need to do this same thing would be:

#set ($str1 = "Hello ")
#set ($str2 = "World")
The concatenated string would be: $str1$str2

Source: Velocity User Guide