Skip to content

Instantly share code, notes, and snippets.

@kosyfrances
Last active August 19, 2017 19:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosyfrances/27dd777f86e59b23482e to your computer and use it in GitHub Desktop.
Save kosyfrances/27dd777f86e59b23482e to your computer and use it in GitHub Desktop.
How To SetUp NGINX VirtualHosts on Ubuntu 14.04

#Set up nginx virtual host on Ubuntu 14.04

In this example, we will create a website called devops.com and create site to publish "Hello World!"
We are assuming you have nginx installed. If not, follow the steps here to install nginx .   We want to make the necessary directories in /var/www folder.

sudo mkdir -p /var/www/devops.com/httpdocs

And we want to write Hello world! in Index.html file.

sudo echo "Hello World!" > /var/www.devops.com/httpdocs/index.html

Next is to set up proper file permissions, so that Nginx web server can access it. This is with the assumption that we are running Nginx with its default user www-data.

sudo chown -R www-data:www-data /var/www/devops.com
sudo chmod -R 755 /var/www/devops.com

Now we need to create virtual host configuration file for our domain.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/devops.com.conf

And we need to edit the new virtual host configuration file.
In the end, ignoring the commented out lines, we should have something like this -

server {
    listen   80; 

    root /var/www/devops.com/httpdocs;
    index index.html index.htm;

    server_name devops.com www.devops.com;
}

Now we need to enable our server blocks by creating symbolic links from these files to the sites-enabled directory, which Nginx reads from during startup.

sudo ln -s /etc/nginx/sites-available/devops.com.conf /etc/nginx/sites-enabled/devops.com.conf

Now let us check that there were no errors in our nginx configuration by doing nginx -t.
You should see this if there are no errors.

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

Now you can restart nginx service using the following command -

sudo service nginx restart

Now go to your computer's /etc/hosts file to add your domain to route to your nginx ip address.

sudo vi /etc/hosts

Add this entry -

xx.xx.xx.xx  devops.com 

xx.xx.xx.xx is your nginx ip address.
Now you can visit devops.com and you will see the Hello World.

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