Detailed configuration diagram of CentOS + Nginx + PHP + MySQL

Source: Internet
Author: User
Tags php website
: This article mainly introduces the detailed configuration diagrams of CentOS + Nginx + PHP + MySQL. if you are interested in the PHP Tutorial, refer to it. 1. install MySQL

Currently, few web servers run static pages. if you want to run dynamic websites, you can't do without databases. although I have written in my previous articles how to install MySQL, I feel that I haven't installed MySQL for a long time, now I only paste the steps out, so I will not explain them too much.

# Useradd mysql

# Tar zxvf mysql-5.0.40.tar.gz

# Cd mysql-5.0.40

#./Configure -- prefix =/usr/local/mysql

# Make & make install

#/Usr/local/mysql/bin/mysql_install_db -- user = mysql // initialize the MySQL database

# Chown-R mysql/usr/local/mysql/var

#/Usr/local/mysql/bin/mysqld_safe & // start MySQL

#/Usr/local/mysql/bin/mysqladmin-u root password 123456 // Set the MySQL password

# Cp support-files/my-medium.cnf/etc/my. cnf

# Echo "/usr/local/mysql/bin/mysqld_safe &">/etc/rc. local

II. install PCRE

PCRE is a regular expression used by perl to support the installed software. By default, Nginx only processes static webpage requests, that is, html. for dynamic webpage requests, such *. php, then Nginx needs to query the path based on the regular expression, and then set *. PHP to PHP for processing

# Rpm-qa | grep pcre // whether the PCRE is installed in the query system. Generally, the installed system is installed by default, so we need to delete the built-in

# Cp/lib/libpcre. so.0 // before deleting the built-in PCRE, back up libpcre first. so.0 file, because the RPM package is too correlated, no libpcre after deletion. we cannot install PCRE in so.0.

# Rpm-e -- nodeps pcre-6.6-1.1 // delete the built-in PCRE

# Tar zxvf pcre-8.00.tar.gz

# Cd pcre-8.00

# Cp/libpcre. so.0/lib // Copy the libpcre. so.0 backed up before we delete the built-in PCRE to the/lib directory.

#. /Configure // configure PCRE. because PCRE is a library rather than a program such as pache, php, and postfix, you can select the default path during installation, this will avoid unnecessary troubles when installing other things later. after executing this section, it will be displayed, which shows our PCRE configuration.

# Make & make install

3. install Nginx

On the Internet, it is very troublesome for many people to install Nginx. a lot of options are used for configuration. do you really implement this function? The more I see it, the more depressing it is. If you follow the steps above to install Nginx, you only need to specify the Nginx installation path.

# Tar zxvf nginx-0.8.24.tar.gz

# Cd nginx-0.8.24

#./Configure -- prefix =/usr/local/nginx // you only need to specify a path in this step.

# Make & make install

#/Usr/local/nginx/sbin/nginx // start Nginx

# Echo "/usr/local/nginx/sbin/nginx">/etc/rc. local

There are two processes after Nginx is started. The master is the master process and the worker is the worker process, as shown in figure

After NGINX is started, you can enter http: // localhost in the browser to view it, as shown in figure

4. install PHP

Since PHP is installed, GD is indispensable and will not be described here.

1. install libpng

# Tar xvf libpng-1.2.10.tar.tar

# Cd libpng-1.2.10

#./Configure -- prefix =/usr/local/png

# Make; make install

# Ln-s/usr/local/png/lib/*/usr/lib/

2. install jpeg

# Mkdir/usr/local/jpeg

# Mkdir/usr/local/jpeg/bin

# Mkdir/usr/local/jpeg/lib

# Mkdir/usr/local/jpeg/include

# Mkdir/usr/local/jpeg/man

# Mkdir/usr/local/jpeg/man/man1

# Tar xvf restart src.v7.tar.tar

# Cd jpeg-7

#./Configure -- prefix =/usr/local/jpeg -- enable-shared -- enable-static

# Make; make install

# Ln-s/usr/local/jpeg/lib/*/usr/lib/

3. install freetype

# Tar xvf freetype-2.3.9.tar.tar

# Cd freetype-2.3.9

#./Configure -- prefix =/usr/local/freetype

# Make; make install

4. install fontconfig

# Tar zxvf fontconfig-2.4.2.tar.gz

# Cd fontconfig-2.4.2

#./Configure -- prefix =/usr/local/fontconfig -- with-freetype-c/local/freetype/bin/freetype-config

# Make; make install

5. install GD

# Tar zxvf gd-2.0.32.tar.gz

# Cd gd-2.0.32

#. /Configure -- prefix =/usr/local/gd -- with-png =/usr/local/png -- with-jpeg =/usr/local/jpeg -- with-freetype =/usr /local/freetype -- with-fontc/local/fontconfig

# Cp/usr/local/png/include/png. h ./

# Cp/usr/local/png/include/pngconf. h ./

# Make; make install

6. install PHP

This is the most important place, because Nginx and PHP do not feel any difference by default. Many friends have built Apache + PHP. after compiling Apache + PHP, the module file is generated, while Nginx + PHP requires PHP to generate executable files, therefore, fastcgi technology should be used to achieve integration of nginx and PHP, as long as FastCGI is enabled for installation. This time we install PHP not only use FastCGI, but also use the PHP-FPM such a stuff, PHP-FPM said White is a management FastCGI a manager, it as a PHP plug-in pure in, install PHP in the form of a patch to install the PHP-FPM to PHP to use the PHP-FPM, and PHP to be consistent with the PHP-FPM version, this is necessary, remember!

Downloaded to the same directory

# Tar xvf php-5.3.0.tar.bz2

# Gzip-cd php-5.3.0-fpm-0.5.12.diff.gz | patch-d php-5.3.0-p1 // add php-5.3.0-fpm-0.5.12.diff.gz to the php-5.3.0 in patch form

# Cd php-5.3.0

#. /Configure -- prefix =/usr/local/php -- with-gd =/usr/local/gd -- with-jpeg-dir =/usr/local/jpeg -- with-png- dir =/usr/local/png -- with-freetype-dir =/usr/local/freetype -- with-mysql =/usr/local/mysql -- enable-fastcgi -- enable-fpm

Note: for Nginx + PHP integration, you must enable -- enable-fastcgi and -- enable-fpm during installation. the two options are described above. After Execution, the system will prompt that -- enable-fastcgi is an unknown option.

# Make

# Make install

# Cp php. ini-dist/usr/local/php/etc/php. ini

Next we will start PHP-FPM

#/Usr/local/php/sbin/php-fpm start

The above error is reported when starting the PHP-FPM because the PHP-FPM itself does not know to run PHP with that user and group, so we need to modify a file, remove the comments from the file (open the file and delete the red part), and then the PHP-FPM will run PHP with nobody users and groups.

# Vi/usr/local/php/etc/php-fpm.conf

#/Usr/local/php/sbin/php-fpm start

# Ps-aux | grep php

# Echo "/usr/local/php/sbin/php-fpm start">/etc/rc. local

5. integrate Nginx and PHP

As mentioned above, Nginx does not process dynamic web page requests, and Nginx transfers the dynamic requests to PHP. let's open the Nginx configuration file and take a look.

# Vi/usr/local/nginx/conf/nginx. conf // the part of the mark will be modified later

Nginx already knows how to send the request to PHP. when Nginx receives a *. php request, it will send the request to PHP through Port 9000. Below, we can remove these annotations, as shown in figure

Note: The above/usr/local/nginx/html is the path of our PHP website.

Only Nginx knows how to find PHP, but PHP also needs to know how to find Nginx. PS: You have seen JJMM on the street, but you have never known each other, or do you know how to connect to the other party? This we don't need to worry about, the PHP-FPM has defined in the configuration file where to accept PHP requests, we can open the configuration file to see

# Vi/usr/local/php/etc/php-fpm.conf

As shown in, we have seen before that Nginx forwards PHP requests to PHP through Port 9000 of the local machine, and we can see that PHP listens on data from Port 9000 of the local machine, nginx and PHP complete data requests through the local port 9000.

VI. test

We have defined the storage path of the PHP website in the nginx configuration file. for details, refer to/usr/local/nginx/html.

Next we will create a PHP page test webpage in this directory named test. php. the content is as follows:

After restarting PHP and nginx (the process can be closed by killing and then starting), we enter http: // localhost/test. php in the browser. the following interface is displayed successfully.

The above describes the detailed configuration diagrams of CentOS + Nginx + PHP + MySQL, including relevant content, and hope to help those who are interested in the PHP Tutorial.

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.