Build nginx + PHP + MySQL environment with centos (servers higher than apeche10)

Source: Internet
Author: User
Tags imap php website openldap

Flowchart:



Official Chinese wiki: http://wiki.nginx.org/Chs
Good resource site: the personal blog of www.s135.com is awesome.
Nginx ("engine X") is a high-performance HTTP and reverse proxy server and an IMAP/POP3/SMTP proxy server. Nginx is accessed by Igor Sysoev in Russia... more> nginx ("engine X") is a high-performance HTTP and reverse proxy server and an IMAP/POP3/SMTP proxy server. Nginx was developed by the rambler.ru site with the highest access volume in Russia as Igor Sysoev. It has been running on this site for more than two and a half years. Igor releases the source code in the form of a class BSD license

Use the yum command to install and upgrade the required library before installation.
Yum-y install GCC gcc-C ++ Autoconf libjpeg-devel libpng-devel FreeType-devel libxml2 libxml2-devel zlib-devel glibc-devel glib2 glib2-devel Bzip2 bzip2-devel ncurses- devel curl-devel e2fsprogs e2fsprogs-devel
The krb5 krb5-devel libidn-devel OpenSSL-devel OpenLDAP openldap-devel nss_ldap openldap-clients openldap-Server


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-Config =/usr/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-fontconfig =/usr/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.

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: info-contact@alibabacloud.com 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.