Running wine via php

Open forum for end-user questions about Wine. Before asking questions, check out the Wiki as a first step.
Forum Rules
Locked
asmith
Level 2
Level 2
Posts: 22
Joined: Sun Apr 19, 2009 5:07 am

Running wine via php

Post by asmith »

Hi,

I'm trying to run a command with wine via php:

Code: Select all

<?php
$test = shell_exec('wine c:\\\\.exe');
echo $test;
?>
[/code]
asmith
Level 2
Level 2
Posts: 22
Joined: Sun Apr 19, 2009 5:07 am

Post by asmith »

Please delete my first post.

I'm trying to run a command with wine via php:

Code: Select all

<?php
$test = shell_exec('wine c:\\\\program.exe');
echo $test;
?> 
It doesn't work. but the same command : wine c:\\program.exe
Works fine in the SSH directly.

I tried with php shell_exec('wine --version') and I received my wine version but can't run the program through my browser (php).

any idea?
Ubuntu: 8.04
Wine: 1.1.37
webserver: nginx
php: 5
Martin Gregorie

Running wine via php

Post by Martin Gregorie »

On Mon, 2010-02-08 at 03:16 -0600, asmith wrote:
I'm trying to run a command with wine via php:
Code:

<?php
$test = shell_exec('wine c:\\\\program.exe');
echo $test;
?>

It doesn't work. but the same command : wine c:\\program.exe
Works fine in the SSH directly.

I tried with php shell_exec('wine --version') and I received my wine
version but can't run the program through my browser (php).
The set of environmental variables available in a webserver environment
are often very different from those available after a login. Have you
tried displaying them via php: "$test=shell_exec('env');" might work.
Compare that with the set you'd see by running env from a command prompt
and think about the effect the differences might have on wine.

I don't use nginx or php, but the Apache CGI tutorial warns that this is
a frequent source of problems.


Martin
asmith
Level 2
Level 2
Posts: 22
Joined: Sun Apr 19, 2009 5:07 am

Post by asmith »

Thanks for the replay.

I compared them:

PWD in shell is /root, with php is the document root of my site.
HOME in shell is /root, with php is /var/www
PATH in shell is /usr/lib/kde4/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin but in php it is /sbin:/usr/sbin:/bin:/usr/bin

Another vairable in shell is:
_=/usr/bin/env
in php:
_=/etc/init.d/php-fastcgi

Should I change these for php and how? and how to set them b ack after I ran the program?
Martin Gregorie

Running wine via php

Post by Martin Gregorie »

On Mon, 2010-02-08 at 05:04 -0600, asmith wrote:
Thanks for the replay.

I compared them:

PWD in shell is /root, with php is the document root of my site.
HOME in shell is /root, with php is /var/www
PATH in shell is /usr/lib/kde4/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin but in php it is /sbin:/usr/sbin:/bin:/usr/bin

Another vairable in shell is:
_=/usr/bin/env
in php:
_=/etc/init.d/php-fastcgi

Should I change these for php and how? and how to set them b ack after
I ran the program?
Do you see any error messages?
Is your webserver writing anything useful to the error log?

If so, that should help you figure out which environment variables need
changing.

As I said, I don't know php, but I'd guess that anything you set between
the <php> tags gets discarded once the PHP script has been run, so:
- try setting environment variables before calling shell_exec()
- alternatively, try writing a wrapper shell script that sets the
environment and runs wine and shell_exec() that instead.


Martin
asmith
Level 2
Level 2
Posts: 22
Joined: Sun Apr 19, 2009 5:07 am

Post by asmith »

I changed all the env variables and test them, they were all changed. but still no success.

I found this:
http://stackoverflow.com/questions/1377 ... ned-by-you

What is it?
(I created /var/www/.wine and 'chown www-data /var/www/.wine'. no success still)
Martin Gregorie

Running wine via php

Post by Martin Gregorie »

On Mon, 2010-02-08 at 06:03 -0600, asmith wrote:
I changed all the env variables and test them, they were all changed.
but still no success.
So, exactly what did you do and what error messages did you see? Unless
we see both of these its not exactly easy to help.
Are you using any secure Linux features (AppArmour, SELinux, etc)?
If you were using Apache I'd want to know what AllowOverride and Options
arguments are in force for the directory where you're running wine.

As a crude permissions test, will your web server display the web page
containing the <php> tags if you comment them out or replace the wine
command with something like env or ls?


Martin
vitamin
Moderator
Moderator
Posts: 6605
Joined: Sat Feb 23, 2008 2:29 pm

Post by vitamin »

asmith wrote:I'm trying to run a command with wine via php: It doesn't work. but the same command : wine c:\\program.exe Works fine in the SSH directly.
To run most (all) programs Wine needs X display. If you running this on the server you'll need to setup a virtual X server and point Wine to it (DISPLAY env var).
asmith
Level 2
Level 2
Posts: 22
Joined: Sun Apr 19, 2009 5:07 am

Post by asmith »

Actually I have done that.

I have setup xvfb and I've run 'Xvfb :1 &'
and I run the command I need with:

DISPLAY=:1 wine program.exe

All works perfectly fine directly in shell. I have no idea why in php NOTHING happens. I get a blank screen. no output. no error.

I tested with another exe file, which I created using C++ in windows. the program only print out a test line like 'hello!', when running.

I ran that program in shell: wine program2.exe, and I received the text. Again I tested the same line in php, a blank screen. no output, no error.

in mean time I DO get output for: 'wine --version', or 'ls -al'

been looking for this my whole day. :(
Martin Gregorie

Running wine via php

Post by Martin Gregorie »

On Mon, 2010-02-08 at 15:45 -0600, asmith wrote:
Actually I have done that.

I have setup xvfb and I've run 'Xvfb :1 &'
and I run the command I need with:

DISPLAY=:1 wine program.exe

All works perfectly fine directly in shell. I have no idea why in php
NOTHING happens. I get a blank screen. no output. no error.
Its unlikely that there aren't traces in some log file: one of the
strengths of Linux is the amount of stuff that gets into log files,
especially for web servers, when there are problems.

Its also possible that the system has mailed you reports. Have you
looked for system mail under root?

Do another test, running the wine application from PHP. Note the time
displayed by the computer. Then look in the webserver's log files and
the Linux log files. Look at the latest file for *all* the different
logfiles for your webserver and Linux, not just /var/log/messages for
the Linux set.
I tested with another exe file, which I created using C++ in windows.
the program only print out a test line like 'hello!', when running.
Is that program writing to stdout or stderr?

Do the same with this program and with something that works: what's in
the logs? How does they differ from when you ran Wine?

If you can't sort out the problem from what you find in the logs with a
timestamp for the same minute when you ran the test, maybe we can help.
If its just a few lines, post it here. If its large, stitch the sections
of log together, each preceeded by the logfile name, put them on
pastebin and post the URL here.


Martin
vitamin
Moderator
Moderator
Posts: 6605
Joined: Sat Feb 23, 2008 2:29 pm

Post by vitamin »

asmith wrote:I ran that program in shell: wine program2.exe, and I received the text.
You ran that as what user?
asmith wrote:Again I tested the same line in php, a blank screen. no output, no error.
In most systems apache and php running under highly restricted user with big memory restrictions. Also Wine needs to setup it's prefix on first run.
asmith wrote:in mean time I DO get output for: 'wine --version', or 'ls -al'
Well yeah, wine --version doesn't get any further then wine launcher itself which has version hardcoded during compile time.

Enable php logging in /etc/php.ini into a file. Make sure it's in place where user running apache can write into. See syslogs for apache itself.
asmith
Level 2
Level 2
Posts: 22
Joined: Sun Apr 19, 2009 5:07 am

Post by asmith »

I enabled error logging and no error was there.

But funny thing, I changed

'wine c:\\\\program.exe'

to

'wine /root/.wine/drive_c/program.exe'

and it worked. I tried c:\\program.exe, but nothing.

However I have another problem. the original line was like this: (sending a file to the program)

wine program.exe -file test.ext

now this in shell:
wine /root/.wine/drive_c/program.exe -file test.ext

works fine as it should.
but same line in php, tells that the program can't find the file.

test.ext is in .../drive_c/temp and the program always look for files in the temp folder. so it finds the file in temp folder if it is in shell, but it can't infd the file if it is in php.

Any idea?
Gert van den Berg

Running wine via php

Post by Gert van den Berg »

On Thu, Feb 11, 2010 at 09:15, asmith <[email protected]> wrote:
I enabled error logging and no error was there.

But funny thing, I changed

'wine c:\\\\program.exe'

to

'wine /root/.wine/drive_c/program.exe'

and it worked. I tried c:\\program.exe, but nothing.

However I have another problem. the original line was like this: (sending a file to the program)

wine program.exe -file test.ext

now this in shell:
wine /root/.wine/drive_c/program.exe -file test.ext

works fine as it should.
but same line in php, tells that the program can't find the file.

test.ext is in .../drive_c/temp and the program always look for files in the temp folder. so it finds the file in temp folder if it is in shell, but it can't infd the file if it is in php.

Any idea?
You shouldn't run Wine as root...

You need to put it somewhere writeable to the user that your web
server is running as. Set the WINEPREFIX variable to the location of
your .wine directory. This directory MUST be owned by the user running
Wine.

Also make sure you don't call Wine with any untrusted parameters...

Check apache's error log again... For CGI scripts at least, it should
have everything that was written to stderr.... Not sure about PHP...

Gert
asmith
Level 2
Level 2
Posts: 22
Joined: Sun Apr 19, 2009 5:07 am

Post by asmith »

I'm sorry I'm newbie on linux and wine, but I'm trying my best.

I did:

mv /root/.wine /home/username/.wine

and

env WINEPREFIX=”/home/username/.wine”

But wine didn't changed its prefix.
by
wine program.exe

wine again created .wine in root. (I am logged as root for now)
How do I change it?
vitamin
Moderator
Moderator
Posts: 6605
Joined: Sat Feb 23, 2008 2:29 pm

Post by vitamin »

asmith wrote:wine again created .wine in root. (I am logged as root for now) How do I change it?
Create another user and log in as that user. How exactly are you running it from php?
asmith wrote:wine /root/.wine/drive_c/program.exe -file test.ext
You have to specify full path to both executable and your input file. Both have to be Windows paths not UNIX. To convert paths use program 'winepath'.
Martin Gregorie

Running wine via php

Post by Martin Gregorie »

On Thu, 2010-02-11 at 08:03 -0600, asmith wrote:
I'm sorry I'm newbie on linux and wine, but I'm trying my best.

I did:

mv /root/.wine /home/username/.wine

and

env WINEPREFIX=/home/username/.wine

But wine didn't changed its prefix.
by
wine program.exe

wine again created .wine in root. (I am logged as root for now)
How do I change it?
Assuming you're again logged in as root:

rm -rf /root/.wine
exit

Login as username.
Run wine.

Do as little as possible when running as root. Preferably never run
anything as root. Second choice is to run commands with sudo. Third by a
long, long way is to login as root or via su.


Martin
asmith
Level 2
Level 2
Posts: 22
Joined: Sun Apr 19, 2009 5:07 am

Post by asmith »

I created another profile for wine in /home/username/.wine/

But my php still uses the root username to access, so it uses wine profile for root.

I think I narrowed down the problem. I have permission issues.
Wine can run the program itself. but when program.exe wants to start its processing, it fails creating its own temp files or accessing some files it needs on the system.
When running it directly in shell, it has full access to any file, that's why it works fine. But with php it doesn't.

chowning wine profile doesn't seem to work.
How can I move my .wine to another folder? I dont' wanna create another one. Just move this working one to my public_html folder, so that maybe it works there? or just find the option in apache (I use nginx) to give permission to this one.
vitamin
Moderator
Moderator
Posts: 6605
Joined: Sat Feb 23, 2008 2:29 pm

Post by vitamin »

I asked you how _EXACTLY_ are you starting program from php? I think you using 'sudo' which the problem.
Gert van den Berg

Running wine via php

Post by Gert van den Berg »

On Thu, Feb 11, 2010 at 23:33, asmith <[email protected]> wrote:
I created another profile for wine in /home/username/.wine/

But my php still uses the root username to access, so it uses wine profile for root.
PHP should also not be running as root...
I think I narrowed down the problem. I have permission issues.
Wine can run the program itself. but when program.exe wants to start its processing, it fails creating its own temp files or accessing some files it needs on the system.
When running it directly in shell, it has full access to any file, that's why it works fine. But with php it doesn't.
Get the logs!

You can try changing your method for running wine to something like:
/bin/sh -c "<whatever you were running previously> > /tmp/wine_log.txt 2>&1"

Then have a look at the log / post it here....

My wine_with_log script might also help....

http://forum.winehq.org/viewtopic.php?p=27438
chowning wine profile doesn't seem to work.
How can I move my .wine to another folder? I dont' wanna create another one. Just move this working one to my public_html folder, so that maybe it works there? or just find the option in apache (I use nginx) to give permission to this one.
move it, set the WINEPREFIX environment variable to point to it...
James McKenzie

Running wine via php

Post by James McKenzie »

asmith wrote:
I created another profile for wine in /home/username/.wine/

But my php still uses the root username to access, so it uses wine profile for root.
You my friend are a hacker's delight. Hopefully your php is not public
or you have been pown'd by now.

NEVER, EVER, EVER RUN ANYTHING THAT FACES PUBLIC AS ROOT.

Now, can you change all of your apache/mysql/php directories to another
user, such as nobody so that access to your system is severely restricted?

If you CANNOT do this, at least use a user with no privileges and a
restricted user group.

Also, look at the permissions for your /temp or /tmp directory. It
should be world read/execute for any user (rwxr-xr-s) with the file
sticky bit set (if my memory serves me correctly) that makes any file
created writable by the user that created it without directory
ownership. Someone will step in if I'm incorrect.

The first and foremost thing is to correct the security situation you
are in, if the system is public. If it is private, you still have to
get everything working for a user other than root.

root should ONLY be used for testing and to do things for your system
that cannot be done any other way. BTW, Posix compliant programs will
let non-root users access ports < 1024.

I also recommend deleting the .wine directory under /root and starting
over with the user that you are going to use for php. Any other way may
lead to problems.

James McKenzie
Locked