1. Launch an EC2 Instance
Steps:
- Log in to your AWS Management Console.
- Navigate to the EC2 Dashboard and click Launch Instance.
- Choose an Amazon Machine Image (AMI):
- Select Ubuntu Server 20.04 LTS.
- Choose an Instance Type:
- Use the
t2.micro
for free-tier eligibility or a size that fits your needs.
- Use the
- Configure Instance Details:
- Keep defaults for basic setup.
- Add Storage:
- Set storage size (e.g., 30GB).
- 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].
- Allow the following ports:
- Launch the instance and download the key pair for SSH access.
2. Connect to the Instance
Steps:
- Open your terminal or SSH client (e.g., PuTTY on Windows).
-
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
4. Install NVM and Node.js
Steps:
-
Install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash source ~/.bashrc
-
Install Node.js:
nvm install 18.17.0 nvm use 18.17.0
-
Verify installation:
node -v npm -v
5. Install and Configure PM2
Steps:
-
Install PM2 globally:
npm install -g pm2
-
Verify installation:
pm2 -v
-
Set up PM2 to start on reboot:
pm2 startup
-
Save the PM2 process list:
pm2 save
6. Install and Configure NGINX
Steps:
-
Install NGINX:
sudo apt install nginx -y
-
Start and enable NGINX:
sudo systemctl start nginx sudo systemctl enable nginx
-
Configure NGINX for your application:
-
Create a configuration file (e.g.,
myapp
):
sudo nano /etc/nginx/sites-available/myapp
-
- Add the following configuration:
```
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;
}
}
```
- Enable the site:
```bash
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
```
7. Install MySQL
Steps:
-
Install MySQL:
sudo apt install mysql-server -y
-
Secure MySQL installation:
sudo mysql_secure_installation
- Set a strong root password.
- Configure validation levels as required.
-
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:
```bash
sudo systemctl restart mysql
```
- Grant privileges:
```sql
GRANT ALL PRIVILEGES ON *.* TO 'your_user'@'%' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
```
8. Clone Project and Set Up
Steps:
-
Install Git:
sudo apt install git -y
-
Clone your project:
git clone https://github.com/your-repo.git /var/www/myapp
-
Navigate to the project directory:
cd /var/www/myapp
-
Install dependencies:
npm install
-
Create and configure the
.env
file:
nano .env
9. Start Application with PM2
Steps:
-
Start your application:
pm2 start ecosystem.config.js
- If you don’t have an ecosystem file:
```bash
pm2 start npm --name "myapp" -- run start
```
-
Save the PM2 process list:
pm2 save
-
Check logs:
pm2 logs
10. Configure SSL with Certbot
Steps:
-
Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
-
Obtain and configure SSL certificates:
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
-
Verify renewal:
sudo certbot renew --dry-run
11. Final Steps
-
Test your application:
curl http://localhost curl http://your-domain.com
-
Ensure all services are running:
sudo systemctl status nginx pm2 list
12. Optional: GitHub Actions for CI/CD
- Set up a
.github/workflows/deploy.yml
file. - Use SSH or FTP to automate deployments.
- 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.
Top comments (0)