GitHub - aws-samples/ec2-lamp-server: Provision Apache or Nginx web server EC2 instance with PHP and MySQL, MariaDB or PostgreSQL. Includes option to install WordPress or Moodle (original) (raw)

EC2-LAMP-Server

AWS CloudFormation template to provision Amazon EC2 web server with Apache/Nginx, PHP and MySQL/MariaDB/PostgreSQL.

Description

LAMP is an acronym for the operating system, Linux; the web server, Apache; the database server, MySQL (or MariaDB); and the programming language, PHP. It is a common open source web platform for many of the web's popular applications. Variations include LEMP which replaces web server with Nginx, LAPP which replaces database server with PostgreSQL, and LEPP which uses Nginx and PostgreSQL. According to W3Techs more than 70% of websites use PHP.

This repo provides CloudFormation templates to provision EC2 instances with option to specify PHP version, web server engine and database engine. The instances can be used for software development or deployment of PHP based web applications, and include options to install WordPress or Moodle.

Architecture Diagram

image

Overview of features

The template provides the following features:

Notice

Although this repository is released under the MIT-0 license, its CloudFormation template uses features fromMySQL Community Edition and Webmin which are licensed under GPL and BSD-3-Clause license respectively. Usage of Amazon DCV indicates acceptance of DCV EULA.

By using the template, you accept license agreement of all software that is installed in the EC2 instance.

Requirements

Deploying using CloudFormation console

Download .yaml file for the desired operating system (Amazon Linux 2, Amazon Linux 2023 or Ubuntu/Ubuntu Pro)

Login to AWS CloudFormation console. Choose Create Stack, Upload a template file, Choose File, select your .YAML file and choose Next. Enter a Stack name and specify parameters values.

EC2 Instance

EC2 Network

EC2 Remote Administration

SSH, DCV and Webmin inbound access are restricted to ingressIPv4 and ingressIPv6 IP prefixes.

LAMP

#When stack has been provisioned, open a browser to continue WordPress or Moodle web based installation. The database name, user and password to use can be located in /home/ec2-user/database-credentials (AL2023) or /home/ubuntu/database-credentials (Ubuntu)

Others

EBS

Application Load Balancer (ALB)

Select a subnet even if enableALB is No

ALB HTTPS listener

The above options only apply if enableALB is Yes

Amazon CloudFront

AWS Backup

CloudFormation Outputs

The following are available on Outputs section

If installDCV is Yes

If installWebmin is Yes

If enableALB is Yes

If enableCloudFront is Yes

Troubleshooting

To troubleshoot any installation issue, you can view contents of the following log files (if available)

PHP performance configuration

Based on public articles about PHP performance (many thanks to the authors), the following enhancements were made:

Obtaining certificate for HTTPS

Amazon CloudFront (enableCloudFront) supports HTTPS. You can use AWS Certificate Manager to request a public certificate for your own domain and associate it with your CloudFront distribution.

The EC2 instance uses a self-signed certificate for HTTPS. You can use Certbot to obtain and install Let's Encrypt certificate on your web server.

Certbot prerequisites

Ensure you have a domain name whose DNS entry resolves to your EC2 instance IP address. If you do not have a domain, you can register a new domain using Amazon Route 53 and create a DNS A record.

Using apache plugin

Using nginx plugin

Using Route 53 plugin

The certbot-dns-route53 option requires your DNS to be hosted by Route 53. It supports wildcard certificates and domain names that resolve to private IP addresses. Ensure that Route 53 zone access is granted by specifying r53ZoneID value. From terminal, run the below command based on installed web server type and follow instructions.

sudo certbot --dns-route53 --installer apache  
sudo certbot --dns-route53 --installer nginx  

Refer to Certbot site for help with this tool.

Using Cloudwatch agent

Amazon CloudWatch agent is installed, and enables collection of EC2 system-level metrics and AWS X-Ray traces.

The template configures agent to collect memory and disk utilization metrics. You can configure Cloudwatch agent to collect other data as follows.

Create agent configuration file

Create agent configuration file. You can use agent configuration file wizard:

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

Start Cloudwatch agent

After config.json file is created, start CloudWatch agent:

sudo systemctl enable amazon-cloudwatch-agent
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json

Refer to How do I install and configure the unified CloudWatch agent to push metrics and logs from my EC2 instance to CloudWatch? for more details.

Compiling PHP extensions on Amazon Linux 2023 (AL2023)

If you are looking to compile PHP extensions on AL2023, refer to community article How do I compile PHP extensions on Amazon Linux 2023?

About EC2 instance

Well-architected

To improve performance, reliability, scalability, high availability and functionality, EC2 instance can be extended to use other services such as Amazon RDS, Amazon S3, Amazon ElastiCache and Amazon EFS, and with AWS SDK for PHP. Some useful resources that can help with the integration include:

Restoring from backup

If you enable AWS Backup, you can restore your EC2 instance from recovery points (backups) in your backup vault. The CloudFormation template creates an IAM role that grants AWS Backup permission to restore your backups. Role name can be located in your CoudFormation stack Resources section as the Physical ID value whose Logical ID value is backupRestoreRole

Securing

To futher secure your EC2 instance, you may want to

Clean Up

To remove created resources, you will need to

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.