Install nginx+php and configure PHP-FPM module under Linux

Source: Internet
Author: User
Tags fpm install php install openssl phpinfo nginx server

The following is a CentOS 7.2 example, starting with PHP installation:

Install PHP running environment, first open the downloads page of the PHP official website:

After downloading the installation package, upload it to the server,

Because the PHP installation needs to be compiled, the server should ensure that the GCC and g++ environments are installed,

First release the installation package:

tar -xvzf php-7.0. 5. Tar . GZCD php-7.0. 5


The next parameter configuration, if there is no libxml2 and Libxml2-devel error before configuration, so should update libxml2 and install Libxml2-devel, using online installation:

Yum-y Install libxml2yum-y Install Libxml2-devel

Supplement, because of different operating system environment, the system installs the development environment package is not the same degree of completeness, so it is recommended to install the operating system to do the necessary choice, you can also uniformly execute all the commands, install the components that are not installed, if the installation may be upgraded, the version is fully consistent will not do anything , the command is summarized as follows in addition to the 2 above:

Yum-y Install opensslyum-y Install openssl-develyum-y install curlyum-y install curl-develyum-y install libjpegyum-y Install libjpeg-develyum-y install libpngyum-y install libpng-develyum-y install freetypeyum-y install Freetype-devel Yum-y Install pcreyum-y Install pcre-develyum-y install libxsltyum-y install libxslt-develyum-y install bzip2yum-y I Nstall Bzip2-devel

These packages are basically sufficient, and if the problem is found to be replenished, after the installation is complete, the configuration is performed:

./configure--prefix=/usr/local/php--with-curl--with-freetype-dir--with-gd--with-gettext--with-iconv-dir-- With-kerberos--with-libdir=lib64--with-libxml-dir--with-mysqli--with-openssl--with-pcre-regex--with-pdo-mysql- -with-pdo-sqlite--with-pear--with-png-dir--with-jpeg-dir--with-xmlrpc--with-xsl--with-zlib--with-bz2-- With-mhash--enable-fpm--enable-bcmath--enable-libxml--enable-inline-optimization--enable-mbregex-- Enable-mbstring--enable-opcache--enable-pcntl--enable-shmop--enable-soap--enable-sockets--enable-sysvsem-- Enable-sysvshm--enable-xml--enable-zip

In fact, there are more configuration items here than the above, you can use the ./configure--help command to see all the options, note that in PHP7--with-mysql native support no longer exists, the operation becomes mysqli or PDO;

These options are fully sufficient for normal PHP development, and you can choose to manually open the appropriate modules later if needed.

Then perform the compilation:


The compilation time may be a bit long and after the compilation is complete, the installation is performed:

Make install

The default installation location for PHP is already specified as/usr/local/php, and the appropriate files are then configured:

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, using: vim/usr/local/php/lib/php.ini Open the PHP configuration file to find the Cgi.fix_pathinfo configuration item, which is annotated by default and has a value of 1, according to the official documentation, This is to prevent malicious script injection from being sent to the backend's PHP-FPM module when the file does not exist, so the entry should be stripped of the comment and set to 0


Save and exit after Setup

Also note that one place is the location of the php.ini configuration file can be set in pre-compilation configuration parameters, compile parameters can be written as:--with-config-file-path=/usr/local/php In this case, PHP will go back to the specified directory to read the php.ini configuration file, if not add the default location is the PHP installation directory of the Lib directory, can also be viewed in the phpinfo () output interface, If PHP.ini is placed in another location, PHP cannot read it, then all configuration changes are not effective, this should be noted

At this point you should create a Web user first:

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

Then online some tutorials say let modify php-fpm.conf add the above created users and groups, at this time use vim/usr/local/php/etc/php-fpm.conf open the file after the official prompt location is not found:


If you add in a location at this point, then the next time you start PHP-FPM, you will report the directory can not find errors, so do not add users and groups in php-fpm.conf, this time to php-fpm.conf the last line will find the following ( If you add the--prefix option at compile time the following location will be automatically complete, the default is the following is empty, pay attention to):


All the Conf configuration files in the PHP-FPM.D directory are introduced here, but none needs to be modified to our actual directory:/usr/local


By default, etc/php-fpm.d/has a configuration user file named Www.conf.defalut, which executes the following command to copy a new file and open:

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

The default user and group settings are nobody, which is changed to Www-data


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


After the boot is complete, the PHP-FPM service uses 9000 ports by default, using netstat-tln | grep 9000 to view port usage:


9000 port normal use, indicating PHP-FPM service started successfully

Then execute vim/usr/local/nginx/nginx.conf edit nginx configuration file, the specific path according to the actual nginx.conf configuration file location editing, the following major changes in the Nginx server {} configuration block content, Modify the location block, append index.php let Nginx server default support index.php home page:


Then configure the. PHP request to be routed to the backend PHP-FPM module, where the PHP configuration block is annotated by default, and the comment is removed and modified to the following:


Many of these are default, root is the configuration of PHP program placement root directory, the main modification is fastcgi_param in the/scripts for $document_root

Modify the above, go back to the first line nginx.conf, the default is #user nobody; This is to remove the comment to user www-data, or user Www-data Www-data, which indicates that the permissions of the Nginx server are Www-data

Modify these save and exit, and then restart Nginx:

Next edit a test PHP program, in the Nginx under the HTML directory to create the test.php file, print the PHP configuration:

<?php    phpinfo ();? >

Then open the browser to enter the corresponding address to access, see the output page, stating that Nginx and PHP are configured successfully:


Install nginx+php and configure PHP-FPM modules under Linux

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.