Hosting A PHP Web Application On AWS: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "== Overview == == Process (rough notes) == === Create EC2 instance === Log in to AWS. '''Services''' > '''EC2''' > '''Instances''' '''Launch Instance''' dropdown > '''Laun...")
 
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
Editing Hosting A PHP Web Application On AWS
== Overview ==
== Overview ==


== Process (rough notes) ==
== Process (rough notes) ==


=== Create EC2 instance ===
From the  AWS Management Console home page > '''Build a solution''' group, click on '''Build a web app''', which is a path to creating a new Elastic Beanstalk instance.
 
''-- OR --''
 
'''AWS Management Console''' > '''Services''' > '''Compute''' > '''Elastic Beanstalk'''.
 
'''Create New Application''' at the upper right.
 
Once the application is created, navigate to its management page, '''Actions''' dropdown (upper right) > '''Create environment'''
 
=== Create a web server environment ===
 
'''Select environment tier''': Web server environment. Then click '''Select'''
* '''Environment name:''' ''appropriate token for the environment''
* '''Domain:''' ''leave blank''
* '''Platform:''' PHP, Python, Docker, etc.
* '''Applciation code''': Upload your code
** ''In the next dialog there is an option to use a Public S3 URL, which might be simpler to maintain with Git, etc.? No, this process only accepts zip files, so using an S3 bucket would mean that there is a zip archive located at the URL. Also, Git is an option, but only after using Git to archive a repo.''
** '''Version label:''' Include some indication of version in the token, e.g. version number or date.
* Click '''Create Environment''' to create and launch the new environment. This will create a new EC2 instance.
 
=== More configuration options ===
 
Click '''"Configure more options''' button to define additional properties for the web server.
 
For example, to create a database, or to define security properties for the EC2 instance linked to the web server.
 
==== Security ====
 
This panel controls key pairs for SSH access.
 
'''Virtual machine permissions''' > '''EC2 key pair''' > choose an existing key pair. (This is created via '''AWS Management Console''' > '''EC2''' > '''Network & Security''' > '''Key Pairs''')
 
When connecting to the EC2 instance via ssh, it's necessary that an inbound rule exists for ssh for the EC2 instance's security group. Under instance details, click on the security group > '''Inbound''' tab > '''Edit''' button. There should be a rule for SSH for the IP address of the machine that is used to ssh to the EC2 instance.
 
==== Instances (EC2) ====
 
Control over '''instance type''' and '''security types'''.
 
Once the environment is launched a url will be available that can be used to view the web site.
 
==== Database ====
 
Either select an existing snapshot, or create a new database.
 
For example, MariaDB is not available from the list of database types when creating a new database. In order to link the EBS to a MariaDB database, first create the MariaDB instance in the AWS RDS Management Console. Then create a snapshot of the database. Then select that snapshot when selecting a database for the EBS.
 
Security group rules must be configured to allow the EB instance to make connections to the RDS database.
 
* '''AWS Management Console''' > '''RDS''' > '''Instances''' > ''my instance'' > click for details
* Scroll down to the '''Connect''' pane to view a list of '''Security groups'''
* An inbound rule needs to exist for the VPC that was created for the EB instance.
* Scroll farther down on the RDS instance details page for the '''Details''' pane > '''Security and network''' > '''VPC''' > click on the VPC name to view its details, e.g. its IPv4 CIDR.
* Click on a '''CIDR/IP - Inbound''' security group to edit its settings.
* '''Security Groups listings''' page > Click on the row for the RDS's security group > '''Inbound''' tab > '''Edit''' button
** '''Type:''' e.g. MYSQL/Aurora, or appropriate database type
** '''Protocol:''' TCP
** '''Port range:''' 3306
** '''Source:''' VPC IPv4 CIDR value
 
=== Subdomains ===
 
Create subdomains as separate environments under the common EB application.
 
== Configuration ==
 
From the EB on the the EB instance to view its properties.
 
'''Notifications''' - Enter an email address to receive notifications.
 
== Updating app code ==
 
Source files are located at `/var/app/current/`
 
=== AWS Management Console ===
 
Zip the entire contents of the project (not including the parent folder).
 
* '''EBS Management Console''' > ''application'' > '''application versions'''
* '''Upload''' button
** '''Version label:''' ''name consistent with previous uploads''
** '''Upload application:''' ''choose the zip file''
* After upload completes select the application version > '''Deploy''' button.
** Select the environment that the code should be deployed to.
 
=== EB CLI ===
 
==== Installing eb cli on Mac OS ====
 
Use pip and not homebrew.
 
==== Updating app source without Git ====


Log in to AWS.
First initialize the eb environment with
'''Services''' > '''EC2''' > '''Instances'''
'''Launch Instance''' dropdown > '''Launch Instance'''
'''Step 1: Choose an Amazon Machine Image (AMI)''': Amazon Linux 2 AMI
'''Step 2: Choose an Instance Type''': General purpose t2.micro
* Click '''Review and Launch'''
* Next page click '''Launch'''
'''Select an existing key pair or create a new key pair'''
* `Choose an existing key pair`
* Consider using a single key pair for all AWS instances linked to a particular client (e.g. don't create a new key pair unless there's a compelling reason to do that).
After confirming the instance will be created and start running.


=== Configure EC2 instance
<syntaxhighlight lang="sh">
$ eb init
</syntaxhighlight>


Return to EC2 Instances.  
This will prompt for properties of the EB application and environment, and then store the configuration in `./.elasticbeanstalk/config.yml`
* The name of the new EC2 instance will be blank. Click to the right to edit.  
 
* Click on Alarm Status cell to create an alarm.
Edit the config file with the path of the zip file to use for updating the app:
** '''Send a notifications to''' `North_Rose_Devs_Status_Failed`
 
** '''Take the action:''' Recover this instance
<syntaxhighlight lang="yaml">
** '''Whenever:''' Status Check Failed (System)
deploy:
  path/to/app-source.zip
</syntaxhighlight>
 
Zip the project with
 
<syntaxhighlight lang="sh">
$ zip ../app-source.zip -r * .[^.]*
</syntaxhighlight>
 
Then from the directory where `.elasticbeanstalk/` is located, deploy with:
 
<syntaxhighlight lang="sh">
$ eb deploy
</syntaxhighlight>
 
=== PHPStorm ===
 
It's possible to create a remote host for the EBS, but it is read-only.
 
* '''Type:''' SFTP
* '''SFTP hose:''' ''AWS EC2 Management console > EC2 instnace > IPv4 Public IP''
* '''Root path:''' /var/app/current/
* '''User name:''' ec2-user
* '''Auth type:''' Key pair
* '''Private key file:''' ''path to .pem file''
 
== Importing data ==
 
Any MariaDB (or MySQL) client can be used to connect to the MariaDB database if it is publicly accessible, e.g. MySQLWorkbench. <ref>[https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ConnectToMariaDBInstance.html Connecting to a DB Instance] - AWS documentation</ref>
 
Retrieve the Endpoint from the AWS Management Console after loading the details of the MariaDB RDS instance. This is the host for the purposes of connecting to the database.
 
* Connect to the database with MySQLWorkbench.
* '''Management''' > '''Data Import/Restore'''
* '''Import from Self-Contained File''' > ''Select a dump of the source database''
* ''''Default Target Schema''' > ''Select database''
* '''Dump Structure and Data'''
* '''Start Import''' button
 
== Viewing server logs ==
 
''TK''


== Pricing ==
== Pricing ==
See [[Amazon_Web_Services_Website_Hosting_Fundamentals#Pricing|AWS Web Hosting Fundamentals]]
== Notes ==
<references/>


[[Category:AWS]][[Category:Web Development]]
[[Category:AWS]][[Category:Web Development]]

Latest revision as of 20:55, 20 July 2018

Editing Hosting A PHP Web Application On AWS

Overview[edit]

Process (rough notes)[edit]

From the AWS Management Console home page > Build a solution group, click on Build a web app, which is a path to creating a new Elastic Beanstalk instance.

-- OR --

AWS Management Console > Services > Compute > Elastic Beanstalk.

Create New Application at the upper right.

Once the application is created, navigate to its management page, Actions dropdown (upper right) > Create environment

Create a web server environment[edit]

Select environment tier: Web server environment. Then click Select

  • Environment name: appropriate token for the environment
  • Domain: leave blank
  • Platform: PHP, Python, Docker, etc.
  • Applciation code: Upload your code
    • In the next dialog there is an option to use a Public S3 URL, which might be simpler to maintain with Git, etc.? No, this process only accepts zip files, so using an S3 bucket would mean that there is a zip archive located at the URL. Also, Git is an option, but only after using Git to archive a repo.
    • Version label: Include some indication of version in the token, e.g. version number or date.
  • Click Create Environment to create and launch the new environment. This will create a new EC2 instance.

More configuration options[edit]

Click "Configure more options button to define additional properties for the web server.

For example, to create a database, or to define security properties for the EC2 instance linked to the web server.

Security[edit]

This panel controls key pairs for SSH access.

Virtual machine permissions > EC2 key pair > choose an existing key pair. (This is created via AWS Management Console > EC2 > Network & Security > Key Pairs)

When connecting to the EC2 instance via ssh, it's necessary that an inbound rule exists for ssh for the EC2 instance's security group. Under instance details, click on the security group > Inbound tab > Edit button. There should be a rule for SSH for the IP address of the machine that is used to ssh to the EC2 instance.

Instances (EC2)[edit]

Control over instance type and security types.

Once the environment is launched a url will be available that can be used to view the web site.

Database[edit]

Either select an existing snapshot, or create a new database.

For example, MariaDB is not available from the list of database types when creating a new database. In order to link the EBS to a MariaDB database, first create the MariaDB instance in the AWS RDS Management Console. Then create a snapshot of the database. Then select that snapshot when selecting a database for the EBS.

Security group rules must be configured to allow the EB instance to make connections to the RDS database.

  • AWS Management Console > RDS > Instances > my instance > click for details
  • Scroll down to the Connect pane to view a list of Security groups
  • An inbound rule needs to exist for the VPC that was created for the EB instance.
  • Scroll farther down on the RDS instance details page for the Details pane > Security and network > VPC > click on the VPC name to view its details, e.g. its IPv4 CIDR.
  • Click on a CIDR/IP - Inbound security group to edit its settings.
  • Security Groups listings page > Click on the row for the RDS's security group > Inbound tab > Edit button
    • Type: e.g. MYSQL/Aurora, or appropriate database type
    • Protocol: TCP
    • Port range: 3306
    • Source: VPC IPv4 CIDR value

Subdomains[edit]

Create subdomains as separate environments under the common EB application.

Configuration[edit]

From the EB on the the EB instance to view its properties.

Notifications - Enter an email address to receive notifications.

Updating app code[edit]

Source files are located at /var/app/current/

AWS Management Console[edit]

Zip the entire contents of the project (not including the parent folder).

  • EBS Management Console > application > application versions
  • Upload button
    • Version label: name consistent with previous uploads
    • Upload application: choose the zip file
  • After upload completes select the application version > Deploy button.
    • Select the environment that the code should be deployed to.

EB CLI[edit]

Installing eb cli on Mac OS[edit]

Use pip and not homebrew.

Updating app source without Git[edit]

First initialize the eb environment with

$ eb init

This will prompt for properties of the EB application and environment, and then store the configuration in ./.elasticbeanstalk/config.yml

Edit the config file with the path of the zip file to use for updating the app:

deploy:
  path/to/app-source.zip

Zip the project with

$ zip ../app-source.zip -r * .[^.]*

Then from the directory where .elasticbeanstalk/ is located, deploy with:

$ eb deploy

PHPStorm[edit]

It's possible to create a remote host for the EBS, but it is read-only.

  • Type: SFTP
  • SFTP hose: AWS EC2 Management console > EC2 instnace > IPv4 Public IP
  • Root path: /var/app/current/
  • User name: ec2-user
  • Auth type: Key pair
  • Private key file: path to .pem file

Importing data[edit]

Any MariaDB (or MySQL) client can be used to connect to the MariaDB database if it is publicly accessible, e.g. MySQLWorkbench. [1]

Retrieve the Endpoint from the AWS Management Console after loading the details of the MariaDB RDS instance. This is the host for the purposes of connecting to the database.

  • Connect to the database with MySQLWorkbench.
  • Management > Data Import/Restore
  • Import from Self-Contained File > Select a dump of the source database
  • 'Default Target Schema > Select database
  • Dump Structure and Data
  • Start Import button

Viewing server logs[edit]

TK

Pricing[edit]

See AWS Web Hosting Fundamentals

Notes[edit]

  1. Connecting to a DB Instance - AWS documentation