How to install NGINX on AWS EC2 Linux 2
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.
Alternative way to install that could be easier (has a fairly recent version of Nginx):
$ sudo amazon-linux-extras list | grep nginx
38 nginx1=latest disabled [ =stable ]
$ sudo amazon-linux-extras enable nginx1
38 nginx1=latest enabled [ =stable ]
Now you can install:
$ sudo yum clean metadata
$ sudo yum -y install nginx
$ nginx -v
nginx version: nginx/1.16.1