In this guide we create and deploy a simple PHP-based HTTP web server.
To run this example, follow these steps:
Install the kraft
CLI tool and a container runtime engine, e.g. Docker.
Clone the examples
repository and cd
into the examples/http-php8.2/
directory:
git clone https://github.com/kraftcloud/examples
cd examples/http-php8.2/
Make sure to log into KraftCloud by setting your token and a metro close to you.
We use fra0
(Frankfurt, 🇩🇪) in this guide:
# Set KraftCloud access token
export KRAFTCLOUD_TOKEN = token
# Set metro to Frankfurt, DE
export KRAFTCLOUD_METRO = fra0
When done, invoke the following command to deploy this application on KraftCloud:
kraft cloud deploy -p 443 :8080 .
The output shows the instance URL and other details:
[ ● ] Deployed successfully!
│
├ ────────── name : http-php82-g00si
├ ────────── uuid : 033b2f4b-72ff-414d-b0de-63571477c657
├ ───────── state : running
├ ─────────── url : https://aged-fire-rh0oi0tj.fra0.kraft.host
├ ───────── image : http-php82@sha256:dccaac053982673765b8f00497a9736c31458ab23ad59a550b09aa8dedfabb34
├ ───── boot time : 32.80 ms
├ ──────── memory : 128 MiB
├ ─ service group : aged-fire-rh0oi0tj
├ ── private fqdn : http-php82-g00si.internal
├ ──── private ip : 172.16.3.3
└ ────────── args : /usr/local/bin/php /usr/src/server.php
In this case, the instance name is http-php82-g00si
and the URL is https://aged-fire-rh0oi0tj.fra0.kraft.host
.
They are different for each run.
Use curl
to query the KraftCloud instance of the PHP-based HTTP web server:
curl https://aged-fire-rh0oi0tj.fra0.kraft.host
At any point in time, you can list information about the instance:
kraft cloud instance list
NAME FQDN STATE CREATED AT IMAGE MEMORY ARGS BOOT TIME
http-php82-g00si aged-fire-rh0oi0tj.fra0.kraft.host running 50 seconds ago razvan.unikraft.io/http-php82@sha256:dccaac05398267376... 256 MiB /usr/local/bin/php /usr/src/server.php 32801us
When done, you can remove the instance:
kraft cloud instance remove http-php82-g00si
Customize your Application
To customize the application, update the files in the repository, listed below:
server.php
: the actual PHP HTTP server
php.ini
: the PHP configuration
Kraftfile
: the KraftCloud specification
Dockerfile
: the Docker-specified application filesystem
#!/usr/local/bin/php -q
<? php
error_reporting ( E_ALL );
/* Allow the script to hang around waiting for connections. */
set_time_limit ( 0 );
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush ();
$address = ' 0.0.0.0 ' ;
$port = 8080 ;
if (( $sock = socket_create ( AF_INET , SOCK_STREAM , SOL_TCP )) === false ) {
echo " socket_create() failed: reason: " . socket_strerror ( socket_last_error ()) . " \n " ;
}
if ( socket_bind ($ sock , $ address , $ port ) === false ) {
echo " socket_bind() failed: reason: " . socket_strerror ( socket_last_error ($ sock )) . " \n " ;
}
if ( socket_listen ($ sock , 5 ) === false ) {
echo " socket_listen() failed: reason: " . socket_strerror ( socket_last_error ($ sock )) . " \n " ;
}
do {
if (( $msgsock = socket_accept ($ sock )) === false ) {
echo " socket_accept() failed: reason: " . socket_strerror ( socket_last_error ($ sock )) . " \n " ;
break ;
}
/* Send instructions. */
$msg = " HTTP/1.1 200 OK \r\n " .
" Content-Type: text/html \r\n " .
" Content-Length: 14 \r\n " .
" Connection: close \r\n " .
" \r\n " .
" Hello, World! \n " ;
socket_write ($ msgsock , $ msg , strlen ($ msg ));
socket_close ($ msgsock );
} while ( true );
socket_close ($ sock );
?>
spec : v0.6
runtime : php:8.2
rootfs : ./Dockerfile
cmd : [ " /usr/local/bin/php " , " /usr/src/server.php " ]
FROM scratch
# PHP configuration
COPY ./php.ini /usr/local/etc/php/php.ini
# Simple PHP HTTP server
COPY ./server.php /usr/src/server.php
The following options are available for customizing the application:
If only updating the implementation in the server.php
source file, no other change is required.
If new files are added, these have to be copied in the application filesystem, using the COPY
command in the Dockerfile
.
If new extension are required, that may require the update the of the php.ini
file.
If a new PHP source files is added, update the cmd
line in the Kraftfile
and replace server.php
to run that file when creating the instance.
More extensive changes may require expanding the Dockerfile
with additional Dockerfile
commands .
Learn More
Use the --help
option for detailed information on using KraftCloud:
Or visit the CLI Reference .