Here’s a step-by-step guide for installing and configuring Apache on an EC2 instance running Ubuntu.
1. Launch an EC2 Instance
- Sign in to your AWS Management Console.
- Go to the EC2 Dashboard.
- Click on Launch Instance to create a new EC2 instance.
- Choose an Ubuntu AMI (e.g., Ubuntu 22.04 LTS).
- Select an appropriate Instance Type (e.g., t2.micro for low-cost, basic usage).
- Configure instance details, add storage if needed, and configure a security group.
- Make sure to allow HTTP (port 80) and SSH (port 22) access in the security group.
- Launch the instance and download the Key Pair for SSH access.
2. Connect to the EC2 Instance
- Once the instance is running, get its public IP or DNS name.
- Use SSH to connect to the instance (replace
your-key.pemandyour-public-dnswith your own values):
3. Update System Packages
- Update your package list and upgrade existing packages:
sudo apt update sudo apt upgrade -y
4. Install Apache Web Server
- Install Apache2 using the package manager
sudo apt install apache2 -y
5. Start and Enable Apache Service
- Start Apache to run it on your instance:
sudo systemctl start apache2
- Enable Apache to start automatically on boot:
sudo systemctl enable apache2
6. Verify Apache Installation
- Open a web browser and enter the public IP address or DNS name of your EC2 instance.
- You should see the Apache2 default page saying “Apache2 Ubuntu Default Page.”
Alternatively, check Apache status from the command line:
sudo systemctl status apache2
7. Configure Firewall (if applicable)
If you are using ufw (Uncomplicated Firewall), ensure that Apache is allowed through the firewall:
- Check the status of UFW:
sudo ufw status
- If UFW is enabled, allow Apache traffic:
sudo ufw allow 'Apache Full'sudo ufw enable sudo ufw status
8. Configure Apache (Optional)
- Apache’s configuration files are located in
/etc/apache2/. The main configuration file is/etc/apache2/apache2.conf. - If you want to set up virtual hosts (e.g., to serve multiple websites), you can modify or add files in
/etc/apache2/sites-available/. Example: Edit the default configuration file:sudo nano /etc/apache2/sites-available/000-default.conf
- For other custom configurations, like enabling mod_rewrite or SSL, you can use:
sudo a2enmod rewrite sudo a2enmod ssl
- To apply changes to the configuration, restart Apache:
sudo systemctl restart apache2
9. Secure Apache (Optional)
- Install SSL: If you need to serve content over HTTPS, you can install SSL certificates.
- First, install Certbot
sudo apt install certbot python3-certbot-apache -y
- Then, obtain an SSL certificate for your domain:bashCopy code
sudo certbot --apache - Follow the prompts to configure SSL for your domain.
- First, install Certbot
10. Test and Manage Apache
- Test your Apache configuration for syntax errors:
sudo apache2ctl configtest
- To restart or reload Apache after making changes:
sudo systemctl restart apache2
- To stop Apache:
sudo systemctl stop apache2
- To disable Apache from starting at boot:
sudo systemctl disable apache2