<?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: Farhaan mahbub</title>
    <description>The latest articles on DEV Community by Farhaan mahbub (@farhan_meb).</description>
    <link>https://dev.to/farhan_meb</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%2F753480%2Fe1cb98ea-7185-450b-b813-28d84ad938ca.jpg</url>
      <title>DEV Community: Farhaan mahbub</title>
      <link>https://dev.to/farhan_meb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/farhan_meb"/>
    <language>en</language>
    <item>
      <title>Host a full-stack project on AWS EC2 or Digital Ocean</title>
      <dc:creator>Farhaan mahbub</dc:creator>
      <pubDate>Wed, 03 Jan 2024 11:38:31 +0000</pubDate>
      <link>https://dev.to/farhan_meb/host-a-full-stack-project-on-aws-ec2-or-digital-ocean-1cio</link>
      <guid>https://dev.to/farhan_meb/host-a-full-stack-project-on-aws-ec2-or-digital-ocean-1cio</guid>
      <description>&lt;p&gt;In this article, I will guide you on how you can host a full-stack project on an AWS EC2 hosting cloud server.&lt;/p&gt;

&lt;p&gt;Assuming you know what a cloud server is, I will continue from the very beginning (How to create a server) to how to successfully host your website. If you are using digital ocean droplet, You may start from Step-3.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project stack for this tutorial&lt;/strong&gt;&lt;br&gt;
Front-end: ReactJS&lt;br&gt;
Back-end: NodeJS&lt;br&gt;
Database: MySQL with Prisma&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Step-1: Create an EC2 server in AWS&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Login to your AWS account. Search ec2 from services. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RW0Iyomn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tv43ad5zmvvmnlzfrudo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RW0Iyomn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tv43ad5zmvvmnlzfrudo.png" alt="Image description" width="800" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select EC2 from the list. 
Click on the Launch instance button &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ak1Vg8Me--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ie3948cbpc88ub1fh98m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ak1Vg8Me--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ie3948cbpc88ub1fh98m.png" alt="Image description" width="800" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We will use a free tier instance for this blog. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set a name for the instance and select ubuntu as your AMI. See the screenshot for ubuntu version. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ifLenC_q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yn2nactt7yd4jfsr5iya.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ifLenC_q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yn2nactt7yd4jfsr5iya.png" alt="Image description" width="800" height="554"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can create a key pair and connect to your server via ssh (optional). We will connect to our server directly from aws console for this session. Hence, we will not be creating a key pair.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lKjoqa_o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ud3dzlxzg83uav0cvhay.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lKjoqa_o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ud3dzlxzg83uav0cvhay.png" alt="Image description" width="796" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Leave other settings as it is, and click on Launch instance button&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hOsfC7Ux--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ig8sow34z9rqas9u004y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hOsfC7Ux--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ig8sow34z9rqas9u004y.png" alt="Image description" width="400" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have successfully created a cloud server. We will use this server to host our project!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Step-2: Connect to the instance&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From your instances list, enter to the instance, click on connect, and connect using EC2 Instance Connect.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0pFvkPVs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n9terh5zwyfuolhu8vm1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0pFvkPVs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n9terh5zwyfuolhu8vm1.png" alt="Image description" width="400" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will be redirected to another tab with a connected console to your instance. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F7nQu3r6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hshjjty54z0ji20j1mbq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F7nQu3r6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hshjjty54z0ji20j1mbq.png" alt="Image description" width="800" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Step-3: Set Up Your EC2 Instance&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
After connecting to your EC2 instance or digital ocean droplet, proceed with the setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update the instance for the latest packages
&lt;code&gt;sudo apt update&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once it is updated, we will install all necessary things needed for the projects. This depends on the stack you are using. For the sample project, we need to install NodeJs, MySQL, Pm2, and Nginx. Pm2 &amp;amp; nginx will be needed for all kinds of projects. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install Node.js version 14:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -&lt;br&gt;
cat /etc/apt/sources.list.d/nodesource.list&lt;br&gt;
sudo apt -y install nodejs&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install the latest MySQL, alter root user password, &amp;amp; create database:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;sudo apt install mysql-server&lt;br&gt;
sudo service mysql status&lt;br&gt;
sudo mysql&lt;br&gt;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'your_mysql_new_password';&lt;br&gt;
exit;&lt;br&gt;
sudo mysql_secure_installation&lt;br&gt;
sudo mysql -u root -p&lt;br&gt;
create database sample_db_name;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;after 'sudo mysql -u root -p' command, you will need to enter your updated password to connect to MySQL console. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up PM2 (Process Manager for Node.js):&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;sudo npm install -g pm2&lt;br&gt;
sudo pm2 startup systemd&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install Nginx:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;sudo apt-get install -y nginx&lt;br&gt;
sudo ufw allow OpenSSH&lt;br&gt;
sudo ufw allow 'Nginx Full'&lt;br&gt;
sudo ufw --force enable&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Step-4: Set Up Your Project&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Next, set up your project on the EC2 instance. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to your desired directory and create subdirectories for your project. I will move to root directory and create a folder for our project. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;cd ../../&lt;br&gt;
sudo mkdir app&lt;br&gt;
cd app&lt;br&gt;
sudo mkdir project_dir&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clone your project from your Version control&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;sudo git clone https://your_project_repo_url&lt;br&gt;
git config --global --add safe.directory /app/project_dir&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to your project directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;cd project_dir&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install dependencies and run your server and client applications (Commands depend on your project setup). You need to do everything to run the project! Following is my setup for the blog.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;cd server&lt;br&gt;
sudo npm install&lt;br&gt;
sudo npm run prisma-wohoo&lt;br&gt;
sudo npm start&lt;br&gt;
sudo pm2 start dist/index.js&lt;br&gt;
cd ../client&lt;br&gt;
sudo npm install&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create a build for your front-end&lt;br&gt;
From the client or front-end directory, run&lt;br&gt;
&lt;code&gt;Sudo npm run build&lt;/code&gt;&lt;br&gt;
This will create a fresh build for the front-end.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need to start our backend using pm2. Go to your backend directory and run pm2 start dist/index.js (depends on the your backend is set to start in production). You, then, will be able to see the running pm2 services list and there you can see the backend is actively running. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Step-4: Deploying and Configuring Nginx&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Now, let's configure Nginx for our project and deploy it!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create deployment directories from root directory and copy build files. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;cd ~&lt;br&gt;
sudo mkdir app-deploy&lt;br&gt;
cd app-deploy&lt;br&gt;
sudo mkdir front-end&lt;br&gt;
cd ../&lt;br&gt;
sudo cp -r app/project_dir/your-project/project-frontend/build/ ~/app-deploy/front-end/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;'sudo cp -r app/project_dir/your-project/project-frontend/build/' is your front-end's build folder path. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure Nginx for your project&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Navigate to Nginx's sites configuration directory:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd /etc/nginx/sites-available/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Remove the default configuration files:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo rm -r default&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Navigate to the sites-enabled directory and remove the default configuration symlink:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd ../sites-enabled/&lt;br&gt;
sudo rm -r default&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let's create and configure the Nginx server block for our front-end:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd ../sites-available/&lt;br&gt;
sudo nano frontend&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Paste the following 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;
        listen [::]:80;

        root /home/app-deploy/front-end/build;
        index index.html index.htm index.nginx-debian.html;

        server_name your_domain www.your_domain;

        location / {
                try_files $uri $uri/ =404;
        }
        location /api {
         proxy_pass http://localhost:4000;
         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;
         }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here, server_name will have the website domain that you want to use for your project, and proxy_pass &lt;a href="http://localhost:4000"&gt;http://localhost:4000&lt;/a&gt;; will basically pass any route with /api to backend (4000 is the port your project is running on). Now save the file (ctl x, y, enter)&lt;/p&gt;

&lt;p&gt;Then, we need to set up nginx for backend:&lt;br&gt;
type command : sudo nano api&lt;br&gt;
and use the following cinfiguration,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
server_name ip your_backend_domain ww.your_backend_domain;
location / {
proxy_pass http://localhost:4000;
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;
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;save the file (ctl x, y, enter)&lt;/li&gt;
&lt;li&gt;cd ../sites-enabled/&lt;/li&gt;
&lt;li&gt;sudo ln -fs ../sites-available/frontend&lt;/li&gt;
&lt;li&gt;sudo ln -fs ../sites-available/api&lt;/li&gt;
&lt;li&gt;sudo nginx -t (checks if the nginx configs are correct)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And we are done setting our project! Restart the server using the following command&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;systemctl restart nginx&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Form your instance overview, you will find Public IPv4 address. Open that address with http, you will the project is running! If the dns settings are done in you dns management of the domains you used in the nginx configuration with your instance ip, you will be able to see the project is hosted on your domain as well.  &lt;/p&gt;

&lt;p&gt;This is for today guys! We will discuss how you can add ssl certificates for your domains using certbot. In the meantime, you can checkout the following tutorial on how to add ssl certificates for domains in your instance server. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=R5d-hN9UtpU"&gt;https://www.youtube.com/watch?v=R5d-hN9UtpU&lt;/a&gt;&lt;/p&gt;

</description>
      <category>hosting</category>
      <category>aws</category>
      <category>webdev</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
