Upload
kamal-sharma
View
19.117
Download
7
Tags:
Embed Size (px)
DESCRIPTION
This article describe installing PHP on Amazon EC2 with Amazon Linux AMI
Citation preview
Installing PHP on Amazon EC2 (Using Amazon Linux AMI)
Kamal Sharma
Table of Contents
Requirements .......................................................................................................................................................... 2
Create and Launch Amazon Linux AMI ................................................................................................................... 2
Create Security Group ......................................................................................................................................... 2
Create Key Pairs .................................................................................................................................................. 3
Convert Key Pair to PuTTY Format ...................................................................................................................... 4
Create Instance ................................................................................................................................................... 5
Controlling Instance via PuTTY ................................................................................................................................ 7
Installing PHP ........................................................................................................................................................ 10
Transferring Files ................................................................................................................................................... 11
Enable SEO Friendly URLs Support (.htaccess) ..................................................................................................... 13
Running CronJobs .................................................................................................................................................. 15
Requirements
� Amazon Web Services Account
� WinSCP (for transfer files)
� PuTTY (SSH client)
Create and Launch Amazon Linux AMI
Create Security Group
Log in to Amazon Management Console with your username and password. Switch to EC2 Tab.
On the Network & Security tab click on Security Groups to view and create Security Groups.
Security Group frame is active, where all Security Group is listed.
Click Create Security Group button to create new group. Popup window will open, fill the required fields.
New Group created with name MyTestGroup. Assign ports to security group using inbound tab. select option
from Create a new rule drop down.
22 (SSH) – Port 22 is used for connecting SSH server.
80 (HTTP) – Default port of Web server.
Apply Rule Changes
Create Key Pairs
On the Key Pairs Panel, click on Create Key Pair button.
Fill desired Key Pair Name.
Key Pair is created successfully and able to download on your local disk.
Convert Key Pair to PuTTY Format
PuTTY can’t read (.PEM) files that provide by Amazon. It understands its own (.PPK) format. We can translate
the (.PEM) file format to (.PPK) file format using PuTTYgen.
� Open PuTTYgen; most probably locate in (c:/program files/putty/puttygen.exe)
� Find the (.PEM) file using Load button.
� Key Passphrase like a password, you can leave this field blank otherwise it authenticate when you
connecting to instance.
� Save private key on your disk.
Create Instance
� On Amazon EC2 Console Dashboard, Launch the instance using Launch Instance button.
� Choose an Amazon Machine Image (AMI) from one of the tabbed lists below by clicking its Select
button.
� Pick a meaningful name for your Instance, select
Edit details to choose your Security Group
Instance is launched and Public DNS is available.
Pick a meaningful name for your Instance, select Key Pair and choose Amazon Linux AMI
Security Group and Instance Type, click Edit details.
is available.
Amazon Linux AMI. Click Continue.
Controlling Instance via PuTTY
In Auth section, browse your private key file for authentication.
� Use Public DNS as Host Name.
� Connection Type to SSH
� And Port 22
After connecting to server, on terminal login as ec2-user
Change the user (ec2-user) to root with the following command.
sudo -i
If you try to open Public DNS URL, browser throws error Unable to connect because server is not installed on
AMI.
First need to install Apache server with following command.
yum install httpd
Type and Enter y to confirm installation.
After Apache is successfully installed, start the Apache with following command.
service httpd start
Test the URL (Public DNS), Apache is installed and running successfully.
Installing PHP
Install PHP using following command.
yum install php
If you need to install separate package such as MySQL, IMAP etc.
Name Package
MySQL php-mysql
GD php-gd
XML php-xml
IMAP php-imap
yum install php php-mysql php-gd php-xml php-imap
After install PHP, need to restart Apache to take effect, use the following command to restart Apache.
service httpd restart
Transferring Files
For transferring files need SCP client such as WinSCP.
� Open WinSCP, use Public DNS as Host name
� User name as ec2-user
� Browse your Private Key file.
� And Choose File protocol - SCP
After successful login move to root directory (Server Path).
/var/www/html
Use drag and drop for transfer files (client to server
error message such as Permission denied
directory.
So need to change the Permission for ec2
� chgrp (Change Group Permission
� chmod (Change File Permission
After edit the permission, files successfully
client to server), when you trying to transfer file, WinSCP generate an
Permission denied because ec2-user don’t have permission to copy files
Permission for ec2-user. The following command need to change
Change Group Permission) – chgrp -R “user” “directory
Change File Permission) – chmod permission “directory
edit the permission, files successfully transferred.
hen you trying to transfer file, WinSCP generate an
permission to copy files in root
change the permissions.
directory”
directory”
Test the URL, PHP is successfully installed.
Second method, transfer files without changing the group permission for ec2
ec2-user directory path in (httpd.conf
Enable SEO Friendly URLs Support (.htaccess)
By default, Amazon not provides this option with
(httpd.conf) configuration.
Testing the clean URL with browser, it
First needs to edit the (httpd.conf) file
command.
installed.
without changing the group permission for ec2-user; change
httpd.conf) file.
URLs Support (.htaccess)
this option with Apache installation; need to interfere
with browser, it generates an error because Apache not able to handle
(httpd.conf) file in etc/httpd/conf for editable by ec2-user with the following
change the server path to
interfere with Apache
Apache not able to handle (.htaccess) file.
with the following
� Chgrp –R “ec2-user” “/etc/httpd/conf/httpd.conf”
� Chmod 0774 “/etc/httpd/conf/httpd.conf”
Open the directory (/etc/httpd/conf) using WinSCP and edit the (httpd.conf) file. You need to change the
AllowOverride None
To
AllowOverride all
Restart the Apache with
service httpd restart
Apache commands
Start - service httpd restart
Stop – service httpd stop
Restart – service httpd restart
Test and SEO friendly URL is working.
Running CronJobs
Cron is a time-based job scheduler in Unix
Cron Job commands:
crontab -l – Listing the running cron
crontab -r – Removing the running cron.
crontab -e – creating and editing the cron.
Checking the any Cron Job is running for the user
crontab -l
Sample Cron Job for every one minute.
Min Hour Day of Month Month of Year
* * * *
* * * * * php /var/www/html/mycronfile.php
Create Cron Job using following command
service httpd restart
httpd restart
is working.
based job scheduler in Unix-like computer operating systems.
Listing the running cron.
the running cron.
and editing the cron.
is running for the user with the following command.
for every one minute.
Month of Year Day of Week Commands
* php /var/www/html/mycronfile.php
* * * * php /var/www/html/mycronfile.php
Cron Job using following command
php /var/www/html/mycronfile.php
crontab -e
After CronTab editor open, press the insert key to write on the file.
� Press enter for blank line.
� Use ESC key to close the editing
� Type : x command and press enter to close and save changes.
Cron Job is installed.