FirstServed Tech Blog - FirstServed and the Art of Server Tuning

Archive for the ‘Hosting, Servers,…’ Category

Optimize all MySQL databases with one command

Thursday, September 15th, 2011

A frequent problem we have is the clutter of MySQL databases created by shared webhosting customers. In order to maintain a well performing MySQL server we now and then have to optimize the MySQL tables. Doing this by hand, or waiting until the user does this is no option, therefore, we run this command, through a cron job:

First, we create a user named mysql_optimizer (with a strong password) and give it select and insert priveleges on the databases. You do not want your root user to be used in a cronjob!

mysql> GRANT SELECT, INSERT ON *.* TO ‘mysql_optimizer’@'localhost’ identified by ‘strong password’;
mysql> flush privileges;
mysql> exit;

Second thing is setting up the crontab, this time a weekly simple schedule:

> crontab -e
# MySQL weekly optimisation
0 0 * * 0 /usr/bin/mysqlcheck -Aos --optimize -u mysql_optimizer -pstrongpassword > /dev/null 2>&1

That’s it!

Iptables NAT

Tuesday, May 24th, 2011

Here is a quick and dirty iptables based NAT solution for linux servers:

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 --source 192.168.0.1 -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 -j REJECT

Here we will provide internet access to the host 192.168.0.1 behind eth1 over our own internet connection on eth0.
The ip of eth0 can be dynamic. The host 192.168.0.1 will be using the ip of eth0 on the internet.

We only accept traffic from the ip 192.168.0.1, the rest is rejected.

Oh, and don’t forget to enable ipv4 forwarding in the kernel!
Add:

net.ipv4.ip_forward = 1

to /etc/sysctl.conf and run:

sysctl -p /etc/sysctl.conf

Or just run:

echo 1 > /proc/sys/net/ipv4/ip_forward

Your Milage May Vary…

Sorting Apache access_logs

Tuesday, May 24th, 2011

When working in a clustered environment you might encounter situations where you need to sort merged access_logs from 2 different servers for one website. In this case the timestamps may not be ordered corretly due to time offsets in the individual servers or delays in the logging proces. The ordered data is needed since some statistics processing engines expect correctly sorted data.

The sorting can easily be done by the following command:

sort -t ' ' -k 4.9,4.12n -k 4.5,4.7M -k 4.2,4.3n -k 4.14,4.15n -k 4.17,4.18n -k 4.20,4.21n access_log.1 > access_log.1.sorted

Adjust, change and implement to match your own personal preference!

Microsoft SQL Server: How to recover a suspect database

Wednesday, January 19th, 2011

Hi,

Normaly you should always have backups of your databases, restoring suspected databases is not a good option, but I think at some point in time, in some situation you’ll want to do it anyway.
So today I set up a test case for this. I mirrored one of our database servers and made it crash, badly.
Two databases were corrupted, and went to the "suspect state".

Microsoft SQL Server started throwing errors in our general direction like:

"Database cannot be opened. It has been marked SUSPECT by recovery."

Not good, but just what we wanted…

So how would it be possible to recover these databases?

First, set them to emergency mode, this way you will be able to retrieve control.
In emergency mode you can actually open the database:

alter database "test-database" set emergency;

Second, put them in single mode (needed for the check we will be performing):

alter database "test-database" set single_user;

Then, check the database (this repair allows data-loss, since I’m sure some data will have to be lost in my scenario):

dbcc checkdb ('test-database', REPAIR_ALLOW_DATA_LOSS);

Finally, if the check succeeded, you can re-enter multi-mode:

alter database "test-database" set multi_user;

And there you go, the database is restored…
Not a best practice, but it may save some time once. So if you ever have a problem on your dedicated server database hosting, you know this might to the trick.

Could not load file or assembly System.EnterpriseServices

Friday, October 16th, 2009

When running through what should be a simple and painless Microsoft Sharepoint installation on a Windows Server 2003 server (with IIS 6.0 pre-installed – I don’t recall if it was a virtual server or a dedicated server), I encountered the following problem:

Server Error in '/_layouts' Application.

IIS is kind enough to hide all the information about the error, so there you are, without a clue.
Maybe this can solved in the "SharePoint Central Administration"? No luck either, now it throws a similar error.
So, to resolve this issue, we first need to activate the error output in the web.config.
This is located at the following location:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\template\admin\1033\web.config

Open the file using your favourite text editor and search for the line which sets the customErrors (line 3), change it to:

<customErrors mode="RemoteOnly" />

Now you’ll be able to view the errors when browsing to your Sharepoint installation from the server itself.
This time it presents you the real problem:

Parser Error Message: Could not load file or assembly 'System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' could not be loaded.

So, how do we fix this missing assembly?
Open two windows explorer screens with these locations:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
C:\WINDOWS\assembly

(The .NET Framework version and Windows Path may differ depending on your installation.)

Then drag the file "System.EnterpriseServices.dll" from the Microsoft.NET\Framework\v2.0.50727 folder to the assembly folder.

Now the problem should be fixed. (It is possible you’ll have to recycle the Application Pool.)
Reload your browser windows, both the Central Administration page at port 9503 en the normal sharepoint pages at port 80 should work.

Thanks goes out to the people at forums.asp.net!

YUM update is taking ages…

Friday, October 2nd, 2009

If the good old and trustworthy CentOs image you are using is suddenly being VERY slow when performing a "yum upgrade" command, try this rule to speed things back up a bit:

yum clean all

Most likely yum is using some old cached information.

We encountered this problem when upgrading from CentOS 5.0 to 5.3.

Speeding up drupal hosting by setting up a quality LAMP environment

Wednesday, July 8th, 2009

One of our customers works with drupal… Unless you are from another planet, you know drupal is currently one of the leading open source CMS systems with nice features, but it also is quite a bottleneck on server performance. Not to mention the load on a database server etc…

We suggest drupal hosting customers to opt for a virtual server setup. Does this mean you can’t host drupal on a shared hosting platform? Not at all, but if you have a high load website, a isolated server instance might give you some advantages.This means you can set up a nice isolated environment for a Drupal driven CMS website with dedicated server performance at severe reduced cost.

When setting up the environment be sure to run through the following checklist:

  • CentOS 5.3 (for example)
  • Apache 2.2 webserver
  • MySQL 5+ database server
  • PHP 5+
  • and, very important: eAccelerator 9.5.3 or higher.

Why the eAccelerator? Well, Drupal uses a lot of cached pages. Enabling Drupal caching already gives you a boost but installing eAccelerator makes sure frequently used php scripts are stored in a compiled state thus allow retrieval faster.

On top of that, you may optimize the MySQL query_caching settings, as they Drupal also uses a lot of the same queries.

You can find the latest version of eAccelerator here: http://eaccelerator.net/

Windows folders hidden after migration to virtual machine

Friday, June 5th, 2009

A few months ago, we’ve undertaken the project of migrating all our old Pentium IV servers to a virtual platform.  Apart from the extra flexibility which this offers in terms of provisioning, we’re also benefiting from a huge power saving, since a modern Dell platform can easily host a dozen older servers.

However, after having migrated a Windows server, we observed that the attributes of folders on the filesystem had been modified.  In Windows Explorer, everything seemed all right, but when FTP’ing to certain folders, or when viewing them with the ‘dir’ command, the folder were hidden.  It appeared that the ‘system’ attribute had been set throughout the C drive.

We solved the problem by executing

attrib /S /D -S -H -R

Regenerating SSH keys

Wednesday, April 15th, 2009

We thought it would be good to share this simple command to regenerate SSH keys:

rm -fr /etc/ssh/*key*
service sshd restart

Please be careful when executing this command, it will remove files without confirmation!

In normal situations you would never need this…
However it can be usefull when cloning machines.

Clearing local DNS cache

Wednesday, February 18th, 2009

When modifying DNS settings, a local DNS cache can be pretty annoying.
Often you’ll want to clear the local cache of your computer and get new records from your provider (which may speed up the process if the provider has already updated the DNS records).

This can be done with these commands:

Windows:

ipconfig /flushdns

Linux:

/etc/init.d/nscd restart

(or service nscd restart, or /etc/rc.d/init.d/nscd, depending on your distribution)

Mac OSX:

dscacheutil -flushcache

(or lookupd -flushcache for Tiger)

These commands should be entered in a terminal ("cmd.exe" for the Windows users).