installing PHP module from Elastic Beanstalk - php

I am trying to configure my AWS Elastic Beanstalk to work with mongo, all I need to do is install the mongo driver for PHP and update the php.ini file
To do this, usually I would ssh into the EC2 and run:
sudo pecl install mongo
But this would require using a custom AMI which isnt the best way to go.
It is better to use config files to install the software required onto the standard AMI.
So to do this, I have done the following:
created directory .ebextensions
created file mongo.config
in it I have put the following:
packages:
pecl: install mongo
However upon deployment, I get the following error:
"option_settings" in one of the configuration files failed validation. More details to follow.
and
'null' values are not allowed in templates
So I am wondering how this config file needs to be laid out in order to install the mongo extension?
I have read the info here: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html
but I am not quite understanding how to do this specific task
Help would be appreciated , thanks! :)

pecl is not a valid package manager on Amazon Linux and therefore cannot be used under the packages key of an .ebextensions config.
To install a PECL package it is enough to add a single command under the commands key. To avoid that Beanstalk tries to install the extension twice on follow-up deployments add a PHP console command to the test key that checks if the extension is already installed:
commands:
install_mongo_driver:
command: pecl install mongo
test: "php -r \"exit(extension_loaded('mongo') ? 1 : 0);\""
If the test result is true, i.e. exit(0), then the command gets executed - otherwise not. Please note that a exit code of 0 means "No errors" in a shell context.
See also the description at http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-commands.

I have figured it out and thought I would share what I found. Thanks to Hudku (http://blog.hudku.com/2013/02/innocuous-looking-evil-devil.html#elastic-beanstalk.config) for the excellent article:
1) Create myapp.config
2) enter the following into it
packages:
yum:
dos2unix: []
container_commands:
01-command:
command: rm -rf /myapp/ebextensions
02-command:
command: mkdir -p /myapp/ebextensions
03-command:
command: cp -R .ebextensions/* /myapp/ebextensions/
04-command:
command: dos2unix -k /myapp/ebextensions/mongo.sh
05-command:
command: chmod 700 /myapp/ebextensions/mongo.sh
06-command:
command: bash /myapp/ebextensions/mongo.sh
Then create mongo.sh file and put in it something like:
#!/bin/bash
if [ ! -f /mongostatus.txt ];
then
pecl install mongo
echo "mongo extension installed" > /mongostatus.txt
apachectl restart
fi
This will install mongo php extension and restart apache so the install takes affect.

I just accomplished the same thing thanks to the answer above, and figured out it can be done with less lines and less files for those interested...
# ~/project/.ebextensions/project.config
# Logger messages can be viewed in /var/log/messages
files:
"/tmp/test.sh":
content: |
# This file will be created and can then
# be executed by a command call below.
logger TEST FILE CALLED
commands:
01-command:
command: logger CALLING TEST FILE; sh /tmp/test.sh;

Related

How do I add php ext-mailparse to an elastic beanstalk instance?

I've been unable to add the MailParse PHP extension (https://pecl.php.net/package/mailparse) to an instance of Elastic Beanstalk running PHP 7. My goal is to get it added into the boot sequence so that it's always installed when an instance is created.
My problem is that Amazon's version of Linux for EB doesn't offer PECL, so I am unsure how to get it loaded.
I've tried to adapt various approaches for installing other php extenions/modules, but haven't had any success.
https://packagist.org/packages/php-mime-mail-parser/php-mime-mail-parser - I tried including this via my composer.json file, but it failed because "ext-mailparse" wasn't installed.
http://wiki.cerbweb.com/Installing_PHP_Mailparse_Ubuntu - I tried running these commands to install the extension, but the first command to install the dependencies failed.
https://serverpilot.io/community/articles/how-to-install-the-php-mailparse-extension.html - "sudo: apt-get: command not found"
I have a feeling there is an easier way to get this done but I'm stuck. Can anyone help?
Create two files:
.ebextensions/01mailparse.config
commands:
01install_mailparse:
command: "pecl7 install --force mailparse"
Note the use of the --force flag. I added this since sometimes AWS EB automatically re-deploys the app in a way that PECL fails if it find the extension being already installed.
.ebextensions/02prioritize.config
commands:
01change_mailparse_load_priority:
command: "sed '/extension=\"mailparse.so\"/d' /etc/php.ini > /etc/php.ini && echo 'extension=\"mailparse.so\"' > /etc/php-7.0.d/zz_mailparse.ini"
This removes the mailparse extension registration from the php.ini file (PECL was adding the line at the top, weird) and registers it to be loaded at the end of the list (zz prefix).
Note that I used two files. For some reason, using two commands on the same file was making the deploy file. I'd appreciate if someone could clarify this.
To add to #Mauro's answer the following allows you to install mailparse and remove the extension from /etc/php.ini in a single file.
.ebextensions/01_mailparse.config (PHP 7.x)
commands:
01_mailparse_install:
command: |
pecl7 install --force mailparse
sed -i '/extension="mailparse.so"/d' /etc/php.ini
files:
"/etc/php.d/mailparse.ini":
mode: "000644"
owner: root
group: root
content: |
extension="mailparse.so"
.ebextensions/01_mailparse.config (PHP 5.6)
commands:
01_mailparse_install:
command: |
pecl install --force mailparse-2.1.6
sed -i '/extension="mailparse.so"/d' /etc/php.ini
files:
"/etc/php.d/mailparse.ini":
mode: "000644"
owner: root
group: root
content: |
extension="mailparse.so"
The | allows multi line values. I modified the sed command and added the files block to allow it to work on multiple PHP versions without much change.

How to run bundle from PHP script

I'm writing a webhook to automatically publish a site when I push to GitHub. Part of the process requires that I build the site with
bundle exec middleman build --clean
I'm trying to invoke that with a PHP script, the script called by the GitHub webhook, so the user is www-data. No matter what I try, however, I'm getting an error that bundle cannot be found.
How can I run a bundle command from a PHP script?
I was able to figure this out. First, I installed rvm as a multi-user installation to ensure the www-data account can access it.
$ curl -sSL https://get.rvm.io | sudo bash -s stable
Install the desired ruby version, in my case 2.3.1, then set rvm to use it:
$ rvm install 2.3.1
$ rvm use 2.3.1
Run gem to install any gems that are needed. Because rvm is a multi-user installation, these gems are stored to the system and not your specific user.
$ gem install packagename
I don't know if this is necessary, but I would close the SSH session and reopen it. rvm messes with environment variables, so better safe than sorry.
Run env to print all environment variables. printenv also works if env doesn't for some reason. You'll get a big list of everything set, you only need the ruby-related ones. Do not copy/paste these values, they are examples I pulled from my system. Yours will be different!
PATH=/usr/local/rvm/gems/ruby-2.3.1/bin:/usr/local/rvm/gems/ruby-2.3.1#global/bin:/usr/local/rvm/rubies/ruby-2.3.1/bin:/usr/local/rvm/bin:/home/steven/bin:/home/steven/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
rvm_bin_path=/usr/local/rvm/bin
GEM_HOME=/usr/local/rvm/gems/ruby-2.3.1
IRBRC=/usr/local/rvm/rubies/ruby-2.3.1/.irbrc
MY_RUBY_HOME=/usr/local/rvm/rubies/ruby-2.3.1
rvm_path=/usr/local/rvm
rvm_prefix=/usr/local
rvm_ruby_string=ruby-2.3.1
GEM_PATH=/usr/local/rvm/gems/ruby-2.3.1:/usr/local/rvm/gems/ruby-2.3.1#global
RUBY_VERSION=ruby-2.3.1
Now we need PHP to recognize these variables. You'll need to find the right file on your system, which can be tricky. I don't have a way of knowing which one is correct, I used trial and error.
The file on my system is /etc/php/5.6/fpm/pool.d/www.conf. Add all of the environment variables you previously grabbed into this file with the below format. Note that you DO need PATH in here as well!
env[rvm_path] = /usr/local/rvm
env[rvm_prefix] = /usr/local
Now restart php-fpm. Your service name may be different from mine; I'm using the 5.6 build from ondrej/php.
Ubuntu 15.04 and newer (systemd):
$ sudo systemctl restart php5.6-fpm
Ubuntu 14.10 and newer:
$ sudo service php5.6-fpm restart
Finally, in the script itself you'll need to cd to the directory you're running the bundle command from. My short script is this:
cd /opt/slate
/usr/bin/git reset --hard
/usr/bin/git pull
bundle exec middleman build --clean
cp -R /opt/slate/build/* /var/www/docs
Works for me!

Laravel Installation/Configuration - Command Not Found

Trying to install Laravel on Ubuntu Server 14.04. After installing PHP 7 I enter:
curl -sS https://getcomposer.org/installer | php
I get: Composer successfully installed to: /home/ubuntu/composer.phar
sudo mv composer.phar /usr/local/bin/composer
composer
Works fine
composer global require "laravel/installer"
I get:
nano ~/.bashrc
I include this export PATH="~/.composer/vendor/bin:$PATH" at the very bottom.
sudo service apache2 restart
Restart it
laravel
I get: "laravel: command not found"
Why does it not understand the command laravel?
I'm writing this answer for all the future Google searchers. I had a similar issue and I solved it in the following way:
First run:
sudo apt-get install zip unzip php7.0-zip
Then, edit your ~/.bashrc file and append the following line:
export PATH="$PATH:$HOME/.composer/vendor/bin"
or
export PATH="$PATH:$HOME/.config/composer/vendor/bin"
Which one is yours, depends on the line composer printed out during installation of laravel, followed by /vendor/bin. In my case (and in the case of the question), this was $HOME/.config/composer, so I had to do it the second way:
Don't forget to call
source ~/.bashrc
at the end. Enjoy Laravel!
From your screenshot, I can see that there may be a chance the laravel packages were not installed correctly as you don't have zip and unzip installed on your system.
Run the following command to install these first and then try again to install laravel installer:
sudo apt-get install zip unzip
Regarding the export PATH use $HOME instead of the tilde ~ sign. Hope this will solve your problem. Seems like a path error.
export PATH="$PATH:$HOME/.composer/vendor/bin"

Installing ElastiCache Cluster Client on PHP AWS Elastic Beanstalk (without creating resource)

Elastic Beanstalk does not, by default, install the ElastiCache Cluster Client PHP module. This is needed to connect to an ElastiCache node cluster. Reading around, most of the instructions relate to creating an ElastiCache resource (which I assume will also install the PHP module on the Elastic Beanstalk). I want to install the PHP module without creating the resource as I want to use an existing cluster.
(64bit Linux PHP5.5)
The module is not installed by default in Beanstalk nor any EC2 instances. You have to do this yourself. This also is something completely different than creating a resource. You can do one without the other.
The ElastiCache Cluster Client for PHP is an extension that you can install via pecl on your instances. You can do this manually but if the instance is ever destroyed you have to do this again. Therefore it is much better to include the extension's install procedure as part of your deployment process. In a beanstalk app you can do this by adding configurations files in your .ebextensions dir.
For example, create these two files. I took these from an actual config file:
#.ebextensions/01fileselasticachephp.config
files:
"/tmp/AmazonElastiCacheClusterClient-latest-PHP54-64bit.tgz" :
mode: "000777"
owner: ec2-user
group: ec2-user
source: http://elasticache-downloads.s3.amazonaws.com/ClusterClient/PHP-5.4/latest-64bit
#.ebextensions/02setupelasticachephp.config
commands:
01install:
command: "pecl install /tmp/AmazonElastiCacheClusterClient-latest-PHP54-64bit.tgz"
The actual name of the files don't matter. They are for your own organization purposes. Anything in that directory with a .config extension will be executed in alphabetical order, that's why you want to prefix your files with a number so that they get executed in the right order: first download the extension and then install it. Mind you that you can also do it all at once in one file. I split it in two because because my actual config files were a lot bigger.
Once you have these files in place do a deployment and the Elastic Cache Cluster Client will be installed.
Note that at the time I deployed this, only the 5.4 client was available that's why my example shows that. I don't know if there is a 5.5 client so it's up to you to find out. You should only need to change the file name and URL to point to the 5.5 extension and should be all set to go.
UPDATE (as of 10/2020)
The solution above didn't work for me with the current software versions, but it definitely pointed me in the right direction. What didn't work was specifically the pecl install command (even using pecl7): it always threw the error "could not extract the package.xml file from [...]" and I couldn't find a solution for it.
So here's the config file that worked for me:
commands:
02-get-file:
command: "wget https://elasticache-downloads.s3.amazonaws.com/ClusterClient/PHP-7.3/latest-64bit"
02-untar:
command: "sudo tar -zxf latest-64bit amazon-elasticache-cluster-client.so"
03-move-file:
command: "sudo mv amazon-elasticache-cluster-client.so /usr/lib64/php/7.3/modules/"
04-create-ini:
command: "grep -qF 'extension=amazon-elasticache-cluster-client.so' /etc/php-7.3.d/50-memcached.ini || echo 'extension=amazon-elasticache-cluster-client.so' | sudo tee --append /etc/php-7.3.d/50-memcached.ini"
05-cleanup:
command: "sudo rm latest-64bit*"
06-restart-apache:
command: "sudo /etc/init.d/httpd restart"
Hope this helps other people!

How do I install Composer on a shared hosting?

I have these things:
the file http://api.odtu.lu/composer.phar
http://api.odtu.lu/phpinfo.php
ftp access
cPanel
Cron jobs on FreeBSD
PHP, Perl, CGI-BIN, Python, Curl.
How can I install Composer? (My aim is to install Restler)
Edit: I do not have SSH access.
This tutorial worked for me, resolving my issues with /usr/local/bin permission issues and php-cli (which composer requires, and may aliased differently on shared hosting).
First run these commands to download and install composer:
cd ~
mkdir bin
mkdir bin/composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar bin/composer
Determine the location of your php-cli (needed later on):
which php-cli
(If the above fails, use which php)
It should return the path, such as /usr/bin/php-cli, /usr/php/54/usr/bin/php-cli, etc.
edit ~/.bashrc and make sure this line is at the top, adding it if it is not:
[ -z "$PS1" ] && return
and then add this alias to the bottom (using the php-cli path that you determined earlier):
alias composer="/usr/bin/php-cli ~/bin/composer/composer.phar"
Finish with these commands:
source ~/.bashrc
composer --version
It depends on the host, but you probably simply can't (you can't on my shared host on Rackspace Cloud Sites - I asked them).
What you can do is set up an environment on your dev machine that roughly matches your shared host, and do all of your management through the command line locally. Then when everything is set (you've pulled in all the dependencies, updated, managed with git, etc.) you can "push" that to your shared host over (s)FTP.
I have successfully installed Composer (and Laravel) on my shared hosting with only FTP access:
Download and install PHPShell on a shared hosting
In PHPShell's config.php add a user and an alias:
php = "php -d suhosin.executor.include.whitelist=phar"
Log in to PHPShell and type: curl -sS https://getcomposer.org/installer | php
When successfully installed, run Composer: php composer.phar
You can do it that way:
Create a directory where you want to install composer (let's say /home/your_username/composer)
Go to this directory - cd /home/your_username/composer
Then run the following command:
php -r "readfile('https://getcomposer.org/installer');" | php
After that if you want to run composer, you can do it this way (in this caseyou must be in the composer's dir): php composer.phar
As a next step, you can do this:
alias composer="/home/your_username/composer/composer.phar".
And run commands like you do it normally: $ composer install
Hope that helps
I was able to install composer on HostGator's shared hosting. Logged in to SSH with Putty, right after login you should be in your home directory, which is usually /home/username, where username is your username obviously. Then ran the curl command posted by #niutech above. This downloaded the composer to my home directory and it's now accessible and working well.
SIMPLE SOLUTION (tested on Red Hat):
run command: curl -sS https://getcomposer.org/installer | php
to use it: php composer.phar
SYSTEM WIDE SOLLUTION (tested on Red Hat):
run command: mv composer.phar /usr/local/bin/composer
to use it: composer update
now you can call composer from any directory.
Source: http://www.agix.com.au/install-composer-on-centosredhat/
Most of the time you can't - depending on the host. You can contact the support team where your hosting is subscribed to, and if they confirmed that it is really not allowed, you can just set up the composer on your dev machine, and commit and push all dependencies to your live server using Git or whatever you prefer.

Categories