Wednesday, July 6, 2011

Getting started with Amazon EC2 and PHP – MySql

Do you know nothing about AWS or linux, but want to run PHP/MySql server set up on AWS? Are you new comer on AWS?
I was like you, relying on webhosting services to manage/configured on servers, so it was tough to work on AWS EC2 but later I feel it’s simpler.
In this tutorial I will show you how to setup PHP and MySql and how to make it web-accessible.

AWS:
Amazon has different products, but we are more interested in Amazon Elastic Compute Cloud (EC2).
Click on link and Sign up for Amazon EC2.
Once you have setup for EC2, go to AWS Console and click on Instance in left hand side panel.
You can see more details on Amazon documentation.
Now you create a Key Pair - this will be the credentials you will use to SSH.
Next is the Security Group - which will be used to specify the firewall used for your instance.
For now use the default Group and add port numbers as you want to open.
Now Elastic IP address — In the AWS Management Console, click on “Elastic IPs”, then “Allocate New Address” under “Addresses”. Once the address is created, click on it, then “Associate Address”. Select the instance and associate it.

You should now be able to SSH into your instance using your .pem file.
Now question is how do I access my instance? Couple of Software.
PuTTY.exe — Secure Shell client
PuTTYgen.exe — SSH public/private key generator
Putty available at your finger tip, For that you need http://www.putty.nl/download.html

Configuring the Linux Server:
We have got server up and running, now lets setup some packages.
I’ll show you how to set up PHP and MySQL on the server.

PHP:
sudo yum install php-mysql php php-xml php-mcrypt php-mbstring php-cli mysql httpd
Press ‘y’ for each of the prompts that show up. Note that you’re logged in as ec2-user, so you need to sudo all of these command (No need on fedora).
You should now be able to create and run a PHP sample file.

MySql:
Install MySql server.
sudo yum install mysql-server
Now start the mysql server
sudo /etc/init.d/mysqld start
Next, set the root password.
mysqladmin -u root password '[PASSWORD]'
Now we set up two users for MySQL: the administrator, which you’ll use to create and modify tables; and the app user, which the app will use to query the DB (with more limited privileges).
Log into MySQL as root (mysql -u root -p) for mysql prompt.
If you want to grant privileges for other server (MySql is not setup on same server)then type following command
GRANT ALL PRIVILEGES ON *.* TO root@'SCRIPTSERVERIPADDRESS' IDENTIFIED BY 'MYSQLUSERNAME' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Now you have PHP and MySql running on server, but cannot accessible though browser.

Make it web accessible:
Now we need to configure the web server.
To set up web server, we need to modify config file which is httpd.conf, located at /etc/httpd/conf/httpd.conf.
You can open this file using editor, I used vi.
Now go to bottom of the file, where there is a small section on the VirtualHost.
Add the following code in it.
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot [your document root]
ServerName [your server name]
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

Note: If you are planning to run multiple sites on same server, you can add multiple virtual host section with document root and server name changes according to site.

Now restart the httpd process or server using following command.
sudo /etc/init.d/httpd restart


Using two Instances


It will be better to separate the web/script server and the DB server. For this, you will need to set up two Instances with two Elastic IPs, one of which has the web server httpd running, and the other of which has the mysql server mysqld running.
To make accessible on the web, instead of using “localhost” as the MySQL host, use the IP address of the MySQL instance. On the DB instance, set up your grant permissions (Given the query in MySql section) to allow db from anywhere.

Now you are done with setting up Amazon server and your server is loaded with PHP and MySql.
In upcoming blogs, I will introduce you with FTP server setup and phpmyadmin setup.

No comments: