For offline development I would like to make make a standalone PHP 5.4 binary so I can use the PHP self hosting option.
eg php -S localhost:8000
Which would startup a server for hosting PHP files.
Since installing and configuring PHP 5.4 on mac is not trival for non tech people. I would like to be able to include a working PHP binary in my files. So essentially I can run.
./php -S localhost:8000 in my folder and it will work.
I have followed the instruction here to create a standalone PHP binary by running on the source code.
./configure --enable-static --enable-cgi --enable-mbstring\
--enable-force-cgi-redirect \
--with-config-file-path=/etc/php5cgi \
--prefix=/usr/local/php5cgi \
--with-curl \
--enable-sockets \
--with-zlib --with-zlib-dir=/usr/include \
--with-pear
Then editing the Makefile to have -all-static
BUILD_CGI = <other commands here> $(ZEND_EXTRA_LIBS) -all-static -o $(SAPI_CGI_PATH).
BUILD_CLI = <other commands here> $(ZEND_EXTRA_LIBS) -all-static -o $(SAPI_CGI_PATH).
This does work. File is standalone.
But when I run the binary.
./php-cgi -h
There is no "-S" option for the built in webserver?
Do I need to change something in my configure options to allow the builtin webserver? My source was downloaded from PHP.net (I did download the 5.5 latest source, but that still does have the -S option).
Thanks, John.
thanks to #devZer0
I cd'ed into
sapi/cgi/php-cgi
and tried to run this.
Needed to cd into sapi/cli/php
That works now! php -h return the -S option for webserver.
Related
Im looking for a linux command line tool for MSSQL/SQLSRV that would allow me to do equivalent commands to these -
pg_dump --clean --dbname=postgresql://username:password#host:port/database >b database/dump.sql
psql postgresql://username:password#host:port/database < database/dump.sql
This is meant for automatic browser automation testing where base data gets generated and before every test gets reverted to the base state.
The project uses docker for everything and is written mainly in PHP but I will gladly look at other ways of accomplishing these needs if you have any suggestions.
After finding this stackoverflow answer - Linux cli tool to dump SQL Server schema to a text file that links to a microsoft tool - https://github.com/microsoft/mssql-scripter
And going down a rabbit hole of bugs, fixes and patches - https://github.com/microsoft/mssql-scripter/issues/236#issuecomment-886079545
I managed to make a working container for these needs.
Heres a dockerfile part that allows anyone to get the tools needed to accomplish this -
FROM ubuntu:21.04
# ....
ARG sqltools=https://github.com/microsoft/sqltoolsservice/releases/download/v3.0.0-release.205/Microsoft.SqlTools.ServiceLayer-rhel-x64-net6.0.tar.gz
RUN apt-get update && apt-get install -y msodbcsql18 mssql-tools18 python pip libunwind8 tar \
&& update-alternatives --install /usr/bin/python python /usr/bin/python3 1 \
&& pip install mssql-scripter \
&& curl -SL $sqltools -o Microsoft.SqlTools.ServiceLayer.tar.gz \
&& mkdir Microsoft.SqlTools.ServiceLayer \
&& tar -xzf Microsoft.SqlTools.ServiceLayer.tar.gz -C Microsoft.SqlTools.ServiceLayer \
&& mv Microsoft.SqlTools.ServiceLayer /opt/Microsoft.SqlTools.ServiceLayer \
&& sed -i 's/utf-8/utf-16/g' /usr/local/lib/python3.9/dist-packages/mssqlscripter/main.py
ENV MSSQLTOOLSSERVICE_PATH=/opt/Microsoft.SqlTools.ServiceLayer
# ....
Whats happening here is that we have to download and specify a newer version of sqltoolsservice that works with the newer openssl lib. Then we patch a bug with utf8 and set the ENV variable for mssql-scripter to use the correct sqltools.
After all that its possible to run this command that dumps sql.
mssql-scripter -S host -d database -U sa -P password --schema-and-data > database/dump.sql
And then this command that imports it.
/opt/mssql-tools18/bin/sqlcmd -S host -d database -U sa -P password -i database/dump.sql
The actual question: is it possible to choose the PHP version (5.6, 7.1, 7.2, ...) to generate code?
I got a swagger.json from https://api.otto.market/docs
which should be an openapi.json or so, since the file contains "openapi": "3.0.3",. Anyway ...
What i found out so far is that they (swagger and openapi-generator) seem to have templates for the code generation.
And those templates are written in the language for the version in use.
F.e. this (openapitools/openapi-generator-cli)
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
-i /local/path/to/swagger.json \
-g php \
-o /local/path/to/generated/code/
creates a composer.json with
...
"require": {
"php": "^7.3 || ^8.0",
...
},
...
I now COULD use the templates and change the requirements.
But i think this would not be the target of a code generator.
Info about templates added to bottom.
What i guess is that there should be templates written for other PHP version.
But how would i choose them?
Are there any?
Swagger: as i read swagger came first, and openapi-generator is a fork.
So i tried swagger:
docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli generate \
-i /local/path/to/swagger.json \
-l php \
-o /local/path/to/generated/code/
Swagger created code for PHP 5.5 (as of the generated composer.json).
But it brings warnings and the generated code looks "broken".
I actually do not wonder since the file is made for openapi": "3.0.3.
But what i see is: swagger has templates for PHP 5.5.
My personal current conclusion:
I can generate code real quick f.e. for an API that i do not really (have to) know.
But how does it help me if i cannot change the language version?
F.e. i need an API client in 2 projects.
One is in PHP 7.1 and the other one in 7.3. How do i solve this?
Anybody had to deal with this? Or any ideas?
INFO
templates: how to use templates:
Call docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli author template -g php -o /local/templates/
to save all templates to path templates/.
You then can f.e. copy the composer.mustache to f.e. deploy/templates/ and change it.
On generating code you use -t /local/deploy/templates to use your changed template.
Example:
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
-i /local/path/to/swagger.json \
-g php \
-o /local/path/to/generated/code/ \
-t /local/deploy/templates
Old versions of OpenAPI Generator (e.g. 3.0.0, 4.0.0) support old PHP versions.
To easily switch between OpenAPI Generator CLI versions, you may want to use the NPM CLI wrapper for OpenAPI Generator.
I built a number of custom PHP extensions under windows 8-10 years ago. I made the move to Ubuntu Linux for all of my web stuff some years ago and find I need to create another custom extension. I'll be doing the development under Ubuntu this time.
I've created a very simple extension (a no-op actually) just to make sure I have the build process working. It's not.
Here is what I have done:
Cloned PHP from Git
Checked out PHP-5.5
Configured with --disable-all --enable-debug --enable-maintainer-zts \
--prefix=
Build PHP
Success php -i shows:
Zend Extension Build => API220121212,TS,debug
PHP Extension Build => API20121212,TS,debug
Created ext/a1 for my new, very simple extension
Created the basic extension (from Sara Goleman's book)
Ran phpize in ext/a1
Ran ./configure --enable-a1
Ran make
Build was successful.
Copied a1.so to the extensions directory
phpdir/bin/php -dextension=a1.so -v
Fails. Results in:
Module compiled with build ID=API20121212,NTS
PHP compiled with build ID=API20121212,TS,debug
So. Color me confused. According to what I've read, the phpize command is supposed to match the extension build settings to the php build settings.
I've apparently missed something basic here somewhere.
Help will be most appreciated.
Its hard to say what exactly was going wrong, I can only say that the extension was build using a different config than the php version.
I will describe some reproducible steps how to compile a most basic extension with debug symbols within the PHP source folder. The extension contains no code except of some boilerplate code created by ext_skel. It just describes the compilation process on UNIX. It is a shell script, you might execute it.
#!/bin/sh
# Should work for all PHP5 versions
VERSION="5.6.9"
# Download the PHP source code
wget \
--continue "http://de2.php.net/get/php-$VERSION.tar.gz/from/this/mirror" \
-O "php-$VERSION".tar.gz
tar xf "php-$VERSION.tar.gz" && cd "php-$VERSION/ext"
# Create a hello extension from skeletons
./ext_skel --extname="hello"
# Uncomment two lines in ext/hello/config.m4
# Read the comments there and you'll know what I'm doing
sed -ri.original \
-e 's/(dnl )(PHP_ARG_ENABLE\(hello)/\2/' \
-e 's/(dnl )(\[ --enable-hello)/\2/' \
hello/config.m4
# Build PHP and the extension
cd ..
./buildconf --force
./configure \
--enable-debug --enable-maintainer-zts \
--enable-hello=shared
make -j
# Test if it is working
sapi/cli/php \
-dextension=modules/hello.so \
-r 'var_dump(extension_loaded("hello"));'
You can now start to enter code to ext/hello/hello.c and create your extension. If you want to compile the changes you make just issue make without arguments.
Since we've compiled using --debug we you can now use gdb to debug the C code and explore how PHP internally works. To start a debugging session use:
gdb sapi/cli/php
...
(gdb) break main
(gdb) run -dextension=modules/hello.so some.php
Of course you'll mostly set breakpoints into your extension functions rather than in the php main() function once you've added code to the extension. However, this should show the basic steps to get there.
Have fun! :)
gdb
I'm currently using vim for my PHP development. A few weeks ago I bought myself a mac, and wanted to re-generate my tags for a new Zend Framework version.
I used the following script in the past (linux machine) to generate the tags:
#!/bin/bash
cd ~/www/ZF/
ctags-exuberant -f ~/.vim/tags/zend \
-h ".php" -R \
--exclude="\.svn" \
--totals=yes \
--tag-relative=yes \
--PHP-kinds=+cf \
--regex-PHP='/abstract class ([^ ]*)/\1/c/' \
--regex-PHP='/interface ([^ ]*)/\1/c/' \
--regex-PHP='/(public |static |abstract |protected |private )+function ([^ (]*)/\2/f/'
You can see I used the "ctags-exuberant" command. The problem is that this isn't found on my system (mac). I only have the ctags command. I tried installing the newest version of the ctags library (http://ctags.sourceforge.net/) but didn't gave me that command.
The ctags command does not have the same parameters as the ctags-exuberant. So 2 questions:
What's the difference between the 2 commands?
How can I install ctags-exuberant or use the normal ctags command for PHP?
Thanks!
Ward
Install Homebrew, then do:
brew install ctags
Install MacPorts, then do:
port install ctags
For a more friendly way to do this instead of using mac-ports or homebrew and running the risk of causing errors download ctags from here and compile with xcode (starting with version 4.3 command line tools are not installed by default but rather through Preferences -> Downloads) then simply move the compiled ctags to /usr/bin/ctags-exuberant to preserve the original.
Commands Used:
Change Directory to Downloads and ctags directory:
$cd$cd Down<tab>ctags<tab>
Configure:
$./configure
Make:
$make
Move to /usr/bin:
$sudo mv ctags /usr/bin/ctags-exuberant
NOTE: <tab> is equal to pressing the tab key.NOTE: XCode can now be downloaded from the Apple App Store!
Very useful for getting Tagbar for vim to work!
Tagbar is available here.
I need a standalone independent-from-system-libraries version of PHP to be able to distribute it as a .zip file containing binaries. Previously the .zip was containing libxml2.so.2, libpng12.so.0, php.ini and php executable.
Recently I needed to add some functionalities and recompile the PHP. I did it with:
./configure \
--prefix=/home/user/php/out \
--libdir=. \
--enable-static=YES \
--enable-shared=NO \
--with-pdo_mysql \
--with-gd \
--with-mysql \
--enable-zip \
--enable-zend-multibyte \
--enable-cgi \
--enable-fastcgi \
--with-ldap=shared
make && make install
Then I took php from out/bin and libmysqlclient.so.16 from /usr/lib/. But the new php doesn't want to pick up the library. Once I run it I get the following error:
./php: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory
libmysqlclient.so.16 and php are in the same directory. If i put libmysqlclient.so.16 to /usr/lib, php works fine.
Is there any additional option that I've forgotten about during compilation? Why isn't the "--libdir=." option working?
First, the directive is --with-libdir not --libdir. Second, I think it is relative to /usr, so putting . there means you need to put the shared libraries into /usr.
If you have done any C coding with shared libraries, you must know about ld. The easiest way to handle shared libraries path is to use ld.
You can set the library path for the current (shell) session:
bash-4.1$ export LD_LIBRARY_PATH=/path/to/your/libs
You can also configure ld to do it all the time:
bash-4.1$ echo /path/to/your/libs >> /etc/ld.so.conf
bash-4.1$ ldconfig
Maybe you could use XAMPP and save yourself sometime? It comes prepackaged and pretty portable.
http://www.apachefriends.org/en/xampp.html