Thursday, September 12, 2013

Quick start with AWS: Create a new instance, install LAMP and Drupal.

Step-by-step guide to getting started with a new AWS (Amazon web services) account and hosting a Drupal powered website.

Estimated time to complete 40 minutes. For more info or help on any of the sub-steps, please do a quick Google search.


1. Create a new AWS account.
2. Launch a new instance :
 > make sure the volume, i.e the disk is of type ebs, increase the size if  you want.
 > use a AMI, i.e the OS from the list.
 > Generate a key value pair. Download the .pem file.
 > In the security group enable TCP port 80 (for HTTP) and 22 for SSH.

3. Generate an elastic IP
4. Associate the IP with the generated instance.
5. Create a monitoring alarm for the instance for high CPU.

6. Copy the .pem in some secure location.

7. Set up SSH to the instance, by configuring the ~/.ssh/config file.

Host ANY_NAME_YOU_WANT_TO_CALL_THE_INSTANCE.
HostName: ec2-xxxxxxx.compute-1.amazonaws.com
User: ec2-user
IdentityFile "~/.ssh/ec2.pem"

>chmod 700 ~/.ssh/config
>ssh Hostname <--- as="" config="" file.="" in="" p="" ssh="" the="" used="">

// Set up all utilities: Fedora


sudo yum -y update
sudo yum -y install httpd mysql mysql-server php php-cli php-gd php-intl php-mbstring php-mysql php-pdo php-pear php-xml php-xmlrpc 

// Check apache version for apache1

httpd -v

// for apache2
apache2 -v

// check php version 

echo '' | php


Make sure services automatically start on a reboot:
sudo chkconfig httpd on
sudo chkconfig mysqld on
sudo service httpd start
sudo service mysqld start 
 
Edit your httpd.conf:
sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.old
sudo vi /etc/httpd/conf/httpd.conf
 
find and under that change AllowOverride None to AllowOverride All
Press Ctrl + C and type :wq to save and quit.
Restart apache for changes to take effect:
sudo service httpd restart 


Set your mysql root password
sudo mysqladmin -u root password 'change-password' 
 
Mysql security:
sudo mysql -u root -p
mysql> DROP DATABASE test; 
mysql> DELETE FROM mysql.user WHERE user = ''; 
mysql> FLUSH PRIVILEGES; 
 
Create the database that drupal will use:
mysql> CREATE DATABASE drupal;
 
Press Ctrl + C to exit mysql.
Now your server is ready for drupal. I'm going to use drush to install:
pear is a php tool which allows reuse of php code and patches etc.
drush is a tool used to manage all of drupal.
sudo pear upgrade
sudo pear channel-discover pear.drush.org
sudo pear install drush/drush 
 
Next use drush to download the latest drupal version and then move it to the web root directory (Make sure to substitute the the name of the latest distribution for "drupal-7.x")
cd /var/www/html/
sudo drush dl
sudo mv drupal-7.x/* ./
sudo mv drupal-7.x/.htaccess ./
sudo rm -r drupal-7.
 
Create the files directory and the settings.php file
sudo mkdir sites/default/files
sudo chmod 777 sites/default/files/
sudo cp sites/default/default.settings.php sites/default/settings.php
sudo chmod 777 sites/default/settings.php 

Open your site in a browser and complete drupal install process, your site can be reached at the public DNS listed in your AWS console when you select an instance and should look something like ec2-00-00-000-00.compute-1.amazonaws.com.

After that go back to your terminal and set the permissions on the settings.php file.
sudo chmod 644 sites/default/settings.php 
That's it, you should now have the latest version of drupal up and running on your ec2 instance.


Running Appache as a different user:

If php needs to create log files while running under appache, apache should be run under a proper user:

Depending on your OS, this might be 'www-data', 'nobody', 'http', or any variation. If your website is the only website running, this is easy to change by changing the user Apache runs under. If you have Debian, like I tend to, you can edit the file /etc/apache2/envvars (as root), and change the value for APACHE_RUN_USER. Depending on your OS, this variable might be set in a different configuration file, so if you can't find it in /etc/apache2/envvars, try to search for the variable declaration by using:

More info: 
http://stackoverflow.com/questions/5165183/apache-permissions-php-file-create-mkdir-fail

This might cause PHP to loose access to session variables:


open(/var/lib/php/session/sess_isu2r2bqudeosqvpoo8a67oj02, O_RDWR) failed: Permission denied (13) in Unknown on line 0
you have to check file permission change mode this /var/lib/php/session/

Giving permissions to file so that apache works (apache runs as www-data)

Keep the owner as yourself for the folder:
chown -R eve contoso.com
Make group as the apache group. 
 chgrp -R www-data contoso.com
Change permission for the folder.
 chmod -R 750 contoso.com
 Make the folder permissions sticky, such that new files created insider the folder also has the same permissions.
 chmod g+s contoso.com ; needs to be run as sudo, 'g' is for group, 's' is for sticky
Make a apache writeble folder.
chmod g+w uploads

SetUp Mysql User:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
 GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';  (*.* == db.table)

FLUSH PRIVILEGES;


Install AWS SDK for PHP:

Via Composer:
   > In the project file create a file named: composer.json and add the aws dependency as:
    {
       "require": {
          "aws/aws-sdk-php": "2.*"
       }
   }

> Install composer and move it to make it a utility.
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
Now Install the dependencies via composer by running the below in the project root directory:
composer install
More info: https://getcomposer.org/doc/00-intro.md
This will create a folder 'vendor' with all the libraries, use composer autoloader to load all libs.
require '/path/to/sdk/vendor/autoload.php';
include the AWS namespaces by
use Aws\someserices\xyz

AWS setting up credentials profile:

 Use a cred file in the home dir to store access keys:
            http://docs.aws.amazon.com/aws-sdk-php/guide/latest/credentials.html#credential-profiles

 AWS email service:

Sending emails via SES:
     Creating a email client:
                   http://docs.aws.amazon.com/aws-sdk-php/guide/latest/service-ses.html
   
      Things that can be included in an email via sendEmail() function:

           http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.Ses.SesClient.html#_sendEmail


No comments:

Post a Comment