Workerman and thinkphp combined, workermanthinkphp
To run workerman, you must install pcntl, event, or libevent.
Pcntl Installation Method:
Take the php-5.5.20 as an example, the actual situation according to your own installed PHP Directory
A, 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 find phpize directory, run/usr/local/php-5.5.20/bin/phpize 3. Run. /configure -- with-php-config = path of the PHP configuration file, 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. php loaded by PHP. added the extended echo "extension = pcntl in INI. so ">/etc/php. ini (enter the command php -- ini to view the current php. ini configuration path) 6. Restart nginx-s reload 7. View Service ps-aux | grep pcntl
For Version 5.3, run the following command:
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
Ii. pecl install event
Tip: Include libevent OpenSSL support [yes]: enter no and press Enter. Otherwise, press Enter.
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 // press Enter when the prompt libevent installation [autodetect ]:
3. Check whether libevent. so is generated under the directory of PHP Directory lib/php/extensions/no-debug-non-zts-20121212
4. Enter the php-v command to view the installed extensions.
Workerman: https://github.com/walkor/workerman
Integrate Workerman into Thinkphp:
1. Put the downloaded workerman directory to the project \ ThinkPHP \ Library directory.
2. Change the file name except \ Lib \ Constants. php In the workerman directory to *. class. php.
3. Create the worker. php file in the project root directory. The content is as follows:
<? 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>
// + ----------------------------------------------------------------------
// Application entry file
Define ('Bind _ module', 'worker ');
Define ('Bind _ controller', 'worker ');
Define ('Bind _ action', 'start ');
// Define ('app _ mode', 'cl ');
// Other ThinkPHP settings
Define ('app _ path', dirname (_ FILE _). '/Application /');
Require dirname (_ FILE _). '/ThinkPHP. php ';
?>
4. Copy a copy of the Home module and change its name to Worker.
5. Create a Worker controller in the Worker module. Its content is as follows:
<? 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: 100'); // instantiate the Worker and set the protocol, IP address, and port (custom addresses and ports)
$ Worker-> count = 4;
$ Worker-> onMessage = array ($ this, 'onmessage ');
// For other worker callback methods, refer to onMessage
Worker: runAll ();
}
Public function onMessage ($ connection, $ data)
{
$ Connection-> send ('hello ');
}
}
?>
6. Go to the Linux system and enable port 8989.
Iptables-I INPUT-p tcp -- dport 8989-j ACCEPT
7. Enter the project root directory and enter the startup command:
Php worker. php start
Show OK, indicating that the startup is successful
8. Create a server connection and enter telnet 127.0.0.1 8989 for testing.
Workerman accessing MySQL:
1. Install related extensions such as pdo and pdo_mysql. 2. Add database-related configurations in config. php under the Conf directory of the Worker module. 3. Use IP127.0.0.1 for the server address instead of localhost.
4. After configuring the database, you can directly use the Thinkphp Db method.