working with ebextensions in aws - apache

I am working with AWS elastic beanstalk and since I can't modify the httdp conf file to AllowOverride All I was suggested to work with ebextensions:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html
Hence, I have created an .ebextensions folder, and within it a setup.config file with the following command:
container_commands:
01_setup_apache:
command: "cp .ebextensions/enable_mod_rewrite.conf /etc/httpd/conf.d/enable_mod_rewrite.conf"
I am not even sure if this is the proper command to enable mod rewrite, but I get the following error while trying to upload the instance:
[Instance: i-80bbbd77] Command failed on instance. Return code: 1 Output: cp: cannot stat '.ebextensions/enable_mod_rewrite.conf': No such file or directory. container_command 01_setup_apache in .ebextensions/setup.config failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.

You can't copy from ".ebextensions/enable_mod_rewrite.conf" because that relative path will not be valid from the init script. Using absolute paths may work, but there i'd suggest you fetch from S3 instead:
container_commands:
01_setup_apache:
command: "aws s3 cp s3://[my-ebextensions-bucket]/enable_mod_rewrite.conf /etc/httpd/conf.d/enable_mod_rewrite.conf"
But if you need complex changes to your instance, it may be a better option to run a docker container instead: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker.html

Related

Move files in S3 bucket to folder based on file name pattern

I have an S3 bucket with a few thousand files where the file names always match the pattern {hostname}.{contenttype}.{yyyyMMddHH}.zip. I want to create a script that will run once a day to move these files into folders based on the year and month in the file name.
If I try the following aws-cli command
aws s3 mv s3://mybucket/*.202001* s3://mybucket/202001/
I get the following error:
fatal error: An error occurred (404) when calling the HeadObject operation: Key "*.202001*" does not exist
Is there an aws-cli command that I could run on a schedule to achieve this?
I think the way forward would be through the --filter parameter used in S3 CLI commands.
So, for your case,
aws s3 mv s3://mybucket/ s3://mybucket/202001/ --recursive --exclude "*" --include "*.202001*"
should probably do the trick.
For scheduling the CLI command to run daily, I think you can refer to On AWS, run an AWS CLI command daily

Deploying Symfony 4 Application to AWS Elasticbeanstalk

I have a working Symfony 4.0.1 application running on PHP 7.1.14 (locally) that I would like to deploy to AWS Elastic Beanstalk using the EB CLI
I have a dist package of the application on my master git branch configured for production (vendor folder removed etc) that I am able to successfully deploy to Heroku. Now I need to deploy to AWS EB.
The AWS EB environment has already been set up (although I dont have access to the console). Some environment details are as follows:
Platform: arn:aws:elasticbeanstalk:us-east-2::platform/Tomcat 8 with Java 8 running on 64bit Amazon Linux/2.7.7
Tier: WebServer-Standard-1.0
At first, I was able to successfully deploy the application, but accessing the URL gave a 404 error for every page.
I did some googling and found a few articles describing the use of .config files. I have added one named 03_main.config with the following contents.
commands:
300-composer-update:
command: "export COMPOSER_HOME=/root && composer.phar self-update -n"
container_commands:
300-run-composer:
command: "composer.phar install --no-dev --optimize-autoloader --prefer-dist --no-interaction"
600-update-cache:
command: "source .ebextensions/bin/update-cache.sh"
700-remove-dev-app:
command: "rm web/app_dev.php"
Deploying with this .config file gives the following deployment failure error:
ERROR: [Instance: i-0c5f61f41d55a18bc] Command failed on instance. Return code: 127 Output: /bin/sh: composer.phar: command not found. command 300-composer-update in .ebextensions/03-main.config failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.
I understand the purpose of .config files but do not understand what additional configuration is needed for get this Symfony app running.
I guess you should use the full path to composer like bellow :
100-update-composer:
command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update -n

Is it possible to use AWS Beanstalk's .ebextensions config to install mod_pagespeed Apache module?

I'm using AWS Beanstalk for my Django/Python application, and I would like to use Google's mod_pagespeed module. Is it possible to install and run mod_pagespeed using the .ebextensions/.config file?
Download the package
Add the rpm into your ebextensions directory
create a .config file in the .ebextensions directory
add commands to the config file like this:
container_commands:
01-command:
command: rm -rf /pagespeed/ebextensions
02-command:
command: mkdir -p /pagespeed/ebextensions
03-command:
command: cp -R .ebextensions/* /pagespeed/ebextensions/
04-command:
command: rpm -U /pagespeed/ebextensions/mod-pagespeed.rpm
Ensure the commands are indented as shown, with no tabs, otherwise it wont work.
swap "mod-pagespeed.rpm" for whatever the actual rpm file name is.
Ok so I want to add Charlie Smith's answer. I would suggest you make sure you have the following things turned on.
mod_deflate - You probably want to Gzip your html, css, xml, and javascript.
Enable the rewrite domains filter in your Apache.conf if you use CDN (ex. AWS CloudFront)
Set a short cache-control for images and css so pagespeed will be able to extend the cache when you turn on the extend_cache filter.
I also like the rewrite_javascript, dns_prefetch, collapse_whitespace, and combine_javascript filters.
Here are the GitHub Gists that show you how its done.
The apache conf file
The Beanstalk container_commands (they are mostly the same as Charlie's)
Thanks guys! I got it working great following your answer #man2xxl.
You don't have to mess with the /pagespeed/extensions directory though, the beanstalk .ebextensions config can simply be:
packages:
yum:
at: []
10_setup_apache_for_mod_pagespeed:
command: "cp enable_mod_pagespeed.conf /etc/httpd/conf.d"
20_install_mod_pagespeed:
command: rpm -U -iv --replacepkgs mod-pagespeed-*.rpm
30_clear_mod_pagespeed_cache:
command: touch /var/cache/mod_pagespeed/cache.flush
You can install packages by URL. So you don't have to download and distribute the RPM. Something like this works:
packages:
rpm:
pagespeed: https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm
files:
"/etc/httpd/conf.d/zzzz-pagespeed-options.conf":
mode: "00644"
owner: root
group: root
encoding: plain
content: |
# put your pagespeed configuration here
Note that I titled the file zzzz-pagespeed-options.conf so that the httpd server will load it last.
Another advantage of this is you really don't need include any commands whatsoever or worry about copying files over
and maintaining the files in your .ebextensions folder. You just update the files entry in the .config file.

Running S3cmd from PHP Script not working

I want to use s3cmd from my PHP script. Everything is working from shell but same not working from my PHP script.
shell_exec('s3cmd --config=/root/s3cmd.conf ls');
this is not working, and then i gave full path to my s3cmd installation
shell_exec('/usr/sbin/s3cmd --config=/root/s3cmd.conf ls');
Not working in PHP script but the same is working in command,
The PHP file which is calling the shell_exec is in webroot.
Might be the problem that s3cmd is configured as a root user and i am running from PHP which is www-data. if this is the problem how can i create config file for www-data.
Help me what i am doing wrong.
Thanks
EDIT
I am using S3cmd. To run commands in my cron script. cron Script is a PHP script. The user running the cron is web11 and the s3cmd is configured using the root user.
so when i run s3cmd using shell_exec() in my PHP script it fails. But when i run in shell it works fine.
s3cmd ls
This works fine. as i am login using root user.
i tried to run it using runuser command
runuser -l root -c "s3cmd ls"
This works fine and displays list of buckets. But when i run using
runuser -l root -c "s3cmd ls"
This does not work. I tried by giving full path of the s3cmd
/usr/bin/s3cmd ls
this works in shell but not in my PHP script.
I changed permissions 777 for the php script and made root the owner of that user. but still does not work.
How can i run s3cmd from PHP script. ? i am on amazon Ec2 Instance.
Why not use the AWS SDK for PHP? You can have the same functionality using the S3Client.listObjects() method
shell_exec('s3cmd --config=/root/s3cmd.conf ls');
What might be missing here is the target of the S3 command "ls" ?!
It's working fine in PHP scripts like this :
exec("/usr/bin/s3cmd --config=.s3cfg info s3://YOUR-BUCKET/YOUR-FILE 2> /dev/null", $s3output, $s3return);
// switch due to return code (of shell !)
if($s3return == "0"){
echo "my file exists";
}
else {
echo "Error Code : " . $s3return;
}
This asumes, you ran "s3cmd --configure" successfully.

~/.ec2/id_rsa-gsg-keypair not accessible: No such file or directory

I'm trying to launch a Hadoop cluster on Amazon ec2, using the instructions in "Hadoop in Action" (also here: http://wiki.apache.org/hadoop/AmazonEC2).
I've set up my private ssh key and configurations, but when I try to launch a cluster using the command-line tools:
hadoop-ec2 launch-cluster test-cluster 2
I repeatedly get this error:
Warning: Identity file ~/.ec2/id_rsa-gsg-keypair not accessible: No such file or directory.
Permission denied (publickey,gssapi-with-mic).
The ~/.ec2/id_rsa-gsg-keypair definitely exists, though, and I did chmod 600 it:
> chmod 600 ~/.ec2/id_rsa-gsg-keypair
> ls -l id_rsa-gsg-keypair
-rw------- 1 my-username
Any idea what's wrong?
You may have already realized this, but the problem is possibly related to the ~/ path usage. Try using the absolute path /home/username/.ec2