Table of Contents
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:
- Click on the “Launch Instance” button.
- 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.
- Select an Instance Type. Choose an instance type based on your computing needs. For testing purposes, a t2.micro instance is sufficient.
- Configure Instance Details. Leave the default settings or customize as needed.
- Add Storage. Configure the storage settings for your instance.
- Add Tags (Optional). Add tags to organize your instances.
- Configure Security Group. Create a new security group and configure inbound rules to allow traffic on ports 22 (SSH), 80 (HTTP), and 443 (HTTPS).
- 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.
- Choose “Create a new key pair” from the dropdown menu.
- 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:
- Open your terminal or SSH client.
- Navigate to the directory where your key pair (.pem) file is saved.
- Set permissions for your key pair file using the following command:
chmod 400 your-key-pair.pem
- Connect to your EC2 instance using the following command:
ssh -i your-key-pair.pem ec2-user@your-instance-public-ip
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:
- Update the package repository:
sudo yum update -y
- Install Apache web server:
sudo yum install httpd -y
- Start the Apache service:
sudo systemctl start httpd
- Enable Apache to start on boot:
sudo systemctl enable httpd
- Install MySQL server:
sudo yum install mysql-server -y
- Start the MySQL service:
sudo systemctl start mysqld
- Secure MySQL installation:
sudo mysql_secure_installation
- Install PHP and necessary modules:
sudo yum install php php-mysql -y
- Restart the Apache service to apply changes:
sudo systemctl restart httpd
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:
- Install the EPEL repository:
sudo amazon-linux-extras install epel
- Install phpMyAdmin using the following command:
sudo yum install phpmyadmin -y
- During the installation process, you will be prompted to configure phpMyAdmin. Select “apache” as the web server.
- After installation, open the phpMyAdmin configuration file for editing:
sudo nano /etc/httpd/conf.d/phpMyAdmin.conf
- Locate the lines containing “Require ip” and “Require ip6” and replace them with the following lines to allow access from any IP address:
Require all granted
- Save and exit the file (press
Ctrl + X
, then pressY
, and finally pressEnter
). - Restart the Apache service to apply the changes:
sudo systemctl restart httpd
- Access phpMyAdmin in your web browser by navigating to
http://your-instance-public-ip/phpmyadmin
. Replaceyour-instance-public-ip
with the public IP address of your EC2 instance. - 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.
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:
- Install the required PHP modules using the following command:
sudo yum install php-gd php-xml php-mbstring php-json -y
- After installation, restart the Apache service to apply the changes:
sudo systemctl restart httpd
*If you need to install additional PHP modules for specific functionality or requirements, you can do so using the following steps:
- 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).
- 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:
sudo yum install <php-module> -y
Replace <php-module>
with the package name of the PHP module you wish to install.
- Restart Apache: After installing the PHP modules, restart the Apache service to apply the changes:
sudo systemctl restart httpd
- 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:
<?php
phpinfo();
?>
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.
- 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:
- Open the PHP configuration file for editing:
sudo nano /etc/php.ini
- Locate the following settings and adjust them accordingly:
upload_max_filesize = 500M
post_max_size = 500M
max_execution_time = 1200
Modify the values as needed based on your requirements.
- Save and exit the file (press
Ctrl + X
, then pressY
, and finally pressEnter
). - Restart the Apache service to apply the changes:
sudo systemctl restart httpd
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:
- Open the Apache configuration file for editing:
sudo nano /etc/httpd/conf/httpd.conf
- Add the following lines to enable Gzip compression:
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
- Save and exit the file (press
Ctrl + X
, then pressY
, and finally pressEnter
). - Restart the Apache service to apply the changes:
sudo systemctl restart httpd
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:
- Install the Memcached server and PHP Memcache extension:
sudo yum install memcached php-pecl-memcache -y
- Start the Memcached service and enable it to start on boot:
sudo systemctl start memcached
sudo systemctl enable memcached
- Restart the Apache service to load the PHP Memcache extension:
sudo systemctl restart httpd
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!