Photo by Chris Ried on Unsplash

Install nginx on to AWS EC2 / Lightsail Amazon Linux 2

Eika Chiu
3 min readMar 8, 2024

--

In the Lightsail when creating a linux box, you have several Amazon Linux version to choose from. This time I am creating a second Linux node which fully represent the currently running linux node which is Amazon Linux 2 (not Amazon Linux 2023). If you’re using Amazon Linux 2023, in order to install nginx you can refer to “Install nginx on Amazon Linux 2023” I published a bit earlier.

1. Confirming the nginx module from Amazon Linux Extra

$ amazon-linux-extras list | grep nginx

You’ll probably get the results like below.

[ec2-user@ip-172-26-11-240 ~]$ amazon-linux-extras list | grep nginx
38 nginx1 available [ =stable ]

You can use the following command to see what does nginx1 mean.

$ amazon-linux-extras info nginx1
nginx1 recommends nginx # yum install nginx

2. Install the nginx using Amazon Linux Extras

$ sudo amazon-linux-extras install nginx1

You probably will see the screen similar as below.

[ec2-user@ip-172-26-11-240 ~]$ sudo amazon-linux-extras install nginx1
Installing nginx
Failed to set locale, defaulting to C
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Cleaning repos: amzn2-core amzn2extra-docker amzn2extra-nginx1
12 metadata files removed
4 sqlite files removed
0 metadata files removed
Failed to set locale, defaulting to C
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core | 3.6 kB 00:00:00
amzn2extra-docker | 2.9 kB 00:00:00
amzn2extra-nginx1 | 2.9 kB 00:00:00
(1/7): amzn2-core/2/x86_64/group_gz | 2.7 kB 00:00:00
(2/7): amzn2-core/2/x86_64/updateinfo | 817 kB 00:00:00
(3/7): amzn2extra-nginx1/2/x86_64/primary_db | 55 kB 00:00:00
(4/7): amzn2extra-docker/2/x86_64/primary_db | 109 kB 00:00:00
(5/7): amzn2extra-nginx1/2/x86_64/updateinfo | 3.0 kB 00:00:00
(6/7): amzn2extra-docker/2/x86_64/updateinfo | 15 kB 00:00:00
(7/7): amzn2-core/2/x86_64/primary_db | 72 MB 00:00:01
Resolving Dependencies
...
Dependencies Resolved

==========================================================================================================================
Package Arch Version Repository Size
==========================================================================================================================
Installing:
nginx x86_64 1:1.22.1-1.amzn2.0.3 amzn2extra-nginx1 55 k
Installing for dependencies:
gperftools-libs x86_64 2.6.1-1.amzn2 amzn2-core 274 k
nginx-core x86_64 1:1.22.1-1.amzn2.0.3 amzn2extra-nginx1 559 k
nginx-filesystem noarch 1:1.22.1-1.amzn2.0.3 amzn2extra-nginx1 25 k
openssl11-libs x86_64 1:1.1.1g-12.amzn2.0.20 amzn2-core 1.4 M
openssl11-pkcs11 x86_64 0.4.10-6.amzn2.0.1 amzn2-core 61 k

Transaction Summary
==========================================================================================================================
Install 1 Package (+5 Dependent packages)

Total download size: 2.4 M
Installed size: 6.7 M
Is this ok [y/d/N]:

After you typing “y” and press “return”, you will get nginx installed.

Installed:
nginx.x86_64 1:1.22.1-1.amzn2.0.3

Dependency Installed:
gperftools-libs.x86_64 0:2.6.1-1.amzn2 nginx-core.x86_64 1:1.22.1-1.amzn2.0.3
nginx-filesystem.noarch 1:1.22.1-1.amzn2.0.3 openssl11-libs.x86_64 1:1.1.1g-12.amzn2.0.20
openssl11-pkcs11.x86_64 0:0.4.10-6.amzn2.0.1

Complete!

And the prompt will also display a summary of enabled modules. It probably has the following line.

 38  nginx1=latest            enabled      [ =stable ]

3. Start nginx and make it a service starting with the server reboot

Use “sudo systemctl start nginx”, “sudo systemctl status nginx”, to start or check the status of nginx service.

The prompts probably outputs something like below.

[ec2-user@ip-172-26-11-240 ~]$ sudo systemctl start nginx
[ec2-user@ip-172-26-11-240 ~]$ sudo systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2024-03-08 08:17:35 UTC; 10s ago
Process: 2050 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 2047 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2045 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 2053 (nginx)
CGroup: /system.slice/nginx.service
├─2053 nginx: master process /usr/sbin/nginx
├─2054 nginx: worker process
└─2055 nginx: worker process

Mar 08 08:17:35 ip-172-26-11-240.ap-southeast-2.compute.internal systemd[1]: Starting The nginx HTTP and reverse prox.....
Mar 08 08:17:35 ip-172-26-11-240.ap-southeast-2.compute.internal nginx[2047]: nginx: the configuration file /etc/ngin...ok
Mar 08 08:17:35 ip-172-26-11-240.ap-southeast-2.compute.internal nginx[2047]: nginx: configuration file /etc/nginx/ng...ul
Mar 08 08:17:35 ip-172-26-11-240.ap-southeast-2.compute.internal systemd[1]: Started The nginx HTTP and reverse proxy...r.
Hint: Some lines were ellipsized, use -l to show in full.

To make nginx a system service, run the following command. You will see the symlink is created as the output.

$ sudo systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

You can double check that nginx is really configured as a service by running the following command. It should prompts “enabled” if the service is enabled.

[ec2-user@ip-172-26-11-240 ~]$ systemctl is-enabled nginx
enabled

--

--