<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rose Ahmed</title>
    <description>The latest articles on DEV Community by Rose Ahmed (@roseahmed).</description>
    <link>https://dev.to/roseahmed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2480149%2F0ccb4423-7f90-4d63-8bcd-c6b7d0e0f71a.jpg</url>
      <title>DEV Community: Rose Ahmed</title>
      <link>https://dev.to/roseahmed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/roseahmed"/>
    <language>en</language>
    <item>
      <title>Deploy Vite-React Project in AWS EC2 using custom domain and free SSL Certificate.</title>
      <dc:creator>Rose Ahmed</dc:creator>
      <pubDate>Wed, 01 Jan 2025 14:15:54 +0000</pubDate>
      <link>https://dev.to/roseahmed/deploy-vite-react-project-in-aws-ec2-using-custom-domain-and-free-ssl-certificate-fj8</link>
      <guid>https://dev.to/roseahmed/deploy-vite-react-project-in-aws-ec2-using-custom-domain-and-free-ssl-certificate-fj8</guid>
      <description>&lt;p&gt;&lt;strong&gt;Requirements:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;AWS account and Configured EC2 instance. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Atleast One Domain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React Js project.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I am considering that you have all the above requirements but If you haven't already please fell free to comment so that I can write article on the above requirements or you can reference this article &lt;br&gt;
&lt;a href="https://dev.to/lanreogunlade1998/deploy-an-ec2-instance-in-aws-connect-to-it-and-install-nginx-118g"&gt;Setup AWS EC2 Instance&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Connect to EC2 instance:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;SHH Command to connect to EC2 instance:(Linux/Mac OS)
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh -i /path/to/private-key username@server_ip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Example
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh -i /Users/Downloads/privatekey.pem ubuntu@2.107.215.86
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Install Node.js and NPM
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Node.js is an open-source, cross-platform JavaScript runtime environment for developing server-side scalable applications. The Node Package Manager (NPM) is an essential tool for managing libraries, dependencies, and scripts in a Node.js project. By installing Node.js and NPM on your server, you can use JavaScript as a server-side language.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Commands for installation
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update
sudo apt install -y nodejs npm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Verify the installation
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node -v
npm -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Install Vite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Commands to install
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g vite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Verify the installation
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vite --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Install PM2
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;PM2 is ideal for managing Node.js applications in production because it minimizes downtime, optimizes resource usage, and simplifies complex tasks like monitoring and scaling. It ensures your application stays online and runs smoothly without requiring constant manual intervention.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo npm install -g pm2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Install Nginx
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is a lightweight choice that can be used as either a web server or reverse proxy.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Command to install
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install nginx -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Verify the nginx installation
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl status nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, all the dependencies have been installed, now we move to actual steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Enable UFW firewall
&lt;/h2&gt;

&lt;p&gt;First we need to active firewall and If you are using SSH to access the system, ensure you allow SSH (port 22) before enabling UFW, to avoid being locked out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Commands to allow SSH port and enable UFW
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw allow 22
sudo ufw enable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Command to check UFW status and all activated ports
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Upload project and run using PM2
&lt;/h2&gt;

&lt;p&gt;We can import the project using git commands if you are maintaining git repository or can upload the project directory to server using bash command.&lt;/p&gt;

&lt;p&gt;a). Commands to import project from git repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github-repo-link
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On success, you will asked for github username and password, follow the steps.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Instead of password you need to enter the generated token.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;b). Upload project using scp command:&lt;br&gt;
&lt;code&gt;scp -i /path/to/private/key file/folder username@server_ip:/tmp&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scp -i /Users/Download/privatekey.pem project ubuntu@2.102.23.234:/tmp 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;We can upload only to /tmp directory&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we have successfully uploaded the project in server.&lt;/p&gt;

&lt;p&gt;First we will build the project using npm script &lt;code&gt;npm run build&lt;/code&gt; and once the project is build we will run the build file using npm script but with the help of pm2.&lt;/p&gt;

&lt;p&gt;Sample Package.json&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "sample-package",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "lint": "eslint .",
    "preview": "vite preview"
  },
  "dependencies": {
    "react": "^17.0.2"
    your dependencies...
  },
  "devDependencies": {
    your dependencies...
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run npm command using pm2:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pm2 start npm --name $myProject -- run $npmScript&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pm2 start npm --name sampleProject -- run preview
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great, now we have successfully run the project in server using pm2, we can confirm that by following command &lt;code&gt;pm2 list&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Create A record in you Domain Provider
&lt;/h2&gt;

&lt;p&gt;Domain provider can be goDaddy, google domain etc&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create "A" record and save it
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Type= A 
Name= @ or subdomain 
Value= $serverIp
TTL=$default
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sample Screenshoot&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1allkkf9lvfgehupynoa.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1allkkf9lvfgehupynoa.jpg" alt="Create A Record" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Enable and Configure Nginx
&lt;/h2&gt;

&lt;p&gt;a). Enable Nginx:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;command to enable Ngnix
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl enable nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;command to start nginx
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl start nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;command to check nginx status
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl status nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b). Enable default Nginx ports using UFW:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw allow 80
sudo ufw allow 443
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will open the port 80 and 443.&lt;br&gt;
By default nginx listen to ports 80 (http) and 443 (https).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Also make sure to enable this ports 80 and 443 in EC2 instance security groups&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Command to verify
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;c). Configure Ngnix:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change the working directory to /etc/nginx/sites-available
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /etc/nginx/sites-available
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Create a file inside a directory same as your domain or subdomain, it is not mandatory that the file name should be same as domain or subdomain just make sure that extension is .conf&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo vim /etc/nginx/sites-available/example.com.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will open vim editor now copy paste the given below nginx configuration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000; # Replace 3000 with your PM2 app's 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;

        # Optional: Increase client request timeout
        client_max_body_size 100M;
    }

    error_page 404 /404.html;
    location = /404.html {
        internal;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above sample you need to modify the 2 lines of code, first is server_name this will be your domain or subdomain and the 2nd line proxy_pass change the port, that is running in server which previously we have run it through pm2. For changing these lines first we need to enable vim editor write mode by typing &lt;code&gt;i&lt;/code&gt;, once the lines are modified you need to enter &lt;code&gt;esc&lt;/code&gt; button, now type this command &lt;code&gt;:wq&lt;/code&gt; this will close and save the file. You can verify the changes by viewing the file using this command &lt;code&gt;cat example.com.conf&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;d). Enable the site configuration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a symbolic link
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Command to test the Nginx configuration
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nginx -t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Command to reload Nginx to apply the changes
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl reload nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great, We have successfully configured nginx, we will be able to access the app using domain for example &lt;a href="http://example.com" rel="noopener noreferrer"&gt;http://example.com&lt;/a&gt;, now we shall obtain SSL certificate in order to access our app using https which is safe for security purposes.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Install Certbot and Obtain Free SSL Certificate
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Command to install certbot
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install certbot python3-certbot-nginx -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Command to obtain SSL certificate
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo certbot --nginx -d example.com -d www.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt; Enable Automatic Renewal
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo certbot renew --dry-run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Test Nginx configuration
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nginx -t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Reload, Restart and check the status of Nginx
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl reload nginx
sudo systemctl restart nginx
sudo systemctl status nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great, We have successfully deployed our app in AWS EC2 instance using custom Domain over https.&lt;/p&gt;

&lt;p&gt;Fell free to ask any question if you have any doubts. &lt;/p&gt;

</description>
      <category>aws</category>
      <category>ssl</category>
      <category>ec2</category>
      <category>react</category>
    </item>
  </channel>
</rss>
