DEV Community

Cover image for Step-by-Step Guide to Setting Up an AWS EC2 Instance and Configuring Server
Divyanshu
Divyanshu

Posted on

Step-by-Step Guide to Setting Up an AWS EC2 Instance and Configuring Server

1. Launch an EC2 Instance

Steps:

  1. Log in to your AWS Management Console.
  2. Navigate to the EC2 Dashboard and click Launch Instance.
  3. Choose an Amazon Machine Image (AMI):
    • Select Ubuntu Server 20.04 LTS.
  4. Choose an Instance Type:
    • Use the t2.micro for free-tier eligibility or a size that fits your needs.
  5. Configure Instance Details:
    • Keep defaults for basic setup.
  6. Add Storage:
    • Set storage size (e.g., 30GB).
  7. Configure Security Group:
    • Allow the following ports:
      • SSH (Port 22)
      • HTTP (Port 80)
      • HTTPS (Port 443)
      • MySQL/Aurora (Port 3306) [if needed for remote database access].
  8. Launch the instance and download the key pair for SSH access.

2. Connect to the Instance

Steps:

  1. Open your terminal or SSH client (e.g., PuTTY on Windows).
  2. Connect using the following command:

    ssh -i /path/to/key.pem ubuntu@your-instance-ip
    
    

3. Update the Server

Commands:

sudo apt update && sudo apt upgrade -y

Enter fullscreen mode Exit fullscreen mode

4. Install NVM and Node.js

Steps:

  1. Install NVM:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
    source ~/.bashrc
    
    
  2. Install Node.js:

    nvm install 18.17.0
    nvm use 18.17.0
    
    
  3. Verify installation:

    node -v
    npm -v
    
    

5. Install and Configure PM2

Steps:

  1. Install PM2 globally:

    npm install -g pm2
    
    
  2. Verify installation:

    pm2 -v
    
    
  3. Set up PM2 to start on reboot:

    pm2 startup
    
    
  4. Save the PM2 process list:

    pm2 save
    
    

6. Install and Configure NGINX

Steps:

  1. Install NGINX:

    sudo apt install nginx -y
    
    
  2. Start and enable NGINX:

    sudo systemctl start nginx
    sudo systemctl enable nginx
    
    
  3. Configure NGINX for your application:

    • Create a configuration file (e.g., myapp):

      sudo nano /etc/nginx/sites-available/myapp
      
      
- Add the following configuration:
Enter fullscreen mode Exit fullscreen mode
    ```
    server {
        listen 80;
        server_name your-domain.com;

        location / {
            proxy_pass http://127.0.0.1:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }

    ```
Enter fullscreen mode Exit fullscreen mode
- Enable the site:
Enter fullscreen mode Exit fullscreen mode
    ```bash
    sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl restart nginx

    ```
Enter fullscreen mode Exit fullscreen mode

7. Install MySQL

Steps:

  1. Install MySQL:

    sudo apt install mysql-server -y
    
    
  2. Secure MySQL installation:

    sudo mysql_secure_installation
    
    
- Set a strong root password.
- Configure validation levels as required.
Enter fullscreen mode Exit fullscreen mode
  1. Allow remote access (optional):

    • Edit MySQL configuration:

      sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
      
      
- Set `bind-address = 0.0.0.0`.
- Restart MySQL:
Enter fullscreen mode Exit fullscreen mode
    ```bash
    sudo systemctl restart mysql

    ```
Enter fullscreen mode Exit fullscreen mode
- Grant privileges:
Enter fullscreen mode Exit fullscreen mode
    ```sql
    GRANT ALL PRIVILEGES ON *.* TO 'your_user'@'%' IDENTIFIED BY 'your_password';
    FLUSH PRIVILEGES;

    ```
Enter fullscreen mode Exit fullscreen mode

8. Clone Project and Set Up

Steps:

  1. Install Git:

    sudo apt install git -y
    
    
  2. Clone your project:

    git clone https://github.com/your-repo.git /var/www/myapp
    
    
  3. Navigate to the project directory:

    cd /var/www/myapp
    
    
  4. Install dependencies:

    npm install
    
    
  5. Create and configure the .env file:

    nano .env
    
    

9. Start Application with PM2

Steps:

  1. Start your application:

    pm2 start ecosystem.config.js
    
    
- If you don’t have an ecosystem file:
Enter fullscreen mode Exit fullscreen mode
    ```bash
    pm2 start npm --name "myapp" -- run start

    ```
Enter fullscreen mode Exit fullscreen mode
  1. Save the PM2 process list:

    pm2 save
    
    
  2. Check logs:

    pm2 logs
    
    

10. Configure SSL with Certbot

Steps:

  1. Install Certbot:

    sudo apt install certbot python3-certbot-nginx -y
    
    
  2. Obtain and configure SSL certificates:

    sudo certbot --nginx -d your-domain.com -d www.your-domain.com
    
    
  3. Verify renewal:

    sudo certbot renew --dry-run
    
    

11. Final Steps

  1. Test your application:

    curl http://localhost
    curl http://your-domain.com
    
    
  2. Ensure all services are running:

    sudo systemctl status nginx
    pm2 list
    
    

12. Optional: GitHub Actions for CI/CD

  1. Set up a .github/workflows/deploy.yml file.
  2. Use SSH or FTP to automate deployments.
  3. Store secrets in GitHub and trigger workflows on push events.

This comprehensive guide ensures your server and application are set up, secured, and running effectively.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay