Home  >  Article  >  Backend Development  >  Analysis of PHP multiplayer module development principles_php skills

Analysis of PHP multiplayer module development principles_php skills

韦小宝
韦小宝Original
2017-12-15 09:54:131433browse

This article explains in detail the principles of PHP multi-person development and what needs to be paid attention to, for those who have just learned the PHP language or have already started looking for a job. Hey guys, don’t miss this PHP Analysis of the Principles of Multiplayer Module Development! !

As the best language in the world, it occupies about 80% of the web. Small and medium-sized companies basically use the lnmp architecture. When there are more than 1 or 20 developers in a warehouse, each person may develop different modules and functions, and use code version control tools such as git to open different branches. The process is probably to first set up a complete environment locally and develop it. Deploy in the test environment. After self-test or tester testing, deploy in the pre-release environment. The pre-release is basically the same as the online environment, and then the product is accepted. After the acceptance is completed, it is released online.

Because it is developed in parallel, there must be situations where several functions are accepted or tested at the same time. At this time, whose code is deployed in the pre-release environment? If you switch to A's branch, B will not be able to accept it. Therefore, we hope that there will be a multi-person development environment where everyone's development process does not affect each other.

PHP operating principle

First of all, let’s analyze the operating principle of PHP and look at the language characteristics of PHP. When we initiate a request from the browser, our web server (Nginx, Apache, etc.) listens to port 80 or 443. Let’s look at the simplest Nginx vhost configuration:


server {
 listen    80;
 server_name test.com;
 
 root /data/gateway/html;
 index  index.php;

 location ~ \.php$ {
  fastcgi_pass  127.0.0.1:9001; #unix:/Users/run/php-fcgi.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include    fastcgi_params;
 }
}


Nginx listens to port 80 and uses the corresponding vhost configuration when it matches that the domain name visited by the user is test.com. PHP-FPM starts a service in the server and listens to a port (such as 9001) or a unix socket. Nginx is configured through fastcgi_pass and passes the request to PHP-FPM to parse the PHP code. The PHP parser each time Start parsing from index.php, process it all the way, perform a series of logical processing, query database or cache and other operations, return an HTML or other results to Nginx, and Nginx returns it to the browser. The process is as follows:

#CGI: It is a protocol for data exchange between Nginx and PHP_FPM.

FastCGI: Same as CGI, it is a communication protocol, but it has some optimizations in efficiency than CGI.

PHP-CGI: It is PHP’s interface program for the CGI protocol provided by Nginx.

PHP-FPM: It is PHP's interface program for the FastCGI protocol provided by Nginx. It also provides relatively intelligent task management.

Multi-person development environment

We can see from the PHP principle that PHP is actually just an interpreted script Language, each request must be parsed from index.php, then can we name many folders on the server according to the names of different developers, and in each folder, clone the code repository and switch to its own branch? . Then let Nginx handle the index in everyone's directory. For example, directly access http://wulv.test.com/, obtain wulv in Nginx, and set root to the wulv directory, so that you can access the code in the wulv directory. You can set Nginx like this:


set $who www;
if ($http_who != "") {
  set $who $http_who;
}
root /data/gateway/$who/html;


We can let the URL carry the user's directory and intercept it in Nginx. Places to carry:

host: http://wulv.test.com

path: http://www.test.com/wulv

query: http://www.test.com?http_who=wulv

This can generally achieve the requirements, but there are still some problems. For example, some links on the page are hard-coded, and there are no If you use a relative path, you will go to www.test.com as soon as you click it, or some third-party applications such as OAuth need to verify the domain name, and you will not be able to log in if it is inconsistent with the online domain name. So other ways are needed to achieve it, such as:

http request header

cookie

We can use Modify Headers to browse Server plug-in, modify the http request header information, set a parameter http_who to wulv, and then obtain it in Nginx.

Expansion

If conditions permit, you can actually make a gateway server and create a configuration page in the configuration page Configure the directory you need to access. Next time you visit, the gateway will directly help you set the http header and proxy it to the corresponding server. In this way, you don’t even need to install browser plug-ins, which is more friendly to operations and product design.

The above is all the content of this article, I hope it can be helpful to everyone! !

Related recommendations:

Detailed explanation of the principles of PHP multi-player development environment

Method for implementing Session in Redis in PHP distributed system

The perfect solution to PHP’s inability to upload large files

The above is the detailed content of Analysis of PHP multiplayer module development principles_php skills. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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