You read about what is NGINX and its advantages were before this page. On this page, we give some configuration steps for Node.js project deployment in NGINX with AWS.
1. Create Free AWS Accountfirst of all you should create free AWS Account. Amazon Web Services (AWS) is a comprehensive cloud computing platform provided by Amazon.com. Launched in 2006, AWS offers a wide array of cloud services that cater to businesses, individuals, and organizations, allowing them to access and utilize computing power, storage, and various other resources over the internet. Here we give some steps to create an AWS account
Go to the AWS Free Tier Page : Open your web browser and go to the AWS Free Tier page AWS Free Tier
Amazon Elastic Compute Cloud (EC2) is a web service provided by Amazon Web Services (AWS) that allows users to run virtual servers in the cloud. EC2 instances provide scalable compute capacity, allowing you to easily scale up or down based on your application requirements. Here's an overview of key concepts and steps related to Amazon EC2. Steps to Launch an EC2 Instance:
Remember to terminate instances when you no longer need them to avoid unnecessary charges. EC2 offers various features and options for customization, making it a versatile solution for hosting applications in the cloud.
3. Connecting to an EC2 InstanceTo connect to an Amazon EC2 instance running Linux, especially for a Node.js project, you'll typically use SSH (Secure Shell). Here's a step-by-step guide assuming you already have an EC2 instance set up:
chmod 400 /path/to/your/keypair.pem
ssh -i /path/to/your/keypair.pem ec2-user@your-instance-ip
Connect to your EC2 instance using SSH, and then install Node.js. You can use a package manager like `yum` for Amazon Linux or `apt` for Ubuntu
For Amazon Linux:
sudo yum install -y nodejs
For Ubuntu:
sudo apt update
sudo apt install -y nodejs
You can use various methods to transfer your Node.js project to the EC2 instance. The most common two methods are scp (secure copy) and cloning your project from GitHub.You use only any one method
1. secure copy (scp)
scp -i /path/to/your/keypair.pem -r /path/to/your/local/project ec2-user@your-instance-ip:/path/on/ec2
2. Clone your project from Github
If Git is not already installed on your EC2 instance, you can install it using the package manager. For example, for Amazon Linux:
If Git is not already installed on your EC2 instance, you can install it using the package manager.
For Amazon Linux:
sudo yum install -y git
For Ubuntu:
sudo apt update
sudo apt install -y git
After Clone the GitHub Repository, Navigate to the directory where you want to clone the GitHub repository and use the git clone command. Replace repository-url with the URL of the GitHub repository:
git clone repository-url
For example:
git clone https://github.com/username/repo.git
Navigate to your project directory and install dependencies. Start your Node.js application.
npm install
npm start
Before this page we covered about `pm2`.You might want to use a process manager like pm2 to keep your application running:
npm install -g pm2
pm2 start your-app.js
Open the necessary ports in the firewall. Here we give some example commands for enable Firewall, check status of firewall and allow specific ports
sudo ufw enable
sudo ufw status
sudo ufw allow ssh (Port 22)
sudo ufw allow http (Port 80)
sudo ufw allow https (Port 443)
Before this page, we explained in detail about Firewall commands. Those commands will help you understand about firewall Please check this link Basic FireWall Commands
9. Install NGINX and configureTo install NGINX and configure it for a Node.js project, follow these general steps. Keep in mind that specifics might vary based on your operating system and Node.js application setup.
sudo apt install nginx
sudo nano /etc/nginx/sites-available/myapp
server {
listen 80;
server_name your_domain_or_server_ip;
location / {
proxy_pass http://localhost:your_node_app_port;
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;
}
}
Replace `your_domain_or_server_ip` with your actual domain or server IP.Replace `your_node_app_port` with the port where your Node.js application is running.
sudo nginx -t
sudo nginx -s reload
To add SSL to your NGINX setup using Let's Encrypt, you can use the Certbot tool. Certbot is an easy-to-use tool that automates the process of obtaining and renewing SSL certificates. Here are the steps to add SSL with Let's Encrypt to your NGINX setup:
1. Install Certbot
sudo apt update
sudo apt install certbot
2. Obtain SSL Certificate
Run the following command to obtain an SSL certificate for your domain
sudo certbot --nginx -d your_domain_or_server_ip
Follow the interactive prompts to configure Certbot. It will automatically detect your NGINX setup and ask for your email address for renewal notifications.
2. Automatically Renew Certificates
Certbot will automatically set up a cron job to renew your certificates. However, it's a good practice to test the renewal process manually.If the dry run succeeds, it means the automatic renewal process is set up correctly.
sudo certbot renew --dry-run
Follow the interactive prompts to configure Certbot. It will automatically detect your NGINX setup and ask for your email address for renewal notifications.
2. Test Your SSL Configuration
Restart NGINX to apply the changes.
sudo systemctl reload nginx
Visit your site using `https://your_domain_or_server_ip` in a web browser to verify that SSL is working correctly.your NGINX server is configured with SSL using a Let's Encrypt certificate. Certbot will automatically handle the renewal of your SSL certificates, providing a secure connection to your Node.js application.
This guide provides a basic setup. Adjust configurations, security settings, and NGINX parameters based on your specific project requirements. Ensure your security groups and firewall rules allow traffic on ports 80 and any other ports your Node.js application uses.