To load my ini file from a path outside the codebase I use apache to define the path and with php I use apache_getenv / getenv
It seems phpunit doesn't understand the apache_getenv command - is this a known issue and are there any solutions besides hardcoding paths?
When you run php from the commandline it does not use any of the Apache configuration. But you can still access environment variables. So, first change to use getenv everywhere (instead of apache_getenv). Then, if you're starting php from bash, run it like this:
export MY_CONFIG = "/path/to/my.ini"
phpunit
You only need to do the export once per bash session. You could wrap that up in a 2-line bash script.
This approach has one issue, which is that you have configuration in two places now. Murphy's Law tells us those two files will try really hard to get out of sync. So, an alternative approach is to use a custom entry in php.ini (*). And then in your code use get_cfg_var to fetch those values. Be aware that some people do not like this approach, e.g. create arbitrary values in php.ini Use it wisely: if your machine is dedicated to the application that needs this setting, then it is a fair choice. If it is just one script among many, and on a multi-user machine, then it is dubious.
A third approach is a symlink to your real ini file that is kept with your script. Then your script will always find it, however it is started. I like this approach best, but it is not so dynamic. I'm guessing the point of using an environment variable was because you use a different value for different virtual hosts, or something like that.
*: Make sure you use the php.ini file that is common to apache and cli: some setups also have a configuration file that is just for apache, and just for the cli. On Ubuntu machines I would create a file under /etc/php5/conf.d called myconfig.ini, as that is easier to maintain, and that directory is used by both apache and cli.
Related
I got a problem with an API (PHP) I created. The API should create a pdf document depending on the data that is sent. The script works perfectly, running it directly on Ubuntu, Plesk is running on.
The part that is not working is the following:
$cmd = sprintf("pdflatex -interaction nonstopmode %s", escapeshellarg(file));
exec($cmd, $foo, $ret);
The tex-file seems not to be touched... no log files or anything.
Any ideas about this?
Thank you
Latex is usually run by experienced users either raw on the command line or via IDE with console interaction, since there is often the need to see from console feedback why a compilation may fail.
The Simplest check for checking is PdfLaTex installed? is at the system console to enter pdflatex --version this should confirm at least a minimal pdflatex has been found in the environment path and show the variant after a rough value for the level of pi.
So in this case responded pdfTeX 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian)
When trying to run unseen it is not easy to get feed back so the smallest test with a small known sample is again at the console to run in a current working directory with batch mode :-
pdflatex -interaction=nonstopmode sample.tex
This should run with little need for checks, however if the paths to pdflatex are not configured you may need to add the location of the pdflatex executable. Thus you discovered needed /usr/bin/pdflatex -interaction=nonstopmode file.tex
There are many different dependencies in LaTeX packages and modules that require the "env" settings be well defined in terms of inter-related paths (e.g. to support files etc.) so it is worth checking those out from the extensive documentation.
Some modules may require elevation to escape shell restrictions via -shell-escape and the above command may need temporary elevation for one run (it should not be needed constantly) so only include when you know exactly from documentation why a module requires it.
It is tempting for TeX novices to think there is an advatage to use sub folder in a working directory for "chapter and verse", since the working directory may "look" cluttered by potentially dozens of runtime files, however much of LaTeX depends on very specific relative pathing and the best way to avoid errors is not need to keep redefining images or other components by variables, simply say it is in the same folder.
Likewise for the packages they should also be set to a fixed location by means of env variables i.e. for bin folder and fonts and other modular supporting components. It is worth reviewing https://tug.org/texlive/quickinstall.html and note the section about minimal path definition but there is much more than that basic setting required by post installed components
Obviously this is a common issue: we have PHP web applications that are managed by git or other SCMs, and are deployed (ideally) in 3+ different environments that should be extremely similar. The only differences should be in the specific configuration such as addressing of external resources (eg: database host, memcache, etc).
Here are the three methods I've seen or used:
Maintain an array of hostnames => environment types:
$hosts = array ('host1.example.com' => 'production', 'staging.example.com' => 'staging' ...)
Have a config file that is in the .gitignore (or equivalent for other SCMs) with an example version to copy and edit
Use http.conf to set an env_var: "SetEnv ENVIRONMENT dev" in your http.conf and then $environment = get_env('ENVIRONMENT'); in your scripts.
Are there other methods as well? I know each of these have benefits and pitfalls--what are those?
On *nix:
You can set environment variables directly in /etc/environment to apply them system-wide.
You can set environment variables for Apache only in /etc/apache2/envvars (Ubuntu) or /etc/sysconfig/httpd (CentOS).
Since environment configuration is environment-specific and not application-specific, it doesn't make sense to use a .htaccess or other application config file to set these.
We use Jenkins to pull from git server whenever a push event is issued.
In it's config we have a line that is different for each server:
mv config_<env>.php config.php
while config.php is in git ignore.
Use set environment option can be done by any major server.
Default to production if it is not defined.
This way you will not have to maintain list of servers. And can keep it out of git
The web application I've been working on for the past couple of years uses a Java API backend with a PHP layer on the front for templating etc. We use Maven to control the difference in environments.
I'm not entirely sure how it all fits together as I didn't set it up myself. What we have though is a large settings.xml document separated into various environments. Configuration options are then pulled from this file depending on the environment.
Similar to Udan we also use Jenkins to pull it all together when publishing to dev/staging/live.
It appears there is a Maven specifically designed for PHP here: http://www.php-maven.org/
I've never used it so can't say how useful it would be, worth a look though maybe.
My setup looks like this:
I work on WinXP, while my Test-Environment (Debian / Apache / MySQL / Squid / PHP / XDebug / ...) is running in a VMWare-environment. The project files are directly accessible in a shared folder, so I can do XDebugging using Eclipse.
Now I have a script that has to be invoked on the CLI and I want to perform something as similar as possible to the usual XDebugging ... not neccessarily live but I need information about which method is invoked when and what value is held by any local variable or attribute at any given time, basically.
How do I accomplish that?
I produced an XDebug-profile, but examining it in Wincachegrind did not reveal anything about variables whatsoever.
The CLI-Debugging from Eclipse-PDT seems to founder on the non-available access to the PHP-interpreter, as far as I understand the problem.
I would be happy not having to place echos everywhere.
I'm open for pretty much anykind of solution also if it is not trelated to XDebug, but I guess if there is one it's connected to it.
Best
Raffael
My investigations revealed the existence of some xdebug-features I didn't use so far which might do the job. Due to certain restrictions I couldn't try my hand at it.
But I figured that I could just require_once the php-script within another php-file which I just put into a folder that I could access from my browser. So I just started my XDebug-Session from Eclipse using this go-between.
I'm trying to make a web app that will manage my Mercurial repositories for me.
I want it so that when I tell it to load repository X:
Connect to a MySQL server and make sure X exists.
Check if the user is allowed to access the repository.
If above is true, get the location of X from a mysql server.
Run a hgweb cgi script (python) containing the path of the repository.
Here is the problem, I want to: take the hgweb script, modify it, and run it.
But I do not want to: take the hgweb script, modify it, write it to a file and redirect there.
I am using Apache to run the httpd process.
Ryan Ballantyne has the right answer posted (I upvoted it). The backtick operator is the way to execute a shell script.
The simplest solution is probably to modify the hgweb script so that it doesn't "contain" the path to the repository, per se. Instead, pass it as a command-line argument. This means you don't have to worry about modifying and writing the hgweb script anywhere. All you'd have to do is:
//do stuff to get location of repository from MySQL into variable $x
//run shell script
$res = `python hgweb.py $x`;
You can run shell scripts from within PHP. There are various ways to do it, and complications with some hosts not providing the proper permissions, all of which are well-documented on php.net. That said, the simplest way is to simply enclose your command in backticks. So, to unzip a file, I could say:
`unzip /path/to/file`
SO, if your python script is such that it can be run from a command-line environment (or you could modify it so to run), this would seem to be the preferred method.
As far as you question, no, you're not likely to get php to execute a modified script without writing it somewhere, whether that's a file on the disk, a virtual file mapped to ram, or something similar.
It sounds like you might be trying to pound a railroad spike with a twig. If you're to the point where you're filtering access based on user permissions stored in MySQL, have you looked at existing HG solutions to make sure there isn't something more applicable than hgweb? It's really built for doing exactly one thing well, and this is a fair bit beyond it's normal realm.
I might suggest looking into apache's native authentication as a more convenient method for controlling access to repositories, then just serve the repo without modifying the script.
I'm not much of a programmer, PHP is where I'm comfortable. And sometimes I find that I need to do things, such as arrange files or rename files on a mass scale on my computer. And I think I could do this with PHP but I can't of course.
So I was curious, is there a way I could run PHP files as kind of exe files.
EDIT: Fairly important point, using Windows.
just use php.exe (put it in your path) and the name of the php file you want to execute
You should have a look at php gtk
It's not as bad as you put it. PHP may be a very good tool for string related stuff like parsing, renaming etc. Especially if you know PHP.
To use php as script you should add #!/path/to/php as first line and set execution permissions on unixoid systems. In windows you can simply assign the php file ending with your php cli exe so you can click on them or use the script with the "start" command in the windows shell. But make sure that you write your scripts in a way that it is sensible to the current working directory. It may be different to what you might expect sometimes.
to be able to execute php files with double click, just like normal programs, go to the command line, then type
ftype php_script "C:\path\to\php.exe" "%1"
assoc .php=php_script
Check out WinBinder
Sure, just add #!/path/to/php to the top of the file, add the code in tags, and run it as a shell script.
Works fine - the php binary you use is either the cgi one or the purpose built CLI version.
http://www.php-cli.com/
http://php.net/manual/en/features.commandline.php
it would appear so, yes.
Download Wamp Server, install it. Once thats done, add the path to the php.exe to your PATH settings. You can do this by going to control panel->system->change settings->advanced->environment variables. Edit the PATH, add a ';' to the end of the line and then past the path to the php.exe. This is on Vista, it might be different on XP or Windows 7.
My path looks like this after: C:\Sun\SDK\jdk\bin;C:\wamp\bin\php\php5.3.0
Once thats done, you'll be able to execute a php file from the command line. You could create shortcuts too.
C:\Users\Garth Michel>php test.php
This is a test
C:\Users\Garth Michel>
I used php for years as a scripting language before I even bothered to use it as a web programming language.
maybe php is not the right tool to doing this and it's about time to learn another language...use this chance to expand your programming horizion
See the .reg file in this gist, it makes it possible to use .php files exactly like .bat files, e.g. my_script.php foo bar baz
Don't forget to edit paths to suit your setup.
http://www.appcelerator.com/products/download/ Still use html & css as a desktop app and now has support for php.
PHP based web apps like Wordpress and Mediawiki I think uses php to setup and configure itself. Just give IIS proper read/write rights and you can make a simple web app that does massive renaming, etc.. PHP doesn't have to always be used for writing out html.
See ExeOutput for PHP. It has excellent customization and MAGNIFICENT features. Yet it is not an IDE or something. It compiles your PHP+HTML into fully-fledged EXEs.