Nginx sites-enabled, sites-available: Cannot create soft-link between config files in Ubuntu 12.04
Asked Answered
O

5

112

I am trying to create soft links between config files containing server blocks in the sites-enabled and sites-available directories in /etc/nginx/.

The command I am using is:

sudo ln -s sites-available/foo.conf sites-enabled/

When I then execute:

ls -l

The result is:

lrwxrwxrwx 1 parallels parallels 27 Aug  6 20:44 immigrationinformation.conf -> immigrationinformation.conf

where the immigrationinformation.conf -> immigrationinformation.conf part has a charcoal with red typeface.

When I then try and access this soft-link, I am told that it is broken.

When I create the soft-link in the sites-available directory i.e.

sudo ln -s sites-available/foo.conf sites-available/foo_link.conf

it works as normal. However, if I then move this to the sites-enabled directory, the link is broken again.

I can create the soft link via the file manager GUI but not via the command line. I can also create hard-links with no problem.

I suspected it was a permissions issue so I have played around with setting all permissions to 777 of both directories and the directories themselves and also changing the owners to something other than root, but still with no luck.

Any help would me much appreciated, thank you.

Octroi answered 6/8, 2013 at 20:10 Comment(1)
I know the question is already answered but i wanted to point where your mistake was, it was actually very simple, you needed to specify the file name in the second argument not the first sudo ln -s sites-available/ sites-enabled/foo.conf while standing inside the /etc/nginx directoryObservatory
P
224

You should start by understanding the target of a symlink is a pathname. It can be absolute or relative to the directory which contains the symlink.

Assuming you have foo.conf in /sites-available:

cd sites-enabled
sudo ln -s ../sites-available/foo.conf .
ls -l

Now, you will have a symlink in /sites-enabled called foo.conf which has a target ../sites-available/foo.conf.

Just to be clear, the normal configuration for Apache is for potential sites to live in /sites-available and the symlinks for the enabled sites to live in /sites-enabled, pointing at targets in sites-available.

That doesn't seem to be the case based on the way you described your setup; however that is not your primary problem.

If you want a symlink to ALWAYS point at the same file, regardless of the where the symlink is located, then the target should be the full path; ie:

ln -s /etc/apache2/sites-available/foo.conf mysimlink-whatever.conf

Here is (line 1 of) the output of my ls -l /etc/apache2/sites-enabled:

lrwxrwxrwx 1 root root  26 Jun 24 21:06 000-default -> ../sites-available/default

See how the target of the symlink is relative to the directory that contains the symlink (it starts with ".." meaning go up one directory).

Hardlinks are totally different because the target of a hardlink is not a directory entry but a filing system Inode.

Pentosan answered 6/8, 2013 at 20:54 Comment(1)
Ok I see my problem now, I thought the ln command would be resolving the path for me, however I now see that I just need to specify the full path of the target, or the relative path from the location on the symlink.Octroi
W
73

My site configuration file is example.conf in sites-available folder So you can create a symbolic link as

ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/
Who answered 29/6, 2020 at 7:1 Comment(3)
I found the above command more intuitive because you can write above command from anywhere after you logged into terminal. And you don't have to do cd as the above path is absolute.Who
You can still use a relative path for the target without having to cd eg ln -s ../sites-available/example.conf /etc/nginx/sites-enabled/ as when the symlink is resolved it neither knows nor cares where ln was run from. (ln could even be run on a different machine then the symlink copied in). The relative path (for the target) has the advantage that it remains valid if the root of the installation is moved, or is viewed from a different perspective such as inside a container vs on the container host, or inside vs outside of a chrootPentosan
@Pentosan my point is you need to resolve the symbolic link by yourself if you are using the relative path. sometimes../sites-available/example.conf path is valid, sometimes you need to do ../../sites-available/example.conf and so on, the absolute path can be executed from anywhere, no require to resolve the path by yourself.Who
A
4

You can overwrite the existing file ../sites-enabled/myproject by forcing ln like this

sudo ln -sf ../sites-available/myproject ../sites-enabled/

then

sudo service nginx restart
Alumroot answered 17/6, 2021 at 10:53 Comment(0)
H
3

In my case, I have multiple sites in site-available folder. All sites symbolic links are created with a single command:

ln -s /etc/nginx/sites-available/* /etc/nginx/sites-enabled/

Then test and update nginx

# For testing nginx config
sudo nginx -t

# For updating nginx config
sudo nginx -s reload
Hannigan answered 16/11, 2023 at 18:59 Comment(0)
P
2

To shorten manual typing of each conf file, you can try from sites-enabled folder:

ln -s ../sites-available/*.conf .
sudo systemctl restart nginx
Piecework answered 26/10, 2021 at 5:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.