XAMPP htdocs Permission issue and Fix in Ubuntu

This tutorial explains how to make the htdocs folder writeable in the Ubuntu. Learn how to fix the file permission and ownership issue of the htdocs folder.

A brief intro of the Linux files permission

Linux categorize all users in three types; owner, group and others. Based on these types, it allows system administrator to configure the file permission on all objects such as files and folders. There are three types of the file permission; read, write and execute.

What a user can do with an object is depend on the type of permission that he has on that object. Following table lists the actions that a user, based on the type of permission, can take on a file or folder.

Type of object/ Permission Read Write Execute
Text file Can read the content of file Can add/edit/delete/modify the fie Can execute the file, if it contains executable contents
Folder or directory Can list the contents of the file Can create new file or directory Can navigate through the directory

A user who creates an object is considered as the owner of that object. The users those belong to the owner’s primary group are considered as the group owner of that object. Reset users are considered as the other users.

By default, Linux grants the write permission to the owner and group only.

To learn the Linux file permission in detail, you can check this tutorial.

Linux file permission explained in easy language

Why htdocs folder is not writeable

In Linux, only the user root or the user who has the root privilege can install the software. When the XAMPP is installed under the root privilege, the user root automatically becomes the owner of all files and folders those are created during the installation.

Since the folder htdocs is also created during the installation, it is also owned by the user root. By default, Linux doesn’t add any member in user’s primary group. Thus, both owner and group owner of the htdocs folder is the user root.

To verify it, we can use the following command.

$ls –ld /opt/lampp/htdocs

owner and group owner of the htdocs folder

As explained above, by default Linux grants the write permission to the owner and owner’s primary group members only. This makes the folder htdocs unwritable for all users except the user root.

Let’s take some examples. Login from a regular user account and create a file and a directory in this folder.

$cd /opt/lampp/htdocs/
$cat > index.html
$mkdir myweb

htdocs folder not writable

As expected, permission has been denied in both cases.

Let’s do one more testing. This time, open this folder from the GUI console and create a new file or folder in the opened folder.

htdocs folder permission denied

As we can see in above figure, just like command prompt, user is not allowed to create a new file or folder in this folder.

Fixing htdocs folder permission issue

To fix this issue, we have two options; grant the write permission to all users or change the ownership of this folder. Let’s understand both options.

Granting the write permission to all users

To grant the write permission to all users, use the following command.

$sudo chmod -R 777 /opt/lampp/htdocs

making htdocs folder writable for all user

This command sets the full permission for all users on the htdocs folder and all of its contents. This permission applies only on the existing files and directories. Any new file or directory that will be created after this command will get the default permission.

To test it, make a new directory again.

$mkdir /opt/lampp/htdocs/test

testing htdocs folder

As we can see in above output, users are now allowed to create the file and folders in the htdocs folder.

Although granting the write permission to all users is the easiest fix of this issue, still it should not be used. This option solves one problem at the cost of several other security related problems.

Making this folder world writable means inviting everyone to write and execute scripts from this folder. Any network user, who can access this folder remotely, can not only write and execute a harmful script from this folder, but also modify the existing code.

Unless your system is not connected with network and you are only the user who uses the system you should not use this method. To set the default file permission back, use the following command.

$sudo chmod -R 755 /opt/lampp/htdocs/

reverting back to orignial setting

Changing ownership

As mentioned above, owner and group owner have the write permission. If we change the ownership of an object, new owner will automatically get the write permission.

In order to change the ownership, first we need the username and primary group name of the new user. To know these, we can the following commands.

$id –gn

username and group owner name

First command prints the username of current user while second command shows the primary group name of the current user.

Once we know the both names, we can use the following command to change the both types of ownership.

sudo chown –R [Username]:[Groupname] /opt/lampp/htdocs

Replace Username and Groupname with your own username and group name respectively. For example, to set the username and group name to sanjay, use the following commands.

$ls –ld /opt/lampp/htdocs
$sudo chown –R sanjay:sanjay /opt/lampp/htdocs
$ls –ld /opt/lampp/htdocs

First and third commands are optional. They are used only to view the owner name before and after the change.

changing owner and groupowner

Once the ownership changes, the new owner automatically gets all the permissions that were set for the current owner. Since the previous owner (root) had all the permissions, the new user automatically gets all the permissions. To test it, create a file and folder in the htdocs folder.

testing new ownership

That’s all for this tutorial. If you like this tutorial, please don’t forget to share it through your favorite social network.

ComputerNetworkingNotes Linux Tutorials XAMPP htdocs Permission issue and Fix in Ubuntu