Amazon Linux 2023 is a popular operating system developers use to build applications on the Amazon Web Services (AWS) cloud platform. Nginx is a powerful web server known for its high performance, scalability, and flexibility. In this tutorial, we will walk you through installing Nginx on Amazon Linux 2023.
Table of Contents
Step 1: Connect to your Amazon Linux Instance
To get started, you need to connect to your Amazon Linux instance via SSH or Session Manager. To connect via SSH run the following command
ssh -i "youkeyfile.pem" ec2-user@{instancePublicIP}
Replace “instancePublicIP” with the public IP address of your Amazon Linux instance.
Step 2: Update the Package Repository
Before installing Nginx, updating the package repository on your Amazon Linux instance is a good practice. To do this, run the following command:
sudo dnf update -y
This will update all the packages on your example to the latest version.
Step 3: Install Nginx
Next, we need to install Nginx on Amazon Linux 2023. To do this, run the following command:
sudo dnf install nginx -y
This command will install the Nginx web server and all its dependencies.
Step 4: Start Nginx Once the installation is complete, start the Nginx service using the following command:
sudo systemctl start nginx
This command will start the Nginx service on your Amazon Linux instance.
Step 5: Verify Nginx Installation
To verify that Nginx is installed and running correctly, use the following command to check the status of the Nginx service:
sudo systemctl status nginx
If Nginx is running correctly, you will see output similar to the following:
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2023-04-28 14:29:09 UTC; 1 day 8h ago
Main PID: 30676 (nginx)
CGroup: /system.slice/nginx.service
├─30676 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
├─30677 nginx: worker process
└─30678 nginx: worker process
Step 6: Enable Nginx at Boot
To ensure that Nginx starts automatically when your Amazon Linux instance boots up, use the following command:
sudo systemctl enable nginx
Setup Server Block For Nginx
Please create a server block into this path sudo vi /etc/nginx/conf.d/sameple.com.conf
server {
listen 80;
server_name gcptips.com;
root /var/www/wordpress;
index index.html;
charset UTF-8;
}
Step 7: SSL Setup
To install an SSL certificate use Letsencrypt. To do that, first, install Certbot
sudo dnf install python3 augeas-libs
Then Set up a Python virtual environment
sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip
Run this command on the machine’s command line to install Certbot.
sudo /opt/certbot/bin/pip install certbot certbot-nginx
Execute the following instructions on the command line on the machine to ensure that the certbot command can be run.
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot
Run this command to get a certificate and have Certbot edit your Nginx configuration automatically to serve it, turning on HTTPS access in a single step.
sudo certbot --nginx
To renew the Certificate automatically you need to set a cronjob via crontab. The commands are given below.
sudo dnf install cronie -y
sudo systemctl start crond.service
sudo systemctl enable crond.service
sudo crontab -e
After running this command, you will see a new blank screen for editing. Over there you need to give the commands that need to be executed and also time and free frequency.
0 3 * * * sudo certbot renew >/dev/null 2>&1
The command above will run this renewal command everyday morning at 3 AM.
That’s it! You have successfully installed Nginx on Amazon Linux 2023. You can now use Nginx to host websites, reverse proxy, or load balance traffic to multiple servers.
Conclusion
With that, you should now have a fully functional Nginx web server running on your Amazon Linux 2023 instance. We’ve covered the essential steps from installation and basic configuration to starting the service and ensuring it starts on boot. This sets the foundation for hosting websites or using Nginx as a reverse proxy or load balancer. Remember to explore Nginx’s extensive documentation to further customize your setup and leverage its powerful features for optimal web performance and security.
Pingback: How to install Node.js on Amazon Linux 2023 – AWS with Atiq