This blog post gets into the details of setting up a PHP and MySQL based environment on an Amazon Web Services (AWS) EC2 instance. AWS has created a versatile environment for web and application hosting that allows full access to the virtualized hosting environment. AWS also has a wide variety of services to choose from when building and hosting your application. To start with you can create an Amazon AWS account at:
https://aws.amazon.com/free/
Once you've created an AWS account select the services item on the top menu. Next select EC2 under Compute. This will place you in the EC2 Dashboard where you can create and work with your resources.
From the resources screen click the Launch Instance button.
On the choose an amazon machine image screen check the free tier only box. In the search box above type ubuntu and press enter. This will give you a list of ubuntu machine images that are free tier eligible. The latest version at this time is Ubuntu Server 18.04. Lets choose the Ubuntu Server 18.04 LTS (HVM), SSD Volume Type, then click the select button on the far right.
The instance type of t2 micro (free tier eligible) is preselected. Click the review and launch button below. On the Review Instance Launch screen click on the launch button. This will create your ec2 machine image along with your storage volume and security group.
Select create new key pair from the choose an existing key pair dropdown list. Next type in a name for the keypair in the key pair name textbox, then click the download key pair button. After clicking download key pair button the launch instances button becomes enabled. click the launch instances button to launch your ec2 instance.
The launch status screen appears with a message indicating that your instances are now launching. Click on the instance launch identifier and you'll be taken to the running instances screen. The configured ec2 instance should now be running.
Next we will need to convert the key pair file (.pem) you downloaded earlier to a PuTTY Private Key File (.ppk). To do this we will need to run puttygen. After running puttygen we will need to load the .pem file and then save the private key in the .ppk format. Next we will need to run putty and specify ubuntu@(public dns name shown below in the red box) for the hostname. Then under category > connection > ssh > auth click browse and locate the private key file. Create a name for the session you would like to save in the saved sessions textbox and click save. For more information on configuring a putty connection with AWS this reference is available.
To connect with your server run the putty application and click open. At this point you should see a terminal window open with a connection to your aws ec2 server. Once you have a connection type:
sudo apt-get update
This command will update the package list on your server. After the update finishes let's run the upgrade command:
sudo apt-get upgrade
We can now install the LAMP stack using tasksel. We will need to run the following command to install tasksel:
sudo apt install tasksel
After tasksel is installed we can install lamp-server (Apache, MySQL, and PHP) with the following command:
sudo tasksel install lamp-server
Now that the lamp-server package has been installed, let's check that Apache2 is installed and running. At this point we should see an Active (running) instance of the application noted in the output.
sudo systemctl status apache2
Now lets check that mysql and php are installed.
mysql -V
php -v
At this point we need to add an inbound rule for http to the AWS security group launch-wizard-1. This will allow incoming connections to reach your server. When you add a rule for http, it defaults to 0.0.0.0/0 for incoming traffic which means that anyone anywhere can connect to your server over port 80. It will work for the purposes of testing our connection with apache but you may want to restrict this further for security purposes. From the security group screen, right click on launch-wizard-1 and select edit inbound rules. From the edit inbound rules screen, click add rule, then select http from type and click the save button.
Now that we've added an opening in the security group for http, copy the IPv4 Public IP address located on the running instances screen. Open up a new tab on your browser and paste the public ip address into the browser url box and hit enter. You should now see the following webpage. You've successfully connected to apache on your ec2 server!