Here is a reconstructed step-by-step guide based on the general best practices for a beginner EC2 deployment.
Step 1: Set Up Your EC2 Instance (The Virtual Server)
This step involves provisioning a virtual machine, known as an instance, to host your web application.
Log in to the AWS Management Console: Access your AWS account.
Launch a New EC2 Instance: Navigate to the EC2 service under the Compute section and click the "Launch Instance" button.
Choose an AMI (Amazon Machine Image): Select the operating system for your server. For beginners, Amazon Linux 2 or Ubuntu are popular and great choices.
Select an Instance Type: Choose the
t2.microinstance type, as it's typically included in the AWS Free Tier.Configure Instance Details & Storage: You can accept most defaults. Ensure Auto-assign Public IP is set to "Enable" so the server is accessible from the internet. The default 8 GB of storage is generally sufficient.
Configure Security Group (The Firewall): Create a new security group and add rules to allow incoming traffic for the following ports:
SSH (Port 22): To securely connect to and manage your server.
HTTP (Port 80): To serve non-secure web traffic.
HTTPS (Port 443): To serve secure web traffic.
Launch Your Instance: Click "Review and Launch." When prompted, create a new key pair (a
.pemfile). Download and save this file securely, as it is the only way to log in to your server via SSH.
Step 2: Connect to Your EC2 Instance
Once your instance is running, you need to connect to it to install your application code and necessary software.
Retrieve Connection Information: In the EC2 console, select your running instance and find its Public IPv4 address.
Connect via SSH: Use your terminal (Linux/macOS) or an SSH client (Windows) with the key pair you downloaded.
Change key permissions (required for Linux/macOS):
chmod 400 /path/to/your/keypair.pemRun the SSH command:
ssh -i /path/to/your/keypair.pem ec2-user@<Your-Public-IP>(Note:ec2-useris common for Amazon Linux; useubuntufor Ubuntu AMIs).
Step 3: Install Web Server and Application Dependencies
After successfully connecting, you will prepare the environment for your web application.
Update the System: Run the package manager update command (e.g.,
sudo yum update -yorsudo apt update -y).Install a Web Server: Install a server application like Apache or Nginx to handle web requests.
Example for Apache (Amazon Linux):
sudo yum install httpd -y
Start and Enable the Web Server: Ensure the service is running and configured to start automatically upon reboot.
Example for Apache:
sudo systemctl start httpdandsudo systemctl enable httpd
Install Application Runtimes: Install the required language runtime for your application (e.g., Node.js, Python, PHP, or Java).
Step 4: Deploy Your Application Code
The final step is to transfer your application files and start the service.
Transfer Files: Use a secure file transfer protocol like SCP or SFTP to copy your local application files into the web root directory on your EC2 instance (e.g.,
/var/www/html/for Apache).Configure and Run: Set up any required environment variables or database connections. If your application needs a process manager (like PM2 for Node.js), configure it to keep your app running continuously.
Test: Open a web browser and navigate to your EC2 instance's Public IPv4 address. You should see your web application running.
No comments:
Post a Comment