How to Edit Webroot (/var/www) Files via SFTP

If you’ve followed my guide to setting up your own server and using WordOps to install and manage your WordPress site, you’ve probably realized that I don’t install any FTP server, such as PureFTP or VSFTP.

So how do you access and edit your files easily?

Firstly, it’s always best to install the minimum number of apps and packages that are required to achieve what you want. Secondly, we already have a perfectly valid method of accessing and editing the files on our server, including our WordPress files within the /var/www directory, courtesy of SFTP.

SFTP stands for SSH file transfer protocol and, as the name suggests, it is like an FTP server that runs on SSH. You will recall that we used SSH extensively to setup our server to begin with, and we also use it to execute various WordOps commands (like adding a new WordPress sites to our server).

The beauty of SSH is that it can also list, edit and transfer files, just like a traditional FTP server. Of course, on it’s own, the shell prompt is nowhere near as intuitive because all the commands would need to be entered manually via the shell text editor, as opposed to the graphical user interface you might be used to in your favorite FTP client, like CrossFTP or FileZilla.

However, FileZilla supports SFTP too (as do many other FTP apps)!

You can login via SFTP to you server, and then just press/click the buttons in FileZilla as you normally would. FileZilla will list, download, upload and edit the files and folders you want to manage, just like a normal FTP client. Check out the image below for the settings you’ll need to use. “Host” is the IP address of your server, and the port is the port you decided on for SSH when you set the server up.

The site manager in Filezilla

In terms of what user you should login with, in my server setup guide we disabled “login via root” in our SSH config file for security purposes, so that won’t work. And while you could technically login as another system user you create, they won’t have permission to edit the files in your /var/www directory, which is we want, without some further tweaking.

So, what I recommend doing in this case, is amending the user “www-data” so that you can use it to login via SFTP and manage your files as you wish. All the files in your webroot directories will be owned by “www-data” and that is the user created by nginx when that software is installed by WordOps.

“www-data” manages your web server files, reading them and serving them as your website, so it already has the permissions we require. We just need to alter the account so that we can utilize it with SFTP.

First, we change its shell to /bin/bash (the “shell” is the program on your system that interprets and executes commands). “/bin/bash” is the most common shell in Linux systems. Execute this command in your SSH client:

chsh -s /bin/bash www-data

Then we need to setup a strong password (long, special characters, numbers, capitals, etc) for the user “www-data”. Do that with this command:

passwd www-data

Now you can head back to FileZilla, enter the username “www-data” and the password you created, and login. You can then edit your web server files just as you would normally with a FTP client – you’ll find them under /var/www/.

By Sean Robinson

My name is Sean Robinson and I’ve been building and running my own blogs and websites since 2004, and have been doing it full-time for over a decade. If you'd like to know more about my story, please click here.

Leave a comment

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