How do I choose between thread-safe and not thread-safe PHP? - php

I am developing a Laravel 9.x app in Windows 10. My current version of PHP is PHP 8.1.5 (cli) (built: Apr 12 2022 17:38:57) (ZTS Visual C++ 2019 x64). Is this a thread-safe or not-thread-safe version? How can I tell? When should I prefer a thread-safe over a not-thread-safe version?
I've tried researching this issue here on StackOverflow but the questions/answers I see all seem to be a decade or more old - like this - and I strongly suspect that I might get a different answer if I asked the question today simply because the technology has changed in the intervening years. Is that a reasonable assumption? (At the very least, my current version of PHP evidently uses a newer compiler, VC++ 2019, rather than VC6 or VC9.)
I have no idea yet what my production environment is going to be or even IF the app I'm developing will ever go to a production environment since it's just an app I'm writing to (re-)learn Laravel. I may put it in production as a demonstration of a working Laravel app when the time comes but whether it will be on a hosting service or Netlify or something else, I just don't know at this point.
Just to give you some context, this issue only came up because I am trying to learn how to step through my Laravel source code to debug problems and this apparently requires me to add XDebug to XAMPP. The instructions I found for installing XDebug point me to here and recommend that I download the Windows binaries for my version of PHP. There are no binaries of 8.1.5 so I don't know if one of the binaries for 8.1 would work or if I'd be better to use 8.1 thread-safe or 8.1 not-thread-safe. If it would be better to upgrade my PHP first to 8.2, I still don't know if thread-safe or not-thread-safe is a better choice.
Can someone enlighten me on these matters?

Is this a thread-safe or not-thread-safe version? How can I tell?
You can use the good old phpinfo() function and lookup for the Thread Safety column.
If you have your PHP setup in PATH environment, you can even get it via CLI using a quick command php -i | grep Thread
When should I prefer a thread-safe over a not-thread-safe version?
As per PHP official documentation
If you choose to run PHP as a CGI binary, then you won't need thread safety, because the binary is invoked at each request. For multithreaded webservers, such as IIS5 and IIS6, you should use the threaded version of PHP.
I have no idea yet what my production environment is going to be ...
Generally, there is no difference on TS or NTS code execution. It's more towards the webserver.
If I am not mistaken, by default, XAMPP uses Apache Handler, so it will likely be a thread safe build. And yes, the Xdebug binary for PHP8.1 will just work fine.
Technically, NTS should be slightly faster because it doesn't need to cater for thread safety.
Personally, I think it doesn't matter, unless you want to spend slightly more time to tinker around the webserver switching to FastCGI or PHP-FPM, since you are still in development phase, I think stick to the default setup and focusing on getting it up is more efficient.

Related

PHP Manager IIS fails to install on Windows Server 2016

I have recently installed Windows Server 2016 and I'm trying to get my IIS 10 set-up to create a site.
The PHP 5.6.30 installed just fine, but the PHP Manager 1.2, WinCache 1.3 for PHP 5.6 and CGI didn't install!
The dialog image:
The PHP Manager log: http://pastebin.com/Y9Ud1XUU
The WebPI log: http://pastebin.com/H13fJU0Z
I've tried setting the MajorVersion to 7 (and other values), but the installer still fails. I have .NET 4.5> installed.
as far as i know this addon/tool has not been working since the release of Windows Server 2012 R2.
i have still to find Another tool like it, but so far nothing has come up
though i have found this solution:
https://answers.microsoft.com/en-us/windows/forum/windows_10-other_settings/php-manager-for-iis-on-windows-10/33ef32f0-6a86-4803-abc1-6de81110f9a8
(confirmed to work, just remember to restart the IIS manager)
PHP Manager requires .NET Framework 3.5 installed and may require a small hack in the registry as outlined here:
I was able to find a workaround at the PHP manager's site.
Just make sure you have .NET 3.5 installed and modify the following registry:
HKLM/System/CCS/Services/W3SVC/Parameters/MajorVersion
The value is 10 (Ax0). Just change the value to 9 (or 8), then try to install it again.
To avoid future issues with other IIS stuff, change back to 10 (Ax0). It seems that PHP Manager validates the value converting it into string or something related.
On my Windows Server 2016 machine for some reason the Web Platform Installer (downloaded from https://php.iis.net/) gave me a less recent PHP version (5.3.28) than yours, but all components were correctly installed, with the exception of PHP Manager (which I was expecting given it has been discontinued, and I didn't really care anyway...):
Also, if you want, you can install the components manually, by following this detailed post:
Steps to Install PHP manually on Windows 2016 server
It's really not complicated and it will give you a better understanding of what is not working, should that be the case.
With the help of above article I could also verify that the Web Platform Installer did not miss any important parts (modifying php.ini as required by extensions, setting environment variables, and so on).
Hope it helps.
We have several Windows Server 2012 R2 servers with PHP Manager running without problems (sorry Robert). I have just setup a new one and added PHP Manager and others by installing PHP 5.6 with Web Platform installer. The only thing needed extra was .NET 3.5. Have you tried to setup with .NET 3.5 installed?
When you try to install PHP Manager using the direct download link, the setup will ask you to install .NET 2.0. That isn't necessary :).

What to use instead of apc user data cache in php 5.5?

PHP 5.5 includes zend opcache by default, which basically means that almost nobody will use APC.
But what to use instead of the user data cache part of APC (apc_store & apc_fetch & similar)?
One use case where I really like to use APC user data cache are "versions" of static assets (javascript, css..). Whenever I reference static file, I add hash of its content into the url (e.g. <script src=/script.js> will became <script src=/script.js?v=hash>), so that browser always uses current version and can cache it permanently.
I can imagine using redis or memcache to store the hashes of static files, but it seems silly to ask another process over network or socket just to get a hash of file content. APC user data cache (which is in shared memory and accessing it is almost as fast as accesing php variable) seems just the right thing to use for such data.
So the question is: what to use in php 5.5 to cache small bits of data instead of APC?
Starting from PHP 5.5 the APC user data storage is packaged separately as PECL APCu.
Source code: http://pecl.php.net/package/APCu
MacOS homebrew package is php55-apcu (brew install php55-apcu)
Debian/ubuntu is called php5-apcu (apt-get install php5-apcu)
Fedora/RedHat is called php-pecl-apcu (yum install php-pecl-apcu)
Windows - use precompiled APCu dll - follow instructions provided in the answer below.
This allows you to use all user cache functions, such as apc_store(). It will also return true for extension_loaded('apc') - this means that all libraries depending on APC will work similarly to PHP 5.4.
I recently dealt with this question after upgrading from php 5.3 to php 5.5 beta 2.
I looked at Memcache and Redis. Depending upon whom you ask, the performance between the two is approximately the same. Some claim that Redis is marginally faster. However, Redis has a lot more features than Memcahe so I decided to go with Redis.
For a PHP client, I chose Phpredis over Predis. Phpredis is a C extension whereas Predis a pure PHP implementation. Thus, Phpredis is generally faster.
I'm primarily using Redis to cache and retrieve serialized objects. I started the project I'm currently developing in PHP 5.3 with APC. I'm continuing to develop the project with php 5.5 and Redis. While I don't have benchmark statistics, I can tell you that the app "feels" faster. This is likely due to performance enhancements in php 5.5 as opposed to APC user cache verses Redis. Either way, I'm happy with my choice.
I hope that helps. Good luck and happy hacking :-)
Nothing more to say. You got the correct answer already. I guess I can provide you with a link to tutorial to how to download and install APCu on XAMPP on Windows for php 5.5 and 5.6:
Link do download APCu for php build from 5.3 and higher:
download APCu different versions
Installation tutorial: installation instructions (The newest version should be on the very bottom of the file list - use this one)
Also bear in mind that you will have two to choose from few options like 64 or 84 version as well as nts or ts and vc9 or vc11 (it can be different in your case) and of course the correct PHP version (in my case that would be PHP 5.6 for my xampp).
EXAMPLE:
if you want to pick right you have to run phpinfo() first and check for those parameters:
Zend Extension Build and Architecture
In my case that would be:
[Zend Extension Build:] API220131226,TS,VC11
[Architecture:] x86
That mean that in my case I would have to choose ACLu wchich contain
in file name those parameters 5.6, TS, VC11, 86
file name to download: php_apcu-4.0.7-5.6-ts-vc11-x86.zip
Hope that clear things up for you.
Some aditional explanations on different PHP builds:
difference ts vs nts:
TS refers to multithread capable builds. NTS refers to single thread only builds. Use case for TS binaries involves interaction with a multithreaded SAPI and PHP loaded as a module into a web server. For NTS binaries the widespread use case is interaction with a web server through the FastCGI protocol, utilizing no multithreading (but also for example CLI).
difference vc9 vs vc11 vs vc14:
More recent versions of PHP are built with VC9, VC11 or VC14 (Visual Studio 2008, 2012 or 2015 compiler respectively) and include improvements in performance and stability.
The VC9 builds require you to have the Visual C++ Redistributable for Visual Studio 2008 SP1 x86 or x64 installed
The VC11 builds require to have the Visual C++ Redistributable for Visual Studio 2012 x86 or x64 installed
The VC14 builds require to have the Visual C++ Redistributable for Visual Studio 2015 x86 or x64 installed
difference 86 vs 64:
The x64 builds of PHP for Windows should be considered experimental, and do not yet provide 64-bit integer or large file support.
Take a look at the XCache opcode cacher, from the authors of lighttpd. It supports both php 5.5 and user data cache: http://xcache.lighttpd.net/wiki/XcacheApi
I didn't try it my self (still using APC and php 5.4).

Is there any harm in using a thread-safe extension (APC) in a non-thread environment? (PHP)

I was going to use this Simple PHP Upload with a Progress Bar, and it says I need the APC extension. I have two options with APC, thread-safe, and non-thread-safe.
using phpinfo, I find that thread-safety is enabled, but I'm not sure if php is actually running mutithreaded. I hear that php is still kinda beta for mutithread.
Anyways, if my PHP is not mutithreaded and wasn't "thread-safe", would there be problems using a thread-safe version of APC?
tl;dr version: Does thread-safe APC mean more compatibility (works in both php single/mutithread), or does it mean mutithread required (works only in php mutithread)?
if my PHP is not mutithreaded and wasn't "thread-safe", would there be problems using a thread-safe version of APC?
Yes - it won't work, period. Thread safety is a compilation setting that must match up for PHP itself and all the extensions, otherwise they can't be loaded. Here's an article that explains the difference in great detail.
Note that besides the TS/NTS setting, you also need the CPU architecture (x86 vs x64) and the version of Visual Studio used to compile (VC6 vs. VC9) to match in order for an extension to work, and the version of the extension may work only with a certain major version of PHP.
All this means that it can be pretty damn hard to get the right precompiled binary of an extension to work with your PHP installation.
I advise using the thread-safe version. While the non-thread-safe version may be faster, if ever PHP supports threads (or its extensions do) then having that extra compatibility is a good idea.

Can someone compile PHP runkit DLL extension for me?

I give up. I've asked this question some time ago but I'm again into that issue. I'm still stuck with compilation errors / missing files / other stuff that I can't / don't have time to fix. Don't get me wrong - I've compiled it several times, but none of the DLL files I've got worked. I use WAMPServer 2.1e [Apache 2.2.17, PHP 5.3.5] - after enabling php_runkit it does not start. Even already compiled DLLs from the Internet haven't worked. Another time Windows seems to be simply against me.
Now as a programmer I feel really, really lame to ask you the following: can someone compile PHP runkit DLL extension for me or point me to the WORKING one? Please test if it works and provide your WAMP stack configuration, so I can adjust mine.
You would be my hero.
I compiled Zenovich's fork for 5.3, no ZTS, VS9, commit f8daf39 with trivial changes in order to build in Visual Studio.
Unfortunately, the ZTS version seems to broken and did not compile. Its problems would require some effort to fix. So you must run PHP as a CGI binary, not as an Apache module.
Artefacto's binaries didn't work correctly for me so I compiled the newest runkit for PHP 5.3, 5.4 and 5.5 (both TS and NTS versions). You can grab it here - https://github.com/Crack/runkit-windows.
PHP 5.3 and 5.4 modules are compiled with VC9, 5.5 with VC11 so they work with official PHP binaries.

What are the technical differences between the Thread Safe and Non Thread safe PHP Windows Installation Packages?

I'm currently about to install PHP for an Apache/Windows-based development environment, but it seems I'm about to fall at the first hurdle: Choosing the right package to install.
PHP is available in no less than four flavours:
VC9 x86 Non Thread Safe
VC9 x86 Thread Safe
VC6 x86 Non Thread Safe
VC6 x86 Thread Safe
What's the difference between these versions in a practical sense?
If this wasn't complicated enough, version 5.3 of PHP is only available in VC9 (with 5.2 coming with the VC6 packages). And yet, according to the PHP site, you should not use VC9 with Apache... So why does Apache get the older version?
It's all very confusing and I'd like some help understanding the choices.
After a lot of research, I've managed to find my own answers to this question.
In its most basic form, the answer is: What version of PHP you should install comes down what webserver you are running.
Here's a deeper explanation of the terms used in picking a version of PHP based on what I learned:
VC6 vs VC9
Firstly, different versions of Apache for Windows are compiled with different compilers. For example, the versions on Apache.org are designed to be compiled using Microsoft Visual C++ 6, also known as VC6. This compiler is very popular, but also very old. (It dates back to 1998.)
There are different versions of Apache made for different compilers. For example, the versions available for download from ApacheLounge.com are designed to be compiled with the popular and more much recent compiler, Microsoft Visual C++ 9 from 2008. Also known as VC9.
(Note: These two compilers are the two most popular options. So while it's possible to have a VC7, VC8, etc. compiled version of Apache, it's unlikely that you'll come across them.)
The use of this more recent compiler (VC9) is important because the latest versions of PHP are only being distributed in VC9 form (although older versions are still available for VC6).
On top of that, according to ApacheLounge there are numerous improvements when using a version of Apache compiled with VC9, "in areas like Performance, MemoryManagement and Stability".
If that wasn't enough, the developers of PHP made the following statement on their site:
Windows users: please mind that we do
no longer provide builds created with
Visual Studio C++ 6 (VC6). It is
impossible to maintain a high quality
and safe build of PHP for Windows
using this unmaintained compiler.
We recommend the VC9 Apache builds as
provided by ApacheLounge.
All PHP users should note that the PHP
5.2 series is NOT supported anymore. All users are strongly encouraged to
upgrade to PHP 5.3.6.
In all, this is an extremely compelling argument to use VC9 versions of Apache and PHP, if you ask me.
So if you're using a version of Apache from the official Apache site, it will be compiled with VC6, and as such, you should use the older version of PHP for that compiler. If you're using a version of Apache compiled with VC9, like the one available on ApacheLounge.com, you can use the latest version of PHP (for VC9).
For me, running a local development environment, it would be preferable to have the latest version of PHP, so a VC9 version of Apache is required, so I can use the VC9 version of PHP.
Thread Safe vs Non Thread Safe
Once again this comes down to your webserver. By default Apache is installed on Windows as Module, but it can be changed to run as FastCGI. There's plenty of differences between the two, but essentially FastCGI is more modern, faster, more robust, and more resource hungry. For someone running a local development environment, FastCGI might be overkill, but apparently lots of hosting companies run as FastCGI for the reasons I've stated, so there are good arguments for doing so in a development environment.
If you're running Apache (or IIS) as FastCGI (or CGI) then you want the Non Thread Safe version of PHP. If you're running Apache as default (as a Module), then you'll want the more traditional Thread Safe version.
Please note: This all only applies to Windows users.
I'm not going to bother with FastCGI (unless someone convinces me otherwise), so for me, I want the VC9 Thread Safe version of PHP.
And that's it.
Further reading:
Official statement regarding PHP and VC6
Difference between PHP thread safe and non thread safe binaries
FastCGI at Wikipedia
FastCGI for IIS
Visual C++ at Wikipedia
Compile your own PHP (explanation of VC6/VC9)
Personally, I use a virtualised LAMP server. Every hosting service I use is on some flavour of Linux, and there are too many differences between WAMP and LAMP. Then I just use the default tasksel LAMP server for that version of Linux.
My actual setup right now is with VMWare (Fusion on Mac, Player on Windows). I have 3 VMs - one for PHP5.3 with Ubuntu 10.04 LTS, and another for PHP 5.1 on Ubuntu 8.04 LTS. (One of the hosts I use is on RedHat, which currently supports only PHP 5.1). I have a third VM for RubyOnRails dev.
In other words, I try to get my development environment as close to my production environment as possible. So work out what version of Apache and PHP is on your host, and use that as your guide.

Categories