How to Create Virtual Host on Debian Squeeze with vsftpd And MySQL

Vsftpd is considered as the most reliable and quickest FTP servers for Linux. Normally, it is used to work effectively with system users. This tutorial helps in explaining the installation process of vsftpd server that uses virtual users from a MySQL database as an alternative to real system users. Because of this, thousand of ftp users can connect on a single machine and will eventually give high class performance.

In order to administer MySQL database, web based tools like phpMyAdmin can also be used. phpMyAdmin gives you a user friendly graphical interface and you do not have to meddle with the command line predicament.

This document is entirely rooted in Debian Squeeze (Debin 6.0). For this, you need to have a basic Debian Squeeze system.

In this guideline, hostname server1.example.com along with the IP address 192.168.0.100 is used. The settings might vary from system to system, so you have to adjust them accordingly.

Instructions

  • 1

    Install vsftpd, MySQL And phpMyAdmin

    Since Vsftpd hasn’t any by default support for MySQL, so you have to use PAM to validate against the MySQL database. Therefore, install libpam-mysql along with MySQL, phpMyAdmin and vsftpd.

    apt-get install vsftpd libpam-mysql mysql-server mysql-client phpmyadmin

    You will come across the following questions.

    New password for the MySQL "root" user: <-- yourrootsqlpassword

    Repeat password for the MySQL "root" user: <-- yourrootsqlpassword

    Web server to reconfigure automatically: <-- apache2

    Configure database for phpmyadmin with dbconfig-common? <-- No

  • 2

    Create The MySQL Database For vsftpd

    Now you have to make database with the name of vsftpd and a MySQL user named vsftpd, which will be used by vsftpd daemon after a while in order to link to the vsftpd database:

    mysql -u root –p

    CREATE DATABASE vsftpd;

    GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON vsftpd.* TO 'vsftpd'@'localhost' IDENTIFIED BY 'ftpdpass';

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

    FLUSH PRIVILEGES;

    Now choose a password for the MySQL user vsftpd and replace it with the string ftpdpass.

    USE vsftpd;

    CREATE TABLE `accounts` (

    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,

    `username` VARCHAR( 30 ) NOT NULL ,

    `pass` VARCHAR( 50 ) NOT NULL ,

    UNIQUE (

    `username`

    )

    ) ENGINE = MYISAM ;

    quit;

  • 3

    Configure vsftpd

    At the very first, you make a non-privileged user known as vsftpd which is related to the group nogroup. Now run vsftpd under this user domain, and the FTP directories of your virtual users will be in the /home/vsftpd directory (like /home/vsftpd/user1, /home/vsftpd/user2, etc.).

    useradd --home /home/vsftpd --gid nogroup -m --shell /bin/false vsftpd

    Create a backup of the original /etc/vsftpd.conf file:

    cp /etc/vsftpd.conf /etc/vsftpd.conf_orig

    cat /dev/null > /etc/vsftpd.conf

    vi /etc/vsftpd.conf

    The file should consist of the following code.

    listen=YES

    anonymous_enable=NO

    local_enable=YES

    write_enable=YES

    local_umask=022

    dirmessage_enable=YES

    xferlog_enable=YES

    connect_from_port_20=YES

    nopriv_user=vsftpd

    chroot_local_user=YES

    secure_chroot_dir=/var/run/vsftpd

    pam_service_name=vsftpd

    rsa_cert_file=/etc/ssl/certs/vsftpd.pem

    guest_enable=YES

    guest_username=vsftpd

    local_root=/home/vsftpd/$USER

    user_sub_token=$USER

    virtual_use_local_privs=YES

    user_config_dir=/etc/vsftpd_user_conf

    With the help of user_config_dir option you can easily identify a directory for per-user configuration wp-content/uploads that supersede various parts of global settings. This feature is not compulsory to use and it entirely depends upon you whether you want to use it or not. But anyhow make that directory now:

    mkdir /etc/vsftpd_user_conf

    Now you have to configure PAM, so despite of /etc/passwd and /etc/shadow it can make use of MySQL database to validate virtual FTP users. The PAM configuration for vsftpd is in /etc/pam.d/vsftpd. Create a backup of the original file and now make a new file by following the procedure given below:

    cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd_orig

    cat /dev/null > /etc/pam.d/vsftpd

    vi /etc/pam.d/vsftpd

    auth required pam_mysql.so user=vsftpd passwd=ftpdpass host=localhost db=vsftpd table=accounts usercolumn=username passwdcolumn=pass crypt=2

    account required pam_mysql.so user=vsftpd passwd=ftpdpass host=localhost db=vsftpd table=accounts usercolumn=username passwdcolumn=pass crypt=2

    Once you are done with this, restart vsftpd:

    /etc/init.d/vsftpd restart

  • 4

    Make The First Virtual User

    In order to settle the database, use MySQL shell:

    mysql -u root –p

    USE vsftpd;

    Make the virtual user testuser with the password secret.

    INSERT INTO accounts (username, pass) VALUES('testuser', PASSWORD('secret'));

    quit;

    testuser's homedir is /home/vsftpd/testuser; Sadly, vsftpd doesn’t make that directory by itself if it doesn’t exist. So, make it by yourself and give the ownership rights to vsftpd user and nogroup group:

    mkdir /home/vsftpd/testuser

    chown vsftpd:nogroup /home/vsftpd/testuser

    Start your FTP client program on your system and connect it. The hostname that is used is server1.example.com, username is testuser, and the password is secret.

  • 5

    Database Administration

    It will be convenient for them who are using graphical front-end to MySQL, otherwise you have the option to use phpMyAdmin (in this example under http://server1.example.com/phpmyadmin/) to administrate the vsftpd database.

Leave a Reply

Your email address will not be published. Required fields are marked *


4 × seven =