Create an EC2 Instance and Install LAMP Stack on AWS : Step By Step

LAMP Stack on AWS

Share This Post

Introduction

LAMP Stack on AWS: Welcome, aspiring cloud enthusiasts! Today, we’re diving into the world of Amazon Web Services (AWS) to unlock the secrets of creating an EC2 instance and setting up a LAMP (Linux, Apache, MySQL, PHP) stack. Whether you’re a seasoned developer or just starting your journey into the cloud, this comprehensive tutorial will guide you through every step of the process on Creating an EC2 Instance and Installing LAMP Stack.

Introduction to AWS EC2 Instances

Amazon Elastic Compute Cloud (EC2) provides scalable computing capacity in the AWS cloud. EC2 instances are virtual servers that you can launch and manage according to your computing needs. With EC2, you have full control over your virtual computing environment, including the choice of operating system, instance type, and security settings.

Creating an EC2 Instance and Installing LAMP Stack

Step 1: Sign Up for AWS and Access the Management Console

Before diving into EC2, you’ll need an AWS account. If you haven’t already, sign up for an AWS account here, and log in to the AWS Management Console.

Step 2: Navigate to EC2 Dashboard

Once logged in, navigate to the EC2 dashboard by selecting “Services” from the top navigation menu and then clicking on “EC2” under the “Compute” section.

Step 3: Launch Your EC2 Instance

Now, let’s launch your EC2 instance:

  1. Click on the “Launch Instance” button.
  2. Choose an Amazon Machine Image (AMI). For this tutorial, we’ll use the Amazon Linux 2023 AMI (Free tier Eligible), which comes pre-configured with the necessary tools for LAMP stack setup.
  3. Select an Instance Type. Choose an instance type based on your computing needs. For testing purposes, a t2.micro instance is sufficient.
  4. Configure Instance Details. Leave the default settings or customize as needed.
  5. Add Storage. Configure the storage settings for your instance.
  6. Add Tags (Optional). Add tags to organize your instances.
  7. Configure Security Group. Create a new security group and configure inbound rules to allow traffic on ports 22 (SSH), 80 (HTTP), and 443 (HTTPS).
  8. Review Instance Launch. Review your instance configuration and click “Launch.”

Screenshots

Step 4: Create and Download a Key Pair

Before launching your instance, you’ll need to create and download a key pair to securely connect to your EC2 instance via SSH.

  1. Choose “Create a new key pair” from the dropdown menu.
  2. Enter a name for your key pair and click “Download Key Pair.” Save the .pem file to a secure location on your local machine.

Step 5: Connect to Your EC2 Instance

Once your instance is launched, it’s time to connect to it via SSH:

  1. Open your terminal or SSH client.
  2. Navigate to the directory where your key pair (.pem) file is saved.
  3. Set permissions for your key pair file using the following command:
  1. Connect to your EC2 instance using the following command:

Replace “your-key-pair.pem” with the name of your key pair file and “your-instance-public-ip” with the public IP address of your EC2 instance.

Step 6: Install LAMP Stack

To connect to your instance through ssh, the default user id is: “ec2-user”. You can also enable “root” user for your instance and reset the password if you don’t want to use key file to login**. (Not recommended, un-secure). Now that you’re connected to your EC2 instance, let’s install the LAMP stack:

  1. Update the package repository:
  1. Install Apache web server:
  1. Start the Apache service:
  1. Enable Apache to start on boot:
  1. Install MySQL server:
  1. Start the MySQL service:
  1. Secure MySQL installation:
  1. Install PHP and necessary modules:
  1. Restart the Apache service to apply changes:

Congratulations! You’ve successfully created an EC2 instance and installed the LAMP stack on AWS. You’re now ready to deploy your web applications and websites in the cloud.

Install phpMyAdmin

phpMyAdmin is a free and open-source tool written in PHP intended to handle the administration of MySQL or MariaDB with the use of a web browser. Let’s install phpMyAdmin on your EC2 instance:

  1. Install the EPEL repository:
  1. Install phpMyAdmin using the following command:
  1. During the installation process, you will be prompted to configure phpMyAdmin. Select “apache” as the web server.
  2. After installation, open the phpMyAdmin configuration file for editing:
  1. Locate the lines containing “Require ip” and “Require ip6” and replace them with the following lines to allow access from any IP address:
  1. Save and exit the file (press Ctrl + X, then press Y, and finally press Enter).
  2. Restart the Apache service to apply the changes:
  1. Access phpMyAdmin in your web browser by navigating to http://your-instance-public-ip/phpmyadmin. Replace your-instance-public-ip with the public IP address of your EC2 instance.
  2. Log in to phpMyAdmin using your MySQL root credentials.

Congratulations! You’ve successfully installed phpMyAdmin on your EC2 instance. You can now use phpMyAdmin to manage your MySQL databases via a user-friendly web interface.

s5 2
Get your server IP address

Additional Notes

  • Remember to secure your phpMyAdmin installation by restricting access to specific IP addresses or enabling authentication.
  • Regularly update phpMyAdmin and other software packages to patch any security vulnerabilities and ensure optimal performance.

With phpMyAdmin installed, you have a powerful tool at your disposal for database management on your AWS EC2 instance. Happy database administration!

Now point your domain to your server’s IP address. You can upload files and scripts or install wordpress by connecting to your server using WinSCP

Note: As you can register your domain from any registrar, it is always recommended to use Cloudflare for better speed, security and ease of access. Just signup for a free cloudflare account, add your domain, follow instructions and point your domains nameserver (from your domain registrar client portal) to cloudflare’s provided name server, Now add a “A” record to your aws server ip address. that’s it.

Extra Essentials

Install Important PHP Modules

To enhance the functionality of your PHP environment, let’s install some important PHP modules on your EC2 instance:

  1. Install the required PHP modules using the following command:
  1. After installation, restart the Apache service to apply the changes:

*If you need to install additional PHP modules for specific functionality or requirements, you can do so using the following steps:

  1. Identify Required PHP Modules: Determine the additional PHP modules you need based on the requirements of your web applications. Common PHP modules include extensions for database connectivity (e.g., PDO MySQL), image processing (e.g., GD), and encryption (e.g., OpenSSL).
  2. Install PHP Modules: Use the yum package manager to install the desired PHP modules. For example, to install the PDO MySQL extension, you can use the following command:

Replace <php-module> with the package name of the PHP module you wish to install.

  1. Restart Apache: After installing the PHP modules, restart the Apache service to apply the changes:
  1. Verify Installation: You can verify that the PHP modules have been installed successfully by creating a PHP file with the phpinfo() function and accessing it in your web browser. For example:

Save this code in a file named info.php in your web server’s document root directory (e.g., /var/www/html). Then, access http://your-instance-public-ip/info.php in your web browser to view the PHP information page. Look for the newly installed modules in the list of PHP modules.

  1. Repeat for Additional Modules: If you need to install multiple PHP modules, repeat the installation process for each module using the yum command.

By following these steps, you can easily install additional PHP modules on your EC2 instance to meet the requirements of your web applications.

Congratulations! You’ve successfully installed important PHP modules on your EC2 instance, enabling additional features and capabilities for your web applications.

Increase PHP Default Limits

To accommodate larger file uploads and execute longer scripts, let’s increase the default PHP limits on your EC2 instance:

  1. Open the PHP configuration file for editing:
  1. Locate the following settings and adjust them accordingly:

Modify the values as needed based on your requirements.

  1. Save and exit the file (press Ctrl + X, then press Y, and finally press Enter).
  2. Restart the Apache service to apply the changes:

Step 10: Enable Gzip Compression

Enabling Gzip compression can significantly reduce the size of your web pages and improve loading times for your visitors. Let’s enable Gzip compression on your Apache server:

  1. Open the Apache configuration file for editing:
  1. Add the following lines to enable Gzip compression:
  1. Save and exit the file (press Ctrl + X, then press Y, and finally press Enter).
  2. Restart the Apache service to apply the changes:

Step 11: Enable Memcache Module for Caching

Memcached is a distributed memory caching system that can improve the performance of your web applications by caching frequently accessed data. Let’s enable the Memcache module for PHP:

  1. Install the Memcached server and PHP Memcache extension:
  1. Start the Memcached service and enable it to start on boot:
  1. Restart the Apache service to load the PHP Memcache extension:

Congratulations! You’ve successfully installed and configured important PHP modules, increased PHP default limits, enabled Gzip compression, and enabled the Memcache module for caching on your EC2 instance. Your web applications are now optimized for performance and scalability.

Conclusion

In this tutorial, we’ve covered the essential steps to create an EC2 instance and set up a LAMP stack on AWS with extra Modules and settings. By mastering these skills, you’ll be well-equipped to harness the power of cloud computing for your projects and applications. Keep exploring AWS and experimenting with different services to unleash your full potential in the cloud.

Happy coding, and may your deployments be seamless!

Share this post:

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

LAMP Stack on AWS

Share This Post

Share this post:

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Do You Want To Boost Your Business?

drop us a line and keep in touch

small_c_popup.png

Share your details to letus know about your needs!

Let's have a chat