In this day and age, the Internet is popular, driven by the Internet in all walks of life to flourish, it is also due to the timely seize the development opportunity brought by the Internet, made a large number of technology giants, they use the Internet technology has introduced a large number of technology products, the products of science and technology to bring people convenient, fast life at the same time, And slowly changing the world.
Under the background of the great prosperity of science and technology, there are countless technologies to provide support, and there are many scientific and technological talents to constantly develop and improve technology; In the IT circle, there are a variety of practitioners, they are using their own strength in the continuous advancement of various technologies, so that IT continues to improve, but also because of this, they are active in various technical communities, forums continue to discuss the pit, debating which language is the best development language.
Hence the meme, “If you want to start a big debate, go to the Java community and talk about PHP being the best language in the world”. This meme was triggered by none other than the official PHP language document from 2001:
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.
Today we are going to share something about PHP, the best language in the world. It is about ThinkPHP. Thinkphp is a word that many developers are familiar with, especially developers in the PHP language field. This is a PHP integrated development environment developed by Chinese people, currently has a great influence in the PHP language field in China, I also have a lot of contact with it, after a period of use, I feel good. If you are interested, you can go to the official website to download and try it.
There are many good examples of integrated development environments like Laravel, Yii, CodeIgniter, etc., that are worth a try.
The origin of things
So why am I, as a Java programmer, sharing PHP content? The reason is simple: the company needs it, and that’s enough. The project is not very large, the function is relatively simple, so the early development, testing are relatively smooth; However, when I finished the development and deployment of the project, I realized the problem. Because the development and deployment are using a mature PHP integration environment, I do not know the whole running process of PHP, and I always feel floating in the air. So I decided to manually deploy a ThinkPHP runtime environment without the integration environment.
preparation
Web Server and Web Application
Web development, will often refer to the web server, Web appliction these two words, many times are easy to cause ambiguity because of improper definition, here is a brief introduction:
Web Server
: 即web
Server, usually refers toApache, Nginx, IIS, Lighttpd, Tomcat, Wildfly
Such as software;Web Appliction
: 即web
An application program, usually a project program developed in a programming language;
After a Web application is developed, it needs to be compiled and then sent to a Web server to run.
Selection of technical solutions
Before you can start deploying your environment, you need to do some preparatory work. There are several mature and stable options available for PHP environments. Here are two of the more mature options:
PHP + Apache
To:ISAPI
Way to runPHP
The project,PHP
Version selected asTS
(thread-safe version); This way of configuration is more responsible, the configuration process is easy to make mistakes due to artificial configuration, but the stability of this scheme is higher;PHP + Nginx
To:FAST-CGI/PHP-FPM
Way to runPHP
The project,PHP
Version selected asNTS
(thread-unsafe version); This approach has fewer configurations and performs well with high concurrency and high load, but is less stableApache
;
Finalize the details of the plan
PHP + Nginx PHP + Nginx PHP + Nginx PHP + Nginx
- A working computer,
windows
Operating system; Nginx + 1.13
; providephp
Project operation environment support;thinkphp 5+
;php
The project;PHP - NTS - 5.6
;php
Language support,php
As an interpreted language, it is necessary to install an official language interpreter to explain the executionphp
Code; The choicents
Version, because of the choiceNginx
As a server;- For serious programmers, this is especially important;
Then download and decompress the software in 2, 3 and 4 and wait for use.
PHP Installation and Configuration
The selected version is PHP 5.6. When you download PHP, you will find different versions to choose from, as shown in the following figure:
PHP is available in NTS and TS versions, which are thread-safe versions and thread-safe versions respectively. The two versions are quite different:
Ts edition
: that is, the thread safety version, its multi-threaded access, the use of locking mechanism, can ensure that the same data or operation at the same point in time can only be accessed by a thread operation, so as to avoid multiple threads at the same time to operate a data caused by dirty data problems; But its performance is better thannts
Version of some poor, suitable for high data security requirements of the scene.nts
version: that is, the thread is not safe version, in multi-threaded access does not provide data access protection, there may be multiple threads successively or at the same time operation of the same data, which may cause data disorder; butnts
Version has better performance and is suitable for scenarios where data security is not a priority and better performance is required.
For this article, select the NTS version, click Zip to download the PHP package, and unzip it to get the following directory:
PHP: PHP execution tool. You can run PHP files by running php.exe xxx. PHP from CMD.
Php-cgi. exe: PHP interpreter, an execution tool for CGI, often used in Web projects.
Php-win. exe: you can also execute PHP files. The difference is that the console is opened, but the output is not displayed. It can be used to write file processing, network services and other programs without display interface.
Phpdbg.exe: PHP development debugging tool.
PHP. Ini: PHP configuration file, including PHP extension installation, session path, file size, and many other configurations. If you need to modify THE PHP configuration, open the file to modify it.
Run the PHP – cgi
Next, get php-CGI up and running for future use:
- in
php
Open it in the root directorycmd
Command; - Enter the command:
PHP -cgi.exe -b 127.0.0.1:9000 -c "php.ini"
; php-cgi
with127.0.0.1:9000
Up and running;
Php-cgi running problem
If the following error occurs when you run the php-cgi command, it indicates that the Visual C++ Redistributable Package is missing and you need to download and install it.
Problem solving
1. Download Visual C++ Redistributable Package.
2. Choose64
Bit installation package;
3. Click Install after downloading.
4. Go backcmd
Running in thephp-cgi
, the problem has been solved;
FastCGI
In the overall architecture of a site, Web Server software (such as Nginx, Apache, HTTPD, etc.) acts as a content distributor and responds to client requests. For static resource requests, they go to the set path and return, or if they don’t find it, an error message is returned. For dynamic requests, they pass the data to the corresponding program for processing, and then return the results of the program’s processing.
CGI
CGI is a protocol for data exchange between Web Server and Web Application. Its full name is Common Gateway Interface (CGI), which can realize data interaction between browser and Server. CGI is not a programming language, nor does it belong to any language, but most languages provide support for CGI.
CGI is a communication protocol that converts user-submitted data into a K-V dictionary. CGI itself is not responsible for data transfer, data transfer is done by socket. Each time a user sends a CGI request, the Web Server software will generate a new CGI interpreter process (such as php-cgi.exe). When the CGI script processes the request, the CGI interpreter process will be released by the Web Server. This process is known as fork-and-execute.
In high-concurrency scenarios, the fork-and-execute mode consumes a lot of server resources because of repeated creation and destruction of CGI interpretation processes, resulting in low server performance. FastCGI was created to solve this problem.
FastCGI
FastCGI is also a protocol for data exchange between Web Server and Web Application. It is an extended version of CGI, which is much more efficient in execution than CGI. FastCGI is like a long-live CGI that can be executed as long as it is activated and doesn’t have to fork every time.
FastCGI is also a language-independent, scalable CGI open extension. The main reason for CGI’s poor performance is the repeated loading of the CGI interpreter. The main behavior of FastCGI is to keep the CGI interpreter process in memory and schedule it by the FastCGI process manager, thus improving performance.
PHP-CGI
Php-cgi is the official PHP implementation of the FastCGI manager, but it is only a CGI program, it itself can only parse requests, return results, does not manage the process.
PHP-FPM
Php-fpm is the PHP language’s implementation of the FastCGI protocol. It is responsible for managing a pool of processes to handle requests from the Web server. It is also a hypervisor for scheduling and managing the PHP parser php-CGI. After PHP5.3, phP-FPM is built into the PHP kernel and does not require a separate download.
The php-cgi process cannot be smoothly restarted for any changes to the php.ini file. But with php-fPM, changes to php.ini can be smoothed over. However, phP-FPM is not supported on Windows. If you want to try it, you can choose Linux.
Nginx configuration
Nginx is a lightweight Web server, reverse proxy server, email (IMAP/POP3/SMTP) proxy server, running less memory, concurrent ability; It was developed by Russian programmer Igor Sessoyev and has been open source since 2004.
catalogue
Module is introduced
Conf is the nginx service configuration file. In the configuration file, you can do many flexible and powerful configurations for Nginx, such as network basic configuration, virtual host binding, reverse proxy, load balancing, etc. The following are some main modules:
1. The global block
The global block is a part of the configuration file from the start to the Events block. It mainly sets some configuration instructions that affect the overall operation of the Nginx server. Therefore, the scope of these instructions is the whole nginx server. These configuration instructions include: user (group), worker process number, PID path, log path, etc.
2. The events
The setting instructions involved in the Events block include whether to enable serialization of network connections under multiple worker processes, whether to allow simultaneous acceptance of multiple network connections, which event-driven model to process connection requests, and the maximum number of connections that each worker process can support at the same time.
3. HTTP block
HTTP blocks are particularly important in nginx configuration. Most of the functions such as proxy, cache, log definition, file type, network infrastructure configuration and third-party module configuration can be placed in this module. Details include file import, miME-type definition, log customization, whether to use SendFile to transfer files, connection timeout duration, and upper limit of the number of single connection requests.
4. Server block
Each server block is a bound virtual host, a set of services (or websites). There can be multiple servers in one HTTP. Each Server block can contain multiple Location blocks.
The domain name and port of each bound site are configured in the server block, as well as all the data needed for routing configuration, access device judgment and so on.
5. The location of
Used to configure the routing of requests and the processing of various pages. Each server block can contain multiple location blocks. Location is an instruction for the server block, based on the request string received by the Nginx server (for example: Server_name or URL_string), matching a string other than the virtual host name (or IP) such as URL_stirng to handle a specific request. Address direction, data caching, and response control are all implemented in this section.
The configuration here is application-specific.
Run the command
Open the nginx root directory in CMD and run the command to nginx.
- To view
Nginx
Version number ofnginx -V
- Configuration check:
nginx -t
- Start the
nginx
:start nginx
- Configuration file modification reload command:
nginx -s reload
- Stop or close quickly
Nginx
:nginx -s stop
- Stop or close normally
Nginx
:nginx -s quit
PHP Runtime Configuration
Nginx requires the support of FastCGI to run PHP projects, so it needs to introduce fastCGI configuration in nginx. Since your project is using ThinkPHP, you need to locate it in the public directory of your project. So, the complete configuration is as follows:
1. Inhttp
In the module,server
Add the following configuration to the block:
2.server
The complete block configuration is as follows:
3. After nginx is configured, you can access the project using the configured domain name and port. The configuration is complete.
summary
Take the initiative, language does not choose people, people choose language. Keep learning is king. If you need the complete configuration, you can send me a private message.
The end, although the old man is not serious, but the old man a talent! Follow me for more knowledge of programming technology.