How to refresh changes in swoole PHP

871 Views Asked by At

I created one folder with two files, one is index.php and one is Dockerfile and in terminal I run php index.php and it started on http://127.0.0.1:9501/, but when I change hello world in something else, it doesn't show new change on that link.. it stays hello world. I am trying to learn Swoole and I don't know how to work with this.. Also I created in same folder Dockerfile

index.php

<?php

use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

$server = new Swoole\HTTP\Server("127.0.0.1", 9501);

$server->on("Start", function(Server $server)
{
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$server->on("Request", function(Request $request, Response $response)
{
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$server->start();

Dockerfile

FROM php:8.1.0-cli

RUN apt-get update && apt-get install vim -y && \
    apt-get install openssl -y && \
    apt-get install libssl-dev -y && \
    apt-get install wget -y && \
    apt-get install git -y && \
    apt-get install procps -y && \
    apt-get install htop -y

RUN cd /tmp && git clone https://github.com/openswoole/swoole-src.git && \
    cd swoole-src && \
    git checkout v4.11.0 && \
    phpize  && \
    ./configure --enable-openssl --enable-swoole-curl --enable-http2 --enable-mysqlnd && \
    make && make install

RUN touch /usr/local/etc/php/conf.d/openswoole.ini && \
    echo 'extension=openswoole.so' > /usr/local/etc/php/conf.d/zzz_openswoole.ini

RUN wget -O /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.2/dumb-init_1.2.2_amd64
RUN chmod +x /usr/local/bin/dumb-init

RUN apt-get autoremove -y && rm -rf /var/lib/apt/lists/*

ENTRYPOINT ["/usr/local/bin/dumb-init", "--", "php"]

And when I run,

docker build -f ./Dockerfile -t openswoole-php .

I am not able to build docker image for php-swoole I get error

executor failed running [/bin/sh -c cd /tmp && git clone https://github.com/openswoole/swoole-src.git &&     cd swoole-src &&     git checkout v4.11.0 &&     phpize  &&     ./configure --enable-openssl --enable-swoole-curl --enable-http2 --enable-mysqlnd &&     make && make install]: exit code: 2
4

There are 4 best solutions below

0
On BEST ANSWER

I found solution to stop the web server, press Control+C. And again run php index.php

0
On

In terminal just press Control+C and write again the php index.php

At some point you will need to put your index.php to run in Supervisor, so when that time comes, just type:

sudo service nginx restart && service supervisor restart
1
On

I've found two ways to refresh PHP content:

  1. Using the HTML meta tag: echo ("<meta http-equiv='refresh' content='1'>"); //Refresh by HTTP 'meta'
  2. Using PHP refresh rate:
0
On

How about using Docker Compose? Inside a folder you've created, do the following:

  1. Rename your file to server.php and change:

$server = new Swoole\HTTP\Server("127.0.0.1", 9501);
to
$server = new Swoole\HTTP\Server("0.0.0.0", 9501);

  1. Create a docker-compose.yml with following content:
services:
  app:
    image: phpswoole/swoole
    environment:
      AUTORELOAD_PROGRAMS: "swoole"
      AUTORELOAD_ANY_FILES: 1
    ports:
      - 80:9501
    volumes:
      - .:/var/www #copy the content (server.php) to the container
  1. run docker compose up or docker compose up -d

After that you can make changes to your code and refresh your browser or make another request to it

Source: https://github.com/swoole/docker-swoole/tree/master/examples/00-autoreload