Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and configure PHP-FPM and Nginx on the server

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to install and configure PHP-FPM and Nginx on the server". In the daily operation, I believe that many people have doubts about how to install and configure PHP-FPM and Nginx on the server. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to install and configure PHP-FPM and Nginx on the server". Next, please follow the editor to study!

PHP-FPM

PHP-FPM (PHP FastCGI Process Manager) is the software used to manage the PHP process pool and to receive and process requests from Web servers such as Nginx. PHP-FPM creates a master process (usually run as root in the operating system) that controls when and how HTTP requests are forwarded to one or more child processes for processing. The PHP-FPM main process also controls when PHP child processes are created and destroyed. Each process in the PHP-FPM process pool exists longer than a single HTTP request and can handle 10, 50, 100, or more HTTP requests.

Install # Ubuntusudo apt-get install python-software-propertiessudo add-apt-repository ppa:ondrej/php-5.6sudo apt-get updatesudo apt-get install php5-fpm php5-cli php5-curl\ php5-gd php5-json php5-mcrypt php5-mysqlnd global configuration

In Ubuntu, the main configuration file for PHP-FPM is / etc/php5/fpm/php-fpm.conf.

The following is the most important global configuration of PHP-FPM. It is recommended to change the default value to the following value:

Emergency_restart_threshold = 10

Within a specified period of time, if the number of failed PHP-FPM child processes exceeds this value, the PHP-FPM main process will restart gracefully.

Emergency_restart_interval = 1m

Set the time span adopted by emergency_restart_threshold

These two configurations are the basic security of the PHP-FPM process and can solve simple problems, but not major problems caused by poor PHP code.

Configure process pool

The other configuration content of the PHP-FPM configuration file is an area called Pool Definitions, where the configuration is used to set up each PHP-FPM process pool, which is a series of related PHP child processes. Typically, a PHP application has its own pool of PHP-FPM processes.

In Ubuntu, the Pool Definitions area has only the following line:

Include=/etc/php5/fpm/pool.d/*.conf

The purpose of this line of code is to have PHP-FPM load each process pool definition file in the / etc/php5/fpm/pool.d directory. Enter this directory and you should see a file named www.conf, which is the default PHP-FPM process pool configuration file named www.

Each PHP-FPM process pool runs as a specified operating system user and user group. We will configure the default wwwPHP-FPM process pool to run as the deploy user and user group:

User = deploy

Group = deploy

Listen = the IP address and port number that the 127.0.0.1:9000:PHP-FPM process pool listens to, so that PHP-FPM only accepts requests from Nginx from here, and 127.0.0.1 IP 9000 lets the specified PHP-FPM process pool listen for connections entered by local port 9000.

Listen.allowed_clients = 127.0.0.1: you can send one or more IP addresses of requests to this PHP-FPM process pool. For security, I set this to 127.0.0.1, that is, only the current device can forward requests to this PHP-FPM process pool.

Pm.max_children = 15: this setting sets the maximum number of processes in the PHP-FPM process pool at any point in time. There is no absolutely correct value for this setting. You should test your PHP application to determine how much memory each PHP process needs, and then set this setting to the total number of PHP processes that can be held in available memory.

Pm.start_servers = the number of processes immediately available to the PHP-FPM process pool when 3:PHP-FPM starts.

Pm.min_spare_servers = the minimum number of processes that can exist in the PHP-FPM process pool when the 2:PHP application is idle. The value of this setting is generally the same as pm.start_servers.

Pm.max_spare_servers = the maximum number of processes that can exist in the PHP-FPM process pool when the 4:PHP application is idle.

Pm.max_requests = 1000: the maximum number of HTTP requests that each process in the PHP-FPM process pool can handle before the process is recycled. This setting helps avoid memory leaks caused by poorly written PHP extensions or libraries.

Slowlog = / path/to/slowlog.log: the value of this setting is the absolute path of a log file in the file system. This log file is used to record HTTP requests that take more than n seconds to process in order to identify bottlenecks in PHP applications and then debug them. It is important to note that the users and user groups to which the PHP-FPM process pool belongs should have write access to this log file.

Request_slowlog_timeout = 5s: if the processing time of the current HTTP request exceeds the specified value, the requested backtracking information is written to the log file specified by the slowlog setting.

Save the file after editing, and then restart the main PHP-FPM process:

Sudo service php5-fpm restartNginx

Nginx is a Web server, similar to Apache, but easier to configure and takes up less memory at run time. We won't delve into Nginx here, just show you how to install it and forward the corresponding request to the PHP-FPM process pool.

Install # Ubuntusudo add-apt-repository ppa:nginx/stablesudo apt-get updatesudo apt-get install nginx# CentOSsudo yum install nginxsudo systemctl enable nginx.servicesudo systemctl start nginx.service virtual host

Next, we need to configure a virtual host for the PHP application. A virtual host is a series of settings that tell the domain name of the Nginx PHP application, its location in the file system, and how HTTP requests have been forwarded to the PHP-FPM process pool:

Server {listen 80; server_name example.com; index index.php client_max_body_size 50M; error_log / home/deploy/apps/logs/example.error.log; access_log / home/deploy/apps/logs/example.access.log; root / home/deploy/apps/example.com/current/public; location / {try_files $uri $uri/ / index.php$is_args$args } location ~\ .php {try_files $uri = 404; fastcgi_split_path_info ^ (. +\ .php) (/. +) $; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_index index.php; fastcgi_pass 127.0.0.1 try_files 9000;}}

The following is a brief description of each virtual host setting:

Listen: sets which port Nginx listens for incoming HTTP requests. Generally, HTTP requests enter from port 80, and HTTPS requests enter from port 443.

Server_name: the domain name used to identify the virtual host, which should be set to the domain name used by your application, and point to the IP address of the server. If the value of the Host header in the HTTP request header matches the value of server_name in the virtual host, Nginx sends the HTTP request to the virtual host.

The default file when index:HTTP requests that URI does not specify a file.

Client_max_body_size: for this virtual host, Nginx accepts the maximum length of the HTTP request body. If the request body length exceeds this value, Nginx returns a 4XX response.

Error_log: the path of the virtual host error log file in the file system.

Access_log: this virtual host accesses the path of the log file in the file system.

Root: apply the root path.

In addition to the above settings, there are two location blocks that tell Nginx how to handle HTTP requests that match the specified URL pattern:

Location / {} uses the try_files instruction to find files that match the requested URI. If not, look for the directory that matches the requested URI. If no directory is found, the URI requested by HTTP is rewritten as / index.php. If there is a query string, the query string is appended to the end of the URI. The rewritten URL, and all URI ending in .php, are managed by the location ~\ .php {} block.

The location ~\ .php {} block forwards the HTTP request to the PHP-FPM process pool for processing. In this block, we forward the PHP request to port 9000 for PHP-FPM to process the request.

In Ubuntu, we must execute the following command to create a symbolic link to the virtual host profile in the / etc/nginx/sites-enable directory:

Sudo ln-s / etc/nginx/sites-available/example.conf / etc/nginx/sites-enabled/example.conf

Finally, restart Nginx by executing the following command:

# Ubuntusudo service nginx restart# CentOSsudo systemctl restart nginx.service

Now the server can run the PHP application. There are many kinds of Nginx configurations. This is only the most basic configuration. For more configuration information, please refer to the following resources:

Http://nginx.orghttp://www.nginx.cnhttps://github.com/h6bp/server-configs-nginx automatic configuration server

Configuring servers is a long and tedious process, which is even stronger if you configure many servers manually. Fortunately, there are some tools that can help us configure servers automatically. Here are several popular server configuration tools:

Puppet: https://puppet.com/Chef: https://www.chef.io/Ansible: https://www.ansible.com/SaltStack: https://www.ansible.com/

There are differences between the tools, but the goal is the same-- to automatically configure the new server according to precise rules. If you want to manage multiple servers, I strongly recommend that you study the use of configuration tools, which can save a lot of time.

At this point, the study on "how to install and configure PHP-FPM and Nginx on the server" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

  • Spring cxf configuration

    Version-- cxf-2.5.2

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report