Search code examples
linuxbashln

Symbolic Link broken after creation


  • The links get broken right after I create them.
  • I use ln correctly. ln -s SOURCE TARGET

Create symbolic link

$ sudo ln -s ./sites-available/examble.domain.com ./sites-enabled

Compile NGINX - fails due to broken symbolic links

Note: The problem is not related to NGINX, NGINX compilation was just there to help me realize that the problem exists. The solution described below applies to any other related problem.

$ sudo nginx -t
nginx: [emerg] open() "/etc/nginx/sites-enabled/example.domain.com" failed (2: No such file or directory) in /etc/nginx/nginx.conf:62
nginx: configuration file /etc/nginx/nginx.conf test failed

Confirm that the symbolic links are broken

$ file ./sites-enabled/example.domain.com
./sites-enabled/example.domain.com: broken symbolic link to ./sites-available/example.domain.com

Solution

  • The problem is that SOURCE is not re-interpreted when placed to the target directory.

    So, if your file that you want to link is ~/file and you want to link it to ~/folder using:

    ln -s ./file ./folder/
    

    Then the symbolic link will think that file is at ~/folder/file instead of ~/file

    So, instead, you have to get into the directory ~/folder and execute the ln command from there.

    So, Problem is ...

    ln needs the relative SOURCE directory to TARGET directory. Not the relative SOURCE directory to your current one.

    Final Solution

    # Getting into the folder
    cd ./sites-enabled
    
    # Creating symbolic link
    ln -s ../sites-available/example.domain.com ./