Skip to content

Instantly share code, notes, and snippets.

@suhailvs
Created June 5, 2015 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suhailvs/1228d0ba71bbbc1b2b34 to your computer and use it in GitHub Desktop.
Save suhailvs/1228d0ba71bbbc1b2b34 to your computer and use it in GitHub Desktop.
How To Set Up nginx Virtual Hosts on Ubuntu

taken from here

1: Install nginx:

sudo apt-get install nginx

2: 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.

mkdir ~/nginx_html

3: Create the Page

We need to create a new file called index.html within the directory we made earlier.

vi nginx_html/index.html

We can add some text to the file so we will have something to look

<h1>Success: You Have Set Up a Virtual Host</h1>

4: Set Up the Virtual Hosts

create an nginx config file:

sudo vi /etc/nginx/sites-available/nginx_html

add these few lines:

server {
    listen 4000; ## listen for ipv4; this line is default and implied
    #listen [::]:80 default ipv6only=on; ## listen for ipv6
    root /home/<user>/nginx_html;
    index index.html index.htm;
    # Make site accessible from http://localhost/
    # server_name localhost;
}

5: Activate the Host

Activate the host by create a symbolic link between the sites-available directory and the sites-enabled directory (in apache, the command to accomplish this is a2ensite, but nginx does not have an equivalent shortcut):

sudo ln -s /etc/nginx/sites-available/nginx_html /etc/nginx/sites-enabled/nginx_html

6: Restart nginx

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

sudo service nginx restart

6: See Your Virtual Host in Action

Visit http://localhost:4000 in your browser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment