Home > PHP Framework > Swoole > body text

Introduction to Swoole development: How to quickly build a simple web server

王林
Release: 2023-11-08 10:03:59
Original
1032 people have browsed it

Introduction to Swoole development: How to quickly build a simple web server

Introduction to Swoole development: How to quickly build a simple Web server

Introduction:
Swoole is a high-performance PHP extension that provides asynchronous and concurrency The network communication capabilities enable PHP programs to handle a large number of concurrent requests. This article will introduce how to use Swoole to quickly build a simple web server and provide specific code examples.

1. Install the Swoole extension
First, we need to install the Swoole extension. It can be installed in the following ways:

# 安装swoole扩展
pecl install swoole
Copy after login

After the installation is completed, add the extended configuration in the php.ini file:

extension=swoole
Copy after login

2. Create a simple Web server
Next, we You can start creating a simple web server. First, we need to create a PHP file (for example, server.php) and add the following code:

<?php
$http = new SwooleHttpServer('0.0.0.0', 8000);

$http->on('request', function ($request, $response) {
    $response->header('Content-Type', 'text/plain');
    $response->end('Hello, Swoole!');
});

$http->start();
Copy after login

In this code, we first create a Swoole Http server instance and bind it to port 8000. Then, we process each HTTP request by listening to the request event. In each request, we set the response Content-Type to text/plain and return the response content by calling the $response->end() method.

3. Run the Web server
After saving the above code, we can run the Web server through the command line:

php server.php
Copy after login

In this way, our Web server is already running. You can access http://localhost:8000 through your browser, and you will see a simple page showing Hello, Swoole!.

4. Optimize Web server performance
In order to further improve the performance of the Web server, we can add some optimization configurations. Modify the server.php file and add the following content:

<?php
$http = new SwooleHttpServer('0.0.0.0', 8000);

$http->set([
    'worker_num' => 2,      // 设置工作进程数为2
    'max_request' => 1000,  // 设置每个工作进程的最大请求数为1000
]);

$http->on('request', function ($request, $response) {
    $response->header('Content-Type', 'text/plain');
    $response->end('Hello, Swoole!');
});

$http->start();
Copy after login

Here, we set some optimization parameters by calling the $http->set() method. We set the number of worker processes to 2 so that we can take advantage of multi-core CPUs. We also set the maximum number of requests per worker process to 1000 to avoid memory leaks caused by long runs.

5. Processing routing
In addition to simple responses, we can also add routing processing to achieve more complex functions. Modify the server.php file and use Swoole's router component to handle different URL requests:

<?php
$http = new SwooleHttpServer('0.0.0.0', 8000);

$http->set([
    'worker_num' => 2,
    'max_request' => 1000,
]);

$http->on('request', function ($request, $response) {
    $response->header('Content-Type', 'text/plain');
    
    $router = new SwooleHttpServerRouter();

    $router->get('/', function () use ($response) {
        $response->end('Hello, Swoole!');
    });

    $router->get('/about', function () use ($response) {
        $response->end('This is about page.');
    });

    $router->get('/contact', function () use ($response) {
        $response->end('This is contact page.');
    });

    $router->dispatch($request->server['request_uri']);
});

$http->start();
Copy after login

In this code, we create a SwooleHttpServerRouter instance to handle requests from different URLs. We added three routes, namely root path /, about page /about, and contact page /contact. Depending on the requested URL, we return different content by calling the corresponding handler function.

6. Summary
Through the above steps, we successfully built a simple web server and learned how to use Swoole to develop web applications. Starting from this simple example, you can further explore Swoole's various functions and advanced features to implement more complex network applications. I hope this article will help you understand and get started with Swoole development!

The above is the detailed content of Introduction to Swoole development: How to quickly build a simple web server. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template