how to install mysql using php - php

me and my hair-brained ideas again...
i am building a mailserver install script and i have hit on something i want to ask before i do just to cover my bases. i know how to use php with the shell_exec command and proc_open commands to run shell commands like sudo and apt-get but what i do not know is how do i do an install that asks for user input? my example is part of the installation calls for mysql server and when you install it via the command line it asks for the password of the admin user for mysql to be set. i have my script get that info from the user in a form and can pass the data along to other pages but how do i do pass it to the shell?
What i know how to do:
$mySql_password = $_POST['mysql_password'];
$output = shell_exec("apt-get -y install <package-id>");
or even:
$mySql_password = $_POST['mysql_password'];
$output = shell_exec("apt-get --force-yes install <package-id>");
how would i go about passing the password to it after i have it set as a variable?
keep in mind that this script is going to be run from the desktop of the server(if they have a desktop) or via a web browser and the script will have a self destruct mode at the end to erase the install script to prevent someone from forgetting about it. ssl is also going to be recommended as pre-install checklist.

After looking around a bit (google "Unattended MySQL Install" or something among these lines), something that should help you is DEBIAN_FRONTEND=noninteractive.
Try this to install without password and set it afterwards :
<?php
shell_exec('echo '.$rootPassword.' | DEBIAN_FRONTEND=noninteractive sudo -S aptitude install -q -y mysql;
mysql -uroot -e "UPDATE mysql.user SET password=PASSWORD("'.$mysqlPassword.'") WHERE user='root'; FLUSH PRIVILEGES;');
?>
Alternatively, debconf-set-selections should allow you to set a default password so mysql is directly installed with it.

Related

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!

Questions on an external raspberry pi database

Okay so I need to have a server with a databse for a smartphone applications I'm working on, and there are a few requirements.
-SQL database
-Returns queries with JSON
So i had this old Raspberry Pi laying around and i wanted to set it up for it. But theres something im uncertain of, and google haven't helped me yet.
I planned on use SQLite on the Pi, and wanted to interact with it in PHP. Then use the PHP to convert into JSON, which would then be retrieved by the smartphone.
The thing is i don't know what service i can use on the PI to make this .PHP file able to be found from another device
You can install nodejs by running:
sudo apt-get install nodejs nodejs-legacy npm
For SQL database you can run
sudo apt-get install mariadb-server mariadb-client
Install your nodejs mysql module by:
sudo npm install -g node-mysql
That should get you started.

PDO database browser for php (like phpmyadmin)

I would like to know if there is a php application able to browse any database supported by PDO. Something like phpmysqladmin but for PDO.
I have tried google and I didnt found any, checked http://alternativeto.net/software/phpmyadmin/ too, and nothing found...
I just want to browse a firebird/odbc database using a webbrowser, and I though that a pdo browser would be the best option for that.
You can install an php administration tool like Firebird Web Admin
sudo apt-get install php5-interbase git-core
sudo /etc/init.d/apache2 restart
git clone git://github.com/mariuz/firebirdwebadmin.git
mv firebirdwebadmin /var/www/firebirdwebadmin
load in the browser
localhost/firebirdwebadmin

sudo command over php ssh2

I have a user with sysadm rights, and can login as root by using sudo su -, which is nopasswd set in etc/sudoers.
Now, I am trying to execute a sudo command from php, running from WAMPserver on windows.
I've tried using ssh2_exec(), exec() of phpseclib, but the commands return no output.
My ssh2 connect function uses the same user with sysadm rights.
How do I achieve this?

Problem with exec() in PHP

Well, i have this program i need to run via either functions however it is located on my dekstop (this ubuntu 11.04).
I moved it to /home/Username, but no dice.
I run
$blah = exec('sudo | echo mypassword | /home/server1/program commandhere', $test);
var_dump($test);
var_dump($blah); ?>
The output is nothing.
I was told if i wanted to run it via sudo i needed to add the Apache user which is www-data to the sudoers list, i added it, but no luck again.
Basically, i've tried A LOT of things, it just wont run. Why?
EDIT:
If i paste that into the terminal it works great, just not with exec,system nor passtrhu.
Use echo mypassword | sudo -S instead.
It also depends on which user has sudo privileges. If you want to run this from the apache process, you need to give the apache user sudo privileges as well.
Also, just to clarify, the command should be:
echo mypassword | sudo -S /home/server1/program commandhere
Look into your security log. Not sure where this is on Ubuntu, possibly /var/log/secure or /var/log/messages. I'm betting that you find a message there similar to sudo requires a TTY, or sorry, you must have a TTY to run sudo indicating that sudo is configured not to work without a real interactive shell. That is, sudo won't permit you to use it in a script or to be called by an external program.
I recently dealt with this issue myself while trying to bind a Gnome keyboard shortcut to a sudo command.
If this is the case, you'll need to comment out the following line in /etc/sudoers
#Defaults requiretty

Categories