Can’t edit files in Amazon Lightsail

amazon-lightsailamazon-web-services

I'm trying to modify a child theme installed on a WordPress site on Amazon Lightsail. I'm using Panic's Transmit to access the server, but whenever I try to perform any write operations (edit, upload, new folder, new file, paste, move, etc.) I get an error that tells me it couldn't do what I asked it to do, "Make sure you have permission to modify this file."

Any ideas? I'm a junior developer and not super server savvy (brand new to Lightsail), so if you think I'm missing something basic I probably am. 🙂

Best Answer

It's most likely file permissions. I assume you're logging in as ec2-user with a certificate. That user won't have permission to write files to where you're trying.

You can add ec-2 user to the group that has permission to write to those folders and files, or you can create a new user that has appropriate permissions.

The way I did it was I create a new user (eg bob), then added bob to the www-data group. I made sure files and folders were owned by the www-data group

I've got an article on this here. They key parts are below, see the link for a bit more explanation.

sudo su
sudo useradd fred
passwd fred

su fred
ssh-keygen -f fred-t rsa

mkdir .ssh

touch .ssh/authorized_keys
chmod go-w ~/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

cat fred.pub >> /home/fred/.ssh/authorized_keys

vi /etc/ssh/sshd_config
PasswordAuthentication no
AllowUsers ec2-user fred

You'll need to give fred access to those files

groupadd www-data
chown -R fred:www-data /var/www

# Change webroot permissions
find /var/www -type d -exec chmod 755 {} \;
find /var/www -type f -exec chmod 644 {} \;
# Wordpress specific
find /var/www/wp-content/uploads -type f -exec chmod 664 {} \;
find /var/www/wp-content/plugins -type f -exec chmod 664 {} \;
find /var/www/wp-content/themes -type f -exec chmod 644 {} \;
chmod 440 /var/www/wp-config.php
chmod -R g+s /var/www/

In either case, now you know what the problem is, you can Google things like permissions, adding users to groups, creating groups, etc. I didn't know anything about this stuff 18 months ago, I had a developer background, I learned by doing, reading tutorials, and experimenting. Keep backups.