Ubuntu – Reset default permissions for /var/www

Apache2chmodchownpermissionsserver

I completely messed up my /var/www/ folder after attempting to change some permissions. Now I am unable to open my localhost in browser.

Following are my permissions in relevant folders.

in root,

d---------  14 root root  4096 Jan 16 23:57 var

in var,

drwxr-xr-x  2 root     root     4096 Jan 17 04:10 backups
drwxr-xr-x 17 root     root     4096 Jan 17 00:57 cache
drwxrwsrwt  2 root     whoopsie 4096 Jan 17 04:10 crash
drwxr-xr-x 69 root     root     4096 Jan 17 03:35 lib
drwxrwsr-x  2 root     staff    4096 Oct 19 14:44 local
lrwxrwxrwx  1 root     root        9 Jan 16 21:13 lock -> /run/lock
drwxrwxr-x 12 root     syslog   4096 Jan 17 16:31 log
drwxrwsr-x  2 root     mail     4096 Oct 21 21:19 mail
drwxrwsrwt  2 root     whoopsie 4096 Oct 21 21:30 metrics
drwxr-xr-x  2 root     root     4096 Oct 21 21:19 opt
lrwxrwxrwx  1 root     root        4 Jan 16 21:13 run -> /run
drwxr-xr-x  8 root     root     4096 Oct 21 21:27 spool
drwxrwxrwt  5 root     root     4096 Jan 17 17:27 tmp
drw-------  3 www-data www-data 4096 Jan 16 23:57 www

in /var/www/

drwxrwx--- 3 www-data www-data 4096 Jan 17 00:33 html

in /var/www/html/

-rwxrwx--- 1 www-data www-data    20 Jan 17 00:00 info.php
-rwxrwx--- 1 www-data www-data 11321 Jan 16 23:57 index.html
drwxrwx--- 4 www-data www-data  4096 Jan 17 03:01 movie

The following are some of the commands I did that messed the whole thing up

  199  sudo chmod -R 777 /var/www/html/
  200  sudo chown -R www-data:www-data /var/www
  201  chmod go-rwx /var/www
  202  chmod go-rwx /var/www/html/
  203  sudo chown -R www-data:www-data /var/www/html/
  204  chmod go-rwx /var/www/html/
  205  chmod go+x /var/www/html/
  206  chgrp -R www-data /var/www/html/
  207  hmod -R go-rwx /var/www/html/
  208  chmod -R go-rwx /var/www/html/
  209  chmod -R g+rx /var/www/html/
  210  chmod -R g+rwx /var/www/html/
  224  sudo chmod +x /var
  225  systemctl restart apache2
  230  sudo chmod +r -w -x /var/
  231  sudo chmod /var/ +r -w -x
  232  sudo chmod /var/ -r -w -x
  235  chmod 755 /var/www/
  236  chmod 644 /var/www/*
  237  sudo chgrp www-data /var/www
  238  sudo chmod 775 /var/www
  239  sudo chown -R www-data:www-data /var/www
  240  chmod go-rwx /var/www
  241  chmod go+x /var/www
  242  chgrp -R www-data /var/www
  243  chmod -R go-rwx /var/www
  244  chmod -R g+rx /var/www
  245  chmod -R g+rwx /var/www
  246  chown -R www-data:www-data /var/www/*
  247  chown -R www-data:www-data /var/www/
  248  chmod -R og-r /var/www/html/
  249  chown -R root:root /var/www/
  250  chmod -R 777 /var/www
  253  chmod -R 777 /var/www/html/

Is there any way I can reset my permissions so that I can run my server again? Just for development purposes!

Best Answer

/var/ and /var/www/ are wrong.

sudo chmod 755 /var/    
sudo chmod 755 /var/www/

(sudo required). To fix directories and files inside /var/www/:

sudo find /var/www/ -type d -exec chmod 755 {} \;
sudo find /var/www/ -type f -exec chmod 644 {} \;

(sudo probably not required but there might be "root" owned files in those directories) Directories need the executable set, so 755, you might want to change it to 750, 775, 770 based on preferences; it lets group and others have more/less permissions). files do not, so 644, you might want to change it to 640, 664, 660 based on preferences; same reason).

That should be it.

In case you also want to reset the user and group setting (not needed if I look at the history):

sudo chown -R www-data:www-data /var/www/
Related Question