Setting Up an Ubuntu Server

Introduction
This is tutorial on how to set up an Ubuntu server with apache, mysql, proftpd, webmin, setting up a user account and adding it to the sudoers list. In this tutorial the name you want to use will be represented as myname and passwords as mypassword. This tutorial also assumes you are logged in as root to set up the server.

First Update
To load a list of the latest updates into the server:

apt-get update

To perform the upgrade:

apt-get upgrade

To perform a distribution upgrade:

apt-get dist-upgrade

Retrieving Required Packages
These are the packages required for the server setup:

apt-get install nano libnet-ssleay-perl libauthen-pam-perl libio-pty-perl apt-show-versions libapt-pkg-perl libauthen-pam-perl mysql-server mysql-client proftpd-mod-mysql php5 libapache2-mod-php5 php5-sqlite php5-mysql apache2

You will be asked to enter a password for mysql and set the startup mode for proftpd as standalone.

Adding Your User Account
To add your user account:

useradd -d /home/myname -s /bin/bash -m myname

To set your account password: (It will ask you to type your password)

passwd myname

To add your account to the sudoers list use the following command:

visudo

In the section “User privilege specification” under root add:

myname    ALL=(ALL:ALL) ALL

Exit and save the file

Create The FTP Directory
Create the directory and change to it. This is done now so the webmin file can be downloaded into it. I usually create the FTP directory in var:

mkdir /var/ftp
cd /var/ftp

Download and Install Webmin
Check for the latest file to download and install here. The link used in this tutorial may be old. You can download the file like this:

wget http://prdownloads.sourceforge.net/webadmin/webmin_1.580_all.deb

Install webmin with:

dpkg -i webmin_1.580_all.deb

(remember to change the name of the file if there is a newer one available)
You can log into webmin with:

https://yourdomain.com:10000

Installing ProFTPd
Add the user and group for proftpd:

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

Log into MySQL:

mysql -u root -p

create the database and account for the FTP database:

CREATE DATABASE ftp;
GRANT SELECT, INSERT, UPDATE, DELETE ON ftp.* TO 'proftpd'@'localhost' IDENTIFIED BY 'mypassword';
GRANT SELECT, INSERT, UPDATE, DELETE ON ftp.* TO 'proftpd'@'localhost.localdomain' IDENTIFIED BY 'mypassword';
FLUSH PRIVILEGES;

Select the database ftp to use:

USE ftp;

Create the required tables:

CREATE TABLE ftpgroup (
groupname varchar(16) NOT NULL default '',
gid smallint(6) NOT NULL default '5500',
members varchar(16) NOT NULL default '',
KEY groupname (groupname)
) TYPE=MyISAM COMMENT='ProFTP group table';

CREATE TABLE ftpquotalimits (
name varchar(30) default NULL,
quota_type enum('user','group','class','all') NOT NULL default 'user',
per_session enum('false','true') NOT NULL default 'false',
limit_type enum('soft','hard') NOT NULL default 'soft',
bytes_in_avail bigint(20) unsigned NOT NULL default '0',
bytes_out_avail bigint(20) unsigned NOT NULL default '0',
bytes_xfer_avail bigint(20) unsigned NOT NULL default '0',
files_in_avail int(10) unsigned NOT NULL default '0',
files_out_avail int(10) unsigned NOT NULL default '0',
files_xfer_avail int(10) unsigned NOT NULL default '0'
) TYPE=MyISAM;

CREATE TABLE ftpquotatallies (
name varchar(30) NOT NULL default '',
quota_type enum('user','group','class','all') NOT NULL default 'user',
bytes_in_used bigint(20) unsigned NOT NULL default '0',
bytes_out_used bigint(20) unsigned NOT NULL default '0',
bytes_xfer_used bigint(20) unsigned NOT NULL default '0',
files_in_used int(10) unsigned NOT NULL default '0',
files_out_used int(10) unsigned NOT NULL default '0',
files_xfer_used int(10) unsigned NOT NULL default '0'
) TYPE=MyISAM;

CREATE TABLE ftpuser (
id int(10) unsigned NOT NULL auto_increment,
userid varchar(32) NOT NULL default '',
passwd varchar(32) NOT NULL default '',
uid smallint(6) NOT NULL default '5500',
gid smallint(6) NOT NULL default '5500',
homedir varchar(255) NOT NULL default '',
shell varchar(16) NOT NULL default '/sbin/nologin',
count int(11) NOT NULL default '0',
accessed datetime NOT NULL default '0000-00-00 00:00:00',
modified datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (id),
UNIQUE KEY userid (userid)
) TYPE=MyISAM COMMENT='ProFTP user table';

Quit mysql with:

quit;

Open up the file /etc/proftpd/modules.conf with: (or use your favorite editor)

nano /etc/proftpd/modules.conf

Activate the following modules by removing the # symbol in front of the line:

LoadModule mod_sql.c
LoadModule mod_sql_mysql.c
LoadModule mod_quotatab_sql.c

Open the following file:

nano /etc/proftpd/proftpd.conf

Add the following lines and remember to change mypassword to the value you used when creating the mysql account for ftp:

# Choose a SQL backend among MySQL or PostgreSQL.
# Both modules are loaded in default configuration, so you have to specify the backend
# or comment out the unused module in /etc/proftpd/modules.conf.
# Use 'mysql' or 'postgres' as possible values.
#
#<IfModule mod_sql.c>
# SQLBackend                    mysql
#</IfModule>

DefaultRoot ~

SQLBackend              mysql
# The passwords in MySQL are encrypted using CRYPT
SQLAuthTypes            Plaintext Crypt
SQLAuthenticate         users groups

# used to connect to the database
# databasename@host database_user user_password
SQLConnectInfo  ftp@localhost proftpd mypassword

# Here we tell ProFTPd the names of the database columns in the "usertable"
# we want it to interact with. Match the names with those in the db
SQLUserInfo     ftpuser userid passwd uid gid homedir shell

# Here we tell ProFTPd the names of the database columns in the "grouptable"
# we want it to interact with. Again the names match with those in the db
SQLGroupInfo    ftpgroup groupname gid members

# set min UID and GID - otherwise these are 999 each
SQLMinID        500

# create a user's home directory on demand if it doesn't exist
CreateHome on

# Update count every time user logs in
SQLLog PASS updatecount
SQLNamedQuery updatecount UPDATE "count=count+1, accessed=now() WHERE userid='%u'" ftpuser

# Update modified everytime user uploads or deletes a file
SQLLog  STOR,DELE modified
SQLNamedQuery modified UPDATE "modified=now() WHERE userid='%u'" ftpuser

# User quotas
# ===========
QuotaEngine on
QuotaDirectoryTally on
QuotaDisplayUnits Mb
QuotaShowQuotas on

SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM ftpquotalimits WHERE name = '%{0}' AND quota_type = '%{1}'"

SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM ftpquotatallies WHERE name = '%{0}' AND quota_type = '%{1}'"

SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = '%{6}' AND quota_type = '%{7}'" ftpquotatallies

SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" ftpquotatallies

QuotaLimitTable sql:/get-quota-limit
QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally

RootLogin off
RequireValidShell off

In the same file comment out the following lines by adding a # symbol in front of them link this:

#<IfModule mod_quotatab.c>
#QuotaEngine off
#</IfModule>

Restart proftpd:

/etc/init.d/proftpd restart

Get back into mysqland use the ftp database:

mysql -u root -p
USE ftp;

Insert the FTP group into the database:

INSERT INTO `ftpgroup` (`groupname`, `gid`, `members`) VALUES ('ftpgroup', 2001, 'ftpuser');

The next two inserts can be done multiple times depending on how many user accounts you want to create for your ftp server. The value after ‘hard’ can be set (in bytes) to the quota for the user account, 0 is for unlimited. This is for one account:

INSERT INTO `ftpquotalimits` (`name`, `quota_type`, `per_session`, `limit_type`, `bytes_in_avail`, `bytes_out_avail`, `bytes_xfer_avail`, `files_in_avail`, `files_out_avail`, `files_xfer_avail`) VALUES ('myname', 'user', 'true', 'hard', 0, 0, 0, 0, 0, 0);
INSERT INTO `ftpuser` (`userid`, `passwd`, `uid`, `gid`, `homedir`, `shell`, `count`, `accessed`, `modified`) VALUES ('myname', 'mypassword', 2001, 2001, '/var/ftp', '/sbin/nologin', 0, '', '');

And that is it, let me know what you think…

Tags: , , , , , , , ,

8 Responses to "Setting Up an Ubuntu Server"

Leave a Comment