Home  >  Article  >  Backend Development  >  How to deploy php project under nginx

How to deploy php project under nginx

藏色散人
藏色散人Original
2021-09-24 09:38:2118719browse

How to deploy PHP projects under nginx: 1. Install nginx and php-fpm; 2. Find the configuration file of the nginx server; 3. Specify the deployment location of the PHP project; 4. Put the configured various The server conf can be placed in sites-enabled.

How to deploy php project under nginx

The operating environment of this article: linux5.9.8 system, nginx version 1.9, Dell G3 computer.

How to deploy php project under nginx?

Deploying php projects on nginx server

nginx itself cannot process PHP pages, it is just a web server. When a request is received, if it is a PHP request , forwarded to the PHP interpreter for processing through a reverse proxy, and the results are returned to the client. Therefore nginx and php-fpm or other php interpreter need to be installed on the server.

After installing nginx and php-fpm, find the configuration file of the nginx server

[root@test24266conf]# ps -ef | grep nginx.conf
root     31441    1  0  2018 ?       00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c/usr/local/nginx/conf/nginx.conf

In this file, specify the deployment location of the php project, that is, modify the file root specified by root under the server field Table of contents.

In many distribution versions, there is no server field in nginx.conf. This is for the convenience of management. In the precompiled version of some distributions, there will be two include conf.d sentences at the end of the http section in nginx.conf. /*.conf; include sites-enabled/*or only one include

sites-enabled/*.conf; In this way, you can put various configured server confs in sites-enabled, such as

[root@test24266conf]# ll sites-enabled/
-rw-r--r--1 root root 603 103 2017 captcha443.conf
-rw-r--r--1 root root 287 9 1 2017 commrisk.conf
-rw-r--r--1 root root 194 129 2016 imagerotate.conf
-rw-r--r--1 root root 402 9 2 2016 msgqapi.conf
-rw-r--r--1 root root 295 102 2017 pointriskapi.conf
-rw-r--r--1 root root 290 6 2 2017 risktrade.conf
-rw-r--r--1 root root 309 814 2017 rotateapi.conf
-rw-r--r--1 root root 313 100 2016 watchdog.conf
[root@test24266conf]#
       这样每个.conf文件就可以对应一个虚拟主机,查看某个配置文件,如
[root@test24266conf]# cat sites-enabled/pointriskapi.conf
server{
    listen     8013;
    server_name     point.risk.api;
    index index.php;               #默认访问的文件
    root       /var/www/pointriskapi/hosts;
    access_log on;
#当请求网站下php文件的时候,反向代理到php-fpm
    location ~ .*\.php?$ {
       include fastcgi.conf;
    }
 
   location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
 
}
[root@test24266conf]#

We put the php project file in this /var/www/pointriskapi/hosts path. As

[root@test24266hosts]# ll
 
 -rw-r--r-- 1 apache apache 339 102 2017risk_point.php
[root@test24266hosts]# pwd
/var/www/pointriskapi/hosts

generally does not need to restart Nginx and php-fpm. Now you can access http://ip:8013/risk_point.php on the client.

So how does nginx forward the request to the PHP interpreter through reverse proxy? We noticed that there is a section

    location ~ .*\.php?$ {
       include fastcgi.conf;          #加载nginx的fastcgi模块
    }

in the server field. This section specifies who will process the .php file. We check the factcgi.conf file in the same directory as nginx.conf, such as

[root@test24266 conf]# cat fastcgi.conf
fastcgi_pass  127.0.0.1:9000;
#fastcgi_pass  unix:/tmp/phpcgi.socket;
fastcgi_indexindex.php;
 
access_log  /var/log/httpd/access_log main;
 
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
 
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;
 
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
 
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
 
# PHPonly, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
[root@test24266conf]#

Fastcgi_pass specifies the IP address and port that the fastcgi process listens on, that is, nginx will forward the request to this socket. Therefore we need to specify the same socket in the php-fpm configuration file. Start Php-fpm monitoring and check

[root@test24266~]# netstat -anp | grep 9000
tcp        0     0 127.0.0.1:9000             0.0.0.0:*                  LISTEN      3719/php-fpm

To summarize, in nginx and php-fpm mode, the complete request and response process is as follows:

1. The client requests a certain request on the server .php file

2. Nginx finds that dynamic resources need to be routed to the specified root directory

3. Load nginx’s fast-cgi module

4. Fact-cgi monitoring 127.0.0.1:9000 (default socket)

5. php-fpm receives the request and enables the worker process to process the request

6. After php-fpm processes the request, it returns to nginx

7. nginx returns the results to the browser through http

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to deploy php project under nginx. 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