Enable passive mode for Pure-ftpd on AWS

One of the issues you encounter when installing FTP servers on AWS is that you need to user Active mode to transfer files, while traditionally FTP clients tend to use Passive mode (PASV). This is caused by the fact that each instance has an external IP that you get access to and another internal IP, which is used by the FTP server. When the client requests PASV mode, the server replies with the internal IP, which of course is non-routable(is that the correct word?).

Fixing this is quite easy, it involves a little bit of editing Security Groups from AWS Console and adding a couple of lines.

First of all you will need to determine what port range you can use for PASV mode, so execute this command:

cat /proc/sys/net/ipv4/ip_local_port_range

You must choose a range that’s *not* in the interval returned by the command. I will use 10000 – 10100.
Now you will need to find out your external IP. Either request an Elastic IP and attach it to the instance or ping your “Public DNS”(available in the AWS Console, when you select the instance). Write it down somewhere.

Now open the required ports from your “Security Groups”, by adding the following rules:

Custom TCP Rule Ports 20-21
Custom TCP Rule Ports 10000-10100

Don’t forget to “Apply Rule Changes”!!!
Depending on your pure-ftpd installation you may have your FTP server configuration either in /etc/pure-fptd.conf or in /etc/pure-ftpd/conf/*. If you have your configuration in a single file (/etc/pure-ftpd.conf) then you will need to add these 2 lines:

PassivePortRange 10000 10100
ForcePassiveIP YOUR_EXTERNAL_IP_HERE

If you have your configuration in /etc/pure-ftpd/conf/* simply create two files, named after the options you want to change and insert the values into them:

echo "10000 10100" > /etc/pure-ftpd/conf/PassivePortRange
echo "YOUR_EXTERNAL_IP_HERE" > /etc/pure-ftpd/conf/ForcePassiveIP

Obviously you will need to replace YOUR_EXTERNAL_IP_HERE with your real external IP.

Now restart your FTP server and check that you see something similar to “-P YOUR_EXTERNAL_IP -p 10000:10100” in the parameter list. If everything went ok, congratulations!

service pure-ftpd-mysql restart

MySQL errors after update , error 1018 (HY000), errno: 24

Today I’ve updated MySQL server on one of my Ubuntu 12.04 servers. After the upgrade finished, shock and horror: all websites were down, WordPress sites were presenting install page. Quickly connected to mysql via CLI and tried to see if the databases are still there:

mysql> show databases;
ERROR 1018 (HY000): Can't read dir of '.' (errno: 24)

I’ve tried setting limits as in this article, but that didn’t seem to help. Only after adding “open-files-limit = 2048” to my.cnf and restarting the MySQL server things went back to normal. So a big thank you to the author of the article, but I would like to know if there is any need setting those limits or not(obviously I was stressed out over the web sites being down and didn’t test all things).

Anyone else has any experience with this problem?

Freelancer acquires vWorker

Today I’ve received an email from Freelancer.com letting me know that they have acquired vWorker.com (RentACoder for older freelancers). I was quite shocked by this news as I never expected that a big site like vWorker will be bought. But life goes on and nothing stays the same forever. I have to say that so far I really enjoy Freelancer’s interface a lot more than vWorker. It’s clean, it’s modern and it’s logical. Even after the face lift vWorker did some time ago the site looked dated and wasn’t very intuitive.

I am still waiting for Freelancer to import my project ratings, I hope they will do it soon. Without rating is really really hard to get a job on a freelancing site. But while I wait for them I’ve already made a bid to check the process. So far I like what I see, it seems very clean and intuitive. I’ve also checked the taxes (10% for free account and 3% for premium) which are not that bad.

One thing that drives me nuts is the “Skills” section. Although I have checked many skills it only displays 5 for my Freelancer profile, while for my Employer profile it displays a lot more. I don’t get it. I’ll probably contact their support, maybe they can enlighten me.

The Portfolio section is beautiful, I cannot even compare to what vWorker had. It’s exactly what you need to display your work.

I’ll update the article as I discover more things (hopefully pleasant) on Freelancer. Meanwhile if you need my services you can click on the button bellow and give me a job on Freelancer.com

LE: One day later and I have all my ratings and projects imported. Good work Freelancer!
PHP Developer

Installing MongoDB PECL extension

The MongoDB PECL extension has not been installed or enabled

If you have installed MongoDB and you get the above error or something similar to it, you will need to install the php extension. It’s quite easy, shouldn’t take more than a couple of minutes. All the commands were executed as root, if you want to use the sudo mechanism, just prefix all the commands with sudo.

Install the required packages

apt-get install php-pear php5-dev make

If everything went ok, simply install the extension by executing this command:

pecl install mongo

Activate the MongoDB extension

I have Ubuntu 12.04 server edition installed on the server so I simply added a new ini file containing one line:

echo "extension=mongo.so" > /etc/php5/apache2/conf.d/mongo.ini

Now restart web server (in my case Apache 2.2) and enjoy:

service apache2 restart

mod_fcgid: HTTP request length exceeds MaxRequestLen

Symptoms:
When trying to upload a file you get “Error 500, Internal server error”. In the error log file you get something like:

[Tue Aug 21 20:40:39 2012] [warn] [client x.x.x.x] mod_fcgid: HTTP request length 132532 (so far) exceeds MaxRequestLen (131072), referer: http://www.example.com/upload.php

This problem is present in ISPConfig 3 running on Ubuntu 12.04, when running Apache2 with mod_fcgid.

Fix:
Edit “/etc/apache2/mods-available/fcgid.conf” and add:

FcgidMaxRequestLen  1073741824

Server setup ISPConfig 3 with Ubuntu 12.04

Note: This is work in progress! You should probably test this in a virtual machine first and play around with various settings.

There are a lot of tutorials over the Internet about how to configure an Ubuntu server for ISPConfig. I usually follow them, after all there aren’t so many variations on how to setup a LAMP. But I always find some things that bother me. I don’t claim my setup is “perfect”, but please read and make your own decisions.

Installing Ubuntu 12.04

Disk Partitioning

First of all, do yourself a favor and use at least 2 HDDs for the server you are going to setup. It doesn’t matter if they are of different sizes as long you have enough space on the smallest one. If they have same size, the better. The reason why I am insisting on using at least 2 HDDs is because you will be able to setup software RAID1. There are pro and cons of using software RAID1, but I think it’s better to have it. It’s not even hard to configure RAID1:

  • create 2 equal size partitions, one on each drive
  • mark them as “Use as: physical volume for RAID”
  • go to “Configure software RAID” and “Create MD device” -> RAID1, 2 disks, 0 spare and select the two partitions you have marked for RAID (probably /dev/sda1 and /dev/sdb1)

You should see now RAID1 device #0 which is commonly known as /dev/md0. Congrats, you have just finished setting up software RAID1! Press Alt+F2, Enter and check out the progress of RAID sync:

cat /proc/mdstat

Note: If you are using a system that has (U)EFI you might want to partition your disks before using GParted and you will need to create an EFIBoot partition of about 100MB at the beginning of HDD. To keep things looking nice you can create 2x 128MB partitions on both harddisks(one on each). More details about this on my previous article “Install Ubuntu 12.04 with RAID/LVM on UEFI system”

I prefer to use LVM over the RAID1 I’ve just created because I find it easier to manage this way. I usually create different logical volumes/partitions for:

  • /boot: this one is usually 128MB, for kernel; you skip it if you want
  • /: make this one about 10-20GB, for system
  • /tmp: can be anywhere from 2GB to 10GB, set nosuid and noexec for it
  • /var: this is where there are web files and databases, so I usually use the rest of the space for it; also set usrquota and grpquota for this

If you are using UEFI:You will need 1 vfat32 partition for EFIBoot and 1 ext4 partition for /boot, outside the software RAID. After playing around with all kinds of setups, this was the only working solution I have found. The bad thing with this setup is that in case of disk failure you will have to recover either EFI partition or/and the /boot partition. Sorry, it sucks, but I didn’t find a better way to do it.

Here is how to setup LVM

You can use LVM over RAID1 or simply use LVM. Here are the steps:

  • “Configure the Logical Volume Manager” and use /dev/md0 as Physical Volume.
  • Create a Volume Group and named it “VolGroup00” (or whatever you want)
  • Create as many Logical Volumes as you need, set the right size for them (you can resize later). I usually name them LogVolRoot (for /) or LogVolTmp (for /tmp)
  • When you are done click “Finish”

You now have successfully setup LVM over RAID1. Just one more step and you are done partitioning. For each of the LVMs you have created, click on them and set the following:

  • Use as: file system of your choice; I use XFS because of fs freeze and resize on the fly, feel free to use ext4 or whatever you like most in the end I used ext4 because of quota issues with ISPConfig 3
  • Format the partition: yes
  • Mount point: put here where you want to mount it (be it /, /boot, /tmp, /var, /srv)
  • Mount options: set nosuid and noexec for /tmp and usrquota and grpquota for /var if you have them

Note: About swap partition(s) … there are many approaches to this. You can have 2 swap partitions, one on each drive, set same priority for both and put them outside RAID/LVM. Or you can create a LV for swap. If the harddisks don’t have the same size you can create the swap on the free space left. Recommended swap size is (according to RHEL) 2* RAM if you have less than 2GB, and 2+ RAM if you have over 2GB. I had 2 HDDs, one of 500GB and one of 250GB so I created the swap on the free space left on the big drive. The rest of the space I’ve created a partition, put XFS on it and mounted in /srv for later use (probably some FTP storage, whatever is not important)

Installing software and ISPConfig 3

For the most part I’ve followed this excellent tutorial The Perfect Server – Ubuntu 12.04 LTS (Apache2, BIND, Dovecot, ISPConfig 3), but there are some minor things changed.

First of all I don’t like to disable AppArmor as suggested in the tutorial. From my experience is not that hard to figure out when you screwed up things. Most of the times you will have problems with AppArmor if you change data_dir for MySQL or you made changes to Apache, etc. If you have recently made changes to configuration of MySQL(let’s say you have moved database files from /var/lib to /srv/mysql) and the server is not starting up take a quick look at /var/log/syslog and look up for apparmor entries. If you see something like denied, edit the file /etc/apparmor.d/usr.sbin.mysqld and try to find the entries related to the directories modified. For /var/lib you will find 2 entries similar to these:

  /var/lib/mysql/ r,
  /var/lib/mysql/** rwk,

Either replace /var/lib with /srv/mysql or append 2 new lines, your choice. Restart AppArmor and this should be all. MySQL should start now.

Also in the tutorial at some point you are advised to comment out this line in /etc/mysql/my.cnf:

bind-address           = 127.0.0.1

You only need to do this if you plan to access the MySQL server/databases from another server(you have the website files on another server, you need to set replication). But if all you want to do is to run web sites that use “host= localhost” for their database settings don’t comment out that line. Most of the times you should be fine with MySQL listening only on local interface.

Install MySQL

apt-get install mysql-client mysql-server
mysql_secure_installation

I know there is a lot of debate about using innodb_file_per_table, but after getting a corrupted ibdata1 file and losing all the InnoDB tables I will go with having 1 file per table.

Install Apache2 + PHP-FPM

apt-get install apache2 apache2.2-common apache2-doc apache2-mpm-prefork apache2-utils libexpat1 ssl-cert libapache2-mod-php5 php5 php5-common php5-gd php5-mysql php5-imap phpmyadmin php5-cli php5-cgi libapache2-mod-fcgid apache2-suexec php-pear php-auth php5-mcrypt mcrypt php5-imagick imagemagick libapache2-mod-suphp libruby libapache2-mod-ruby

Also I found I was missing php5-curl package:

apt-get install php5-curl

Enable required mods:

a2enmod suexec rewrite ssl actions include

If you plan to use PHP-FPM and fastcgi mod (available in ISPConfig 3.0.5 or svn release):

apt-get install libapache2-mod-fastcgi php5-fpm
a2enmod fastcgi

Install Postfix and Courier

I am only installing Postfix without Amavis and AV/AS capabilities since I am planing to integrate ISPConfig with Zimbra in the future. I find SquirrelMail to be dated and I prefer the modern look and all the features available in the Zimbra Open Source edition. More on the integration of ISPConfig and Zimbra in a future article. Back to work:

apt-get install postfix postfix-mysql postfix-doc openssl getmail4
apt-get install courier-authdaemon courier-authlib-mysql courier-pop courier-pop-ssl courier-imap courier-imap-ssl libsasl2-2 libsasl2-modules libsasl2-modules-sql sasl2-bin libpam-mysql openssl courier-maildrop

Install PureFTPD

apt-get install pure-ftpd-common pure-ftpd-mysql

Install BIND (named, DNS)

apt-get install bind9 dnsutils

Install fail2ban and rkhunter

apt-get install fail2ban rkhunter binutils

Vlogger, Webalizer, Awstats

If you don’t use Awstats you can comment out all the entries in /etc/crond.d/awstats.

apt-get install vlogger webalizer awstats geoip-database libclass-dbi-mysql-perl

Quota tools

apt-get install quota quotatool

Install ISPConfig 3

cd /tmp
wget http://www.ispconfig.org/downloads/ISPConfig-3-stable.tar.gz
tar xfz ISPConfig-3-stable.tar.gz
cd ispconfig3_install/install/
php -q install.php

Fix /etc/postfix/master.cf:

sed -i -e 's/smtpd_bind_address/smtp_bind_address/' /etc/postfix/master.cf

In case you have missed the note at the top of the article: This is Work in Progress!

Install Ubuntu 12.04 with RAID/LVM on UEFI system

After spending almost a day trying boot my newly installed Ubuntu 12.04 server edition I finally figured out what was the problem. So I am posting this maybe someone is in the same situation as me and it will find this helpful. I don’t know if my method is the best/recommended one, but it works.

Symptoms:

After installing Ubuntu and rebooting you are presented with black screen asking to insert bootable media. Not even GRUB. You probably have a newer PC and when entering BIOS you notice that it has a better looking menu (compared to old text modes we had for so many years) and some options about (U)EFI.

Fix:

Download GParted live USB/CD and “burn” it to a CD or USB. I used LinuxLive USB Creator to write it to USB stick from Windows, after reading this page.

Insert the USB stick into the new PC and boot. Press the key to chose Boot menu. In my case it was F12. From the menu select “EFI: USB harddrive” (this may look different for you, but make sure you select the EFI entry). After the live CD/USB loads everything you are presented with GParted interface. I had two harddisks since I was trying to make RAID. I made the following partitions:
/dev/sda:

  • 128 MB vfat32 partition for EFIboot, select “boot” flag for it (right click on partition, Make flags)
  • rest of the space as another partition, unformatted , select “raid” flag

/dev/sdb:

  • 128 MB xfs partition for /boot (you can use anything you want for file system)
  • rest of the space as another partition, unformatted, select “raid” flag

Write the partition changes to HDD and reboot.

Insert Ubuntu 12.04 Server edition USB stick (or CD). Press Boot menu key (F12 for me) and again select “EFI: USB Harddrive” (or whatever is called for you). When you get to the partition scheme chose “Manual” and do the following:

  1.  Select the vfat32 partition and make sure “Boot flag” is “on” and select “Use this -> EFIBoot partition” (where you normally select file system for a partition). Done with this.
  2. Select the other 128 MB partition from 2nd HDD and use it to mount /boot
  3.  Configure software RAID (Create MD device, RAID1, 2 devices, 0 spare, use /dev/sda2 and /dev/sdb2)
  4. Configure the LVM using /dev/md0 as Physical Volume and create a Volume Group on it (I named mine VolGroup00). Now create as many Logical Volumes as you need. I created 4, but feel free to use as many as you need.
    • 10 GB LogVolRoot for /
    • 10 GB LogVolTmp for /tmp
    • 16 GB LogVolSwap for swap space
    • rest of the space as LogVolVar for /var (I was going to install ISPConfig on this server and most of the files are located in /var in this case).

    You can go without /var and /tmp separated volumes if you want. The recommended swap is space is 2x RAM if you are under 2GB RAM, and RAM+2 if you have over 2GB RAM.

  5. Select each of the Logical Volumes you have created and assign them the right mountpoints and use what file system you want. I used XFS for all since it allows me to grow the partition on the fly and freeze the file system. I’ve also selected (nosuid, noexec) for /tmp and (usrquota,grpquota) for /var.

At this point you should be done, write everything to disk and continue with normal install. When you get to boot loader part you will notice that GRUB won’t ask anything about MBR or other things. It will simply install “grub-efi-amd64” (or something like that, was too fast). If it asks you about MBR or where to install itself then you did something wrong.

Things to note:
I know I should/could have tried to put /boot on RAID/LVM. I’ll try this next time.

Recover MySQL InnoDB database from ibdata1 and frm

In this post I will deal with recovery from a corrupted InnoDB database. Remember that sometimes data cannot be recovered. That’s it. Deal with it and move on. As a matter this article is based on my findings when trying to recover several databases after a crash. In the end I couldn’t recover them, but I thought maybe my article will give you some ideas to try. Maybe it will work for you 🙂

If you already tried innodb_force_recovery with no success, prepare for the worst. I am assuming that your MySQL server is not started because of this.

First of all make a backup copy of you ibdata1 file, you will use this to work on it.

cd /var/lib/mysql
dd if=ibdata1 of=ibdata1.recovery conv=noerror

Most of the following things are documented very well here http://www.percona.com/docs/wiki/innodb-data-recovery-tool:mysql-data-recovery:start but I think there are some issues with their approach. First of all they demonstrate how to recover a single table. That’s perfectly fine, but I had several databases crashed with a dozen of tables each, so I couldn’t afford the luxury to recover each table.

Now download the database recovery tools from percona, in your home directory:

wget https://launchpad.net/percona-data-recovery-tool-for-innodb/trunk/release-0.5/+download/percona-data-recovery-tool-for-innodb-0.5.tar.gz
tar zxvf percona-data-recovery-tool-for-innodb-0.5.tar.gz

Next make MySQL server start

cd /var/lib/mysql
mv ibdata1 ibdata1.bak
mv ib_logfile0 ib_logfile0.bak
mv ib_logfile1 ib_logfile1.bak
service mysqld start

The tricky part comes now. Create recovery database and within it create the table structure (this can be done from an old backup, or maybe you can use the frm files from the database you try to recover). Make sure that the tables are using InnoDB as engine.

The following script is modified a bit after the script provided as example here http://www.percona.com/docs/wiki/innodb-data-recovery-tool:mysql-data-recovery:advanced_techniques. Put it in the same directory where you extracted the percona recovery tools.

#!/bin/sh

db=$1

tables=`mysql -ss -u root -p -e "SHOW TABLES" $db`
for i in $tables
do
        #Check how many rows has a table
        rows=`mysql -u root -p -e "SELECT COUNT(*) FROM $i" -s $db`
                # Prepare environment
                echo "Restoring table $i"
                table=$i
                perl create_defs.pl --host=localhost --user=root --password=YOUR_PASSWORD --db=$1 --table=$table > include/table_defs.h.$table
                cd include && rm -f table_defs.h && ln -s table_defs.h.$table table_defs.h
                cd ..
                make clean all
                # Restoring rows
                found=0
                while [ $found -lt 1 ]
                do
                        echo ""
                        ./constraints_parser -5 -f /var/lib/mysql/ibdata1.recovery >> out.$i
                        found=1
                done
done

Now execute the script like:

sh recover-tables.sh recovery_database_you_created

If you are lucky you will get some output in out.TABLE_NAME. Clean the file and load the data into database.

I know the script looks like POS. Sorry for that. You can take a look at percona’s script and modify it to your needs. Please feel free to correct me if I am wrong in this post (probably I am 🙂 ).

file command error for application/msword when checking mime type

If you updated your system and file command started to behave strangely when trying to determine file type for Microsoft Word, giving you  “ERROR: (null)” instead of “application/msword” here is a quick solution to fix it.

# Checkout source files
git clone https://github.com/glensc/file

# generate configure/make and stuff
cd file
libtoolize --force
aclocal
autoheader
automake --force-missing --add-missing
autoconf

# Usual configure/make and checkinstall
./configure
make
sudo checkinstall --pkgname=file --pkgversion "5.11" --backup=no --fstran

# ldconfig
sudo ldconfig -v

Apache2 worker vs prefork for ISPConfig benchmark

I’ve been running ISPConfig latest version(3.0.4) on Amazon cloud t1.micro instance for some time to host several small sites, mostly WordPress. I’m quite happy with the performance of the instance. The OS is Ubuntu 10.04 LTS. Until recently I’ve used the default mpm which is prefork, but I decided to test out worker also. If you are wondering I use mod_fcgid for all the sites. That being said I performed several tests with ab (apache benchmark) to see which mpm can server most requests per second.

While I do not claim this is the best setup, I think worker is better suited for me. Some people said they had problems because of mpm worker. So far so good, but will update this post if there are any issues.

Test results:

prefork worker
Concurrency Level: 32
Time taken for tests: 7.834 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 4972
Total transferred: 84831033 bytes
HTML transferred: 83206915 bytes
Requests per second: 638.27 [#/sec] (mean)
Time per request: 50.136 [ms] (mean)
Time per request: 1.567 [ms] (mean, across all concurrent requests)
Transfer rate: 10575.21 [Kbytes/sec] received
Concurrency Level: 32
Time taken for tests: 7.096 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 4968
Total transferred: 84877824 bytes
HTML transferred: 83247322 bytes
Requests per second: 704.63 [#/sec] (mean)
Time per request: 45.414 [ms] (mean)
Time per request: 1.419 [ms] (mean, across all concurrent requests)
Transfer rate: 11681.17 [Kbytes/sec] received