且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在AWS EC2 Linux 2上安装NGINX

更新时间:2022-11-07 13:21:24

我个人将使用亚马逊自己的存储库.

I'd personally use Amazon's own repo.

Amazon存储库提供的版本相对较旧(在撰写本文时为1.12.2).要查看亚马逊存储库可以运行哪些版本

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

如果您想要更高的版本,请考虑使用EPEL.

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.

使用现成的工具可以使您正常运行.但是,当Nginx更新时,您会面临发生变化的风险.明确提供您自己的配置可让您更好地控制正在运行的内容.

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.

可能最简单的方法是将nginxconfig.io生成的配置上载到S3.

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

然后在创建EC2实例以下载配置时通过用户数据添加脚本.

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

nginxconfig.io提供的配置设置为您启用/可用的所有站点.

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