Search code examples
amazon-web-servicesnginxamazon-ec2amazon-linux-2

How to install NGINX on AWS EC2 Linux 2


I'm new to AWS and trying to understand which version of NGINX I should be installing on my instance. I've found multiple options;

  • Via EPEL as the blog entry
  • Amazon's own (?) version as this answer
  • The 2016 NGINX official tutorial

On my development environment (Centos VM) I used sudo yum install nginx. Having tried the EPEL route I don't get the same set up, in particular sites enabled/available is not created as part of the setup. I want to use nginxconfig.io which requires those. Which version of NGINX should i use for that?


Solution

  • I'd personally use Amazon's own repo.

    The version provided by the Amazon repo is relatively old (1.12.2 at the time of writing). To see what versions the Amazon repo has access to run

    amazon-linux-extras list | grep nginx
    

    If you'd like a later version, consider EPEL.

    In regards to the config, your best bet is to explicitly supply the configuration you require to the server.

    Using the off-the-peg ones are fine to get you up and running. However you run the risk of things changing when Nginx updates. Explicitly supplying your own configuration gives you greater control over what is running.

    Probably the simplest approach would be to upload the configuration generated by nginxconfig.io to S3.

    Then add a script via user data when creating the EC2 instance to download your configuration.

    https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html

    Something like this...

    #!/bin/bash
    
    # Install Nginx
    amazon-linux-extras install nginx1.12
    
    # Back up existing config
    mv /etc/nginx /etc/nginx-backup
    
    # Download the configuration from S3
    aws s3 cp s3://{my_bucket}/nginxconfig.io-example.com.zip /tmp
    
    # Install new configuration
    unzip /tmp/nginxconfig.io-example.com.zip -d /etc/nginx
    

    The configuration supplied by nginxconfig.io sets up all the sites enabled/available for you.