Setting file and folder permissions correctly on web servers is crucial for both security and the proper functioning of the system. In Linux environments, file/folder permissions can be changed with chmod
, and file/folder ownership can be changed with chown
. Below, we discuss the purpose and what these commands do in detail.
1. Setting Folder Permissions (chmod)
sudo chmod -R 0755 uploads2
Explanation:
-
chmod
: Used to assign permissions to a file or folder. -
-R
: Applies recursively (including subfolders and all files within). -
0755
:-
Owner: read, write, execute (7)
-
Group: read, execute (5)
-
Other: read, execute (5)
-
Purpose: Ensures that the web server can read and execute files, but prevents unauthorized individuals from making changes.
2. Changing File Ownership (chown)
sudo chown -R eka:eka uploads2
Explanation:
-
chown
: Changes ownership. -
-R
: Applies recursively, including subdirectories. -
eka:eka
: Changes the user name toeka
and the group name toeka
. -
uploads2
: The folder whose ownership will be changed.
Purpose: Ensures that Laravel or PHP applications belong to the correct user so they can write files.
3. Assigning Ownership to the Web Root Directory
sudo chown -R eka:eka /home/eka/public_html
Explanation:
This command gives ownership of all files and folders in the /home/eka/public_html
directory to the eka
user and the eka
group.
Purpose:
-
In hosting or VPS environments, it is expected that only the user has write permissions in their
public_html
directory. -
It is desired that the web server (Apache, Nginx, etc.) can access this directory but cannot write to it without permission.
Important Notes:
-
755
is generally sufficient for directories,644
can be used for files. -
Avoid using
777
, as it gives full permissions to all users and poses a security risk. -
The Apache/Nginx system user is usually
www-data
,apache
, ornginx
; group permissions should also be adjusted as needed.
In Linux, the chmod
and chown
commands are essential tools for managing your server's file structure in an organized and secure manner. Properly setting these permissions is crucial for web applications to function correctly.