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

Detailed steps for Nginx+php-fpm environment configuration

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Detailed steps for Nginx+php-fpm environment configuration

Download the most complete version of Nginx from: http://nginx.org/download/

First, compile and install php-fpm

What is PHP-FPM?

PHP-FPM is a PHP FastCGI manager that is available only for PHP and can be downloaded from http://php-fpm.org/download.

PHP-FPM is actually a patch to the PHP source code designed to integrate FastCGI process management into the PHP package. You must patch it into your PHP source code before you can use it after compiling and installing PHP.

The new version of PHP has integrated php-fpm, is no longer a third-party package, and is recommended. PHP-FPM provides a better way of PHP process management, can effectively control memory and processes, can smoothly reload PHP configuration, and has more advantages than spawn-fcgi, so it is officially included by PHP. You can enable PHP-FPM with-enable-fpm parameter in. / configure. Other parameters are configured with php. For more information, please see here.

Pre-installation preparation

Execute under centos

Yum-y install gcc gcc-c++ glibc libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel

Installation of the new version of php-fpm (recommended)

Wget http://cn2.php.net/distributions/php-5.4.7.tar.gztar zvxf php-5.4.7.tar.gzcd php-5.4.7./configure-- prefix=/usr/local/php-- enable-fpm-- with-mcrypt-- enable-mbstring-- disable-pdo-- with-curl-- disable-debug-- disable-rpath-- enable-inline-optimization-- with-bz2-- with-zlib-- enable-sockets-- enable-sysvsem -enable-sysvshm-- enable-pcntl-- enable-mbregex-- with-mhash-- enable-zip-- with-pcre-regex-- with-mysql-- with-mysqli-- with-gd-- with-jpeg-dir make all install

The default installation location of php has been specified as / usr/local/php above, and then configure the corresponding files:

Cp php.ini-development / usr/local/php/lib/php.inicp / usr/local/php/etc/php-fpm.conf.default / usr/local/php/etc/php-fpm.confcp sapi/fpm/php-fpm / usr/local/bin

Then set php.ini and use: vim / usr/local/php/lib/php.ini to open the php configuration file to find the cgi.fix_pathinfo configuration item, which is commented by default and has a value of 1. According to the official document, in order to prevent Nginx from sending requests to the back-end PHP-FPM module when the file does not exist, thus avoiding malicious script injection, this item should be uncommented and set to 0

Save and exit after setting up

Another place to note is that the location of the php.ini configuration file can be set in the pre-compilation configuration parameters, and the compilation parameters can be written as:-- with-config-file-path=/usr/local/php, then php will go back and read the php.ini configuration file in the specified directory. If you do not add this parameter, the default location is the lib directory under the php installation directory, and you can also view it in the phpinfo () output interface. If the php.ini is placed in another location and cannot be read by php, then all the modified configurations will not take effect. This should be noted.

At this point, you should first create a web user:

Groupadd www-datauseradd-g www-data www-data

Then some tutorials on the Internet say that you can modify php-fpm.conf to add the users and groups created above. At this time, you can't find the official location after opening the file using vim / usr/local/etc/php-fpm.conf:

If you add it at any location at this time, the error that cannot be found in the directory will be reported when you start php-fpm, so do not add users and groups to php-fpm.conf. At this time, you will find the following if you go to the last line of php-fpm.conf (if you add-- prefix option at compile time, the following location will be automatically completed. The default is that the following is empty, please note):

All the conf configuration files under the php-fpm.d directory are introduced here, but NONE needs to be modified to our actual directory: / usr/local

By default, there is a file for the configuration user named www.conf.defalut under etc/php-fpm.d/. Execute the following command to copy a new file and open it:

Cp / usr/local/etc/php-fpm.d/www.conf.default / usr/local/etc/php-fpm.d/www.confvim / usr/local/etc/php-fpm.d/www.conf

The default user and group are set to nobody and changed to www-data

After the modification is complete, save and exit, and then execute the following command to start the php-fpm service:

/ usr/local/bin/php-fpm

After startup, the php-fpm service uses port 9000 by default, and uses netstat-tln | grep 9000 to check the port usage:

Port 9000 is in normal use, indicating that the php-fpm service started successfully

Then execute vim / usr/local/nginx/nginx.conf to edit the nginx configuration file. The specific path is edited according to the location of the actual nginx.conf configuration file. The following mainly modifies the contents of nginx's server {} configuration block, modifies the location block, and appends index.php to make the nginx server support index.php as the home page by default:

Then the configuration .php request is passed to the backend php-fpm module. By default, the php configuration block is annotated, and the comment is removed and modified to the following:

Many of these are default. Root is the root directory where the configuration php program is placed. The main modification is that the / scripts in fastcgi_param is $document_root.

After modifying the above, go back to the first line of nginx.conf. The default is # user nobody;. If you want to remove the comment and change it to user www-data; or user www-data www-data;, it means that the permission of nginx server is www-data.

After modifying these, save and exit, and then restart nginx:

/ usr/local/nginx/nginx-s stop/usr/local/nginx/nginx

Next, edit a test php program, create an info.php file in the html directory under nginx, and print the php configuration:

Then open the browser and enter the corresponding address to access it, and you will see the output page, indicating that both nginx and php are configured successfully:

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

Servers

Wechat

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

12
Report