Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Setting Up NGINX

DZone's Guide to

Setting Up NGINX

This blog post will help you get NGINX up and running on Ubuntu Linux on Amazon Web Services (AWS), an easy-to-use setup that doesn’t affect the settings on your personal computer.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

Since its introduction more than a decade ago, NGINX has become the most popular web server for busy websites. More than 160 million websites use NGINX, including more than half of the top 100,000 websites.

With NGINX having reached such popularity, it makes sense to use NGINX for all your web serving needs, and additionally to take advantage of its capabilities as a reverse proxy server, caching server, load balancer, and more.

This blog post will help you get NGINX up and running on Ubuntu Linux on Amazon Web Services (AWS), an easy-to-use setup that doesn’t affect the settings on your personal computer. This blog post covers both NGINX and NGINX Plus.

Once you complete the steps given here, you may want to continue with two instructor-led courses offered by NGINX:

  • NGINX Fundamentals. Covers installation, configuration, load balancing, reverse proxying, and caching. On completion, you can take the NGINX Fundamentals Exam to check your understanding.
  • NGINX Advanced. Helps you address more complicated application scenarios for advanced location routing, load balancing, dynamic reconfiguration, monitoring, and more. Includes both open source NGINX features and features specific to NGINX Plus.

These courses will help ready you for Video Streaming with NGINX, Load Balancing in NGINX, Proxy and Caching in NGINX, and others.

This tutorial covers how to install NGINX on an Ubuntu 14.04 AWS virtual machine. For this tutorial, you need a terminal to connect to your Amazon Elastic Compute Cloud (EC2) instance. Macs have a built-in terminal, but Windows users need to download PuTTY or an equivalent tool. If you prefer to use your own local terminal to set up NGINX or NGINX Plus and serve web content, feel free to skip “Getting to your terminal”.

Getting to Your Terminal (Mac):

On a Mac, there is a built-in tool that you can use to do everything in this tutorial — it’s called “terminal.”

  1. In the top right of your screen, click on the magnifying glass icon.
  2. Type in “terminal” and hit enter. That’s all you need!

Getting to Your Terminal (Windows):

Unlike Mac, Windows doesn’t come with a built-in tool, so you will need to download one off the Internet. A good tool to use is PuTTY; it’s a common SSH client used ubiquitously throughout the world.

Setting up Amazon Web Services

To begin, you set up Amazon Web Services, then connect to your new instance:

  1. Navigate to https://aws.amazon.com/?nc2=h_lg.
  2. Sign in to AWS or create a new account.
  3. In the top left of the screen: navigate to Services, to Compute, then to EC2.
  4. Click Launch Instance.
  5. Select Ubuntu Server 14.04 LTS (HVM), SSD Volume Type.
  6. Select Free Tier Eligible t2.micro Instance. (Should be default).
  7. Navigate to Edit Security Groups.
  8. Click Add Rule to allow all TCP Traffic With Anywhere as the source.
  9. Click Review and Launch, then click Launch.
  10. Select create a new key pair and name the key pair anything, such as “NGINX_key”.
  11. Download the key pair and place it in any folder. For this tutorial, we recommend you place it in /Desktop/NGINX.
  12. Select Launch Instance, then View Instances.
  13. Highlight your instance in AWS, then select Connect at the top of the screen.
  14. Follow the directions to finish connecting to your instance (Including connect using PuTTY if you’re on Windows). This includes pasting the example code into your terminal once you’ve navigated to your key.

Now that you have your AWS virtual machine, it’s time to install and configure NGINX! There are two methods available to you when installing NGINX. You can install from the binary distribution (option 1 below), or you can request a free trial from our website (option 2 below). Both options will work in the context of this tutorial, but if you want to further explore the advanced features that NGINX can provide, please feel free to request a free trial

Option 1: Installing NGINX

Now that you have your AWS virtual machine, It’s time to install and configure NGINX! Follow these steps:

  1. Open your terminal.
  2. To download the NGINX signing key, type:
    sudo wget http://nginx.org/keys/nginx_signing.key
  3. Navigate to /etc/apt
  4. Open the sources.list file and append: 
    deb http://nginx.org/packages/ubuntu trusty nginx
    deb-src http://nginx.org/packages/ubuntu trusty nginx
  5. To update NGINX, type: 
    sudo apt-get update
  6. To install NGINX, type: 
    sudo apt-get install nginx
  7. Type Y when prompted.

Next, go to "Opening your web page" below.

Option 2: Installing NGINX Plus

  1. If you don’t already have NGINX Plus, then you can sign up for a 30-day free trial at https://www.nginx.com/products/.
  2. Once you have access to the key and cert, follow the instructions on the website on how to activate NGINX Plus on your machine. The instructions should be located here under the “instructions” hyperlink:
  3. NGINX Plus free trial activation page with subscription certificate and key
  4. Once NGINX Plus is installed on your machine, make sure by using “nginx -v.”

Now that we’ve finished starting NGINX, it’s time to see the web page that comes default before we continue our configuration.

Opening Your Web Page

Follow these steps to open the default web page:

  1. Navigate to the Amazon AWS web page that contains your list of instances. Look for your Public DNS Address in the description and copy it.
  2. Paste the Public DNS Address into a new tab. This is what it should look like:

The welcome page confirms that NGINX is up and running

Now that we know we have a working version of NGINX installed, it’s time to put it to good use! Let’s begin by setting up some files and directories.

Setting Up Sample Files

  1. Make sure you are in your root directory.
  2. Create a new folder named public_html.
  3. Inside the new folder, create a file, index.html.
  4. Inside the folder, create another folder named application1.
  5. Inside the new folder, create a file named app1.html and write some text in it.
  6. Navigate to your root directory.
  7. Create a folder named data.
  8. Within the new folder, create another folder named images.

Our first use case for NGINX will be to serve pages and images to the user via our web page.

Serving Pages and Images

  1. Open /etc/nginx/conf.d.
  2. Rename default.conf to default.conf.bak.
     NGINX will no longer use the renamed file.
  3. Create a file named server1.conf.
  4. Insert this text: 
    Server {
        Root /home/ubuntu/public_html;
    
        Location /application1 {
        }
    
        Location /images {
        Root /home/ubuntu/data;
        }
    }
  5. Navigate to ~/data/images.
  6. Find the link to an image, for instance: https://assets.wp.nginx.com/wp-content/themes/nginx-theme/assets/img//logo.png
  7. Type: 
    sudo wget https://assets.wp.nginx.com/wp-content/themes/nginx-theme/assets/img//logo.png
  8. Note the name of the file in the directory.
  9. Reload nginx by typing: 
    sudo nginx -s reload
  10. Open your web page and reload. The page should have changed.
  11. Also, try inputting into your browser: 
    server-URL/application1/app1.html 
    And:
    server-URL/images/

Setting Up a Proxy Server

Now that we have a working NGINX web server that can serve content, it’s time to learn how to configure it to route traffic. This capability allows the system administrator to push traffic through to other servers and is a major step towards setting up load balancing. Follow these steps:

  1. Create a folder called server2 inside of /data
  2. Inside the server2 folder, create another folder called sampleApp.
  3. Inside the new folder, create a file named index.html and write some text in it.
  4. Navigate to /etc/nginx/conf.d.
  5. Create a new file called server2.conf.
  6. Inside “server2.conf,” write this text: 
    Server {
            Listen 8080;
            Root /home/ubuntu/data/server2;
    }
  7. Modify server1.conf as follows: 
    Server {
        Root /home/ubuntu/public_html;
    
        Location /application1 {
            Proxy_pass http://localhost:8080/sampleApp;
        }
    
        Location /images {
            Root /home/ubuntu/data;
        }
    }
  8. Reload NGINX by executing this command: 
    sudo nginx -s reload
  9. Open server-URL/application1 in your browser.
  10. Observe what has changed.

So that’s it! You now have a working Linux instance running NGINX, which is ready to run as a proxy server.

Conclusion

In this post, we learned how to serve files and images over the internet, as well as configure it as a proxy. If you have any questions about the tutorial, we have a fundamentals training course that goes more in-depth into this material. The course also covers some of the essentials in web server and application development, such as load balancing, location routing, and security. 

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
amazon web services ,load balancing ,amazon elastic compute ,nginx plus ,services ,elastic ,nginx ,amazon ,aws

Published at DZone with permission of Aidan Carson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}