Table of contents
To set up and run a Python HTTP server on your private EC2 instance after logging in, follow these detailed steps:
Step 1: Verify Python is Installed
Check Python Version:
Run the following command to check if Python3 is installed:
python3 --version
If Python3 is not installed, you’ll see an error or no output. Proceed to the next step to install it.
Step 2: Install Python3 (If Not Installed)
For Amazon Linux 2 or Amazon Linux AMI:
Update the System Packages:
sudo yum update -y
Install Python3:
sudo yum install python3 -y
For Ubuntu:
Update the System Packages:
sudo apt update
Install Python3:
sudo apt install python3 -y
For RHEL/CentOS:
Enable EPEL Repository:
sudo yum install epel-release -y
Install Python3:
sudo yum install python3 -y
Verify Installation:
After installation, verify the version again:
python3 --version
Step 3: Start the Python HTTP Server
Navigate to the Desired Directory:
Use the
cd
command to go to the directory where you want to serve files:cd /path/to/directory
For example, if you want to serve files from
/var/www/html
:cd /var/www/html
For testing , you can create simple html file in ubuntu server itself by following below steps:
1. Connect to Your EC2 Instance
Use SSH to connect to your EC2 instance:
ssh -i /path/to/aws_login.pem ubuntu@<your-ec2-public-ip>
2. Create and Edit the index.html
File Using vi
Once logged in, use the following command to create and open the
index.html
file for editing:sudo vi /var/www/html/index.html
- The
sudo
is needed to write to/var/www/html
if you're using a web server like Apache or Nginx, which usually has restricted write permissions for regular users.
- The
3. Enter Insert Mode and Add HTML Content
In
vi
, you need to enter insert mode to start editing:- Press
i
to enter insert mode.
- Press
Now, add your HTML content. Example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Welcome to My Server</title> </head> <body> <h1>Welcome to my EC2 instance!</h1> <p>This is a test index.html file created directly on the server.</p> </body> </html>
4. Save and Exit vi
To save and exit:
Press
Esc
to exit insert mode.Type
:wq
(write and quit) and press Enter.
This will save the changes to index.html
and exit vi
.
Start the HTTP Server:
Run the Python HTTP server on port 8000:
python3 -m http.server 8000
Keep the Server Running:
By default, this command runs the server interactively. To keep it running in the background, use
nohup
:nohup python3 -m http.server 8000
This prevents the server from stopping if you close the SSH session.
Step 4: Access the Server
Private IP:
If you are testing the server internally from another machine in the same VPC, access it using the private IP of the instance:
http://<private-ip>:8000
Public Access (Optional):
If you need external access, ensure:
The EC2 instance’s security group allows inbound traffic on port 8000.
The Bastion host or a public-facing load balancer is configured to forward traffic to this private instance.
Step 5: Stop the HTTP Server
Find the Process:
Identify the process running the server:
ps aux | grep http.server
Kill the Process:
Use the
kill
command with the process ID (PID):kill <PID>
Example:
kill 12345
Security Considerations
Restrict Access:
- Limit access to the server by updating the instance’s security group to allow inbound traffic on port 8000 only from trusted IPs.
Avoid Serving Sensitive Files:
- Ensure the directory being served does not contain sensitive files or credentials.
Use a Firewall or Reverse Proxy:
- For public access, consider setting up a reverse proxy (e.g., Nginx) or a firewall to protect the server.
SSL/TLS:
- For production use, do not use the built-in Python server. Use a robust web server (e.g., Apache, Nginx) with SSL/TLS encryption.
Example Commands for Quick Setup
Install Python3 (Amazon Linux 2):
sudo yum update -y sudo yum install python3 -y
Start the HTTP server:
cd /var/www/html python3 -m http.server 8000
Test the server from a browser or curl
:
curl http://<private-ip>:8000
"Thank you for reading! I hope this blog sparked new ideas and insights. If you have questions or thoughts, drop a comment below. Until next time, keep learning and growing!"
Reach out to me at www.linkedin.com/in/sruthipalle
Happy Coding😊