How to Fix Permission of htdocs Folder in Ubuntu Linux

This tutorial explains how to fix the permission of htdocs folder in Ubuntu step by step with practical examples. By default htdocs folder is own by root user and not writable for regular user. Learn how to change the ownership of htdocs folder and make it writable for regular user by fixing the file permission issue.

XAMPP allows us to run a local web server for testing and developing PHP & MySQL based web applications. By default, XAMPP uses /opt/lampp/htdocs folder to sever the web contents.

For this tutorial I assume that XAMPP is installed in Ubuntu Linux. To learn how to install the latest version of XAMPP in Ubuntu step by step, see this tutorial. Learn how to install the XAMPP in Ubuntu

Since XAMPP is installed under the root account, this folder is also owned by the root user. To verify it, use following command

$ls –ld /opt/lampp/htdocs

ls -ld /opt/lampp/htdocs

As we can see in above output, root user is the both user owner and group owner of this folder. A regular user is not allowed to modify the content owned by root user.

To understand it practically, let’s create a file or directory in this folder.

$cd /opt/lampp/htdocs/
$cat > index.html
bash: index.html: Permission denied
$mkdir myweb
mkdir: cannot create directory “myweb”:Permission denied
$

As expected, permission was denied for both operations.

htdocs permission deined cli

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

htdocs permission deined gui

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; either make the htdocs folder world writable or change the ownership of this folder.

Making htdocs folder world writable

To make this folder world writable, use following command

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

To learn how file permission works in Linux see this tutorial
Linux file permission explained

This tutorial explains basic concepts of Linux file permission in detail including how to manage it step by step.

Although making htdocs folder world writable 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. Anyone who is connected with this system can write a harmful script or can modify the existing code in this folder.

Changing ownership of the htdocs folder

Every file or folder in Linux has two types of ownership; user ownership and group ownership. By default user who creates the file or folder have both types of ownership. For example, htdocs folder was created under the root user account during the installation of XAMPP, so it has the both types of ownership.

In order to change the ownership of htdocs folder, we need the username and group name of new user. We can easily get them with following commands

$whoami
$id –gn

whoami and id -gn

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

Once we know the both names, we can use 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 username and group name to sanjay, use 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.

chown command

Once ownership is changed, new owner will get all existing permissions automatically which are currently set for the current owner. Since previous owner (root) had all permissions, new user would get all permissions automatically.

This way, htdocs folder permission issue can be fixed by any one option explained above.

If you are only interested in fixing the permission issue of htdocs folder, you can skip the next section of this tutorial. We have already learned how to fix the permission issue of htdocs folder. In next section we will learn how we can run apache process under the current user account.

Running apache (httpd) process under the user account

If you want to ensure that you do not face any further permission related issue while running and customizing XAMPP web server, you should run apache process under your own user account. This simple fix removes several permission related issues.

For this, we have to modify the /opt/lampp/etc/httpd.conf file. Before we modify this file, let’s take the backup of current file.

$sudo cp /opt/lampp/etc/httpd.conf /opt/lampp/etc/httpd.conf.bak
$ls /opt/lampp/etc/httpd*

httd-conf backup

Now open this file for editing

$sudo gedit /opt/lampp/etc/httpd.conf

sudo gedit httpd-conf

The httpd.conf file is the main configuration file of apache web server. It includes several configuration values and directives. Editing in wrong section may crash entire apache process.

To locate the correct location, we can use the line numbers. By default, gedit does not display line numbers. To display line numbers, click Ln&Col option available in right bottom corner. In opened pop-up menu, select Display line numbers

gedit display line

Now locate following directives

User daemon
Group daemon

These directives are usually available around line number 170 (approximate).

default directives

Replace daemon value with your username and group name and save the file

updated directives

Once file is saved, restart the XAMPP and verify that all services are started properly

restart xampp

Testing

To ensure that everything works as expected, let’s create a test folder named myweb in /opt/lampp/htdoc folder. In this folder, create a html file named index.html.

simple text file

In this file, add a simple text line in h1 tag for testing.

add line for testing

Now access this file from web browser.

htdocs permission fix sucess

If XAMPP is working fine with new permissions and configurations, it will display webpage as shown in above figure.

That’s all for this tutorial. If this tutorial helps you in anyway, please let the other by sharing it.

ComputerNetworkingNotes Ubuntu Linux Tutorials How to Fix Permission of htdocs Folder in Ubuntu Linux