Running PHP Unikernels on Google Cloud

by bootsity on Jun 4, 2019 Software 1001 Views

1. Introduction

Setting up and managing our own servers can take a considerable amount of time especially if we are not a day to day linux user. That is why we see things like serverless and containers catching on. There’s another option out there though too and it’s called unikernels.

What are unikernels though? Unikernels are simple single application operating systems. Instead of deploying our software on top of linux unikernels create an operating system out of our application and run only that app – nothing else. There’s no concept of usernames/passwords or sshin’g into the application as it is only running one application. Traditionally unikernels have been hard to work with as they used to require expert level of systems knowledge. Now there are newer tools like OPS that fix that.

2. Setup

In the next few minutes we are going to build, run, then deploy a php unikernel. First things first let’s download OPS from https://ops.city:

  1. curl https://ops.city/get.sh -sSfL | sh

We can also download and build it from github. can always go to https://github.com/nanovms/ops and build it from source – it’s written in Go.

3. Build and Run our First PHP Unikernel

Now that we have that let’s create a temporary working directory:

  1. mkdir p && cd p

For this simple example we’ll just use the built-in php webserver just to keep things simple. Put this into a config.json file:

  1. {
  2. "Args":["-S", "0.0.0.0:3030"],
  3. "Files": ["index.php"]
  4. }

What this does is tell the built in php webserver to listen on port 3030 and place the index.php file onto the otherwise barren filesystem. We’ll put this into a index.php:

  1. <?php
  2.  
  3. echo 'hello from php!';
  4.  
  5. ?>

Now let’s run it via:

  1. ops load php_7.2.13 -c config.json -p 3030

Here we tell OPS that we wish to use PHP7. When we are running this it will first download a php package then it looks at the config file and finally opens up a local port on 3030.

We should see this:

  1. [php -S 0.0.0.0:3030]
  2. booting /Users/eyberg/.ops/images/php.img ...
  3. assigned: 10.0.2.15

Assuming that boots we should now be able to hit it with curl:

  1. ➜ ~ curl -XGET http://127.0.0.1:3030/
  2. hello from php!%
  3. ➜ ~ curl -XGET http://127.0.0.1:3030/
  4. hello from php!%

4. Deploy to Google Cloud

Great! We just ran our first php unikernel! Now let’s deploy it. Assume have a google cloud account we’ll need a project id, a service worker key, and a cloud storage bucket to place our built images in. We definitely don’t have to use google but we are using it for this example.

We are going to slightly modify our config.json file and put this inside:

  1. {
  2.  
  3. "Args":["-S", "0.0.0.0:80"],
  4. "Files": ["index.php"],
  5. "CloudConfig" :{
  6. "ProjectID" :"my-project-id",
  7. "BucketName":"my-bucket"
  8. },
  9. "RunConfig" : {"Memory": "2G"}
  10. }

What this does is tell OPS to place our built image in the bucket ‘my-bucket’ and use ‘my-project-id’. We also changed the port to 80 which is the default HTTP port.

If we got a service worker key we can put that somewhere to source like so:

  1. export GOOGLE_APPLICATION_CREDENTIALS=~/gcloud.json

Then let’s build the image. What this does is create a virtual machine on Google Cloud that contains only our application with the php interpreter. It’s important to note that there is no underlying linux system here. When the virtual machine boots it boots straight into our application just like it did locally. This make it run much faster and safer than linux.

  1. ➜ p ops image create -c config.json -p php_7.2.13
  2. [php -S 0.0.0.0:80]
  3. bucket found: nanos-deploy
  4. Image creation started. Monitoring operation operation-1556904873950-587ff29f5fcc5-76e6bf08-ff926925.
  5. ..............
  6. Operation operation-1556904873950-587ff29f5fcc5-76e6bf08-ff926925 completed successfullly.
  7. Image creation succeeded nanos-php-image.
  8. gcp image 'nanos-php-image' created...

We should see something like this when it’s done building:

  1. ➜ p ops image list
  2. +--------------------+--------+-------------------------------+
  3. | NAME | STATUS | CREATED |
  4. +--------------------+--------+-------------------------------+
  5. | nanos-main-image | READY | 2019-03-21T15:06:17.567-07:00 |
  6. +--------------------+--------+-------------------------------+
  7. | nanos-node-image | READY | 2019-04-16T23:16:03.145-07:00 |
  8. +--------------------+--------+-------------------------------+
  9. | nanos-php-image | READY | 2019-05-03T10:34:34.966-07:00 |
  10. +--------------------+--------+-------------------------------+

Now let’s create an instance. Keep in mind an instance is the server. The image is the actual disk image of the server.

  1. ➜ p ops instance create -p prod-1033 -z us-west2-a -i nanos-php-image
  2. Instance creation started using image projects/prod-1033/global/images/nanos-php-image. Monitoring operation operation-1556905041207-587ff33ee1f25-fc8382ab-ec299733.
  3. .....
  4. Operation operation-1556905041207-587ff33ee1f25-fc8382ab-ec299733 completed successfullly.
  5. Instance creation succeeded nanos-php-image-1556905040.

If we look at the instance list we can see it running:

  1. ➜ p ops instance list -z us-west2-a -p prod-1033
  2. +----------------------------+---------+-------------------------------+-------------+--------------+
  3. | NAME | STATUS | CREATED | PRIVATE IPS | PUBLIC IPS |
  4. +----------------------------+---------+-------------------------------+-------------+--------------+
  5. | nanos-php-image-1556905040 | RUNNING | 2019-05-03T10:37:22.465-07:00 | 10.240.0.48 | 35.236.11.13 |
  6. +----------------------------+---------+-------------------------------+-------------+--------------+

Now if we hit that ip up with curl we can see our php unikernel running in Google Cloud!

  1. ➜ ~ curl -XGET http://35.236.11.13
  2. hello from php!%
  3. ➜ ~ curl -XGET http://35.236.11.13
  4. hello from php!%

We can also see the logs from the unikernel:

  1. ➜ p ops instance logs nanos-php-image-1556905040 -p prod-1033 -z us-west2-a
  2. SeaBIOS (version 1.8.2-20190322_093631-google)
  3. Total RAM Size = 0x0000000080000000 = 2048 MiB
  4. CPUs found: 1 Max CPUs supported: 1
  5. found virtio-scsi at 0:3
  6. virtio-scsi vendor='Google' product='PersistentDisk' rev='1' type=0 removable=0
  7. virtio-scsi blksize=512 sectors=2097152 = 1024 MiB
  8. drive 0x000f2a30: PCHS=0/0/0 translation=lba LCHS=1024/32/63 s=2097152
  9. Booting from Hard Disk 0...
  10. assigned: 10.240.0.48
  11. [Fri May 3 17:39:36 2019] 1.2.3.4:57595 [200]: /
  12. [Fri May 3 17:39:37 2019] 1.2.3.4:57596 [200]: /
  13. [Fri May 3 17:39:59 2019] 92.112.48.216:35338 [200]: /

5. Conclusion

Congratulations! We’ve built, ran and deployed our first php unikernel on google cloud – now we can go show our friends that we don’t need servers to deploy our applications anymore! Unikernels give us the convenience of containers and serverless with the protection and speed of traditional virtual machines.

Article source: https://article-realm.com/article/Computers/Software/2508-Running-PHP-Unikernels-on-Google-Cloud.html

Comments

No comments have been left here yet. Be the first who will do it.
Safety

captchaPlease input letters you see on the image.
Click on image to redraw.

Reviews

Guest

Overall Rating:

Statistics

Members
Members: 16984
Publishing
Articles: 79,275
Categories: 202
Online
Active Users: 1979
Members: 15
Guests: 1964
Bots: 29549
Visits last 24h (live): 4976
Visits last 24h (bots): 74769

Latest Comments

Excellent information providing by your Article thank you for taking the time to share with us such a nice article. Jack    
I once spent weeks meticulously documenting a software concept, fearing any oversight might jeopardize future protection, only to find the process even more daunting when a potential investor...
on Sep 3, 2026 about How to Start an Invention Idea
Opting for a Vip Escort Delhi professional is a commitment to a premium, secure, and discreet lifestyle. Their unwavering focus on your desires, set within a private and comfortable...
I really enjoyed exploring Article Realm because it covers a variety of useful topics for people interested in building an online presence. This article especially highlights how SEO, keywords,...
on Sep 1, 2026 about The Latest Online Business
I like the focus on identifying specific gains from personal content marketing because it makes the strategy feel more purposeful than simply posting regularly and hoping something sticks. In my...
I also liked the point about low-traffic websites. AI may identify behavioral patterns faster, but when the sample size is too small, the test results can still be unreliable. A tool can speed up...
The article also made me think about something that's easy to overlook: backlinks should ultimately provide value to real users. If a link has no real context and exists only to increase the...
FNAF Game is one of those games that can make even a quiet room feel terrifying. I really enjoy how the game builds tension slowly instead of relying only on sudden scares. Watching the cameras,...
Deadshot IO is a really enjoyable FPS experience for players who like fast combat and competitive gameplay.
It is the easiest and fastest way to monetize a website. All you need to do is sign up and install the plugin, after which you can decide how many ads to display and how much revenue to generate....

Translate To: