Server SFTP Ubuntu Laravel

Setup SFTP Server on Ubuntu + Connecting with Laravel

This guide is at your own risk of course. We do not have any responsibility.

Why Choose SFTP over MinIO or S3?

There are several reasons to choose SFTP as a file server solution instead of MinIO / S3:

  • Easier, simpler, and less cluttered setup
  • Easier to update than a MinIO server
  • Uses only port 22 (or another assigned port)

Why Choose TransIP or a VPS Provider?

Here are some advantages of using TransIP or a VPS provider for your SFTP server:

  • Existing infrastructure with backups
  • Possibility to link big storage for increased storage capacity
  • Easy upgrade options for additional disk space. Don’t forget to allocate the new disk space in Linux.

Other parties like NetCup have dedicated storage servers. We have not tested them, so we are not sure if those are scalable. We did test a TransIP VPS together with a Big Storage addition.

Setting Up SFTP Server without Big Storage

Step 1: Set Up TransIP / VPS Server with SSD SFTP Group

Choose TransIP as your server provider and set up an SSD SFTP group:

sudo groupadd sftp

Step 2: Configure SFTP Server on Ubuntu

Follow the instructions at https://linuxhint.com/setup-sftp-server-ubuntu/ to configure the SFTP server.

sudo nano /etc/ssh/sshd_config

Add the following lines:

Match group sftp
ChrootDirectory /home
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
sudo systemctl restart ssh
sudo addgroup sftp
sudo useradd -m YOURUSER -g sftp
sudo passwd YOURUSER
sudo chmod 700 /home/YOURUSER/

Step 3: Edit SSH Configuration

Edit the SSH configuration file:

sudo nano /etc/ssh/sshd_config

Change the following line:

PasswordAuthentication yes

This is less secure, so choose at least a strong password. But even better, use SSH Keys, which are WAY more secure (if you don’t leak them).

Restart SSH:

sudo systemctl restart ssh

Step 4: Configure Firewall

Set up your firewall to allow inbound connections only on port 22 from your home IP, office IP, and server IPs if necessary.

Step 5: Login via SFTP Client

Use a client like FileZilla to connect to your SFTP server:

sftp YOURUSER@ip.address

Step 6: Install SFTP Flysystem Laravel Driver

Install the SFTP Flysystem Laravel driver to enable SFTP integration in your Laravel application:

composer require league/flysystem-sftp-v3 "^3.0"

Step 7: Configure SFTP in Laravel

Edit your Laravel’s filesystem configuration (config/filesystems.php):

'sftp' => [
    'driver' => 'sftp',
    'host' => env('SFTP_HOST'),
 
    // Settings for basic authentication...
    'username' => env('SFTP_USERNAME'),
    'password' => env('SFTP_PASSWORD'),
    'throw' => env('APP_DEBUG'),
    'root' => env('SFTP_USERNAME'),
],

Step 8: Create Disk Space Check Script

Create a shell script (disk-space-check.sh) to periodically check the disk space on your server. There are various guides for this, like: https://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html

Step 9: Add Script to Cron

Add the disk space check script to the crontab to run it at specified intervals.

Ordering TransIP Big Storage

If you require additional storage capacity, follow the guide at https://www.transip.nl/knowledgebase/artikel/195-big-storage-aan-linux-koppelen/ to order big storage from TransIP. In English: https://www.transip.eu/knowledgebase/entry/195-how-add-big-storage-linux/

Setting Up Big Storage User or Alternate Directory

If you want to use a different directory or want to use TransIP Big Storage, which is mounted at for example /mnt/bigstorage , follow these steps. Start with creating a big storage sftp group:

sudo groupadd sftpbigstorage
sudo nano /etc/ssh/sshd_config

Add the following lines:

Match Group sftpbigstorage
      ForceCommand internal-sftp
      ChrootDirectory /mnt/bigstorage/%u
      PermitTunnel no
      AllowAgentForwarding no
      AllowTcpForwarding no
      X11Forwarding no

Then run (replace myuser01 with your user name) :

sudo mkdir /mnt/bigstorage/myuser01
sudo chown root:root /mnt/bigstorage
sudo chown -R root:root /mnt/bigstorage/myuser01
sudo chmod -R 755 /mnt/bigstorage/myuser01

sudo adduser --home /mnt/bigstorage/myuser01 myuser01
sudo usermod -a -G sftpbigstorage myuser01
sudo usermod -s /sbin/nologin myuser01

sudo systemctl restart sshd

sudo mkdir /mnt/bigstorage/myuser01/files/
sudo chown -R myuser01:myuser01 /mnt/bigstorage/myuser01/files/

sudo systemctl restart ssh

When copying files via sftp you might need to re-assign the permissions:

sudo chown -R myuser01:myuser01 /mnt/bigstorage/myuser01/files/

MOST IMPORTANT NOTE: sftp needs that the root owns /mnt/bigstorage and /mnt/bigstorage/USERNAME . In order fot the user to edit something, a subdirectory like /mnt/bigstorage/USERNAME/files or /mnt/bigstorage/USERNAME/www is required. This is perfectly described in this post. Also this guide is useful: https://thunderysteak.github.io/sftp-user-chroot

By following these steps, you can set up a basic SFTP file server on your server using TransIP or another VPS provider. SFTP provides a simpler and less complex alternative to services like MinIO or S3, while still allowing secure file transfers. Integrating SFTP with Laravel/PHP enables seamless file management in your applications.

Other Alternatives for File Hosting with SFTP

  • Using Amazon S3: works well, but pricey
  • Bunny Storage: interesting SFTP alternative, pretty decently priced and easy to set up.
  • MinIO: Self-hosted S3 alternative

Leave a Comment

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

Scroll to Top