Amazon Web Services (AWS) provides adaptable distributed computing arrangements, and Amazon Elastic Compute Cloud (EC2) is one of its most popular services. EC2 permits clients to send off virtual servers, called instances, to run applications on AWS's cloud infrastructure. This cycle incorporates setting up the EC2 instance, connecting with it, installing Apache2, and designing the server for ideal execution and security.Understanding Apache 2 on EC2 InstanceUnderstanding Apache2 on an EC2 instance includes understanding the crucial standards of web server usefulness and the cloud foundation provided by AWS. Apache2 is a strong, open-source web server programming that powers many world sites. It is famous for its adaptability, security, and broad support for different highlights and modules, making it an optimal decision for both basic and complex web applications. By introducing Apache2 on an EC2 example, you influence the force of Apache's web-serving capacities close by the adaptability, dependability, and worldwide reach of AWS's cloud framework. This mix empowers engineers and framework chairmen to convey exceptionally accessible, performant, and secure web applications rapidly and effectively.Running Apache2 on an EC2 instance requires a comprehension of both Apache's design and the services of AWS EC2 examples. Apache2 is designed through text documents, where commands control everything from the server's presentation to its security settings. These designs can be tweaked to suit the particular requirements of your web application. Then again, dealing with an EC2 case includes errands, for example, checking framework execution, guaranteeing security through firewalls and security gatherings, and scaling assets depending on the situation to deal with changing web traffic degrees. Together, these abilities empower you to send a strong web server climate on AWS that can deal with an extensive variety of web facilitating needs, from serving static substance to running powerful web applications with complex back-end handling.Prerequisites for Installing Apache 2AWS account.Basic knowledge of Linux command-line interface.SSH client (like PuTTY for Windows or terminal for macOS/Linux).Launching an EC2 InstanceTo launch an EC2 instance, follow these steps:Sign in to AWS Management Console: Explore the EC2 Dashboard.Launch Instance: Click "Launch Instance" and select an Amazon Machine Image (AMI). For this aid, use the "Amazon Linux 2 AMI (HVM), SSD Volume Type."Pick Instance Type: Select the "t2.micro" case, essential for the AWS Free Tier. Click "Next: Configure Instance Details."Configure Instance Details: Default settings are adequate. Click "Next: Add Storage."Add Storage: The default storage setup is typically sufficient. If the size is fundamental, increment it. Click "Next: Add Labels."Add Labels: Add a tag to recognize your instance, e.g., Key: Name, Worth: Apache2-Server. Click "Next: Design Security Gathering."Arrange Security Groups: Make another security bunch or select a current one.Add rules to allow SSH (port 22) and HTTP (port 80) traffic:Type: SSH, Convention: TCP, Port Reach: 22, Source: Anyplace (0.0.0.0/0).Type: HTTP, Convention: TCP, Port Reach: 80, Source: Anyplace (0.0.0.0/0).Review and Launch:Review your settings and click "Launch."Select a current key pair or make another one to connect with your instance securely.Acknowledge that you can access the chosen vital pair by clicking "Launch Instances."Connecting to the EC2 InstanceAfter launching your instance, you need to connect to it using SSH.Open your SSH client: Locate the key pair file (with a .pem extension) you specified during the instance launch.Change permissions: Change the permissions of the key pair file to be read-only:chmod 400 your-key-pair.pemConnect to your instance: Use the public DNS name provided in the EC2 Dashboard:ssh -i your-key-pair.pem ec2-user@your-instance-public-dnsInstalling Apache2Once connected to your EC2 instance, follow these steps to install Apache2.Update the package index: Ensure you have the latest information about the available packages:sudo yum update -yInstall Apache2: Use the following command:sudo yum install httpd -yStart and Enable Apache2:Start the Apache2 service:sudo systemctl start httpd Enable Apache2 to start on boot:sudo systemctl enable httpdConfiguring Apache2Customizing the Default Web PageThe default web page is located at /var/www/html/index.html. You can edit this file to customize your web page:sudo nano /var/www/html/index.htmlSetting Up Virtual HostsTo host multiple websites on a single server, set up virtual hosts:Create a configuration file: For each site in the /etc/httpd/conf.d/ directorysudo nano /etc/httpd/conf.d/your-domain.confAdd the following configuration<VirtualHost *:80> ServerAdmin webmaster@your-domain.com DocumentRoot /var/www/your-domain ServerName your-domain.com ErrorLog /var/log/httpd/your-domain-error.log CustomLog /var/log/httpd/your-domain-access.log combined </VirtualHost>Create the document root directory. Set the appropriate permissions: sudo mkdir /var/www/your-domainsudo chown -R ec2-user:ec2-user /var/www/your-domainSecuring Apache2Configuring the FirewallEnsure the firewall allows traffic on port 80:sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --reloadEnabling HTTPS with SSL/TLSInstall the mod_ssl module.sudo yum install mod_ssl -yObtain a free SSL certificate from Let's Encrypt: Using Certbotsudo yum install certbot python2-certbot-apache -y sudo certbot --apache -d your-domain.comTesting the Apache2 ServerOpen a web browser: Navigate to your EC2 instance's public DNS name or IP address. You should see the default Apache2 web page.If you configured a virtual host: Navigate to your domain to see your custom web page.Setting up an Apache2 web server on an AWS EC2 instance is straightforward. It includes launching the instance, interfacing with it, introducing Apache2, and designing the server. Following the methods shown in this guide, you can quickly create a strong and secure web server on AWS. This arrangement gives areas of strength for facilitating sites and applications, and that's only the tip of the iceberg, utilizing the versatility and unwavering quality of the AWS framework.How to install Apache with Ansible?If you want to install Apache with Ansible, you can create a playbook that will define the task.PrerequisitesAnsible is installed on your control node (the machine from which you will run the Ansible playbook).The target nodes (servers) you want to configure must be accessible via SSH.Ensure that Python is installed.Step 1: Create a playbook file named install_apache.yml--- - name: Install and configure Apache hosts: webservers become: yes tasks: - name: Update the apt package index apt: update_cache: yes - name: Install Apache apt: name: apache2 state: present - name: Ensure Apache is running service: name: apache2 state: started enabled: yes - name: Copy custom index.html to Apache web root copy: src: /path/to/local/index.html dest: /var/www/html/index.html mode: '0644'Step 2: Create an inventory file named hosts.ini to specify the target servers:[webservers] server1.example.com server2.example.comStep 3: Run the Playbookansible-playbook -i hosts.ini install_apache.ymlHow to install Apache on EC2 using Terraform?Step 1: Create a Directory for Your Terraform FilesStep 2: Create the Main Configuration File "main.tf"provider "aws" { region = "us-west-2" } resource "aws_instance" "web" { ami = "ami-0c55b159cbfafe1f0" # Update with a suitable AMI ID instance_type = "t2.micro" tags = { Name = "Apache-Server" } user_data = <<-EOF #!/bin/bash apt-get update -y apt-get install apache2 -y systemctl start apache2 systemctl enable apache2 EOF key_name = "your-key-pair-name" # Update with your key pair name } resource "aws_security_group" "web_sg" { name_prefix = "web-sg" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { from_port = 22Step 3: Initialize Terraformterraform initStep 4: Apply the Configurationterraform applyBonus: Useful Commands for ApacheCommandDescriptionsudo systemctl start apache2Start the Apache servicesudo systemctl stop apache2Stop the Apache servicesudo systemctl restart apache2Restart the Apache servicesudo systemctl reload apache2Reload Apache configuration without stopping the servicesudo systemctl status apache2Check the status of Apachesudo systemctl enable apache2Enable Apache to start on bootsudo systemctl disable apache2Disable Apache from starting on bootsudo apache2ctl configtestTest Apache configuration file syntaxsudo apache2ctl -t -D DUMP_MODULESList loaded Apache modulessudo journalctl -u apache2View the Apache's systemd journal logssudo tail -f /var/log/apache2/error.logView the Apache error logsudo tail -f /var/log/apache2/access.logView the Apache access logsudo a2ensite example.com.confEnable a virtual host configuration file (replace example.com.conf with your configuration file)sudo a2dissite example.com.confDisable a virtual host configuration filesudo apache2ctl gracefulGracefully restart Apache (reloads configuration without dropping connections)Read Morehttps://devopsden.io/article/how-to-check-your-gitlab-versionFollow us onhttps://www.linkedin.com/company/devopsden/