“PHP” does not depend on the integrated environment, and uses nginx to manually configure the ThinkPHP running environment

Time:2021-12-30

In today’s era, the Internet is popular. All walks of life driven by the Internet are booming. It is precisely because of the timely grasp of the development opportunities brought by the Internet that a large number of science and technology giants have achieved. They have launched a large number of science and technology products by using Internet technology. While these science and technology products bring convenient and fast life to people, Also slowly changing the world.

Under the background of the great prosperity of science and technology, countless technologies are providing support, and many scientific and technological talents are constantly developing and improving technology; In the IT circle, there are all kinds of practitioners who are constantly promoting and improving various technologies with their own strength. Because of this, they are active in various technology communities and forums, constantly discussing the pits they encounter and debating which language is the best development language.

So there is such a stem“If you want to start a big argument, goJavaCommunity discussionPHPIs the best language in the world”。 It was no one else who caused the stemphplanguage2001Official documents for:

PHP is the best language for web programming, but what about other languages?
Because PHP is the best language ever, ever. It’s fast, very powerful, and free.

So it has absolutely nothing to do with me, seriously.

What you want to share today is also the same as“The best language in the worldPHP”About: AboutthinkphpYes, mentionedthinkphpThis word is familiar to many small partners engaged in development, especiallyphpDevelopers in the language field;
This is a product developed by Chinese peoplephpIntegrated development environment, currently in ChinaphpThe language field has a great influence. I also have a lot of contacts. After using it for a period of time, I feel pretty good. If you are interested, you can go to the official website to download to try.

There are many excellent cases of integrated development environments like this, such as:Laravel 、Yii、CodeIgniterWait, it’s all worth trying.

The origin of things

So, as aJavaProgrammers, why sharePHPWhat about the content? The reason is actually very simple. The company needs it. This reason is enough. The project is not very big and the function is relatively simple, so the early development and testing are relatively smooth; But when I finished the project development and deployment, I realized the problem, because the development and deployment are maturephpIntegrated environment, so IPHPI don’t understand the whole operation process of, and I always have a feeling of floating in the air; So I decided to put aside the integrated environment and deploy one manuallythinkphpOperating environment.

preparation in advance

Web server and web application

webDuring development, it is often mentionedweb serverweb applictionThese two words are often ambiguous due to improper definition. Here is a brief introduction:

  • Web Server NamelywebServer, usually referred to asApache、Nginx、IIS、Lighttpd、Tomcat、WildflyOther software;
  • Web ApplictionNamelywebApplication program, usually refers to the project program developed in programming language;

Project(web application)After development, it needs to be compiled and handed over towebThe server is running.

Selection of technical scheme

Before the formal deployment of the environment, we need to make some preparations. Just before the so-called east wind, everything must be ready first. usuallyphpThere are a variety of mature and stable schemes to choose from for the operation environment of the, and the following are the more mature ones:

  • PHP + Apache: toISAPIMode operationPHPprojectPHPVersion selection isTS(i.e. thread safe version); this configuration method is more responsible, and the configuration process is easy to make mistakes due to human configuration, but this scheme is more stable;
  • PHP + Nginx: toFAST-CGI/PHP-FPMMode operationPHPprojectPHPVersion selection isNTS(i.e. thread unsafe version); this method has less configuration, and has good performance for high concurrency and high load, but its stability is not as good as thatApache

Finalization of scheme details

We choose the latter one here, namely:PHP + Nginx, sophpYou have to choose the version ofntsVersion, the following software is required:

  1. A computer that works normally,windowsOperating system;
  2. nginx 1.13+; providephpProject operation environment support;
  3. thinkphp 5+phpproject
  4. php-nts-5.6phpLanguage support,phpAs an interpretative language, it is necessary to install an official language interpreter before it can be interpreted and executedphpcode; Why choosentsVersion, because I choseNginxAs a server;
  5. A serious programmer, this is particularly important;

Then2、3、4Download and decompress the software in and wait for use.

PHP installation configuration

The version we choose here isphp 5.6, downloadingphpYou will find that there are different versions to choose from, as shown in the following figure:

PHPProvidedntsEdition andtsThe two versions are the thread safe version and the thread safe version. The two versions are very different:

  • TS version: thread safe version, which uses a locking mechanism when accessing multiple threads, can ensure that the same data or operation can only be accessed and operated by one thread at the same time point, so as to avoid the problem of dirty data caused by multiple threads operating one data at the same time; But its performance is better thanntsThe version is poor, which is applicable to scenes with high requirements for data security.
  • ntsedition: that is, the thread unsafe version does not provide data access protection during multi-threaded access, and multiple threads may operate the same data successively or at the same time, which may cause data disorder; butntsThe version has better performance, which is suitable for scenarios that do not pay much attention to data security and need better performance.

Article selectionntsedition, clickZipdownloadPHPPackage, and then unzip it to get the following directory:

php.exePHPThe execution tool can beCMDCommand execution commandphp.exe xxx.phpTo runPHPFile.

php-cgi.exe: PHP interpreter forCGIImplementation tools provided inwebProjects are always used.

php-win.exe: can also be executedPHPFile, the difference is that the console opens, but the output is not displayed. It can be used to write programs such as file processing and network services without display interface.

phpdbg.exephpDeveloped debugging tools.

php.iniphpConfiguration files, includingphpExtended installationsessionPath, file size and many other configurations, which need to be modifiedphpOpen the file and modify it.

Run PHP CGI

The next step is tophp-cgiRun for later use:

  1. stayphpOpen under root directorycmdCommand;
  2. Enter command:php-cgi.exe -b 127.0.0.1:9000 -c "php.ini"
  3. php-cgiSo that127.0.0.1:9000Running;

PHP CGI running problem

If runningphp-cgiIf the following problems occur during the command, the system is missingVisual C++ Redistributable Package, need to download and install;

Problem solving

1.downloadVisual C++ Redistributable Package
(download link:Download Visual C + + redistributable package);

2. Selection64Bit installation package;

3. After downloading, click Install;

4. Return tocmdMedium operationphp-cgi, the problem has been solved;

FastCGI

In the overall structure of the website,Web ServerSoftware (e.gnginx、apache、httpdIt executes the distributor of the content and responds to the request of the client. aboutStatic resource request, they will search under the set path, and then return. If they are not found, they will return an error message; aboutDynamic request, they will pass the data to the corresponding program for processing, and then return the processing result of the program.

CGI

CGIyesWeb ServerAndWeb ApplicationA protocol for data exchange between. Its full name is the common gateway interface(Common Gateway Interface,CGI), it can realize the data interaction between browser and server.CGIIt is not a programming language, nor does it belong to any language, but most languages provideCGISupport.

CGIIs a communication protocol that converts data submitted by users into ak-vMy dictionary.CGIIt is not responsible for data transmission itself. Data transmission depends onsocketCompleted. Every time the user sendsCGIWhen asked,Web ServerThe software will generate a newCGIInterpreter process (e.gphp-cgi.exe), whenCGIAfter the script processes the request,CGIThe interpreter process will beWeb ServerRelease; This process is calledfork-and-executepattern.

fork-and-executepatternIn high concurrency scenarios, it will be created and destroyed repeatedlyCGIInterpreting the process consumes a lot of server resources, which makes the running performance of the server low. To solve this problem,FastCGIemerge as the times require.

FastCGI

FastCGIAlsoWeb ServerAndWeb ApplicationA protocol for data exchange betweenCGIThe extended version of is more efficient thanCGIMuch higher.FastCGIIt’s like a resident(long-live)TypeCGI, it can be executed all the time. As long as it is activated, it won’t take time to execute every timeforkonce.

FastCGIIt is also language independent and scalableCGIOpen expansion,CGIThe main reason for low performance isCGIRepeated loading of the interpreter, andFastCGIThe main behavior is toCGIThe interpreter process is held in memory and controlled by theFastCGIThe process manager schedules, so as to improve the running performance.

PHP-CGI

PHP-CGIyesPHPOfficially implementedFastCGIManager, but he’s just aCGIThe program itself can only parse the request, return the result, and will not manage the process.

PHP-FPM

PHP-FPMYesPHPLanguage forFastCGIFor the specific implementation of the protocol, he is responsible for managing a process pool to process data fromWebServer requests, not only that,PHP-FPMIt is also used for dispatching managementPHPParserphp-cgiManagement procedures.PHP5.3After the version,PHP-FPMIs built intoPHPThe kernel does not need to be downloaded separately.

aboutphp.iniModification of documents,php-cgiThe process cannot be restarted smoothly. Every time it is modifiedphp.iniAll need to be restartedphp-cgiProcess; But yesphp-fpmAfter that, forphp.iniA smooth restart can be achieved by modifying. butphp-fpmstaywindowsIf you want to experience it, you can chooseLinuxenvironment

Nginx configuration

NginxIt’s a lightweightWebServer, reverse proxy server, e-mail(IMAP/POP3/SMTP)The proxy server occupies less memory and has strong concurrency; It was developed by Igor sesoyev, a Russian programmer2004Open source in.

Directory introduction

Module introduction

fileconf/nginx.confyesnginxService configuration file, in which you cannginxDo many flexible and powerful configurations such as network basic configuration, virtual host binding, reverse proxy and load balancing. The following are some main modules:

1. Global block

The global block is the configuration file from start to finisheventsPart of the content between blocks, mainly setting some effectsnginxThe configuration instructions run by the server as a whole, so the scope of these instructions is the wholenginxServer global. These configuration instructions include: user (Group)worker processNumberpidPath, log path, etc.

2. Events block

eventsThe setting instructions involved in the block include whether to turn on to manyworker processNetwork connection underserialize, whether simultaneous reception is allowedMultiple network connections, which oneEvent driven modelProcessing connection requests, eachworker processCan be supported at the same timeMaximum number of connections.

3. HTTP block

 httpBlock innginxConfiguration is particularly important. Most functions such as agent, cache, log definition, file type, network basic configuration and the configuration of third-party modules can be placed in this module. Including document introductionmime-typeDefinition, log customization, whether to usesendfileTransfer files, connection timeout, upper limit of single connection requests, etc.

4. Server block

EachserverA block is a bound virtual host, which is a group of services (or websites). OnehttpThere can be more than oneserver。 eachserverA block can contain multiplelocationBlock.

The domain name and port of each bound website are configured inserverThe other in the block also includes all the data required for routing configuration, access device judgment, etc.

5. Location block

It is used to configure the routing of requests and the processing of various pages. eachserverA block can contain multiplelocationBlock,locationyesserverBlock, based onnginxThe request string received by the server (for example:server_nameorurl_string), except for the virtual host name (orip)Strings other thanurl_stirngMatch and process specific requests. Address orientation, data caching, and response control are all implemented in this part.

The configuration here is mostly application related.

Run command

staycmdOpen innginxRoot directory, you cannginxExecute the operation command;

  • seeNginxVersion number of:nginx -V
  • Configuration check:nginx -t
  • start-upnginxstart nginx
  • Configuration file modification and reload command:nginx -s reload
  • Quick stop or closeNginxnginx -s stop
  • Normal stop or shutdownNginxnginx -s quit

PHP run configuration

nginxfunctionphpProject needsfastcgiSupport is therefore needed innginxIntroduced infastcgiConfiguration of; Because the project is usedthinkphpTherefore, it needs to be located in the projectpublicDirectory; Therefore, the complete configuration is as follows:

1. InhttpIn the module,serverAdd the following configuration before the block:

2.serverThe complete configuration of the block is as follows:

3.nginxAfter configuration, you can access the project through the configured domain name and port; This completes the configuration.

Summary

It is not language that chooses people, but people choose language. Learning is the king. If there is a small partner who needs the above complete configuration, you can get it by private mail.

After all, I’m not serious, but I’m talented! Follow me and get more programming technology knowledge.