How to install Nginx on an Amazon EC2 instance

To install Nginx on an Amazon EC2 instance running a Linux-based operating system, you can follow these general steps. Note that the specific commands may vary slightly depending on the Linux distribution you are using.

1. Connect to your EC2 instance:

Use SSH to connect to your EC2 instance. Replace your-instance-ip with your actual instance’s public IP address, and your-key.pem with the path to your private key file:

2. Update the package repository:

Update the package repository to ensure you have the latest information about available packages:

3. Install Nginx:

For Amazon Linux or Amazon Linux 2, you can install Nginx using the following command:

If you are using a different Linux distribution, you may use the distribution’s package manager. For example, on Ubuntu/Debian, you can use:

4. Start Nginx:

5. Enable Nginx to start on boot:

Ensure Nginx starts automatically when the server boots:


6. Test Nginx:

Open a web browser and enter your EC2 instance’s public IP address in the address bar. You should see the default Nginx welcome page.

That’s it! You’ve successfully installed Nginx on your Amazon EC2 instance. Remember to configure Nginx according to your specific requirements, such as setting up server blocks (virtual hosts) or securing your server with SSL certificates if needed.