Most likely your Apache is running under the default www-data:www-data
while the /var/www
folder is owned either by root
or your own user. Most likely you are using PHP as an Apache module, which means that it runs under the same user and group as Apache itself. Therefore, Apache (and as a result PHP which is running in the same process, therefore under the same user and group) cannot write into the folder.
If this is a single site setup, do yourself a favour and make things easy. Assuming you have gone with the default “put everything under /var/www” approach.
Change the ownership of /var/www to your user:
sudo chown $USER:users /var/www -R
Change the permissions of /var/www and all its contents recursively:
sudo find /var/www -type f -exec chmod 0644 \{} \;
sudo find /var/www -type d -exec chmod 0744 \{} \;
Edit /etc/apache2/envvars
and change the following lines:
export APACHE_RUN_USER=youruser
export APACHE_RUN_GROUP=youruser
Where youruser
is your username.
Restart Apache for the changes to take effect:
systemctl restart apache2
Now Apache runs under your user, and so does PHP. You are logged in as your user, therefore all files you create over SSH and SFTP are owned by your user. The directory you are hosting your site in is also owned by your user. No more mixed permissions.
If you are not using PHP as an Apache module, or if you are using multiple sites, I recommend using a commercial host for now. It makes me sound like a jerk, but it's the reality of the situation.
Let me give you some word of advice.
You are probably trying to self-host because you were told it's easy and cheap. That wasn't a lie, but it came with undisclosed qualifiers. People giving that advice have a ton of experience in Linux administration. If you are an expert at something, it looks easy. The problem is, everyone I see self-hosting is not an expert.
To make matters worse, computers make things look deceptively easy. Setting up a server is dead easy, as you've found out. Setting up a server correctly, though, is anything but! You need a good grasp of the fundamentals. Without that, you hit problems you don't know how to solve, or even where to begin trying to solve. You start doing random things which either don't work or make things worse. Sounds familiar?
Here is my practical advice if you want to seriously go into self-hosting. Keep your sites on a commercial host for now. Production is not the place to learn (especially unsupervised). Set up a Linux VM on Virtualbox or similar, start setting up a server, and try solving the problems you run into. Embrace your failures, because they will be plenty. But that's how you learn.
And now, the tidbit of wisdom nobody tells you about. It only makes sense to self-host if you are doing it at scale, or if you have an IT team which costs a fraction of the revenue brought in by the self-hosted application. Anything else is a waste of time and money – if you are doing it right. Sure you can self-host without doing any maintenance on the server, but don't cry when you get hacked. Sure you can spend hundreds of hours every year to self-host a site which brings in a few hundred Euro per year, but don't cry when you're bankrupt and overworked.
I don't self-host anything but my dev servers and a single server which runs software I cannot run on a commercial host (or at least not without paying so much for a dedicated server that it makes little sense not going directly to an IaaS to buy compute resources directly). You know why? Because what the host charges me to do proper maintenance on the server is a fraction of how much my own time would cost to do the same. They have economies of scale. I have a dozen sites on two servers; I can't beat them on the economies of scale.
Nicholas K. Dionysopoulos
Lead Developer and Director
🇬🇷Greek: native 🇬🇧English: excellent 🇫🇷French: basic • 🕐 My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!