Auto Setup EC2 with script
School of Science, Computing and Engineering Technologies Swinburne University of Technology
COS80001 1
#!/bin/bash
yum update -y
amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
service httpd start
yum install -y httpd mariadb-server php-mbstring php-xml
systemctl start httpd
systemctl enable httpd
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
echo “Welcome to COS80001. Installed PHP version:
‘. phpversion() . ‘
‘;?>” > /var/www/html/phpinfo.php
Automatically setting an EC2 instance using bootstrap script
When we create a virtual server, we need to install software so it does something.
Commonly, a VM can be used to run a web server like Apache httpd. To install
software/packages when setting up an EC2 Linux instance, we tell AWS to run a
bootstrap script as “User data”. You can do this during the EC2 setup, under AWS
EC2
Launch Instance wizard, in Step 3 Configure Instance Details | Advanced Details. We
recommend you use the following script (also provided in a separate text file):
This script does the following (for more details, visit
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2.html
1. Updates the Linux operating system with the yum command
2. Installs the Apache Web server httpd along with PHP and MySQL and other necessary packages
3. Starts the Web server and checks its configuration
4. Adds ec2-user to apache group
5. Changes file and directory permissions so files can be uploaded to the www directory
6. Copies a sample php web page to the www directory for testing.
NOTE: Pay attention to the syntax when you copy & paste the script above from this document to
the input field in AWS Management Console. Syntax errors often occur with cross-platform copy &
paste. To mitigate this issue, we have provided a bare text file (EC2 setup script.txt file) that contains
the script.
NOTE: On Swinburne Mercury, the directory for html files was called www/htdocs. In this set up, it is
www/html.
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2.html