Nginx, popularly known as “ engine- x, ” is an important, open-source web server known for its effectiveness, scalability, and point set. Installing Nginx on an Amazon EC2 instance allows you to host websites and operations within the robust AWS structure.
Prerequisites
Before starting installation, you must have the following with you:
- An AWS account with access to the EC2 service.
- An existing EC2 instance runs a compatible Linux Distribution (Amazon Linux 2, Ubuntu, etc.). The instance must have SSH access enabled.
- An SSH customer like PuTTY( Windows) or the erected-in terminal( macOS/ Linux) can connect to your EC2 case.
- A basic understanding of Linux commands and file editing.
Step 1: Connect to your EC2 Instance
- Locate your Public DNS (IPv4) address: Log in to the AWS Management Console and navigate to the EC2 dashboard. Select your instance and find the “Public DNS (IPv4)” address under the “Description” tab. You will use this address to connect to your instance via SSH.
- Connect using SSH: Open your preferred SSH client. Enter the following command, replacing <username> with your instance username and <public_dns> with your obtained address.
- Enter your private key passphrase (if applicable): If you used a key pair for SSH access during instance creation, enter the key passphrase when prompted.
Step 2: Update and Upgrade the System
Maintaining an updated system is pivotal for security and stability. Run the following commands to modernize the package lists and upgrade existing packages.
Sudo yum update # For Amazon Linux 2Important: The update command used will depend on your specific Linux distribution.
Step 3: Install Nginx.
Let’s install Nginx using the appropriate package manager for your chosen distribution:
Amazon Linux 2:
sudo amazon-linux-extras install -y nginx.This command will download and install Nginx's latest and most stable version from the sanctioned depository.
Step 4: Verify Nginx Installation
Once the installation is complete, you can verify it by checking the version:
Sudo nginx -vThis command will give you the information on the installed Nginx version.
Step 5: Configure Nginx (Basic Setup)
Nginx configuration files reside in the directory/etc/nginx. The main configuration file is nginx. Conf. We will create a simple server block to serve a basic welcome page.
Create a Server Block File:
Use a text editor like Nano to create a new server block file:
Sudo nano /etc/nginx/sites-available/defaultPaste the Configuration:
With the creation of the new file, paste the following basic server block configuration:
server {
listen 80 default_server; # Listen on port 80 (HTTP)
server_name _; # Handle all requests
location / {
root /var/www/html;
index index.html index.htm;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}Explanation:
- Listen 80 default_server; Defines the port (80 for HTTP) and sets this block as the default server.
- server_name _; Handles all requests directed to the server's IP address.
- Location / { ... }: Defines a location block for the root path (/).
- Root/var/www/html: Set up the document root directory where your website files reside. You should create this directory if it doesn't exist.
- Index index.html index.htm: Specifies default index files to serve when a directory is requested.
- error_log /var/log/nginx/error.log; Defines the location for error logs.
- access_log /var/log/nginx/access.log; Defines the location for access logs, recording user requests.
Save and Close the File:
Press Ctrl+O to save the file, then Ctrl+X to exit the editor.
Enable The Server Block:
Use the following command to produce a symbolic link from the recently created server block file to the spots-enabled directory, effectively enabling it.
sudo ln -s /etc/nginx/sites-available/default/etc/nginx/sites-enabled/Step 6: Start and Manage Nginx
Restart Nginx: Once you are confident about the configuration, restart Nginx to apply the changes:
sudo systemctl restart nginx # For systemd-based systems (Amazon Linux 2)
sudo service nginx restart # For Upstart-based systems (older Ubuntu versions)Verify Nginx Status: Check the status of Nginx to ensure it is running correctly:
sudo systemctl status nginx # For system-based systems
sudo service nginx status # For Upstart-based systemsOptional: Secure Nginx with a Firewall
By default, Nginx acts on port 80 (HTTP). If you haven’t configured a firewall on your EC2 instance, it is crucial to open port 80 to allow incoming web traffic. You can achieve this using the AWS Security Groups feature.
Step 7: Serving Your Website Content
The previous steps set up Nginx to serve an introductory welcome message. To serve your website content, follow these steps:
1. Create a Document Root Directory: Create a directory to store your website files. A typical location is /var/www/html. You can use the following command to create it:
sudo mkdir -p /var/www/html2 . Upload Your Website Files: Use a secure train transfer system like SCP or SFTP to upload your website lines( HTML, CSS, JavaScript, etc.) to the document root directory(/ var/ www/ html).
3. Verify File Permissions: Ensure the web server user (usually nginx) has read permissions for your website files. You can use the chown and chmod commands to adjust permissions if necessary.
4. Reload Nginx: After uploading the files, reload the configuration and inform Nginx about the changes.
Some Additional Considerations
Virtual Hosts
You must configure virtual hosts within Nginx to host multiple websites on the same server. This allows different content to be served based on the domain name accessed.
Security
While the basic setup is a starting point, consider implementing additional security measures, such as setting up a firewall and configuring SSL/TLS certificates for HTTPS encryption.
Advanced Configuration
Nginx offers a wide range of features and configuration options. Explore the official Nginx documentation for details on load balancing, caching, and other functionalities.
This guide will provide a holistic overview of installing and configuring Nginx on your EC2 instance. Remember to adapt the configuration to your specific needs and website requirements. By following these steps and exploring further resources, you have exercised Nginx's power and flexibility to host your web applications on the AWS cloud.




