18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP で port 80 を開いたあと root 権限を捨てる web サーバの例

18
Posted at
$ composer require react/react:\*
$ vim index.php
$ sudo php index.php
index.php
<?php
use React\EventLoop\Factory;
use React\Socket\Server as SocketServer;
use React\Http\Server as HttpServer;
use React\Http\Request;
use React\Http\Response;

require __DIR__ . '/vendor/autoload.php';

$loop = Factory::create();
$socket = new SocketServer($loop);
$http = new HttpServer($socket);

$http->on('request', function (Request $request, Response $response) {
    $uid = posix_getuid();
    $euid = posix_geteuid();
    $name = posix_getpwuid($uid)['name'];
    $response->writeHead();
    $response->write("Hello $request->remoteAddress\n");
    $response->write("I am $name, uid is $uid($euid)\n");
    $response->end();
});

$uid = posix_getpwnam('apache')['uid'] or die(__LINE__);
$socket->listen(80, '0.0.0.0');
posix_setuid($uid) or die(__LINE__);
$loop->run();
$ curl http://localhost/
Hello 127.0.0.1
I am apache, uid is 48(48)
18
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?