Developers learn Linux (7): CentOS7 Compile and install PHP and configure PHP-FPM

Source: Internet
Author: User
Tags fpm install php openssl install wordpress wordpress database heidisql

1. Preface
The previous article described how to compile and install MySQL, although it can be installed through Yum install or RPM, but the Yum install and RPM installation has one feature, that is, some parameters are customized according to the needs of the public, if you need to specify their own specific parameters, This is difficult to do, so a certain Linux based people are compiled installation or according to the company's specific environment to create a suitable environment for the installation package. The previous example describes how to compile the installation and how to configure multiple instances, and finally describes how to configure the master-slave replication configuration.
Formerly open source development often will lamp, is linux+apache+mysql+php, for some start-up companies, lamp can satisfy all their fantasies: can respond to a certain size of the user, the hardware requirements are very small. Now with the rise of Nginx, lamp has become lnmp, that is, linux+nginx+mysql+php, so this article will explain how to compile and install PHP and use PHP-FPM to let Nginx support PHP.
2. Preparation
2.1 Download PHP5.6.3 Source code
Given that many open source PHP are still using the PHP5 version, here is an example of the PHP5.6.3 version of the build installation:

2.2 Chinese version of WordPress
WordPress is a very well-known personal blogging system. Use it here to verify that our php-fpm configuration is correct.

2.3 Install dependent libraries

yum install gcc automake autoconf libtool make --y
yum install gcc gcc-c ++ glibc --y
yum install libmcrypt-devel mhash-devel libxslt-devel --y
yum install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel --y
yum install zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel --y
yum install ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel -y
yum install krb5 krb5-devel libidn libidn-devel openssl openssl-devel -y
Note: Some of the above dependent libraries have been installed in the previous article. For the sake of convenience, I have listed them here again. In the actual installation process, if they are already installed locally, they will not be installed and downloaded again.
3. Compile and set up
3.1 Compiled files
Assuming that the downloaded php-5.6.3.tar.gz is located in the / root directory, first unzip the file:

tar zxvf /root/php-5.6.3.tar.gz

Then execute configure:

cd /root/php-5.6.3
./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 -with-gettext --enable-xml --enable-freetype
If all the dependent libraries are installed, no error will be reported. If you are prompted that the dependent libraries are missing, use yum install to install them. In the above parameters:
--prefix = / usr / local / php: indicates that the program will be installed in the / usr / local / php directory in the future;
--enable-fpm: indicates that the php-fpm module is enabled. Without this nginx enabled, there is no way to forward php page requests to nginx for processing.
Then execute:

make install
Because there is a lot of source code, it takes a little time to compile, please be patient. After success, a php folder will be created under / usr / local, and its file directory structure is as follows:

[[email protected] ~] # ls -l / usr / local / php
total 0
drwxr-xr-x 2 root root 147 Jun 7 23:38 bin
drwxr-xr-x 2 root root 71 Jun 12 22:13 etc
drwxr-xr-x 3 root root 17 Jun 7 23:12 include
drwxr-xr-x 3 root root 85 Jun 12 22:12 lib
drwxr-xr-x 4 root root 28 Jun 7 23:12 php
drwxr-xr-x 2 root root 21 Jun 7 23:12 sbin
drwxr-xr-x 4 root root 28 Jun 7 23:12 var
3.2 Configuration
3.2.1php.ini configuration
First copy the php.ini-development from the unzipped folder to the / usr / local / php / lib directory and name it php.ini:

cp /root/php-5.6.3/php.ini-development /usr/local/php/lib/php.ini

Note: Change php.ini-development to php.ini on the development server. If it is a production server, it is recommended to change php.ini-production to php.ini. Compared with php.ini-development Some debugging output, which can reduce the size of the log file and improve performance in the production environment. It is recommended to open it in the development environment to facilitate debugging. The following explanation is based on the development environment configuration.
Make the following changes to php.ini:
error_reporting = E_ALL & ~ E_NOTICE; output debugging information and notification
error_log = /usr/local/php/var/log/error-log.log; error log storage location
date.timezone = "Asia / Shanghai"; This sentence was originally "; date.timezone =", you need to remove its comment and set it to Chinese time zone
3.2.2php-fpm.conf configuration
First, make a copy of / usr / local / php / etc / php-fpm.conf.default and name it php-fpm.conf:

cd / usr / local / php
cp etc / php-fpm.conf.default etc / php-fpm.conf
Then make the following changes to php-fpm.conf:
; pid = run / This sentence is uncommented and changed to: pid = /usr/local/php/var/run/;
; error_log = log / php-fpm.log uncomment this sentence and change to: error_log = /usr/local/php/var/log/error-log.log
user = nobody changed to: user = nginx
group = nobody changed to: group = nginx
; catch_workers_output = yes This sentence is uncommented and changed to: catch_workers_output = yes
The php-fpm executable file is in the / usr / local / php / sbin directory, and you can perform monitoring through / usr / local / php / sbin. The default is to listen on port 9000.
4. Install WordPress
4.1 Database preparation
4.1.1 Start the MySQL database
In order to observe the effect of our Master-slave configuration in the previous article, this time we will start both database instances:

/usr/local/mysql-5.7.18/data/3306/mysql start
/usr/local/mysql-5.7.18/data/3307/mysql start
4.1.2 MySQL database preparation
For information security, a library named wordpress is specifically created on the main database instance, and a database account is configured for the wordpress library. The command is as follows:

create database wordpress;
grant all privileges on wordpress. * to [email protected] identified by ‘WordPress‘; flush privileges;
That is, a new wordpress library is created, and a new wordpress database account is created at the same time. The account password is WordPress, and it has all permissions to the wordpress library.
4.2 WordPress site and domain name resolution configuration
4.2.1 Unzip WordPress
Assuming is in the / root directory, execute the following command to decompress the file:

cd ~
tar zxvf ./wordpress-4.7.4-zh_CN.tar.gz
Put wordpress in the nginx directory:

mv ./wordpress /usr/local/nginx-1.12.0/html

4.2.2 Add a site in nginx
First add a file in /usr/local/nginx-1.12.0/conf/vhosts, named, whose contents are as follows:

server {
        listen 80;
        access_log logs / wordpress.access.log main;
        error_log logs / wordpress.error.log;
        #root html;
        #index index.html index.htm index.jsp index.php;
        location / {
                root html / wordpress;
                index index.htm index.html index.php;
        location ~ * \ .php $ {
                 root html / wordpress;
                 fastcgi_index index.php;
                 fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
                 include fastcgi_params;
Then include this file in /usr/local/nginx-1.12.0/conf/nginx.conf, the content of the file is as follows:
650) this.width = 650; "src =" "title =" PHP_FPM02_nginx "alt =" wKioL1k_8wuwnIqcAABjd0dN1K8390.png-wh_50 "/>
Then check whether there is a syntax error in the configuration, and if there is no syntax error, reload the configuration:

/usr/local/nginx-1.12.0/sbin/nginx --t

4.2.3 Start the application
First start nginx:

systemctl reload nginx.service

Note: See the previous chapter of this series, since nginx has been configured to start with the system, you can restart it with the above command.
Then start php-fpm:

/ usr / local / php / sbin / php-fpm

This is to observe the startup status of php-fpm:

[[email protected] ~] # netstat -lntp | grep 9000
tcp 0 0* LISTEN 5498 / php-fpm: maste
If the result is not the above, you can view the error log:

tail /usr/local/php/var/log/error-log.log

4.2.4 Configuration detection
We can create a test php page in /usr/local/nginx-1.12.0/html/wordpress, the file name is test.php, and the file content is as follows:

<? php
    phpinfo ();
In order to be able to observe the results through the domain name in the Windows host, you need to modify the hosts file, which is located in the C: \ Windows \ System32 \ drivers \ etc directory.

# Copyright (c) 1993-2009 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP / IP for Windows.
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a ‘#‘ symbol.
# For example:
# # source server
# # x client host
# localhost name resolution is handled within DNS itself.
# localhost
# :: 1 localhost
Note: is the IP address of the CentOS7 virtual machine.
Open the page in the browser, the effect is as follows:
4.3 Installation
Open on the host machine to see the installation interface:
Click "Start Now!" To enter the installation interface, such as:
In this article "4.1.2 MySQL Database Preparation", we have created the wordpress library and the wordpress user, whose password is WordPress, fill in and submit the above information to complete the installation of wordpress.
The effect of the homepage of the wordpress background management after installation is as follows:
As for how to use wordpress, I will not elaborate here. Those who are interested can go to their official website to read the manual. Here mainly use wordpress to check whether nginx + php-fpm is configured correctly.
5. MySQL master-slave effect check
Interested friends can take a look at the MySQL master-slave configuration that we talked about in the previous article. The following is the interface of the master-slave database I checked with HeidiSQL, an open source database management tool:
Note: HeiDiSQL official website:, which can be downloaded and used free of charge, and supports PostgreSQL, MySQL and SQL Server.
6. Summary
This article describes how to compile PHP and configure Nginx to support php applications with php-fpm. Finally, through the installation and configuration of a php application wordpress to check whether nginx + php-fpm is configured correctly, and also checked on this Whether the MySQL master-slave replication described in the article is working properly.

This article is from the "Zhou Gong (Zhou Jinqiao) column" blog, please keep this source

Developers learn Linux (7): CentOS7 compile and install PHP and configure PHP-FPM

============= ↑ Article content display ↑ =============

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.