How to use workerman with thinkphp
The following column workerman Getting Started Tutorial will introduce to you the method of combining workerman with thinkphp. I hope it will be of some help to you.
To run workererman you need to install pcntl and event or libevent
pcntl installation method:
With php-5.5 .20 as an example, the actual situation is based on the PHP directory you installed
1. Find the PHP source code, enter the php-5.5.20/ext/pcntl/ directory cd php-5.5.20/ext/pcntl/
2. Run locate phpize to find the phpize directory and run /usr/local/php-5.5.20/bin/phpize
3. Execute ./configure --with-php-config=PHP Configuration file path For example: ./configure --with-php-config=/usr/local/php-5.5.20/bin/php-config
4. Compile and install make && make install If/ext /pcntl/modules/pcntl.so is generated and compiled successfully
5. Add extension to PHP.INI loaded by php echo "extension=pcntl.so" >> /etc/php.ini ( Enter the command php --ini to view the currently used php.ini configuration path)
6. Restart nginx nginx -s reload
7. View the service ps -aux | grep pcntl
If it is version 5.3, you can directly enter the command to install:
yum install php-cli php-process git gcc php-devel php-pear libevent-devel php-pdo php-mysql_pdo -y
event installation method:
1. yum install libevent-devel -y
2. pecl install event
Tips: Include libevent OpenSSL support [yes]: Enter no and press Enter, otherwise just press Enter
3. echo extension=event.so > /etc/php.ini
libevent installation method:
1. yum install libevent-devel
2. pecl install channel://pecl .php.net/libevent-0.1.0 //Prompt libevent installation [autodetect]: Press Enter
3. Check the PHP directory lib/php/extensions/no-debug-non-zts-20121212 Whether libevent.so is generated under the directory
4. Enter the command php -v to view the installed extensions
Workerman is integrated into Thinkphp: (recommended: workerman Tutorial)
1. Place the downloaded workerman directory into the project\ThinkPHP\Library directory
2. Place the class files in the workerman directory except \Lib\Constants.php Change the file name to *.class.php
3. Create a new file worker.php file in the project root directory with the following content:
<?php header("Content-type: text/html; charset=utf-8"); // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- // 应用入口文件 define('BIND_MODULE', 'Worker'); define('BIND_CONTROLLER', 'Worker'); define('BIND_ACTION', 'Start'); //define('APP_MODE','cli'); //ThinkPHP的其他设定 define( 'APP_PATH', dirname(__FILE__).'/Application/' ); require dirname( __FILE__).'/ThinkPHP/ThinkPHP.php'; ?>
4. Copy a copy of the Home module and change its name to Worker
5. Create a new Worker controller in the Worker module with the following content:
<?php namespace Worker\Controller; use Think\Controller; use Workerman\Worker; class WorkerController extends Controller{ public function Start() { $worker = new Worker('text://0.0.0.0:8989'); //实例化Worker,并设置协议、IP和端口(地址和端口自定义) $worker->count = 4; $worker->onMessage = array($this, 'onMessage'); //worker的其它回调方法可以参考onMessage Worker::runAll(); } public function onMessage($connection, $data) { $connection->send('hello'); } } ?>
6. Enter the Linux system and open port 8989
iptables -I INPUT -p tcp --dport 8989 -j ACCEPT
7. Enter the project root Enter the startup command in the directory:
php worker.php start
displays OK, indicating that the startup is successful
8. Create a new server link and enter telnet 127.0.0.1 8989 to test
Workerman accesses MySQL :
1. Install pdo and pdo_mysql and other related extensions
2. Add database-related configurations to config.php in the Conf directory of the Worker module
3. Do not use localhost for the server address, use IP127.0.0.1
4. After configuring the database, you can directly use Thinkphp’s Db method
The above is the detailed content of How to use workerman with thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

To implement file upload and download in Workerman documents, specific code examples are required. Introduction: Workerman is a high-performance PHP asynchronous network communication framework that is simple, efficient, and easy to use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples. 1. File upload: File upload refers to the operation of transferring files on the local computer to the server. The following is used

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Introduction to how to implement the basic usage of Workerman documents: Workerman is a high-performance PHP development framework that can help developers easily build high-concurrency network applications. This article will introduce the basic usage of Workerman, including installation and configuration, creating services and listening ports, handling client requests, etc. And give corresponding code examples. 1. Install and configure Workerman. Enter the following command on the command line to install Workerman: c

Swoole and Workerman are both high-performance PHP server frameworks. Known for its asynchronous processing, excellent performance, and scalability, Swoole is suitable for projects that need to handle a large number of concurrent requests and high throughput. Workerman offers the flexibility of both asynchronous and synchronous modes, with an intuitive API that is better suited for ease of use and projects that handle lower concurrency volumes.
