Articles

How to Run a Simple FTP Server

This is an advanced tutorial.
ServerPilot cannot provide any support for running an FTP server.

The easiest way to run an FTP server is with a Python script that starts a simple FTP server. This tutorial will show you how to do that.

There are two steps to running an FTP server:

  1. Configuring and starting the FTP server
  2. Opening up your firewall to allow connections to your FTP server

Configuring and Starting an FTP Server

First, SSH in to your server as root and install the Python pyftpdlib library with this command:

sudo apt-get install python-pyftpdlib

Next, log out of your server as root. The rest of your steps should be done while logged in as your app's system user.

Now, SSH into your server as the user serverpilot and create the a file named ftpserver.py in the serverpilot user's home directory. That is, create this file:

/srv/users/serverpilot/ftpserver.py

In that file, put the following contents and change the name, password, and directory for the FTP user that are defined near the top of the file:

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer


# The port the FTP server will listen on.
# This must be greater than 1023 unless you run this script as root.
FTP_PORT = 2121

# The name of the FTP user that can log in.
FTP_USER = "myuser"

# The FTP user's password.
FTP_PASSWORD = "change_this_password"

# The directory the FTP user will have full read/write access to.
FTP_DIRECTORY = "/srv/users/serverpilot/apps/APPNAME/public/"


def main():
    authorizer = DummyAuthorizer()

    # Define a new user having full r/w permissions.
    authorizer.add_user(FTP_USER, FTP_PASSWORD, FTP_DIRECTORY, perm='elradfmw')

    handler = FTPHandler
    handler.authorizer = authorizer

    # Define a customized banner (string returned when client connects)
    handler.banner = "pyftpdlib based ftpd ready."

    # Optionally specify range of ports to use for passive connections.
    #handler.passive_ports = range(60000, 65535)

    address = ('', FTP_PORT)
    server = FTPServer(address, handler)

    server.max_cons = 256
    server.max_cons_per_ip = 5

    server.serve_forever()


if __name__ == '__main__':
    main()

You can now start the FTP server with the following command:

python /srv/users/serverpilot/ftpserver.py >>/srv/users/serverpilot/ftpserver.log 2>&1 &

The above command will start the FTP server with a log file named ftpserver.log.

For more information on customizing this FTP server, see the pyftpdlib documentation.

You can verify your FTP server is running with the command:

netstat -npl --inet | grep 2121

Your output should look like this:

tcp        0      0 0.0.0.0:2121            0.0.0.0:*               LISTEN      29972/python

which shows your FTP server listening on port 2121.

To stop the FTP server, use the kill command with the FTP server processes' PID, which you can find with this command:

ps -ef | grep ftpserver | grep -v grep | awk '{print $2}'

Configuring Your Firewall to Allow FTP Server Access

Now that you've started your FTP server, you need to customize your server's firewall to open up the port you ran your FTP server on. For example, if you used port 2121, you'd need to open port 2121 in your server's firewall.

For information on how to do this, see our article on customizing your server's firewall.

Starting Your FTP Server on Boot

To start your FTP server automatically when your server is rebooted, SSH in as serverpilot and create a cron job like the following:

@reboot python /srv/users/serverpilot/ftpserver.py >>/srv/users/serverpilot/ftpserver.log 2>&1

Last updated: November 02, 2016