安装 EC2 上的 nginx 的备忘录

安装Nginx到EC2。

请确认已安装amazon-linux-extras仓库。

$which amazon-linux-extras
/usr/bin/amazon-linux-extras

如果未安装

$ sudo yum install -y amazon-linux-extras

确认可安装的nginx软件包

$amazon-linux-extras | grep nginx
38  nginx1                   available    [ =stable

安装Nginx

$sudo amazon-linux-extras install nginx1 -y

//インストールされたか確認
$nginx -v
nginx version: nginx/1.22.0

//自動起動
$ sudo systemctl enable nginx

//起動・停止・再起動・ステータス
$ sudo systemctl start nginx
$ sudo systemctl stop nginx
$ sudo systemctl restart nginx
$ sudo systemctl status nginx

浏览器确认

スクリーンショット 2022-11-08 14.22.35.png

nginx的配置文件

我会查看设置文件。

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

我尝试编辑index.html文件。

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>ホゲーーーーーー</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

スクリーンショット 2022-11-08 14.49.43.png

我们来编辑配置文件吧。

将以下内容以中文进行同义转述,只需要一种选项:
将 /etc/nginx/nginx.conf中的内容拷贝至其他位置。

charset UTF-8;

加上这个。

因为修改了设置文件,所以需要重新加载。

$sudo systemctl reload nginx
スクリーンショット 2022-11-08 14.54.36.png

看起来变更已经被反映出来了,似乎没有问题。

最后

我在 nginx 的配置文件中并没有明确地指定 index.html 作为索引,但是我发现实际上 index.html 被加载了。

结束了。

bannerAds