June 7, 2012

Beginner

How To Set Up nginx Virtual Hosts (Server Blocks) on Ubuntu 12.04 LTS

Tagged In: Nginx, Ubuntu

About Virtual Hosts


Virtual Hosts are used to run more than one website or domain off of a single server.

Note: according to the nginx website, virtual hosts are called Server Blocks on the nginx. However, for easy comparison with apache, I'll refer to them as virtual hosts in this tutorial.

Set Up

The steps in this tutorial require the user to have root privileges on the virtual private server. You can see how to set that up in the Initial Server Setup Tutorial in steps 3 and 4. Furthermore, if I reference the user in a step, I’ll use the name www. You can implement whatever username suits you.

Additionally, you need to have nginx already installed on your VPS.

If this is not the case, you can download it with this command:
sudo apt-get install nginx

Step One— Create a New Directory


The first step in creating a virtual host is to a create a directory where we will keep the new website’s information.

This location will be your Document Root in the Apache virtual configuration file later on. By adding a -p to the line of code, the command automatically generates all the parents for the new directory.
sudo mkdir -p /var/www/example.com/public_html

You will need to designate an actual DNS approved domain, or an IP address, to test that a virtual host is working. In this tutorial we will use example.com as a placeholder for a correct domain name.

However, should you want to use an unapproved domain name to test the process you will find information on how to make it work on your local computer in Step Six.

Step Two—Grant Permissions


We need to grant ownership of the directory to the right user, instead of just keeping it on the root system. You can replace the "www-data" below with the appropriate username.
sudo chown -R www-data:www-data /var/www/example.com/public_html

Additionally, it is important to make sure that everyone is able to read our new files.
sudo chmod 755 /var/www

Now you are all done with permissions.

Step Three— Create the Page


We need to create a new file called index.html within the directory we made earlier.
sudo nano /var/www/example.com/public_html/index.html

We can add some text to the file so we will have something to look at when the the site redirects to the virtual host.
<html>
  <head>
    <title>www.example.com</title>
  </head>
  <body>
    <h1>Success: You Have Set Up a Virtual Host</h1>
  </body>
</html>

Save and Exit

Step Four—Create the New Virtual Host File


The next step is to create a new file that will contain all of our virtual host information.

nginx provides us with a layout for this file in the sites-available directory (/etc/nginx/sites-available), and we simply need to copy the text into a new custom file:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

Step Five—Set Up the Virtual Hosts


Open up the new virtual host file— you will see all the information you need to set up virtual host within.
 sudo nano /etc/nginx/sites-available/example.com

We need to make a couple of changes in these few lines:
 server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www/example.com/public_html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name example.com;
}

  • Uncomment "listen 80" so that all traffic coming in through that port will be directed toward the site

  • Change the root extension to match the directory that we made in Step One. If the document root is incorrect or absent you will not be able to set up the virtual host.

  • Change the server name to your DNS approved domain name or, if you don't have one, you can use your IP address

  • You do not need to make any other changes to this file. Save and Exit.

    The last step is to activate the host by creating a symbolic link between the sites-available directory and the sites-enabled directory. In apache, the command to accomplish this is "a2ensite"—nginx does not have an equivalent shortcut, but it's an easy command nonetheless.
    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

    To both avoid the "conflicting server name error" and ensure that going to your site displays the correct information, you can delete the default nginx server block:
    sudo rm /etc/nginx/sites-enabled/default

    Step Six—Restart nginx


    We’ve made a lot of the changes to the configuration. Restart nginx and make the changes visible.
    sudo service nginx restart

    Optional Step Seven—Setting Up the Local Hosts


    If you have pointed your domain name to your server’s IP address you can skip this step—you do not need to set up local hosts. Your virtual hosts should work. However, if want to try out your new virtual hosts without having to connect to an actual domain name, you can set up local hosts on your computer alone.

    For this step, make sure you are on the computer itself, not your droplet.

    To proceed with this step you need to know your computer’s administrative password, otherwise you will be required to use an actual domain name to test the virtual hosts.

    If you are on a Mac or Linux, access the root user (su) on the computer and open up your hosts file:
    nano /etc/hosts 

    If you are on a Windows Computer, you can find the directions to alter the host file on the Microsoft site

    You can add the local hosts details to this file, as seen in the example below. As long as that line is there, directing your browser toward, say, example.com will give you all the virtual host details for the corresponding IP address.
    # Host Database
    #
    # localhost is used to configure the loopback interface
    # when the system is booting.  Do not change this entry.
    ##
    127.0.0.1       localhost
    
    #Virtual Hosts 
    12.34.56.789    www.example.com 

    However, it may be a good idea to delete these made up addresses out of the local hosts folder when you are done to avoid any future confusion.

    Step Eight—RESULTS: See Your Virtual Host in Action


    Once you have finished setting up your virtual host, you can see how it looks online. Type your domain name or ip address into the browser (ie. http://12.34.56.789)

    It should look somewhat similar to my handy screenshot

    Creating More Virtual Hosts


    To add more virtual hosts, you can just repeat the process above, being careful to set up a new document root with the appropriate domain name, and then creating and activating the new virtual host file.

    See More

    Once you have set up your virtual hosts, you can proceed to Create a SSL Certificate for your site or Install an FTP server

    Share this Tutorial

    Vote on Hacker News

    Try this tutorial on an SSD cloud server.

    Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

    Create an account or login:

    108 Comments

    Write Tutorial
    • Gravatar Santiago Navatta over 1 year

      And how do I configure a sub domain ?

    • Gravatar Moisey over 1 year

      You would update the server_name entry to use your subdomain instead of your regular domain.com.

    • Gravatar anthony.silva over 1 year

      I've setup several server blocks this way (although, I put them all in one file instead of having separate files per domain). However, in a web browser, I can browse to http:///domain.com/public_html. Is there any way to make it so this is not possible and only http://domain.com works? Or maybe I did something wrong? I tried playing around with permissions - I set /var/www to 700 and /var/www/doman.com/public_html to 755 but that didn't work - I got 403 errors for http://domain.com.

    • Gravatar anthony.silva over 1 year

      That's supposed to be http://{my ip}/domain.com/public_html that I can browse to and don't want to.

    • Gravatar anthony.silva over 1 year

      OK, I think I found a solution. I had a server block entry (the default one) that mapped my ip address to /var/www. I removed this entry and now I can't browse to http://{my ip}/domain.com/public_html. It rewrites the ip address part to the domain.com in the next server block in the file (which I guess has now become the default). So, typing in http://{my ip}/domain.com/public_html rewrites itself to http://domain.com/domain.com/public_html, which produces a 403 error (which I'm ok with, unless someone knows of a more graceful way to handle this - can't really think of what it *should* do with a url like that, other than error out, tho). Can anyone out there confirm that this sounds like a correct/acceptable solution?

    • Gravatar Moisey over 1 year

      There is no need to separate your different domains into separate files if you do not want to. Each domain that you configure should have its own block: server { } The root directive specifies which directory you would like to serve files for: root /var/www/example.com/public_html; If you point it to public_html/ there is no need to add a domain.com/public_html when browsing your website. server_name domain.com server_name host.domain.com Are the directives to nginx which specify which domains this server { } block will be active for. You can also remove the global root directive and test with just the "server" definitions and then ensure that your server blocks have server_name set and a listen directive to specify which IPs and ports that domain should be active on the server for. If you are still having issues might be a good idea to paste in the specific server block definition which isn't working and we can review that.

    • Gravatar desiredpersona about 1 year

      Delete the default nginx server block to avoid "conflicting server name" error sudo rm /etc/nginx/sites-enabled/default

    • Gravatar Etel Sverdlov about 1 year

      Thanks for the suggestion—I have added that in.

    • Gravatar cosmin about 1 year

      Hi, I've followed this tutorial and it worked for me. But now, accessing example.com/phpmyadmin or 123.45.678.910/phpmyadmin returns a 500 Internal Server Error. What do I need to do to get phpMyAdmin back up and running?

    • Gravatar cosmin about 1 year

      Scratch that, I wasn't adding /public_html at the end of the path for my site's root when creating the symbolic link for phpMyAdmin. So, if anyone moved their site root as above, to /var/www/example/com/public_html , you obviously need to do this for phpMyAdmin : sudo ln -s /usr/share/phpmyadmin/ /var/www/example.com/public_html My question is: is there any way to create a sym link for phpMyAdmin, so that I can access it from any domain I add to my VPS? i.e. access it from example.com/phpmyadmin, example2.com/phpmyadmin and so on.

    • Gravatar about 1 year

      Glad to see this is working for everyone. I am however, having an issue. Everything is setup correctly, but the second site im running is directing to the first site. In other words, it seems like the root directory set for my second site is being ignored or something. Can anyone please help me?

    • Gravatar about 1 year

      This is my second site's nginx conf file and yes, it is linked to sites-avaiable and sites-enabled. http://pastebin.com/2pbYPTNn Please help me! I've had this issue for the past 2 weeks and i dont know what the problem is

    • Gravatar Brad about 1 year

      Note that the symbolic link in "sites-enabled" should use absolute target paths, not relative. Relative links won't get followed to the target and will cause that configuration file to get skipped.

    • Gravatar about 1 year

      I did use a symbolic link. When i double click the folder it takes me to the correct file in sites-available. Any other issues you think could be the cause of this?

    • Gravatar about 1 year

      For better reference, this is the first virtual host i created. http://pastebin.com/rGba8hFM All visits from the second virtual host are directed to the first one for some reason. Any more ideas on this?

    • Gravatar alexandersmith about 1 year

      Instead of creating a symbolic link you can add the file(or directory if you choose) to the nginx.conf.

    • Gravatar joequah1 12 months

      Nice guide. I have a few question. So with the above setup, I just have to provide my router IP address to the domain www.example.com and it will be directed to the correct folder? To test it locally, what IP address to provide? My router IP too? Or there will be a new IP generated? I'm still new to hosting.

    • Gravatar joequah1 12 months

      To make things clear. I would like to know how things work. Lets assume I have 2 websites name www.joequah1.com and www.joequah2.com everything setup as above. 2 different root, same port 80, and server name are joequah1.com and joequah2,com I just have to provide the same IP addressof mine, assume 123.123.123.123 to both domain. So when I browse www.joequah1.com , nginx will read the domain name joequah1.com and lead to the correct path www/joequah1.com and browse www.joequah2.com, nginx will read the domain name to the path www/joequah2.com ? Is this how it should work?

    • Gravatar Rob Dumas 12 months

      Here's a useful script for creating virtual hosts: http://www.rosehosting.com/blog/set-up-a-new-virtual-host-server-block-on-your-nginx-server/

    • Gravatar Rob Dumas 12 months

      And here's a version that's a bit more tailored for use on DO: https://gist.github.com/bitsandbooks/5498838

    • Gravatar ozan 12 months

      I add first domain and its OK. but when I try to add second domain I get this error: Restarting nginx: nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32 nginx: configuration file /etc/nginx/nginx.conf test failed

    • Gravatar ozan 12 months

      Sorry I solved. in /etc/nginx/nginx.conf : server_names_hash_bucket_size 64; # server_name_in_redirect off;

    • Gravatar bernhard.obermoser 11 months

      i have a question...if i set up a new virtual host like this: server_name secure.nattefrost.tk how do i have to config my dns to forward the subdomain to the right virtual host? also, is it possible to make one virtual host with ssl and one without?

    • Gravatar Kamal Nasser 11 months

      @bernhard.obermoser You have to create an A record with the hostname of secure and point it to your droplet's IP. As for with/without SSL, it goes like this: Non-SSL: server { listen 80; .. } SSL: server { listen 443 ssl; .. }

    • Gravatar Wayne Hiner 10 months

      Has anyone found an answer to the anonymous issue above (multiple virtual sites all going to the first site only)? I have been banging my head into this for 3 weeks. I have read the Nginx site documentation and searched using every keyword I could think of but I still am seeing only the first site and not the second. Any thoughts at all?

    • Gravatar Kamal Nasser 10 months

      @Wayne Hiner: Please pastebin all of your virtualhost files so that we're able to determine the issue. Make sure you restart nginx after every change in the configuration files.

    • Gravatar philadelphiaslick 10 months

      Why does this article refer to Apache so much? It's supposed to be about nginx...

    • Gravatar Kamal Nasser 10 months

      @philadelphiaslick: I believe it's to make it easier for people who have previously been using Apache to understand what's going on.

    • Gravatar Wayne Hiner 9 months

      @Kamal Nasser: The issue appears to have been related to my Nginx install or perhaps some OS config. I rebuilt the droplet (for other reasons not related to the Nginx issues), went through the Nginx install and setup again and it now works as it should.

    • Gravatar Kamal Nasser 9 months

      @Wayne: Awesome! Glad to hear it's working :]

    • Gravatar Dzulhelmi Jumat 9 months

      What to do if you want to host 2 websites in the same localhost using nginx? (no domain yet, just localhost)

    • Gravatar Kamal Nasser 9 months

      @Dzulhelmi: Please do NOT duplicate your questions. Thanks! https://www.digitalocean.com/community/questions/can-we-host-multiple-websites-in-nginx-at-localhost

    • Gravatar vikramchandra 8 months

      My nginx is installed inside opt/nginx folder and not etc/nginx folder. Also my nginx folder is missing sites-available folder. Folder inside my nginx folder are 1. client_body_temp 2. fastcgi_temp 3. logs 4. sbin 5. uwsgi_temp 6. conf 7. html 8. proxy_temp 9. scgi_temp Please help.

    • Gravatar Kamal Nasser 8 months

      @vikramchandra: Did you install nginx manually from source? Take a look at this article: https://blog.kamalnasser.net/post/nginx-apache-like-server-structure

    • Gravatar douglas.baroliveira 8 months

      First sorry for my english but I'm from Brazil and I'm using Google Translator. I'm having the same problem up friends. But the second site im running is directing to the first site. In other words, it seems like the root directory set for my second site is being ignored or something Site 1 www.volkoff.com.br pastebin http://pastebin.com/sGJs3LNQ Site 2 www.xshoes.com.br pastebin http://pastebin.com/g4wxy5cd I am creating files in / etc / nginx / conf.d / example.conf The Nginx 1.4 no folders sites-available and sites-enabled With only the files it created. Conf for each site, where I'm going wrong friends? I'm using Centos 6.4 Thanks

    • Gravatar Kamal Nasser 8 months

      @douglas.baroliveira: Please pastebin /etc/nginx/nginx.conf

    • Gravatar douglas.baroliveira 8 months

      Kamal http://pastebin.com/1KEqAVgb

    • Gravatar Kamal Nasser 8 months

      @douglas.baroliveira: Try restarting nginx, does that work?

      sudo service nginx restart

    • Gravatar douglas.baroliveira 8 months

      No work Kamal, the settings are right?

    • Gravatar douglas.baroliveira 8 months

      Kamal, can you help me?

    • Gravatar Kamal Nasser 8 months

      @douglas.baroliveira: I believe the problem is in

      http://pastebin.com/g4wxy5cd
      Try this instead:
      https://p.kk7.me/falebabibe.nginx
      Let me know if that works

    • Gravatar douglas.baroliveira 7 months

      Kamal decided as follows thus placing the server_name .xshoes.com.br Thanks

    • Gravatar Kamal Nasser 7 months

      @douglas.baroliveira: Did it work? :]

    • Gravatar douglas.baroliveira 7 months

      Yes,

    • Gravatar Kamal Nasser 7 months

      Great!

    • Gravatar admin 7 months

      What the fuck ?

    • Gravatar Kamal Nasser 7 months

      @admin: Is there anything I could help you with?

    • Gravatar M N Islam Shihan 7 months

      In a default installation, nginx runs with user www-data which is a member of www-data group, and this is a common standard for all web servers in ubuntu distributions (as configured in /etc/nginx/nginx.conf). So, the ownership of the public_html directory must be granted to www-data instead of www user as you described here. The "sudo chown -R www:www /var/www/example.com/public_html" should be changed to "sudo chown -R www-data:www-data /var/www/example.com/public_html" for better compliance with Ubuntu standards. Also one or two line with a reference to /etc/nginx/nginx.conf can be added, where a user can lookup for the actual user with which nginx daemon is running. Every other bits are useful & very readable. Thanks for the good & handy article.

    • Gravatar Kamal Nasser 7 months

      @M N: Thanks for writing it. The points are perfectly valid however we're not using www in place of www-data. The second paragraph states the following:

      The steps in this tutorial require the user to have root privileges on the virtual private server. You can see how to set that up in the Initial Server Setup Tutorial in steps 3 and 4. Furthermore, if I reference the user in a step, I’ll use the name www. You can implement whatever username suits you. 

    • Gravatar kris 6 months

      I'm stuck at the restart part. this is my error sudo service nginx restart Restarting nginx: nginx: [emerg] "listen" directive is not allowed here in /etc/nginx/sites-enabled/thankful.io:3 nginx: configuration file /etc/nginx/nginx.conf test failed

    • Gravatar Kamal Nasser 6 months

      @kris: Please pastebin the contents of /etc/nginx/sites-enabled/thankful.io

    • Gravatar Novaa 6 months

      I did everything according to the howto The first one worked properly. But the second, is opening the contents of the first domain. I created 2 separate files (one for each domain) and created the symbolic links inside /sites-enabled But still directing always from first domain.

    • Gravatar Kamal Nasser 6 months

      @Novaa: Did you restart nginx? Please pastebin all of your virtualhosts.

    • Gravatar Novaa 6 months

      Yes, i restart nginx. novaa.com.br -> http://pastebin.com/jead6bna novaa.in -> http://pastebin.com/e9tvEU9r The two domains are pointing at the DNS digitalocean (are configured through the menu DNS). Thanks Thanks ;)

    • Gravatar Kamal Nasser 6 months

      @Novaa: The pastes were removed. Can you please paste them to https://p.kk7.me/ and I'll delete them once we're done if you prefer :]

    • Gravatar Nicholas Ong 6 months

      Just some feedback. Most of the DO tutorials have been very well written and easy to understand. Sure, there are the one or two further questions but most of the information is. This tutorial is so vague I am not sure what I'm reading hence more confused. I have wordpress on my domain. I just want to create a sub-domain 'sub.mydomain.com'. Simple and concise steps will be nice. Or am I reading the wrong tutorial? >Step Two—Grant Permissions >We need to grant ownership of the directory to the right user, instead of just keeping it on the root system. >You can replace the "www" below with the appropriate username. >"sudo chown -R www:www /var/www/example.com/public_html" So I do "sudo chown -R USER:USER /var/www/example.com/public_html"?

    • Gravatar Kamal Nasser 6 months

      @Nicholas: This is definitely the correct article. :] Just replace example.com with sub.yourdomain.com Also make sure you replace 'USER' with your main user, see https://www.digitalocean.com/community/articles/initial-server-setup-with-ubuntu-12-04

    • Gravatar Nicholas Ong 6 months

      @Kamal, Thanks. Think I figured this one out. :) p/s: if there's a 'vote' or 'like' button for each threaded response, be cool and I would have clicked on that instead.

    • Gravatar mrgusmuller 5 months

      I suggest *EVERYONE* in server_name to have www.domain.com and domain.com... will look something like this "server_name www.domain.com domain.com;" This solved my problems of typing www.domain2.com and see domain.com etc etc...! Hope it helps anyone! Peace!

    • Gravatar droramo 4 months

      Kamal can you help me with my virtual host

    • Gravatar Kamal Nasser 4 months

      @droramo: Sure! Are you having any issues with nginx?

    • Gravatar droramo 4 months

      Well... I created my virtual host named shanime.tk, the DNS is configured, when i enter to shanime.tk redirects me to my principal website. I think something is wrong with my nginx configuration, i don't know, can you help me?

    • Gravatar droramo 4 months

      @Kamal: here is my nginx configuration for shanime.tk http://pastebin.com/sjJP9bf0

    • Gravatar Kamal Nasser 4 months

      @droramo: Replace server_name 162.243.54.219; with server_name shanime.tk 162.243.54.219;.

    • Gravatar guardian1239 4 months

      Hello! I've been struggling with this for some reason. I got it set up perfectly on my other server, but with my new server (Ubuntu 13.10), I cannot get this to work. /etc/nginx/sites-available/guardian1239.com :: http://pastebin.com/NqbBFhJ6 /etc/nginx/sites-available/myownfriend.tk :: http://pastebin.com/dKuu5vTB When I set up the main domain, guardian1239.com, it works fine! When I try to add the second one (create the file, create the link, etc.), nginx will no longer restart and, if I try restarting the server, nginx won't start up at all. I remove the myownfriend.tk version and it restarts and works again. I tried removing the myownfriend.tk files and restarting and navigating to the domain (myownfriend.tk) and it brings me to the guardian1239.com files, so I know something isn't right with the files, but I've been staring at them for an hour and I cannot figure it out. :( This is my /etc/nginx/nginx.conf (although I haven't touched it) :: http://pastebin.com/GAmqxasd

    • Gravatar Kamal Nasser 4 months

      @guardian1239:

      nginx won't start up at all
      Check the config files for errors:
      nginx -t

    • Gravatar guardian1239 4 months

      nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

    • Gravatar Kamal Nasser 4 months

      @guardian1239: Is there anything in the error logs?

      tail /var/log/nginx/error.log

    • Gravatar Joseph Abdallah 4 months

      I have been trying to figure this out for the last two days. I have two sites, eq2gu.com and buymagicboosterboxes.com, the first shows up fine, the second doesn't seem to work. I followed the directions to the T twice and it didn't work, so I went to the official site and followed the directions there, still not working. http://pastebin.com/EnMS8L6f

    • Gravatar Joseph Abdallah 4 months

      Still using the config in the paste bin above, I decided, I was going to reboot the server entirely and not just sudo service nginx restart, it now when I go to buymagicboosterboxes.com, it shows eq2gu's content.

    • Gravatar Charles Smith 4 months

      First off, I wanted to mention that while learning how to run setup and run my own server, I have found the tutorials here to be some of the highest quality...thank you; it's made learning easier. With that said, I am still a noob, or was that boob...heh who knows...and would like to ask for a little help in configuring a sub-domain. I successfully setup my Ubuntu 12.04 server running Nginx and Memcached at http://chophouse.io and loaded Ghost at the root for my blog. I would like to add a sub-domain i.e http://dojo.chophouse.io to load my client project files. Can you please provide a little "Adding a Sub-domain for Dummies" instructions? Thank you for your help and the First Class tutorials. Chuck

    • Gravatar Kamal Nasser 4 months

      @Charles: To add a sub domain or any other domain to your droplet, simply follow this article and create a virtualhost for the new domain/subdomain, e.g.: server_name dojo.chophouse.io.

    • Gravatar Joseph Abdallah 4 months

      Oddly, how I resolved my issue, is I recreated the "default" server block and put both of the servers in there, it worked, both domains are now showing the appropriate content.

    • Gravatar Charles Smith 4 months

      @Kamal: Thank you for the reply...I'll give it a go.

    • Gravatar Kamal Nasser 4 months

      @Joseph: Glad you were able to figure it out!

    • Gravatar Joseph Abdallah 4 months

      Shortly after, it just kept giving me NGINX errors, didn't actually figured out the issue.. Just created another droplet.

    • Gravatar milesmoyers 4 months

      Thanks for the great tutorial, even though I am still having issues, this is the furthest I have gotten. Here is my issue, when I restart nginx 'sudo service nginx restart' i get the following error Restarting nginx: nginx: [emerg] unknown directive "mysite.com" in /etc/nginx/sites-enabled/mysite.com:29 nginx: configuration file /etc/nginx/nginx.conf test failed "mysite.com' is set up with the digital ocean name servers and shown in my DNS tab within the digital ocean admin panel. I tried adding www.mysite.com with the same result. I commented out the line entirely and it restarts and serves the html files by just putting the droplet ip address in my browser. Any ideas?

    • Gravatar milesmoyers 4 months

      Sorry I forgot to paste the config block server { listen 80; ## listen for ipv4; this line is default and implied #listen [::]:80 default_server ipv6only=on; ## listen for ipv6 root /usr/share/nginx/www; index index.html index.htm; # Make site accessible from http://localhost/ www.mysite.com;

    • Gravatar Kamal Nasser 4 months

      @milesmoyers:

      www.mysite.com; 
      should be
      server_name www.mysite.com; 
      Restart nginx, that should fix it.

    • Gravatar guardian1239 4 months

      I figured out my problem from the error logs! After the listen 80 was "default_server" for both, so it was creating a problem. I just had to remove that and I'm good to go! I knew it was something silly that I forgot to add/remove. :P Thank you so much for your help!

    • Gravatar milesmoyers 4 months

      @kamalnasser Thanks, I missed that. Now, I get this... me@mysite:/etc/nginx/sites-available$ sudo service nginx restart Restarting nginx: nginx: [warn] conflicting server name "mysite.com" on 0.0.0.0:80, ignored nginx. I have a python script that generates my html from markdown files and I have in the past run it as a server on :8888 but then I shut it down. I wonder if that edited some file somewhere.

    • Gravatar milesmoyers 4 months

      @kamalnasser Looks like I was missing the ip address after the listen directive...I added it and it seemed restart. server { listen my.ip.on.vps:80; ## listen for ipv4; this line is default and implied #listen [::]:80 default_server ipv6only=on; ## listen for ipv6 root /home/me/www/html; index index.html index.htm; # Make site accessible from http://localhost/ server_name www.mysite.com mysite.com ; Thanks

    • Gravatar leoadhemartan 4 months

      For those who have their secondsitedomain.com redirecting to firstsitedomain.com, try to do this: server_name .seconsitedomain.com note the dot before the domain.

    • Gravatar medina592002 4 months

      I am having difficulty setting up a subdomain. I'd like to keep it in one file. Here is what I currently have. http://pastebin.com/Ah9gri1u I tried making a second server block but when I go to restart Nginx nothing seems to happen. The symbolic link appears to be in place. Any suggestions would be greatly appreciated.

    • Gravatar Kamal Nasser 4 months

      @medina592002:

      I tried making a second server block but when I go to restart Nginx nothing seems to happen
      That usually means there's a syntax error in your config, try running
      nginx -t
      .

    • Gravatar medina592002 4 months

      @Kamal Nasser Thanks! I ended up separating them but still can't access it. I am thinking it is a DNS issue. I am trying to follow these instructions. https://www.digitalocean.com/community/articles/how-to-set-up-and-test-dns-subdomains-with-digitalocean-s-dns-panel Currently when I ping my subdomain I see Unknown host. Maybe I need to give it more time to propagate.

    • Gravatar medina592002 4 months

      Okay! I got it! I ended up just reinstalling Nginx and basically started from the beginning. Thanks for the nginx -t command! That was really useful for diagnosing why Nginx wouldn't restart.

    • Gravatar Kamal Nasser 4 months

      @medina592002: Glad to hear that it's working now! :]

    • Gravatar medina592002 4 months

      @Kamal Nasser: Thanks! And thanks so much for your help!

    • Gravatar hugopozzoli 4 months

      Thank you @Kamal for your well written article. I've tried it (I'm new to nginx), but I can't get it to work. My subdomain doesn't show and my browser sends me : page doesn't exists. My domain.com is in sites-available/default and my subdomain is in sites-available/dev. Both linked to sites-enabled, the domain.com works but not the subdomain. Any idea ?

    • Gravatar Kamal Nasser 4 months

      @hugopozzoli: What's your domain name and what subdomain are you trying to browse to?

    • Gravatar fajar 4 months

      Hi Kamal, can you help me please : I setup my server for my wordpress websites using this : NGINX, PHP and also i add APC https://www.digitalocean.com/community/articles/how-to-install-wordpress-nginx-php-and-varnish-on-ubuntu-12-04 So i created two blocks for my websites but the second one always direct to my first website http://pastebin.com/8rjDR9GR = First Domain http://pastebin.com/vx0qTYVN = Second Domain already linked from my available to my enable site, then i restart the nginx, php fpm, and also restart varnish but unfortunately when i open my second site in browser, then it will direct me to my first site.... please kamal help me ,,, thank you

    • Gravatar fajar 4 months

      sorry .. it solved :) , but left only one problem :i can't access my phpmyadmin after i put my username and password, "http://manokwari.net:8080/phpmyadmin/index.php?lang=id&token=xxxxxxxxx" What should i do ?

    • Gravatar Kamal Nasser 4 months

      @fajar: What do you mean by "can't access phpmyadmin"? Do you get any errors?

    • Gravatar hugopozzoli 3 months

      @Kamal : my domain name is fiilter.com and my subdomain is dev.fiilter.com

    • Gravatar cheyingtan 3 months

      Hi Kamal, I follow the instruction closely and no error occur until the end, but when i browse the domain, it shown the message below, whats the problem? Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com. Thank you for using nginx.

    • Gravatar Kamal Nasser 3 months

      @hugopozzoli: You have to add an A record for the 'dev' subdomain: https://www.digitalocean.com/community/articles/how-to-set-up-and-test-dns-subdomains-with-digitalocean-s-dns-panel.

    • Gravatar hugopozzoli 3 months

      @Kamal: Thank you very much for your help. I can access to the files I wanted !

    • Gravatar Kamal Nasser 3 months

      Awesome!

    • Gravatar Kamal Nasser 3 months

      @cheyingtan: Did you restart nginx after creating the virtualhost?

    • Gravatar stuartjones93 3 months

      I am getting the following error when I restart nginx: sudo service nginx restart Restarting nginx: nginx [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Then it repeats the following 4 times: { nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) } nginx: [emerg] still could not bind() nginx. When I load the domain it displays the platformers.co.uk folder (inside this is public html -> index.html) Any ideas? Cheers Stu

    • Gravatar stuartjones93 3 months

      Solved it, another process was using port 80, so I killed it using: sudo fuser -k 80/tcp

    • Gravatar aalireza439 3 months

      i have a problem for adding second Server Block and when i want restart nginx it's fail. my solution was removing the default server for listen 80 in my sites-available configurations . I hope this will help .

    • Gravatar mario.taferner 2 months

      Hey there! I just tried to follow the tutorial, but I get the following error when I try to restart apache: * Restarting web server apache2 (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down Unable to open logs Action 'start' failed. The Apache error log may have more information. This is my configuration file: server { listen 80; ## listen for ipv4; this line is default and implied #listen [::]:80 default ipv6only=on; ## listen for ipv6 root /var/www/challenge42day.com/public_html; index index.html index.htm; # Make site accessible from http://localhost/ server_name challenge42day.com; } Can anyone help me, please? :/

    • Gravatar mario.taferner 2 months

      Thanks, after many hours of trying and I figured it out myself. I had to change the port of apache to 8080. And afterwards I had to change some details in the config file of nginx, so that php works again :)

    • Gravatar Jimmy Rittenborg 5 days

      If I want to install a CMS for these virtual hosts, although these clients is never going to have directly access to to server itself (through ssh or ftp/sftp) only through a file archive in the CMS, would it then still be considered a best practice to create a new user for each of these virtual hosts? Would that make it easier to keep an eye on disk usage for each of these in the future? or just complicate stuff as I'm not trying to reinvent PLESK.

    • Gravatar Asterix about 8 hours

      hmm, I only added separate .conf files for each site in /etc/nginx/conf.d like /etc/nginx/conf.d/site1.conf /etc/nginx/conf.d/site2.conf /etc/nginx/conf.d/site3.conf server { listen 80; root /home/user/public_html/site1; index index.php index.html index.htm; server_name www.site1.com site1.com; Is this wrong? It works, but does it have any disadvantage?

    • Gravatar astarr about 3 hours

      @Asterix There's nothing wrong with that. It's really only a matter of preference.

    Leave a Comment

    Create an account or login:
    Ajax-loader