I often use the PHP built-in server from CMD (windows) with: php -S 127.0.0.2:8000
But, I want to create a CMD File, which I just double click to run the server.
I tried with : exec, chell_exec, system but it doesn't work.
What you need is a batch programm.
Take a look on these sources:
[1]
[2]
How to do it:
create a file e.g. yourbatch.bat
add following line: php -S 127.0.0.2:8000 or start php -S 127.0.0.2:8000
save
double click your file
Related
The following command opens foo.js in Sublime Text if executed directly in terminal:
$ subl /var/www/abcproj/js/foo.js
But trying this in PHP (PHP-FPM + Nginx server) does nothing:
exec('subl /var/www/abcproj/js/foo.js');
Is there any way to do this?
It is because of on http request there is php interpreted by user www-data.
Try using a command line. It should work.
You can also set up the server by the command as a current user:
php -S localhost:8000
In my case, it works.
I am using PHP built in server for testing and I was wondering is there a way you can hide cmd window when launching built in server using command php -S 127.0.0.1:809 -t Folder
I am currently working on Windows 10 so I need a Win solution.
not hundred percent sure on this, but you might try this one from:
What is cmd's equivalent to Bash's & (ampersand) for running a command without waiting for it to terminate?
so yours could be something like:
start /B php -S 127.0.0.1:809 -t Folder
You can create vbs script (run.vbs) and you can put this code in it
Dim oShell
Set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "cmd /C CD resource\php && php -S 127.0.0.1:809 -t HTML", 0
Set oShell = Nothing
0 in line signal for not displaying command line window.
How can I serve and open the website from the current directory in one command with php built-in webserver?
The command used for php built-in webserver is:
php [options] -S <addr>:<port> [-t docroot]
However this is a running command, so the following command does not work:
php -S 127.0.0.1:8000 && open 127.0.0.1:8000
Purpose is creating a single alias command to open the website in a browser directly after starting the webserver (all from a single command):
alias lserve="php -S 127.0.0.1:8000 && open 127.0.0.1:8000"
Run the server in background:
php -S 127.0.0.1:8000 & open 127.0.0.1:8000
Note that I'm using just a single & which starts a job in background. This is not related to the logical and operator &&. Bash's syntax does not allow the command that follows the & to be separated by a ;
However, there is still a problem with that solution. Since the server runs in background, you cannot close both the browser and the server with a single ^C. To achieve that you need to start both commands in a sub shell:
(trap 'kill 0' SIGINT; php -S 127.0.0.1:8000 & open http://127.0.0.1:8000)
Now you can put that into an alias:
alias lserve="(trap 'kill 0' SIGINT; php -S 127.0.0.1:8000 & open http://127.0.0.1:8000)"
In the sake of helping someone passing here:
chromium-browser-app=http://127.0.0.1:8000 | php -S 127.0.0.1:8000
I'm new in using Linux, I'm trying to write a PHP code which can run .exe linux compatible file, I've made a short shell script
hello bash script:
#!/bin/bash
./program.exe file.mp4 // file.mp4 is an an input for .exe
echo "Hello World!"
shell.php:
<?php
$output = exec ("./hello ");
echo "<pre>$output</pre>";
?>
Now when I run shell.php using web browser it shows Hello World! but the .exe doesn't run, however when I run php using terminal command php shell.php, It works fine.
I think I'm having problems with permissions but I'm new with Linux and I don't know how to solve this.
Update:
I ignored the shell script and I used
<?php
$output = shell_exec ("cd /var/www/ && ./program.exe file.mp4 2>& " );
?>
also I granted access to program.exe
chmod 777 program.exe
the error I receive in the browser :could not open debug.bin!
use the absolute path to hello executable exec("sh path/to/the/file")
I'm using something similar to call an app compiled with mono on a remote ubuntu webserver and return it's output to the calling script.
For any of this to work properly wine needs to be already installed.
On Ubuntu systems try:
sudo apt-get -y install wine
You then need to know the owner of the web server process. If you are running the apache web server try the following:
cat /etc/apache2/envvars | grep "RUN"
The output will look something like this:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
Now that you have the name of the process owner, which in this case is www-data you should ensure the file is owned the user and its group:
sudo chown www-data /var/www/program.exe
sudo chgrp www-data /var/www/program.exe
Finally, we can invoke the application from inside our PHP script by passsing it as a parameter to 'wine' and using its full file path.
<?php
$output = shell_exec("wine /var/www/program.exe file.mp4" );
?>
Any output from the above shell command sent to the command line will be saved in the PHP script variable $output.
It looks like you are trying to do some output redirection with your use of program.exe file.mp4 2>& so I've left that off of the example for clairity.
Try using the absolute path, such as exec("sh /path/to/file")
Generally, php is run as www or apache, so make sure that the execute access permission is granted to all user.
PHP 5.4 supports a built-in web server for development purposes. The app we are developing is configured via environment variables.
With Apache you'd do this:
SetEnv FAVORITE_COLOR white
With the normal CLI you can do this:
$ export FAVORITE_COLOR=black
$ php -a
php > echo $_SERVER['FAVORITE_COLOR'];
Is there a way to set these variables for the built-in web server?
Looks like E is excluded from variable_order setting running the built-in server. If you add the E to the variable_order setting, it works:
test.php
<?php
var_dump($_ENV['FOO']);
shell:
FOO=BAR php -d variables_order=EGPCS -S localhost:9090 /tmp/test.php
output:
string 'BAR' (length=3)
Tested on PHP 5.4.12
I use Window DOS to start the PHP server. I store my server startup commands in a text batch file (.bat) to save me from having to select and copy all of the commands at once and paste it into the DOS terminal (note the last blank line that I copy as well so the PHP server will automatically start when I paste the commands into DOS, otherwise I would need to manually use the Enter key to start the server).
Q:
cd Q:\GitLabRepos\myapps\test1
set APPLICATION_TITLE=My testing application with this first env variable
set SOME_OTHER_ENV_VAR2={"myJsonElement":"some value"}
E:\PHP8\php.exe -d variables_order=E -S localhost:8000 -c php.ini
The commands above explained:
The first line Q: changes to the drive where my code resides. The second line cd Q:\GitLabRepos\myapps\test1 changes directories to my root PHP application code (which is where I want to start the PHP server). Next I set some environment variables on lines 3 and 4. Then finally I start the PHP server with the -d variables_order=E parameter so I can use either $_ENV or getenv() to retrieve the environment variable values in my PHP code (eg. $_ENV['APPLICATION_TITLE'] or getenv('APPLICATION_TITLE')). If you exclude -d variables_order=E from the server startup command then you can only use getenv() to access the environment variables. I use the -c php.ini parameter to load additional PHP settings from a php.ini file but this can be excluded for simple server setup.
Then if I have a Q:\GitLabRepos\myapps\test1\index.php script with the following code:
<?php
echo getenv('APPLICATION_TITLE').'---'.$_ENV['APPLICATION_TITLE'].'...'.getenv('SOME_OTHER_ENV_VAR2');
?>
If I visit localhost:8000 in a web browser I should see
My testing application with this first env variable---My testing application with this first env variable...{"myJsonElement":"some value"}.
On Windows:
SET FOO=BAR
php -s localhost:9090