Skip to content

Commit

Permalink
Added documentation and some adjustments to Https handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
lotharthesavior committed Aug 21, 2023
1 parent 2ff7fb9 commit 607496b
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 13 deletions.
138 changes: 138 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@

# Jacked Server

[![PHP Composer](https://github.com/Jacked-PHP/jacked-server/actions/workflows/main.yml/badge.svg)](https://github.com/Jacked-PHP/jacked-server/actions/workflows/main.yml)


## Overview

The Jacked Server is a composer package for **Laravel** that runs a Web Server in a few different methods. Between them, you'll find:

- FastCGI - it can point to a host directory and run the PHP files in that directory via FastCGI protocol.

## Installation

Install composer package:

```shell
composer require jacked-php/jacked-server
```

Publish configuration file:

```shell
php artisan vendor:publish --tag=jacked-server
```

Add the Service Provider to the `config/app.php`:

```php
<?php

return [
// ...
'providers' => ServiceProvider::defaultProviders()->merge([
// ...
JackedPhp\JackedServer\JackedServerProvider::class,
]),
// ...
];
```

## FastCGI Proxy

This server proxy the request to a FastCGI Socket or TCP service. IT is capable of serving WordPress websites, advanced Laravel applications or even single PHP files if needed. It can serve HTTPS and HTTP.

The `jacked:server` command, part of the Jacked Server service, provides a CLI interface to start the OpenSwoole server to serve your website via FastCGI proxy.

### Signature

The command can be invoked using:

```
php artisan jacked:server
```

It accepts the following optional parameters:

- `--host`: Specifies the server host. Defaults to the configuration value.
- `--port`: Specifies the server port. Defaults to the configuration value.
- `--inputFile`: Specifies the input PHP file. Defaults to `public/index.php`.
- `--documentRoot`: Specifies the document root directory where assets like js files, css or images will be served from. Defaults to `public`.
- `--publicDocumentRoot`: Specifies the public document root directory. Defaults to `public`.

### Description

The command's description is:

```
JackedPHP OpenSwoole Server
```

### Execution

When executed, the command initializes a new instance of the `Server` class with the provided options and runs the server.

### Events

The server fires several events during its lifecycle:

- **JackedServerStarted:** Fired when the server starts.

- **JackedRequestReceived:** Fired when a new request is received.

- **JackedRequestError:** Fired when there's an error in processing the request.

- **JackedRequestFinished:** Fired when the request processing is finished.

### Configuration

This configuration file provides settings for the Jacked Server. The settings are organized into various sections, each catering to a specific aspect of the server's operation.

#### Running Server Details

- `host`: The **IP address** or **domain** on which the server will run. Default is `'0.0.0.0'`, meaning it will listen on all available interfaces.

- `port`: The port number on which the server will listen. Default is `8080`.

- `server-type`: Specifies the type of server. Default is `Server::POOL_MODE`. (refer to [OpenSwoole Server constructor documentation](https://openswoole.com/docs/modules/swoole-server-construct) for more information)

#### SSL Configuration

> Note that when the SSL is enabled, requests to HTTP will be redirected to HTTPS.
- **`ssl-port`**: The port number for SSL connections. Default is `443`.

- **`ssl-enabled`**: Determines if SSL is enabled. Default is `false`.

- **`ssl-cert-file`**: Path to the SSL certificate file.

- **`ssl-key-file`**: Path to the SSL key file.

#### Running Server Default Options

- **`server-protocol`**: The protocol used by the server. Default is `'HTTP/1.1'`.

- **`content-type`**: The default content type for responses. Default is `'text/html'`.

- **`input-file`**: The default input file for the server. Default is the `index.php` in the public directory.

- **`openswoole-server-settings`**: Contains settings specific to the OpenSwoole server:
- **`document_root`**: The root directory for serving documents. Default is the public directory.
- **`enable_static_handler`**: Determines if static content handling is enabled. Default is `true`.
- **`static_handler_locations`**: Specifies the locations for static content. Default locations are `/imgs` and `/css`.

#### Logging

- **`log`**: Contains settings related to logging:
- **`driver`**: The logging driver to use. Default is `'single'`.
- **`path`**: The path where log files will be stored. Default is `logs/jacked-server.log` in the storage directory.
- **`replace-placeholders`**: A flag to determine if placeholders in the log should be replaced. Default is `true`.

#### FastCgi Client Info

This section provides information on how to connect to the FastCGI client:

- **`host`**: The host for the FastCGI client. Default is `'127.0.0.1'`. If using a Unix socket, the format should be `unix:///path/to/php/socket`.

- **`port`**: The port for the FastCGI client. Default is `9000`. If using a Unix socket, set this to `-1`.
9 changes: 0 additions & 9 deletions config/jacked-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,4 @@
'host' => env('JACKED_SERVER_FASTCGI_HOST', '127.0.0.1'),
'port' => env('JACKED_SERVER_FASTCGI_PORT', 9000),
],

// ------------------------------------------------------------
// Events
// ------------------------------------------------------------

'events' => [
'server-started' => [ JackedServerStarted::class ],
'request-received' => [ JackedRequestReceived::class ],
],
];
2 changes: 1 addition & 1 deletion src/JackedServerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function boot(): void
{
$this->publishes([
__DIR__.'/../config/jacked-server.php' => config_path('jacked-server.php'),
]);
], 'jacked-server');

if ($this->app->runningInConsole()) {
$this->commands([
Expand Down
18 changes: 15 additions & 3 deletions src/Services/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OpenSwoole\Server as OpenSwooleBaseServer;
use OpenSwoole\Http\Server as OpenSwooleServer;
use Psr\Log\LoggerInterface;
use OpenSwoole\Util;

class Server
{
Expand Down Expand Up @@ -56,11 +57,10 @@ public function run(): void
'enable_static_handler' => true,
'static_handler_locations' => [ '/imgs', '/css' ],
// reactor and workers
'reactor_num' => \OpenSwoole\Util::getCPUNum() + 2,
'worker_num' => \OpenSwoole\Util::getCPUNum() + 2,
'reactor_num' => Util::getCPUNum() + 2,
'worker_num' => Util::getCPUNum() + 2,
]));
$server->on('start', [$this, 'handleStart']);
$server->on('request', [$this, 'handleRequest']);

// ssl
if (config('jacked-server.ssl-enabled', false)) {
Expand All @@ -75,11 +75,23 @@ public function run(): void
'open_http_protocol' => true,
]);
$sslPort->on('request', [$this, 'handleRequest']);
$server->on('request', [$this, 'sslRedirectRequest']);
} else {
$server->on('request', [$this, 'handleRequest']);
}

$server->start();
}

public function sslRedirectRequest(Request $request, Response $response) {
$response->status(301);
$response->header(
'Location',
'https://' . $request->header['host'] . $request->server['request_uri'],
);
$response->end();
}

public function handleStart(OpenSwooleServer $server): void
{
$message = 'OpenSwoole Server started'
Expand Down

0 comments on commit 607496b

Please sign in to comment.