Getting File Permissions Right In WordPress
I don’t think too many people, who can boast at least an ounce of credibility, would disagree with me when I say WordPress is becoming the killer app. At least as far as blogging platforms are concerned, anyhow. Mix this with the fact that the projects and work I am tasked to daily are becoming exponentially more complicated, interesting and downright fun, I’ve decided to dive in to what I’ve previously considered as a somewhat egotistical world of blogging.
But where I have previously sheiked “Ooh, web log!” in a sarcastic high-pitched voice, I shall now subdue and admit defeat, letting my hypocrite little fingers do the talking.
If nothing else, I will treat this blog as a diary, or armoury, of things I want to remember or share about the things I find interesting. I firmly believe there has to be at least a handful of people in the world that will find my writings interesting, if no more than for scientific or sociology reasons. Alas, as long as it gives me a place to document what I enjoy, it shall serve some purpose.
I better get to the point. Yes, WordPress is pretty amazing; it’s pretty, usable and powerful all at the same time. It does exactly what it says on the tin, but very, very well. The installation process lasts just minutes until you’re all ready to go. Well, almost. If you want to upload images to place in your posts, or automatically change your permalinks settings without getting your hands dirty, then you will have probably met a few file permission problems.
These problems seem to crop up a lot for those people who run WordPress on a VPS, possibly using web server management software like Plesk. The problems arise when all your web pages and documents are owned by your personal user account, but Apache, our old faithful friend, runs as a different user.
As we all know, a file has set permissions for its owner, the group it belongs to, and for everyone else. These permissions are read, write and execute. By default a files permissions are set to 644 (read/write for the owner, read for group and read for everyone else). This means that whenever Apache wants to write something, as it does when you upload media, or try make changes to you .htaccess file, it will not have write permission as it doesn’t own the file, nor is it in the same group.
In the past I tackled this problem in a number of different ways. The first way, which anyone with a hint of care for their security would grimace at, would be to set permissions on those files that need writing to to 777 (read, write and execute access for everyone!). This is bad.
As I started taking a little more care I thought it would be better to keep the permissions nice and tight, while still allowing the write access for Apache. I ran the chown command (change ownership) to set the files to be owned by the user that Apache was running under. For instance, Apache usually runs under a user like apache, www-data or nobody. Chowning the files in the web root to that user would give Apache the access it so craved, and all was hunky-dorey.
I can’t help but look at the above technique and grimace a little too, though. What happens if you happened to want to edit a file on the fly in Vim? Maybe you need to FTP some files in to a specific folder on the server? Your normal user account would have lost write access in many places — the amount of chowning and chmodding you would have to do on a regular basis can start to get a little bit unbearable.
Surely then there is a better option? Well, yes. But there are a couple of things you need to bear in mind or you may start going round in circles feeling like you’ve achieved nothing.
The third and final method (for me, at least) is to find out what group your user account is (on Plesk this is probably psacln) and add your Apache user to that group too. For instance, if I was running Apache under the user www-data, and my user account was leonard with a default group of psacln, I would run the command:
$ usermod -a -G psacln www-data
Now it should be obvious that if we allow write access to the file’s group, Apache (running as www-data) should be able to write to it. The simplest way to do this is to run the command:
$ chmod g+w .htaccess
Here, you would replace .htaccess with whatever file (or folder) you want to change permissions on. Of course, you can use the recursive -R flag if you want to apply the permissions on the folder and all the files and folders beneath it (such as wp-content/uploads).
Note: I have seen varying results from the above changes on different operating systems. You may find that you need to restart Apache for the changes to work. This would be something like:
/etc/init.d/httpd restart…depending on what version of Linux you’re using. Consult your OS documentation! If it still doesn’t work, continue on.
It all seems a little bit easy, doesn’t it? Well it kind of is really. But as I said there may be one or two caveats that catch you out. If this still doesn’t work for you (and especially if you are using Plesk) then it is likely PHP safe mode is turned on.
PHP Safe mode (set in the Web Setup of a domain in the Plesk control panel), where you enable PHP, Python, CGI, etc, is on by default, and can act rather strangely if you don’t know what to expect. When safe mode is on, PHP will only let you write to a file if you own it. Yes, even if you are in the same group, and have group write permissions set correctly. What’s more, when you create folders inside other folders, they will automatically inherit the permissions of the domain user account, not the user Apache is running under.
Now it’s likely there are things you would probably want to consider when turning PHP safe mode off, especially in a shared environment, but for me it was just an inconvenience that I wanted to go away. So it did. Now, I own all my files and let Apache write to a specific one or two. Problem solved.
Oh, and don’t forget… running commands like chmod and chown will require superuser access. On some systems you can get away with running sudo su - or just su -, but you must be in the sudoers file. If this is not the case, you will have been given a root password, which you can log in to to run these commands.
Again, this all seems so trivial and downright simple. But coming from past experience, when you are still learning your way around the Linux operating system, permissions can be a little daunting to say the least. There are thousands of repeated posts on forums and blogs across the web asking the same questions. Hopefully you (and I) will never have to worry about these problems again.
With that said, welcome to my blog!
8 Responses to Getting File Permissions Right In WordPress
Leave a Reply Cancel reply
What others have been saying
- Leonard on How To Take A Screenshot On An iPhone
- Larry Basham on How To Take A Screenshot On An iPhone
- Leonard on Using Propel with WampServer
- mark on Using Propel with WampServer
- Resize Thumbnail Image to a Fix Square Size with Php Thumbnail Class | Live, Laugh, Love ~~ My Code on Create square thumbnails from any size image in PHP
What I've been tweeting
- No public Twitter messages.
Categories
- Databases (1)
- Gaming (1)
- Geek (1)
- General (4)
- Hardware (2)
- Mobile (3)
- Operating Systems (2)
- Out and About (1)
- Programming (9)
- Quick Tips (5)
- SEO (1)
- Servers (4)
- Software (2)
- Web Sites (1)
Tag Cloud
.htaccess 644 777 android apache apple augmented reality backup blog blogging c++ checkout chmod chown dropbox file permissions google hero htc ios ipad iphone iphone 4 ipod objective-c opencv pear phing php pleask plesk propel propel-gen safe mode server su sudo svn sync upgrade uploads vps wordpress www-data xcode



Hi Leonard,
I have a VPS and have now come across the exact issue you’ve described here. This is exactly what I’ve been searching all over the web for – thank you!
WordPress runs on my VPS as www-data but my FTP runs as psacln and I’m trying to get the two to be friends. I’ve followed your guide here, but by manually editing the /etc/group file instead of using the commands, like this:
psacln:x:2523:www-data,apache
I’d previously also had to use 777 permissions, but this allowed me to change the folders back to 775 (thanks!) and it worked fine as www-data now had permission to write to the psacln group.
But I’m stuck on one last aspect…
When I FTP into the site I still cannot download anything that was uploaded by www-data (like a WordPress image, for example). It says I don’t have permission? Do I need to add change any other user/group in order to allow this? There is an entry in my /etc/group file which says:
www-data:*:33:www-data
Do I also need to put psacln in there too?
Thanks,
Dave
Adding psacln to www-data would mean you could download those files, yes. Bear in mind that this would apply to all files, not just those in the uploads folder.
[...] A better way than using 777 permissions for folders. This entry was posted in Web by vix. Bookmark the permalink. [...]
i’ve same issue about this, everytime edit file header/footer/search..etc there’s a note that saying “You need to make this file writable before you can save your changes” in wordpress editor ,unfortunately i don’t understand what you explain. Can you make simple the command like: (1)….(2)…Thank you very much
If you need a 1) 2) 3) then you probably don’t want to be doing this…
That said, if you really want to copy and paste commands unknowingly, I suggest:
1)
sudo usermod -a -G psacln www-data2)
chmod g+w header.php3)
sudo /etc/init.d/httpd restartHi Leonard,
I know its been a year since you wrote this very informative blog, but I have a problem that you may be able to help me with – I am using a 1and1 VPS with Plesk, safe mode is off. Everything works fine, but when I upload in WordPress, the files get 600 permissions instead of 644. I have tried everything and researched this exhaustively with no solution in site. Have you run into this and if so what is the solution?
Thank you in advance!
Dave
Hi Dave, sorry I’ve not been around a lot lately, but if you’ve not sorted this yet have a few questions/suggestions:
Firstly, What permissions does the parent directory of where files are being upload to have? And is the sticky bit set?
Check this link out: http://www.php.net/manual/en/function.move-uploaded-file.php#85286
Is that the same with you?
Using Plesk, go into your web settings and change PHP to run as FastCGI instead of an Apache Module. This solves the problem until you try to upload a plugin or theme.
Then you need to add the following line to httpd.conf
FcgidMaxRequestLen 33554432
Restart Apache (service httpd restart) and your problem is solved for good :)