Thursday, December 16, 2010

Virtual Hosting With PureFTPd And MySQL (Incl. Quota And Bandwidth Management) On CentOS

This document describes how to install a PureFTPd server that uses virtual users from a MySQL database instead of real system users. This is much more performant and allows to have thousands of ftp users on a single machine. In addition to that I will show the use of quota and upload/download bandwidth limits with this setup. Passwords will be stored encrypted as MD5 strings in the database.
For the administration of the MySQL database you can use web based tools like phpMyAdmin which will also be installed in this howto. phpMyAdmin is a comfortable graphical interface which means you do not have to mess around with the command line.
This tutorial is based on CentOS 5.3. You should already have set up a basic CentOS 5.3 system, for example as described in the first six chapters of this tutorial:
http://www.howtoforge.com/perfect-server-centos-5.3-i386-ispconfig-2
This howto is meant as a practical guide; it does not cover the theoretical backgrounds. They are treated in a lot of other documents in the web.
This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!

1 Preliminary Note
In this tutorial I use the hostname server1.example.com with the IP address 192.168.0.100. These settings might differ for you, so you have to replace them where appropriate.

2 Install MySQL And Apache/PHP
We can install MySQL and Apache/PHP (Apache and PHP are needed by phpMyAdmin) with a single command:

yum install mysql mysql-server httpd php php-mysql php-mbstring

Then we create the system startup links for MySQL and Apache (so that both start automatically whenever the system boots) and start both services:

chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start

chkconfig --levels 235 httpd on
/etc/init.d/httpd start

Create a password for the MySQL user root (replace yourrootsqlpassword with the password you want to use):
mysqladmin -u root password yourrootsqlpassword
mysqladmin -h server1.example.com -u root password yourrootsqlpassword

3 Install phpMyAdmin
Unfortunately there's no phpMyAdmin package in the official CentOS 5.3 repositories, but I've found a phpMyAdmin package for CentOS 4.x in the centos.karan.org repository which works on CentOS 5.3 as well. We can install it like this:

rpm -ivh http://centos.karan.org/el4/misc/testing/i386/RPMS/phpMyAdmin-2.7.0-3.el4.pl2.lsn.noarch.rpm

Afterwards, you should be able to access phpMyAdmin in a browser under this address:

http://server1.example.com/phpMyAdmin/
(you can also use the IP address instead of server1.example.com)

If you find that your phpMyAdmin is missing lots of images and has problems loading, open the file/usr/share/phpMyAdmin/config.inc.php and comment out the $cfg['PmaAbsoluteUri'] line:

vi /usr/share/phpMyAdmin/config.inc.php

[...]
#$cfg['PmaAbsoluteUri'] = 'http://server1.example.com/phpMyAdmin/';
[...]

Afterwards, phpMyAdmin should work as expected.

4 Install PureFTPd With MySQL Support
Again, there's no PureFTPd package in the official CentOS 5.3 repositories, but the centos.karan.org repository has a PureFTPd package for CentOS 5.3 (in the kbs-CentOS-Testing repository). Therefore we add this repository to our official CentOS repositories:

cd /etc/yum.repos.d/
wget http://centos.karan.org/kbsingh-CentOS-Extras.repo

Now we must enable the kbs-CentOS-Testing repository. To do this, we open the file kbsingh-CentOS-Extras.repo and change enabled=0 to enabled=1 in the kbs-CentOS-Testing stanza; also change gpgcheck=1 to gpgcheck=0:

vi kbsingh-CentOS-Extras.repo

[...]
[kbs-CentOS-Testing]
name=CentOS.Karan.Org-EL$releasever - Testing
gpgcheck=0
gpgkey=http://centos.karan.org/RPM-GPG-KEY-karan.org.txt
enabled=1
baseurl=http://centos.karan.org/el$releasever/extras/testing/$basearch/RPMS/

Then we import the GPG key of our new repository:

rpm --import http://centos.karan.org/RPM-GPG-KEY-karan.org.txt

Now we can install PureFTPd:

yum install pure-ftpd

Then we create an ftp group (ftpgroup) and user (ftpuser) that all our virtual users will be mapped to. Replace the group- and userid 2001 with a number that is free on your system:

groupadd -g 2001 ftpgroup
useradd -u 2001 -s /bin/false -d /bin/null -c "pureftpd user" -g ftpgroup ftpuser


5 Create The MySQL Database For PureFTPd
Now we create a database called pureftpd and a MySQL user named pureftpd which the PureFTPd daemon will use later on to connect to the pureftpd database:

mysql -u root -p

CREATE DATABASE pureftpd;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON pureftpd.* TO 'pureftpd'@'localhost' IDENTIFIED BY 'ftpdpass';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON pureftpd.* TO 'pureftpd'@'localhost.localdomain' IDENTIFIED BY 'ftpdpass';
FLUSH PRIVILEGES;

Replace the string ftpdpass with whatever password you want to use for the MySQL user pureftpd. Still on the MySQL shell, we create the database table we need (yes, there is only one table!):

USE pureftpd;

CREATE TABLE ftpd (
User varchar(16) NOT NULL default '',
status enum('0','1') NOT NULL default '0',
Password varchar(64) NOT NULL default '',
Uid varchar(11) NOT NULL default '-1',
Gid varchar(11) NOT NULL default '-1',
Dir varchar(128) NOT NULL default '',
ULBandwidth smallint(5) NOT NULL default '0',
DLBandwidth smallint(5) NOT NULL default '0',
comment tinytext NOT NULL,
ipaccess varchar(15) NOT NULL default '*',
QuotaSize smallint(5) NOT NULL default '0',
QuotaFiles int(11) NOT NULL default 0,
PRIMARY KEY (User),
UNIQUE KEY User (User))
TYPE=MyISAM;

quit;


As you may have noticed, with the quit; command we have left the MySQL shell and are back on the Linux shell.
BTW, (I'm assuming that the hostname of your ftp server system is server1.example.com) you can access phpMyAdmin under http://server1.example.com/phpMyAdmin/ (you can also use the IP address instead of server1.example.com) in a browser and log in as the user pureftpd. Then you can have a look at the
database. Later on you can use phpMyAdmin to administrate your PureFTPd server.

6 Configure PureFTPd
Edit /etc/pure-ftpd/pure-ftpd.conf and make sure that theChrootEveryone, MySQLConfigFile, and CreateHomeDir lines are enabled and look like this:

vi /etc/pure-ftpd/pure-ftpd.conf

[...]
ChrootEveryone yes
[...]
MySQLConfigFile /etc/pure-ftpd/pureftpd-mysql.conf
[...]
CreateHomeDir yes
[...]


The ChrootEveryone setting will make PureFTPd chroot every virtual user in his home directory so he will not be able to browse directories and files outside his home directory. The CreateHomeDir line will make PureFTPd create a user's home directory when the user logs in and the home directory does not exist yet.
Then we edit /etc/pure-ftpd/pureftpd-mysql.conf. It should look like this:

cp /etc/pure-ftpd/pureftpd-mysql.conf /etc/pure-ftpd/pureftpd-mysql.conf_orig
cat /dev/null > /etc/pure-ftpd/pureftpd-mysql.conf
vi /etc/pure-ftpd/pureftpd-mysql.conf

MYSQLSocket /var/lib/mysql/mysql.sock
#MYSQLServer localhost
#MYSQLPort 3306
MYSQLUser pureftpd
MYSQLPassword ftpdpass
MYSQLDatabase pureftpd
#MYSQLCrypt md5, cleartext, crypt() or password() - md5 is VERY RECOMMENDABLE uppon cleartext
MYSQLCrypt md5
MYSQLGetPW SELECT Password FROM ftpd WHERE User="\L" AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "\R")
MYSQLGetUID SELECT Uid FROM ftpd WHERE User="\L" AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "\R")
MYSQLGetGID SELECT Gid FROM ftpd WHERE User="\L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "\R")
MYSQLGetDir SELECT Dir FROM ftpd WHERE User="\L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "\R")
MySQLGetBandwidthUL SELECT ULBandwidth FROM ftpd WHERE User="\L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "\R")
MySQLGetBandwidthDL SELECT DLBandwidth FROM ftpd WHERE User="\L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "\R")
MySQLGetQTASZ SELECT QuotaSize FROM ftpd WHERE User="\L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "\R")
MySQLGetQTAFS SELECT QuotaFiles FROM ftpd WHERE User="\L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "\R")


Make sure that you replace the string ftpdpass with the real password for the MySQL user pureftpd in the lineMYSQLPassword! Please note that we use md5 as MYSQLCrypt method, which means we will store the users' passwords as an MD5 string in the database which is far more secure than using plain text passwords!
Now we create the system startup links for PureFTPd and start it:

chkconfig --levels 235 pure-ftpd on
/etc/init.d/pure-ftpd start

7 Populate The Database And Test
To populate the database you can use the MySQL shell:

mysql -u root -p

USE pureftpd;

Now we create the user exampleuser with the status 1 (which means his ftp account is active), the password secret(which will be stored encrypted using MySQL's MD5 function), the UID and GID 2001 (use the userid and groupid of the user/group you created at the end of step two!), the home directory /home/www.example.com, an upload and download bandwidth of 100 KB/sec. (kilobytes per second), and a quota of 50 MB:

INSERT INTO `ftpd` (`User`, `status`, `Password`, `Uid`, `Gid`, `Dir`, `ULBandwidth`, `DLBandwidth`, `comment`, `ipaccess`, `QuotaSize`, `QuotaFiles`) VALUES ('exampleuser', '1', MD5('secret'), '2001', '2001', '/home/www.example.com', '100', '100', '', '*', '50', '0');

quit;


Now open your FTP client program on your work station (something like WS_FTP or SmartFTP if you are on a Windows system or gFTP on a Linux desktop) and try to connect. As hostname you use server1.example.com (or the IP address of the system), the username is exampleuser, and the password is secret.
If you are able to connect - congratulations! If not, something went wrong.
Now, if you run

ls -l /home

you should see that the directory /home/www.example.com (exampleuser's home directory) has been created automatically, and it is owned by ftpuser and ftpgroup (the user/group we created at the end of step four):

[root@server1 ~]# ls -l /home
total 4
drwxr-xr-x 2 ftpuser ftpgroup 4096 Aug 10 14:04 http://www.example.com/
[root@server1 ~]#

8 Database Administration
For most people it is easier if they have a graphical front-end to MySQL; therefore you can also use phpMyAdmin (in this example under http://server1.example.com/phpMyAdmin/) to administrate the pureftpd database.



Whenever you want to create a new user, you have to create an entry in the table ftpd so I will explain the columns of this table here:


ftpd Table:
User: The name of the virtual PureFTPd user (e.g. exampleuser).
status: 0 or 1. 0 means the account is disabled, the user cannot login.
Password: The password of the virtual user. Make sure you use MySQL's MD5 function to save the password encrypted as an MD5 string:



UID: The userid of the ftp user you created at the end of step two (e.g. 2001).
GID: The groupid of the ftp group you created at the end of step two (e.g. 2001).
Dir: The home directory of the virtual PureFTPd user (e.g. /home/www.example.com). If it does not exist, it will be created when the new user logs in the first time via FTP. The virtual user will be jailed into this home directory, i.e., he cannot access other directories outside his home directory.
ULBandwidth: Upload bandwidth of the virtual user in KB/sec. (kilobytes per second). 0 means unlimited.
DLBandwidth: Download bandwidth of the virtual user in KB/sec. (kilobytes per second). 0 means unlimited.
comment: You can enter any comment here (e.g. for your internal administration) here. Normally you leave this field empty.
ipaccess: Enter IP addresses here that are allowed to connect to this FTP account. * means any IP address is allowed to connect.
QuotaSize: Storage space in MB (not KB, as in ULBandwidth and DLBandwidth!) the virtual user is allowed to use on the FTP server. 0 means unlimited.
QuotaFiles: amount of files the virtual user is allowed to save on the FTP server. 0 means unlimited.

9 Anonymous FTP

If you want to create an anonymous ftp account (an ftp account that everybody can login to without a password), you need a user and a group called ftp. Both have been created automatically when you installed the pure-ftpd package, so you don't need to create them manually. However, ftp's homedir is /var/ftp by default, but I'd like to create the anonymous ftp directory in /home/ftp (the normal users' ftp directories are in /home as well, e.g./home/www.example.com). But of course, you can use the /var/ftp directory for anonymous ftp, if you prefer it.
If you want to use /home/ftp, open /etc/passwd and change the ftp user's homedir from /var/ftp to /home/ftp (don't do this if you want to use /var/ftp):

vi /etc/passwd

[...]
#ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
ftp:x:14:50:FTP User:/home/ftp:/sbin/nologin
[...]

Then move /var/ftp to /home (don't do this if you want to use /var/ftp):

mv /var/ftp /home

Then we create the directory /home/ftp/incoming which will allow anonymous users to upload files. We will give the/home/ftp/incoming directory permissions of 311 so that users can upload, but not see or download any files in that directory. The /home/ftp directory will have permissions of 555 which allows seeing and downloading of files:

chown ftp:nobody /home/ftp
cd /home/ftp
mkdir incoming
chown ftp:nobody incoming/
chmod 311 incoming/
cd ../
chmod 555 ftp/


(If you want to use /var/ftp instead, replace /home/ftp with /var/ftp in the above commands.)
Anonymous users will be able to log in, and they will be allowed to download files from /home/ftp, but uploads will be limited to /home/ftp/incoming (and once a file is uploaded into /home/ftp/incoming, it cannot be read nor downloaded from there; the server admin has to move it into /home/ftp first to make it available to others).
Now we have to configure PureFTPd for anonymous ftp. Open /etc/pure-ftpd/pure-ftpd.conf and make sure that you have the following settings in it:

vi /etc/pure-ftpd/pure-ftpd.conf

[...]
NoAnonymous no
[...]
AntiWarez no
[...]
AnonymousBandwidth 8
[...]
AnonymousCantUpload no
[...]


(The AnonymousBandwidth setting is optional - it allows you to limit upload and download bandwidths for anonymous users. 8 means 8 KB/sec. Use any value you like, or comment out the line if you don't want to limit bandwidths.)
Finally, we restart PureFTPd:

/etc/init.d/pure-ftpd restart

10 Links
PureFTPd: http://www.pureftpd.org/
MySQL: http://www.mysql.com/
phpMyAdmin: http://www.phpmyadmin.net/
CentOS: http://www.centos.org/

Referance Website: http://howtoforge.com/virtual-hosting-with-pureftpd-and-mysql-incl-quota-and-bandwidth-management-on-centos-5.3




1 comment:

  1. Above all information is excellent and you have shared great knowledge about this concept.domain registration india

    ReplyDelete