billysoftacademy.org

AWS Lightsail - Using the aws-php-sdk to create instances

Introduction

The AWS PHP SDK is a collection of tools that enable PHP developers to interact with various AWS APIs from their own PHP application. It simplifies the integration of AWS services, allowing developers to manage resources such as AWS Lightsail instances, S3 buckets, EC2 instances and many others directly from PHP code. In this tutorial you will learn how to create AWS instances using the AWS PHP SDK! I’ll walk you through:

1. Setting up a VirtualHost in OpenLiteSpeed.
2. Accessing the VirtualHost directory using Samba.
3. Creating a new project in the directory with PHPStorm.
4. Downloading and installing the AWS PHP SDK using Composer.
5. Creating the Lightsail Client.

Requirements

The following is a list of items needed to create instances successfully using the aws-php-sdk:
1) An easy to use IDE such as PHP Storm
2) The latest version of PHP. Available at php.net
3) The latest version of the AWS PHP SDK (Can be installed using composer)
4) A Linux server with the LAMP or LOMP stack installed and configured or a web control panel
5) A basic understanding of how to write PHP code.

Overview

The following is an overview of the steps covered in this tutorial:
1) Setup a virtualhost in OpenLiteSpeed
2) Access the virtualhost directory using Samba
3) Create a new project in PHPStorm
4) Download and install the aws-php-sdk
5) Create an AWS Lightsail PHP Client

Step 1: Setup a virtualhost in OpenLiteSpeed

Start by setting up a virtualhost in OpenLiteSpeed Web Server. Open an SSH connection to the OpenLiteSpeed server and run the following commands to create a virtualhost directory:
cd /usr/local/lsws
mkdir aws.local
cd aws.local
mkdir public_html
Open a new browser window and navigate to https://your-server-ip:7080. Enter your OpenLiteSpeed credentials on the login page to access the OLS webadmin dashboard. Click “Virtual Hosts” and click the “Add” button. Configure the following settings:
Virtual Host Name = aws.local
Virtual Host Root = $SERVER_ROOT/aws.local
Config File = $SERVER_ROOT/conf/vhosts/$aws.local/vhconf.conf
Enable Scripts/ExtApps = Yes
Restrained = No
Max Keep-Alive Request = 1000
Click on the “Save” button. An error message may be displayed informing you that the vhconf.conf file does not exist. Click on the “CLICK TO CREATE” link to create the file and click on the “Save” button to create the virtualhost. Click the “General” tab and configure the following settings:
Document Root = $VH_ROOT/public_html
Domain Name = aws.local
Domain Aliases = www.aws.local
Enable GZIP Compression = Yes
Enable Brotli Compression = Yes
Click on the “Save” button to save the settings. Scroll down and click the “Edit” button on “Index Files”. Configure the following settings:
Index Files = index.html, index.php
Auto Index = No
Click the “Save” button and click on the “Rewrite” tab. Click “Edit” on “Rewrite Control” and set “Enable Rewrite” to “Yes”. Set “Auto Load from .htaccess” to “Yes” and click the “Save” button. Click the “Listeners” option on the webadmin menu list on the left and click “Edit” on the default listener. On “Virtual Host Mappings” click the “Add” button and configure the following settings:
Virtual Host = aws.local
Domains = *
Click the Save button and click the restart litespeed icon on the top right corner of the webadmin to restart the web server and apply changes. Open the hosts configuration file for your computer. If you are using a mac, the file is located in the /etc/ directory. If you are using a Windows device, the file should be found in the “c://windows/system32/drivers/etc/” directory. Add the following line in the hosts file:
192.168.88.100  aws.local
Replace the above IP with the actual IP address of your web server.

Step 2: Access the virtualhost directory using Samba

The next step is installing and configuring Samba. Samba makes it possible to access the virtualhost directory on the web server from PHPStorm installed on your laptop or desktop PC. Note that using Samba to access the virtualhost directory from your IDE may not be the best way if you are using a publicly hosted VPS. In this tutorial we’ll assume that OpenLiteSpeed is installed on a Linux Ubuntu Server. Open an SSH connection to the Linux Ubuntu server and run the following command to install Samba:
sudo apt install samba
Once Samba is installed, run the followinf command to open the smb.conf file:
sudo nano /etc/samba/smb.conf
Add the bottom of the smb.conf file, add the following configuration:
[sambashare]
     comment = smb server
     path = /usr/local/lsws/
     read only = no
     browsable = yes
Press “CTRL + O” and press “CTRL + X” to save the changes made to the file. Run the following command to create a samba user:
sudo smbpasswd -a username
Replace username with an actual username and set a good password. Run the following commands to restart samba so that the changes take effect:
sudo service smbd restart
sudo ufw allow samba
Open your Windows device and open the file explorer. Left click on “This PC” and click “Map network drive”. Select a drive name and enter “\\server-ip-address\sambashare” on the folder path. Click “Finish” and enter the samba credentials that you set. If you are using a mac, you can connect to the samba share in Finder. Click Menu and click “Go”. Click “Connect to Server” then enter “smb://server-ip-address”. Connect and enter your samba credentials when prompted. Once you connected successfully you should see the samba network folder in Finder or Windows file manager.

Step 3: Create a new project in PHPStorm

Open PHPStorm and click “Create New Project”. Click the browse button and open the Samba network share. Select the “aws.local” director and select the “public_html” directory. Click the “Open” button and PHPStorm will automatically use the selected directory to setup a PHP project.

In PHPStorm, right click on the “public_html” directory and click “Create New PHP File”. 

Scroll to Top