Unable to change working directory using shell_exec() in php - php

Iam working on my own php MVC. And now, I wanted to create a cli tool for starting a live server which can be used for development purposes. I'm using laravel-zero for building my cli. All the commands are working fine except the command that is supposed to start a server. To start my server, first I need to change my current working directory to public and then start a php server using 'php -S localhost:9000'
I have tried the below code, but it doesn't work:
shell_exec("cd public");
shell_exec("php -S localhost:9000");
I have also tried this, but this too doesn't work:
shell_exec("cd public ; php -S localhost:9000");
Finally after lots of google searches, I tried this, but the same result:
chdir("public");
shell_exec("php -S localhost:9000");
All It shows is just 'The system cannot find the path specified.'

If your current directory when running shell_exec() is your installation root directory, you can just add the path after php
shell_exec("php -S localhost:9000 -t public/");
But since you are using laravel, why dont you just run php artisan serve

Related

Run php server from batch command

Hello everyone I would like to launch a local php server ( php -S localhost:8888 public -t ) from a batch file but it doesn't work .... Do you have an idea how to do that ? Also there are other commands before and after but while debugging I noticed that the problem came from there
you need to add your PHP in your environment path to access it from your cmd
this is how you add a path to your environment path in windows

Why can't I run my laravel app with PHP artisan serve?

I'm working on a laravel app. I made the simple installation of a new laravel app, and in the previous days it was running great.
But now I'm not able to access my project in browser after running php artisan serve. The browser just says page not found and the buffering of that page is like infinite.
I tried to run the server in a different port with:
php artisan serve --port=9000
and then accessed it in http://127.0.0.1:9000/
It run well once, but then it stopped again.
You could try out the following:
php -S localhost:8000 -t public To run withe inbuilt PHP server
Make a fresh installation of a laravel project and copy your files into it.
It didn't work for me but I used composer:
php artisan serve --port=9000
Then it hanged. I press CTRL+C and I got it working without any errors
Try this, I got solved by these;
1) Change the default url to like this - localhost/<your-project-name>/public/ instead of default one 127.0.0.1:8000
or
2) use this php -S localhost:8000 -t public or php -S 127.0.0.1:8000 -t public

PowerBI CLI node: No such file or directory

I am trying to build PHP wrapper for PowerBI. I installed PowerBI Cli (https://github.com/Microsoft/PowerBI-Cli) on my local and when I run any PowerBI Cli command on my terminal, it is working well. It is working well even when I run the commands using the _www user (sudo -u _www powerbi config)
However, when I run them through PHP using either shell_exec or Symphony's Process Component (https://symfony.com/doc/current/components/process.html), I am getting the following exception:
env: node: No such file or directory.
I am facing this issue on Mac Sierra. The commands are working well on Linux using the PHP exec()
Try linking,
"ln -s /path/where/command/is stored/ /to/path/where u want to exec/"
Sometimes the program are stored in usr/local/bin/program meanwhile as per default you are executing in usr/bin/program
And then in shell use the new path you have set.
Example for linking suppose if you have path for command,
/usr/bin/powerbi then with above command you can link new path usr/powerbi after that you can use new path in exec or shell command.
Try using the full path rather than the command. Without knowing your exact path I can't tell you exactly what to do but it would be something like this:
$output = shell_exec("sudo -u _www /path/path/powerbi config");
var_dump($output);
Edit:
Or, change directories first. So using my example above, it would be:
$output = shell_exec("cd /path/path/powerbi; sudo -u _www powerbi config");

How can I run a php without a web server?

I would like to ask if I can run a php without having installed a web server. Do I have to use the php like CGI and run my page via command line? And if so, what are the steps that I do I have to choose through the installation of php? I mean the preferences as CGI and the components after that step?
I installed the php 5.3.3 but is seems not working, I get several message that the php5ts.dll is missing and when I put that file in my ext folder other error messages appear. Are there any configuration files or steps that I have to use?
(is php 5.3.3 suitable for doing something like this?)
If I have to have a web server installed how can I run my php code through the command line?
You should normally be able to run a php file (after a successful installation) just by running this command:
$ /path/to/php myfile.php // unix way
C:\php\php.exe myfile.php // windows way
You can read more about running PHP in CLI mode here.
It's worth adding that PHP from version 5.4 onwards is able to run a web server on its own. You can do it by running this code in a folder which you want to serve the pages from:
$ php -S localhost:8000
You can read more about running a PHP in a Web Server mode here.
For windows system you should be able to run php by following below steps:
Download php version you want to use and put it in c:\php.
append ;c:\php to your system path using cmd or gui.
call $ php -S localhost:8000 command in a folder which you want to serve the pages from.
PHP is a normal sripting language similar to bash or python or perl. So a script with shebang works, at least on linux.
Example PHP file:
#!/usr/bin/env php
<?php
echo("Hello World!\n")
?>
How to run it:
$ chmod 755 hello.php # do this only once
$ ./hello.php
You can use these kind of programs to emulate an apache web server and run PHP on your computer:
http://www.wampserver.com/en/
http://www.apachefriends.org/en/xampp.html

CakePHP Console on Godaddy Hosting

I have been struggling to get my CakePHP site working on a Godaddy "grid hosting" account. My cake app is setup is hosted from a subdirectory on the account, and can be accessed via a subdomain. I had to adjust my .htaccess files to get this working, and now I need to get the CakePHP console working in this environment.
I have the same cake application setup on an Ubuntu server which is hosted on Amazon's EC2 service. Basically a plain out of the box Ubuntu LAMP setup. The CakePHP console works as expected in this environment.
When I try to run the console on Godaddy I get the following message:
CakePHP Console: This file has been loaded incorrectly and cannot
continue.Please make sure that /cake/console is in your system
path,and check the manual for the correct usage of this
command.(http://manual.cakephp.org/)
I've started to add in some debugging code in cake/console/cake.php to find out what's going on. On the godaddy site, when I echo out print_r($this->args) at line 183 I find the array is empty. When I do this on my Ubuntu EC2 instance I get this:
Array
(
[0] => /var/www/www.directory.sdcweb.org/htdocs/cake/console/cake.php
)
It looks like godaddy's command-line PHP isn't passing through the bash shell command line arguments. Does anybody have some advice as to how I might get the CakePHP console working on Godaddy?
The bash script which invokes the Cake shell contains the following
LIB=${0/%cake/}
APP=`pwd`
exec php -q ${LIB}cake.php -working "${APP}" "$#"
exit;
I am thinking that modifying this script may solve the problem.
in the cake shell script (cake/console/cake) change
exec php -q ${LIB}cake.php -working "${APP}" "$#"
to
exec php -q -d register_argc_argv=1 ${LIB}cake.php -working "${APP}" "$#"
after this I found out that calling php like this happened to run the PHP 4 CLI. to fix this here is the final bash script that I am using to invoke PHP 5 on my shared Godaddy hosting
exec /web/cgi-bin/php5 -q -d register_argc_argv=1 ${LIB}cake.php -working "${APP}" "$#"
if you setup a php-based cron job through their hosting control panel, you will find the php command invoked is actually to this php5 executable.
"Please make sure that /cake/console is in your system path."
This is grid hosting so I'm assuming you have a .bashrc file which you can edit. First you need to know the absolute path to your cake sub-directory then use vim or nano to edit your .bashrc
PATH=$PATH:/absolute/path/to/cake/console
Then you can log out and log back in and you should be able to type cake bake from anywhere and it should fix the error your getting (run it from your app directory so it can find your database.php).
Failing a .bashrc file you can export the variable temporarily but you will have to type it every time you log in.
I don't think editing anything in the lib/cake is okay, since it will be gone with your first cake update.
Rather, I changed the register_argc_argv setting from the php.ini by adding the line:
register_argc_argv=On
All seems to work now with me.

Categories