Running shell commands from PHP - With root permissions or sudo - php

I'll explain what I'm trying to do, because someone might have a much better and completely different approach to this.
I'm trying to install FFMPEG, but not just install it, I'm making an automated installer that installs some PHP scripts and also FFMPEG if it's not already installed.
I can't find any automated PHP scripts that can install FFMPEG, so I'm guessing this is more difficuilt than I thought.
I had 3 options to do this:
Build from source on the server using the exec function, this
requires root permissions.
Find a pre-compiled binary of FFMPEG, easily done, and I did it, but
then I realised it didn't have the correct codecs so wasn't working.
Use YUM or APT-GET to install FFMPEG, this would be great but it
requires root permissions.
So, how can I either:
Use YUM/APT-GET from PHP and set sudo
OR
Install codecs for FFMPEG after it has been compiled. Using the pre-compiled binaries that I found, if it were possible then I could just install the missing codecs (lame mp3 I think. Maybe others too). Or do the codecs need to be built into the binary.
I think the prefered option is to use YUM/APT-GET as my targets are mostly Centos 5 and maybe Ubuntu aswell.

sudo in php is a lot easier if you use SSH. Among other things you can make use of "expect" to wait for the appropriate prompts. If there's a way to do that with the built in exec() or system() I'm not aware of it.

If you end up compiling from source or installing from a repo you can handle the install processs through this project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('yum -y install FFMPEG');
//the return will be a string containing the return of the command
echo $return1;
You will need to deal with the edge cases when i.e. there is a GPG key that need to be accepted or dependencies are conflicting. But the project lets you script all Bash interaction.

Related

How to upgrade to PHP 7.0 on macOS Sierra?

I tried to update my PHP version on my mac but I am facing some issues
When i use cURL it freezes and it will never complete the download on the terminal:
This is the cURL command that I am running is: curl -s https://php-osx.liip.ch/install.sh | bash -s 7.2
I tried to download a package manually but I can't extract it or even know how to install it.
You can use home-brew for installation / https://medium.com/#romaninsh/install-php-7-2-on-macos-high-sierra-with-homebrew-bdc4d1b04ea6 , I find it quite better then managing with Mac OS build in PHP
I don't have enough details so I will go in blind guess here.
First judging by screenshot it is stuck on downloading. For how long it was frozen? Maybe you didn't wait enough.
When you downloading the source what you need to do is:
Assuming you downloaded package with name php-7.2.6.tar.gz from PHP Official website.
Execute command tar xzf php-7.2.6.tar.gz from command line. (assuming you are in the same directory as file). It will unpack it in directory called php-7.2.6.
Enter the directory with command cd php-7.2.6.
Enter in terminal ./configure. It will take a while, so make sure to get some coffee.
After ./configure finished, execute command make. This will take a while as well, so you might get a sandwich.
Once command finished executing the last step will be executing command make install. It will be quicker than others, don't worry. After that you will be able to execute php command.
Optional Run make test to make sure everything is fine.
PHP has been installed, HOORAY!
Hope that helped, have a nice day. And if I answered your question, please mark it 'Answered`.

Can I use composer without installing it?

I've noticed on a lot of open source user systems, like UserFrosting or Drupal they some how use composer, without the user actually having to install composer on to there server. I was just wondering how would I be able to do this, I've had a look around and can't find anyway of just using the files.
Thank you.
Yes, you could simply download the composer.phar file, and run it from CLI like:
cd ~/
wget https://getcomposer.org/composer.phar
php composer.phar [command]
You'll need at least PHP v5.6.x and the mcrypt extension installed for the CLI SAPI.

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!

php compile less file using lessc via nodejs on windows 7

I am trying to compile less file using php, and I am trying to use lessc which I installed as nodejs module on my windows 7
I am trying to do :
$command = lessc %s -compress %s
via
exec($command, $output);
and if I do this via cmd it works fine but I can't execute it using above command in php :(.
the path which works are relative to my drive (E:/wamp/my-project/less/hello.less)
Any body knows what I am doing wrong?
Note: I know how I can use lessphp compiler, and I am already using it but I would like to use lessc module installed via nodejs.
Are you able to get the output of the command when it fails? It's possible that lessc isn't directly available in your path. If you installed it via npm install -g then it will likely be in %appdata%, which is a user path'd directory. It really depends upon how the php process gets initiated, but my guess is that this path won't work.
If I need to use an npm module like this, I usually npm install it locally, and invoke the path to lessc.cmd from a relative path to my program.
Hope this helps. Happy coding!

How do I install/use the phpize command?

I am intending to use SQLite 3 with PHP 5. I found this: http://packages.debian.org/etch/web/php5-sqlite3 but I am having problems with installation:
unzip & untar the package
run "phpize"
run "./configure --with-sqlite3=/path/to/your/sqlite3/install
make && make install
(optionally) copy DB/sqlite3.php to /path/to/php/lib/php/DB/sqlite3.php
I understand that phpize is a command used in cmd. But in which folder should I run this command? It seems that I don't have this command installed yet. How do I install this? I'm on Windows, and is using Wampserver for PHP.
Thanks!
The instructions you've got don't typically make sense for Windows, unless you're running GNU Make.
You ought to have a look at this:
http://us2.php.net/manual/en/sqlite3.installation.php

Categories