Transferring local php application to an Amazon EC2 instance

Abhishek Verma
TheLoudCloud
Published in
2 min readJul 11, 2020

--

Launch an EC2 instance with Amazon Linux 2 AMI. Make sure your security group has an inbound rule to enable SSH.

  • SSH into the instance.
  • Install PHP: sudo amazon-linux-extras install -y php7.2
  • Install Apache: sudo yum install -y httpd
  • Start the apache server: sudo systemctl start httpd
  • Now enter the public IP address of the EC2 instance. You’ll be greeted with a Test Page. This will be the default page unless you add you php application code to the Apache document root: /var/www/html/.
  • You can use systemctl command to configure the Apache web server to start at each system boot: sudo systemctl enable httpd.

Transfer local php application to the Apache document Root:

  • The Amazon Linux Apache document root is /var/www/html, which by default is owned by root. In order to transfer the php code on your machine to the EC2 instance, you’ll need to allow the ec2-user account to manipulate files in the apache document root.
  • Add ec2-user to apache group:sudo usermod -a -G apache ec2-user.
  • Change the group ownership of /var/www and its contents to the apache group: sudo chown -R ec2-user:apache /var/www
  • Change the directory permissions of /var/www and its subdirectories to add group write permissions and to set the group ID on future subdirectories: sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;
  • Recursively change the file permissions of /var/www and its subdirectories to add group write permissions: find /var/www -type f -exec sudo chmod 0664 {} \;
  • Now that the ec2-user has permissions to the apache root directory, you can transfer the local php code to the EC2 instance:
pscp -r -i <path to local private key file> <path to local application code> ec2-user@<EC2 public ip address>:/var/www/html/
  • Use scp command for Linux.
  • Your php app will now be running on the EC2 instance.

If you want to automate this process. You can pass the commands as “user data” while launching the EC2 instance. The script will automatically be executed and your instance will be ready to run php applications.

--

--