I have been trying to create a symlink:

/var/www -> /home/USER/code/

Following https://stackoverflow.com/questions/9104337/create-a-symbolic-link-of-directory-in-ubuntu , I entered the following command:

sudo ln -s /var/www /home/USER/code

However the result isn't what I expect. My link is stored inside the /www directory instead of linking the www directory itself. I use apache as the local web server, for my case, i have to access my code as


whereas I wish to have something like


The code directory should just serve as a directory where I store all my projects.

Anything I'm missing out?

  • 1
    Did you delete the www directory? It'll create a symlink inside www if www exists. It should create a symlink called www if www does not exist.
    – Mendhak
    Jan 16 '14 at 20:08
  • No www is there intact. Symlink inside the www directory doesn't serve the purpose I mentioned
    – user236603
    Jan 16 '14 at 20:11
  • You can't have two entries with the same name (a directory and a symlink in your case) in the same directory. What would you mean when you say /var/www/file.txt? You have to decide...
    – Rmano
    Jan 16 '14 at 20:13
  • You should re-read @Shah's comment... you need to delete www and then create the symlink.
    – GabrielF
    Jan 16 '14 at 20:14

I think you have the ln -s 'source' and 'target' the wrong way round


sudo ln -s /var/www /home/USER/code


sudo ln -s /home/USER/code /var/www

you will end up with a symlink in your /home/USER/code folder pointing to var/www which is wrong.

For example, I have a folder for sally's site in /home/webs/sallybydesign/

cd /var/www

There should be no folder or file called sallybydesign in /var/www because we are about to make one.

 sudo ln -s  /home/ndmaque/webs/sallybydesign /var/www/sallybydesign

to see the new file (a symbolic link)

ls -l

you will see a file called sallybydesign, it is a symlink to your home/webs/sallybydesign folder and if you run

ls sallybydesign/ 

you will see the files in your home/webs/sallybydesign folder.

If so you can point your vhost file at /var/www/sallybydesign and it will automagically inherit all the apache /var/www permissions and solve a shed load of issues.

  • Did something change in the symbolic linking the last couple of updates to Ubuntu? I can't seem to do the symlinkning anymore and I get the famous 403 error.
    – Daniel
    May 18 '15 at 12:31
  • @Daniel so do I
    – emrecnl
    Oct 24 '15 at 5:28
  • 1
    The permissions are not automatically inherited by default. Make sure to chmod 0755 -R on sallybydesign in the example above to allow the webserver to read. Also, you have to AllowOverride All in the default directory for / (usually in apache.conf).
    – Daniel
    Oct 26 '15 at 9:59
  • ls sally/, got Too many levels of symbolic links. Jan 7 '18 at 11:59
  • In order to remember in which order it should be written, I simply use the same order as cp. Dec 15 '21 at 16:33

Delete (or move, for safety) the www directory before you create the symlink.

So, you should have a /var, but there should be no /var/www. Then, and only then, will a symlink called www be created that points to your localhost/code/project folder.

  • 2
    this accepted answer is wrong, first of all you say "delete" or "move" while we are talking about commands so you should put the respective shell command as well. Secondly he has the source and destination wrong in the ln command! Jan 14 '16 at 14:08
  • 1
    -1 for answering the question that wasn't asked and not pointing out the actual problem the user was suffering from.
    – Shadur
    Jan 3 '19 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.