Web Server Guide
This guide covers building and deploying web applications with Strata, including development workflows, production deployment, and RoadRunner integration.
Overview
Strata provides powerful tools for building web applications:
- Development server with automatic recompilation
- Production-ready deployment with RoadRunner
- HTTP support for headers, status codes, and request handling
- Flexible configuration for different environments
Quick Start
Development
Start a development server:
strata serve server.strThe server will:
- Watch for file changes
- Automatically recompile on save
- Reload the application
- Refresh the browser automatically
- Use RoadRunner if installed (for better performance)
Production
Build and serve with RoadRunner:
# build the worker
strata build server.str --rr-worker
# start production server (if RoadRunner is installed)
strata serve server.str --production
# OR (standard deployment)
strata build
# serve 'build/index.php' with Nginx/ApacheDevelopment Workflow
For detailed information about the strata serve command, see the CLI Commands: strata serve documentation.
Quick start:
strata serve <file> [--host=HOST] [--port=PORT]The development server automatically:
- Watches all
.strfiles in your project - Recompiles when changes are detected
- Reloads the server
- Sends a
Refreshheader to browsers
The server automatically uses RoadRunner if available, otherwise falls back to PHP's built-in server.
Production Deployment
Prerequisites
- RoadRunner binary - Install from roadrunner.dev
- PHP dependencies:bash
composer require spiral/roadrunner-http spiral/roadrunner-worker nyholm/psr7 - Pre-built worker:bash
strata build server.str --rr-worker
Configuration
Create a .rr.yaml file in your project root. See CLI Commands: strata serve for a complete example configuration.
Starting Production Server
If RoadRunner is installed:
strata serve server.str --productionIf RoadRunner is not installed, builds should be served using Nginx or Apache pointing to the build/ directory. strata serve --production is discouraged without RoadRunner.
strata serve server.str --productionFor detailed production mode information, see CLI Commands: strata serve.
HTTP Handling
Setting Status Codes
Use http_response_code() to set HTTP status codes:
fn main(): Void {
http_response_code(201);
echo "Created";
}Setting Headers
Use header() to set HTTP headers:
fn main(): Void {
header("Content-Type: application/json");
header("X-Custom-Header: value");
echo json_encode(data: ["status" => "ok"]);
}Request Information
Access request data via superglobals:
fn main(): Void {
let method = _SERVER['REQUEST_METHOD'];
let path = _SERVER['REQUEST_URI'];
let query = _SERVER['QUERY_STRING'];
// handle request
if method == "GET" && path == "/api/users" {
http_response_code(200);
header("Content-Type: application/json");
echo getUsers();
}
}RoadRunner Integration
RoadRunner provides high performance, production-ready deployment with persistent worker processes and automatic worker restarts.
For detailed RoadRunner setup, installation, and configuration, see CLI Commands: strata serve.
Best Practices
Development
Use development mode for active development:
bashstrata serve server.strKeep RoadRunner installed for better performance and production parity
Watch console output for compilation status and errors
Production
Always build first:
bashstrata build server.str --rr-workerTest production mode locally:
bashstrata serve server.str --productionConfigure workers appropriately in
.rr.yaml:num_workers: Match your server capacitymax_jobs: 0 for unlimited (or set a limit)- Timeouts: Adjust based on your needs
Set appropriate log levels:
yamllogs: level: error # Use 'error' in production, 'debug' for troubleshooting
Code Organization
Separate concerns:
- Request handling logic
- Business logic
- Response formatting
Use classes for services:
strataclass UserService { // service logic } fn main(): Void { let service = UserService(); // handle request with service }Handle errors gracefully:
stratafn handleRequest(): String { try { return processRequest(); } catch (e) { http_response_code(500); return '{"error": "Internal server error"}'; } }
Troubleshooting
For detailed troubleshooting information, see the Troubleshooting section in CLI Commands: strata serve.
Next Steps
- CLI Commands: strata serve - Detailed command reference
- Web Application Examples - Code examples
- Language Reference - Learn Strata features
- RoadRunner Documentation - RoadRunner guide