Error: Too many open files

Error: Too many open files

Couple days ago, I ran into this error on CentOS 5.2.  To my understanding this error can occur on all flavors of linux.

This totally caught me by surprise as I had never seen this before.  I had just finished installing awstats and wanted to run an update for the first time.  The logs were gathered from 6 webservers and each webserver had about 225 logs.

The awstats log merging script tried to open these log files (225×6 = 1,350 logs) and merge them to read the data but it kept crashing.  The reason it kept crashing was because the limit for the maximum number of files that you are allowed to open in a shell was set to 1024.

To check what your limits are just type in the following command:

ulimit -a

To change the limit for the number of files you are allowed to open, change the “open files” limit.  To do so, type in the following command:

ulimit -n3000

This will set the limit to 3000 files.  You can set it to whatever number you need to.

After changing my “open files” limit, I was able to run my initial awstats update without any issues.

How to Reset the MySQL Root Password

How to Reset the MySQL Root Password

Follow the steps below if you have forgotten your mysql root password and wish to reset it.

1.  Create a text file with the following information in it.

UPDATE mysql.user SET Password=PASSWORD(’NEW_PASSWORD’) WHERE User=’root’;
FLUSH PRIVILEGES;

Replace “NEW_PASSWORD” with your desired mysql password.  Save the file to /etc/mysql-pass-reset

2.  Stop MySQL

/etc/init.d/mysqld stop

3.  Start mysqld_safe with the –init-file option like so:

mysqld_safe –init-file=/etc/mysql-pass-reset &

This will start mysql and execute the query in the text file you created.

4.  Now stop and start mysql back up normally without the –init-file option.

/etc/init.d/mysqld stop
/etc/init.d/mysqld start

Don’t forget to delete the “mysql-pass-reset” file when you are done!

rm /etc/mysql-pass-reset

How to Install ThinkorSwim in Linux

How to Install ThinkorSwim in Linux

Here are the quick an easy steps on how to install TOS in Linux (Debian Distro).

First you will need to setup Java on your computer so lets go ahead and do that. Open up the console and login as super user.

su

Now that we have full privileges, lets edit the sources.list file so that we can fetch the much needed java packages.

pico /etc/apt/sources.list

This will bring up your sources.list file in the editor. You should see something that looks like this:

deb http://debian.lcs.mit.edu/debian etch main
deb-src http://debian.lcs.mit.edu/debian etch main

Add the words “contrib non-free” at the end of both lines so it looks like the following:

deb http://debian.lcs.mit.edu/debian etch main contrib non-free
deb-src http://debian.lcs.mit.edu/debian etch main contrib non-free

Now hit “Ctrl + O” to write the changes to the file. Then hit enter to accept the changes. Now hit “Ctrl + X” to exit the editor. That was the hard part :) . Lets update our package listing and download the java packages by running the following commands:

apt-get update
apt-get install sun-java5-jre sun-java5-jdk

Follow the onscreeen instructions to complete the installation of the java packages. Its pretty much gonna ask you to agree to the licenses. After you are done you can close the console window.

(more…)

Using CSS to Center Vertically and Horizontally

Using CSS to Center Vertically and Horizontally

Centering items using CSS is actually very easy.  If you want to center a picture horizontally then just use the following code in your CSS file.

IMG.centered {
display: block;
margin-left: auto;
margin-right: auto;
}

What the above CSS code does is center an image between the left and right margins automatically. Whenever you insert an image just make sure to call the “centered” class, for example:

<img class=”centered” src=”/images/someimage.jpg” alt=”some text” />

If you want to center something vertically then you would need to use a container.  For example:

DIV.container {
top: 0;
left: 0;
width: 100%;
height: 100%;
position: fixed;
display: table;
}
P {
display: table-cell;
vertical-align: middle;
}

Now that you have the CSS part done lets look at the HTML part.

<div class=”container”>
<p>This text is vertically centered</p>
</div>

Now lets combine both of the techniques together to have an image that is horizontally and vertically centered in a browser window.

CSS Code:

DIV.container {
top: 0;
left: 0;
width: 100%;
height: 100%;
position: fixed;
display: table;
}
P {
display: table-cell;
vertical-align: middle;
}

HTML Code:

<div class=”container”>
<p>The image below is vertically and horizontally centered.<br />
<img class=”centered” src=”images/someimage.jpg” alt=”some text” /></p>
</div>

Thats It!

Automatically Add WWW to All Your URLs

Automatically Add WWW to All Your URLs

It is always a good idea to pick one format for your website URL and stick with it.  If you want to make sure all your URLs have a “www” in front of them then paste the following code at the top of your “.htaccess” file.

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI}\\/%{HTTP_HOST}/www. ^/+(.+/)?[^.]*[^/]\\(/)([^w][^w][^w][^.].*/(www\.)¦.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www. ^(/)?(/)?([^w][^w][^w][^.].*/(www\.))$ [NC]
RewriteRule ^ http://%4%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]

The above code uses the apache mod_rewrite module to rewrite your URL.  It also makes sure that there is a trailing slash at the end of your URL.  This website is using the above code to make sure all URLs load with a “www” and have the appropriate trailing slashes.

How to Change Autoincriment Value in MySQL

How to Change Autoincriment Value in MySQL

If you have a attribute in your table that automatically increments itself for every new row, you can change what the next value is very easily.

You can do so by either of the two following ways.

1. Change the autoincrement value with a simple sql query.

ALTER TABLE {table name} AUTO_INCREMENT = {some number};

Where {table name} is the name of the table of which you are modifying the auto_increment value and {some number} is the actual auto_increment attribute value that you wish the next row to have.

2. When inserting the next row in that table, manually set the value for the auto_increment attribute and it will automatically continue from that number for future entries.

Understanding Linux File Permissions

Understanding Linux File Permissions

File permissions in Linux are actually pretty easy to understand and modify.  To check the permissions on a file just navigate to the directory and use the following command to list the files and their details.

ls -l

As you can see, the first piece of information on each line are the permissions.  There are 3 sets of permissions put together all into one line.  The very first character denotes whether the file is a directory, a link or a file.

- = file
l = symbolic link
d = directory

The 3 characters following the first character represent the permission information for the file owner.  The 3 characters following that denote the permissions for the group and the last 3 characters are the file permissions for everyone else.  A key for what the characters mean is listed below:

- = no read, write, or execute permissions
r = read
w = write
x = execute

For example, lets say we have the following permission information on a file.

drwxr-xr-x

Since the first character is “d” we know that its a directory.  Then next 3 characters are “rwx” which means that the owner has read, write, and execute permissions.  The next set of characters are “r-x” which means the group has read and execute permissions but no write permissions.  The last 3 characters are “r-x” which means that everyone else has read and execute permissions but no write permissions.
You can easily change file permissions by using the “chmod” command.  Using numbers to change permissions on a file is very quick, easy, and precise.  A key for what each number represents is listed below.

4 = read
2 = write
1 = execute

You have to set permissions for the owner, group, and everyone else all at the same time when using the number method so that means there will be 3 numbers.  The first number is the permission for the owner, the second is for the permission of the group and the last number is the file permission for everyone else.

To give the owner, group, and everyone else only read permissions then the command would be the following.

chmod 444 filename

To give the owner, group, and everyone else read and execute permission then you just add the numbers together.  So 4(read) + 1(execute) = 5.

chmod 555 filename

The numbers don’t have to be the same, you can give different permissions to the owner, group and everyone else all in one shot.  So lets say you want the file owner to have read, write and execute permissions but you want the group and everyone else to have only read and execute permissions.  The command to do this would be the following.

chmod 755 filename

If you are applying permissions to a folder and would like to recursively change the permissions of the contents as well then don’t forget to use the capital ‘R’ switch:

chmod -R 755 somedir

Remember, the first digit is for the owner, the second digit is for the group and the last digit is for everyone else.