248

I installed Nginx on Centos 6 and I am trying to set up virtual hosts. The problem I am having is that I can't seem to find the /etc/nginx/sites-available directory.

Is there something I need to do in order to create it? I know Nginx is up and running because I can browse to it.

1

3 Answers 3

473

Well, I think nginx by itself doesn't have that in its setup, because the Ubuntu-maintained package does it as a convention to imitate Debian's apache setup. You could create it yourself if you wanted to emulate the same setup.

Create /etc/nginx/sites-available and /etc/nginx/sites-enabled and then edit the http block inside /etc/nginx/nginx.conf and add this line

include /etc/nginx/sites-enabled/*;

Of course, all the files will be inside sites-available, and you'd create a symlink for them inside sites-enabled for those you want enabled.

22
  • 37
    RedHat relatives (i.e. Fedora and Centos) don't have this feature out of the box like Debian/Ubuntu. It's easy to copy, as @Mohammad said, however not having nice defaults is confusing for newcomers.
    – MGP
    Sep 16, 2013 at 19:17
  • 60
    God, I can't say how many searches I did to figure this out. I am using Amazon Linux AMI release 2014.03. These directories are not created automatically if you install nginx from the rpm files(whether nginx 1.6.2 or nginx 1.7.5) but many nginx configuration examples refer these...making you wonder whether your nginx got installed correctly or not.
    – so_mv
    Sep 23, 2014 at 0:30
  • 1
    Cool thanks, how about the symlink? Is my syntax correct? It doesn't seem to be getting found by the include statement. Sep 24, 2016 at 22:20
  • 9
    I fixed it, started fresh and created sym link like this: sudo ln -s /etc/nginx/sites-available/test.conf /etc/nginx/sites-enabled/test.conf not sure why the full path made a difference :/ Sep 24, 2016 at 22:36
  • 1
    Thank you so much. Built nginx from source and didn't realize ubuntu did this. Dec 7, 2017 at 20:12
84

If you'd prefer a more direct approach, one that does NOT mess with symlinking between /etc/nginx/sites-available and /etc/nginx/sites-enabled, do the following:

  1. Locate your nginx.conf file. Likely at /etc/nginx/nginx.conf
  2. Find the http block.
  3. Somewhere in the http block, write include /etc/nginx/conf.d/*.conf; This tells nginx to pull in any files in the conf.d directory that end in .conf. (I know: it's weird that a directory can have a . in it.)
  4. Create the conf.d directory if it doesn't already exist (per the path in step 3). Be sure to give it the right permissions/ownership. Likely root or www-data.
  5. Move or copy your separate config files (just like you have in /etc/nginx/sites-available) into the directory conf.d.
  6. Reload or restart nginx.
  7. Eat an ice cream cone.

Any .conf files that you put into the conf.d directory from here on out will become active as long as you reload/restart nginx after.

Note: You can use the conf.d and sites-enabled + sites-available method concurrently if you wish. I like to test on my dev box using conf.d. Feels faster than symlinking and unsymlinking.

6
  • 19
    The point of the symlinking "mess" is that you can preserve config files and still disable virtual hosts very easily. If you use your approach, you have to move the files out of conf.d when you want to disable a site which I don't think is any less messy than conveniently deleting a symlink.
    – bviktor
    Sep 29, 2014 at 21:43
  • @bviktor I hear ya. I actually use both ways. I just found it more tedious [when testing configs] to bounce back and forth between the sites-available and sites-enabled dirs. In my production env I'm planning on using symlinking. Sep 30, 2014 at 6:08
  • 3
    Yeah, I agree, typing ln -s ../sites-available/... is rather tedious when quickly testing stuff, but in the long run it's handy and easier to manage :)
    – bviktor
    Oct 1, 2014 at 9:33
  • 14
    @bvktor Only sites with .conf extension are getting included. If you don't want a site to be enabled, just add .disabled extension to the site configuratoin file. That's the standard convention in CentOS / Apache and it will apply to Nginx as well.
    – SenG
    Nov 10, 2015 at 1:30
  • 2
    a little surprised no one has suggested to include /etc/ngins/conf.d/*.enabled or something similar, disabling a virtual host would require just to rename the .enabled away from the name
    – Purefan
    May 22, 2020 at 23:34
-5

I tried sudo apt install nginx-full. You will get all the required packages.

2
  • 7
    CentOS uses YUM package.
    – Salva
    Aug 26, 2020 at 1:30
  • You saved my day. Thanks Jul 12 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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