6

If I would like to distribute PHP application with installer(package system of OS) how should I proceed? I don't want PHP files to be there, just working application, so when I type 'app' into console, it ends up being launching application, without need to install PHP on system(no php installation on host required). I would also like the application to have patch-able byte-code, so it's in parts, loaded when needed and only part needs to be replaced on update.

What I would do now is following:

->Compile PHP with extensions for specific platform.
->Make binary application which launches '/full/php app' when app is launched.
->Pack it in installer in a way, that there would be binary added to path when added, launching specific installation of PHP which is alongside the app with argument of start point->App would be running.

Problem is:

Maybe I don't want my PHP files to be exposed(in application, there will be available source anyway) is there some ready made stuff to do this? Is there some better way than I proposed?

Alternative: Modifying OP Cache to work with "packing" application to deliver byte codes to modified OP Cache which just reads the cache.

rici
  • 234,347
  • 28
  • 237
  • 341
Jer Je
  • 61
  • 1
  • 3
  • maybe interesting? [Convert a PHP script into a stand-alone windows executable](https://stackoverflow.com/questions/9046675/convert-a-php-script-into-a-stand-alone-windows-executable#9046775) – Ryan Vincent Sep 14 '18 at 11:35
  • Windows is kinda not the "only" target platform. – Jer Je Sep 15 '18 at 13:11
  • lol, maybe interesting? searching google found - [Try using HipHop from Facebook](https://stackoverflow.com/a/3633894/3184785) – Ryan Vincent Sep 16 '18 at 01:26

5 Answers5

5

My suggestion would be a tiny tool I just finished, for almost exactly the same problem. (Oh yes I tried all the others but they're old and rusty, sometimes they're stuck with 4.x syntax, have no support, have no proper documentation, etc)

So here's RapidEXE:

http://deneskellner.com/sw/rapidexe

In the classical way, it's not a really-real compiler, just a glorified packer, but does exactly what you need: the output exe will be standalone, carrying everything with it and transparently building an ad-hoc runtime environment. Don't worry, it all happens very fast.

It uses PHP 7.2 / Win64 by default but has 5.x too, for XP compatibility.
It's freeware, obviously. (MIT License.)

(Just telling this because I don't want anyone to think I'm advertising or something. I just took a few minutes to read the guidelines about own-product answers and I'm trying to stay within the Code of the Jedi here.)

However...

I would also like the application to have patch-able byte-code, so it's in parts, loaded when needed and only part needs to be replaced on update.

It's easier to recompile the exe. You can extract the payload pieces of course but the source pack is one big zip; there seems to be no real advantage of handling it separately. Recompiling a project is just one command.

Maybe I don't want my PHP files to be exposed(in application, there will be available source anyway)

In this case, the exe contains your source compressed but eventually they get extracted into a temp folder. They're deleted immediately after run but, well, this is no protection whatsoever. Obfuscation seems to be the only viable option.

If something goes wrong, feel free to comment or drop me a line on developer-at-deneskellner-dot-com. (I mean, I just finished it, it's brand new, it may misbehave so consider it something like a beta for now.)

Happy compiling!

dkellner
  • 8,726
  • 2
  • 49
  • 47
  • 1
    Thank you! Does it work properly? Does it solve the problem, or just "nice to have, maybe some other time"? :) – dkellner Sep 15 '18 at 17:13
  • Hey there! Thanks for the answer. :) I was wondering, can I package PHP games as a `.zip` file and have a copy of the entire portable PHP installation (with it's license files) and let a shell script (called `run.sh`) execute the PHP script? – Ed The ''Pro'' Jan 08 '21 at 15:17
  • Absolutely. That's one way to do it. – dkellner Jan 08 '21 at 15:51
1

PHP doesn't do that natively, but here are a few ideas:

Self-extracting archive

Many archival programs allow you to create a self-extracting archive and some even allow to run a program after extraction. Configure it so that it extracts php.exe and all your code to a temp folder and then runs ir from there; deleting after the script has complete.

Transpilers/compilers

There's the old HPHC which translates PHP code to C++, and its wikipedia age also contains links to other, similar projects. Perhaps you can take advantage of those.

Modified PHP

PHP itself is opensource. You should be able to modify it withot too much difficulty to take the source code from another location, like some resource compiled directly inside the php.exe.

Vilx-
  • 104,512
  • 87
  • 279
  • 422
1

Use Zend Guard tool that compiles and converts the plain-text PHP scripts into a platform-independent binary format known as a 'Zend Intermediate Code' file. These encoded binary files can then be distributed instead of the plain text PHP. Zend Guard loaders are available for Windows and Linux platform that enables PHP to run the scripts encoded by Zend Guard. Refer to http://www.zend.com/en/products/zend-guard

v.j
  • 186
  • 11
1

I would like to add another answer for anyone who might be Googling for answers.

Peach Pie compiler/runtime

There is an alternative method to run (and build apps from) .php source codes, without using the standard php.exe runtime. The solution is based on C#/.NET and is actually able to compile php source files to .NET bytecode.

This allows you to distribute your program without exposing its source code.

You can learn more about the project at: https://www.peachpie.io/

David Refoua
  • 3,476
  • 3
  • 31
  • 55
0

You've got 3 overlapping questions.

1. Can I create a stand-alone executable from a PHP application?

Answered in this question. TL;DR: yes, but it's tricky, and many of the tools you might use are semi-abandoned.

2. Can I package my executable for distribution on client machines?

Yes, though it depends on how you answer question 1. If you use the .Net compiler, your options are different to the C++ option.

3. Can I protect my source code once I've created the application?

Again, depends on how you answer question 1. Many compilers include an "obfuscator" option which makes it hard to make sense of any information you get from decompiling the app. However, a determined attacker can probably get through that (this is why software piracy is possible).

Neville Kuyt
  • 29,247
  • 1
  • 37
  • 52