Web Platform Architecture: LNMP (centos6.5+nginx+mysql+php)

Source: Internet
Author: User
Tags curl fpm openssl pear php cli php source code prepare zts

Web Platform Architecture: LNMP (centos6.5+nginx+mysql+php)
In order to write Nginx series of tutorials, in the introduction of Nginx features, can have a test environment, so write a LNMP architecture, so that those who look at the document can set up their own environment to test nginx function. However, usually nginx is used as a pure Web site is not many, more is the use of nginx in the reverse proxy environment. However, due to the superior performance of nginx processing static files, it is often possible to use Nginx in conjunction with PHP to achieve static and dynamic separation, which is the function of Web response acceleration.

1. Preparatory work

Software Install path:/usr/local/

Note: In preparation, prepare a CentOS system, configure the network environment and the Yum source, prepare the package to be used, decide where to place the package and where to install it. Second, the installation process all use the source package compiled installation.

#yum-y install gcc* compact-gcc* autoconf automake make CMake

Install the compiled software required for compiling and installation, if the system is already installed, you do not need to install it again
#iptables-f;service iptables stop;chkconfig iptables off

Shutting down the firewall is not recommended in production environments iptables

Temporarily close SELinux
#setenforce 0
If you want to permanently turn selinux off, modify the configuration file

2. Installing Nginx

#tar-ZXVF openssl-1.0.1i.tar.gz
#tar-ZXVF pcre-8.35.tar.gz
#tar-ZXVF nginx-1.6.1.tar.gz
#cd nginx-1.6.1
#mkdir-pv/usr/local/nginx {client_body_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp}
#./configure--prefix=/usr/local/nginx/--with-pcre=/root/soft/pcre-8.35--with-http_ssl_module--with-openssl=/ root/soft/openssl-1.0.1i--with-http_gzip_static_module--with-http_realip_module--with-http_stub_status_module- -http-client-body-temp-path=/usr/local/nginx/client_body_temp/--http-proxy-temp-path=/usr/local/nginx/proxy_ temp/--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp/--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp /--http-scgi-temp-path=/usr/local/nginx/scgi_temp/
#make && make Install
#make Clean

In this case, if there is no error, the basic installation is successful. But it's important to note that Pcre and OpenSSL use source packages.

Test Nginx:
#/usr/local/nginx/sbin/nginx//Start Nginx
#ps aux | grep nginx//To see if nginx process started successfully
#netstat-NULTP | grep 80//View 80 port is listening, nginx default listener on 80 port

Browser testing; //If the Nginx Welcome page appears normally, the Nginx installation is successful

Introduce to you a Linux text browser elinks, later local testing, you can directly in the Linux system to test.
#yum-y Install ELinks
#links--dump //Print out the contents of the Web site directly, if you want to access it in an interactive interface, type ELinks enter directly.

3. Install MySQL
#yum-y install zlib-devel openssl-devel ncurses-devel bison//because the following compilation MySQL uses these development files, so must be installed
#tar-ZXVF mysql-5.5.27.tar.gz
#cd mysql-5.5.27
#useradd-S/sbin/nologin mysql-m
-dmysql_datadir=/data/mysqldata (Note: Here in the enterprise environment, usually a separate directory for the MySQL data files, if the business can not be used, the default is placed under/var/lib/mysql)
#make Install
#make Clean

#chown-R Mysql.mysql/usr/local/mysql
#chown-R Mysql.mysql/data/mysqldata
#cd: /support-files
#cp my-large.cnf/etc/rc.d/my.cnf
#cp-P Mysql.server/etc/rc.d/init.d/mysqld
#chown-R Root.mysql/usr/local/mysql
#service mysqld Start
#mysqladmin-U root Passwod "LXM"//Set the password of the MySQL root user
#mysql-U root-p
Enter the root password to test if the MySQL installation was successful

To this, if everything is not wrong, it means that MySQL installation is successful

4. Install PHP
The installation and use of PHP is really worth talking about, but it's a simple installation. For detailed installation use will be a detailed chat in lamp ...
To install the necessary packages:
#yum-y install ncurses-devel net-snmp-devel bzip2 bzip2-devel Curl curl-devel libjpeg libjpeg-devel libpng libpng-devel Freetype-devel libxml2 libxml2-devel libxslt libxslt-devel zlib zlib-devel expat expat-devel OpenSSL openssl-devel gd PHP -GD libjpeg libjpeg-devel libtool Libtool-ltdl libtool-ltdl-devel

Install Libmcrypt and Mhash//These two things are for PHP to provide the encryption algorithm related to the library file
#tar-ZXVF libmcrypt-2.5.7.tar.gz
#cd libmcrypt-2.5.7
#make && make Install
#make Clean

#tar-ZXVF mhash-0.9.9.tar.gz
#cd mhash-0.9.9
#make && make Install
#make Clean

Install PHP:
#tar-ZXVF php-5.5.16.tar.gz
#cd php-5.5.16
#./configure--prefix=/usr/local/php55/--with-libxml-dir=/usr/include/libxml2/--with-config-file-path=/etc-- With-mysql=mysqlnd--with-mysqli=mysqlnd--with-gd--enable-gd-native-ttf--with-zlib--with-mcrypt=/usr/local/ libmcrypt/--with-mhash=/usr/local/mhash/--with-pdo-mysql=mysqlnd--enable-fpm--enable-shmop--enable-soap-- Enable-sockets--enable-wddx--enable-zip--with-xmlrpc--enable-mbstring--with-zlib-dir=/usr/lib--with-bz2-- With-curl--enable-exif--enable-ftp--with-jpeg-dir=/usr/lib--with-png-dir=/usr/lib--with-freetype-dir=/usr/lib- -with-pcre-dir--enable-bcmath--with-config-file-scan-dir=/etc/php.d/--with-openssl
If there are no errors, the following will be displayed:
Generating files
| License: |
| This software was subject to the PHP License, available in this |
|  Distribution in the file LICENSE. By continuing this installation |
|     Process, you is bound by the terms of this License agreement. |
| If you don't agree with the terms of this license, you must abort |
|                            The installation process at this point. |

Thank for using PHP.

Config.status:creating Php5.spec
Config.status:creating Main/build-defs.h
Config.status:creating scripts/phpize
Config.status:creating scripts/man1/phpize.1
Config.status:creating Scripts/php-config
Config.status:creating Scripts/man1/php-config.1
Config.status:creating SAPI/CLI/PHP.1
Config.status:creating sapi/fpm/php-fpm.conf
Config.status:creating SAPI/FPM/INIT.D.PHP-FPM
Config.status:creating Sapi/fpm/php-fpm.service
Config.status:creating sapi/fpm/php-fpm.8
Config.status:creating sapi/fpm/status.html
Config.status:creating SAPI/CGI/PHP-CGI.1
Config.status:creating Ext/phar/phar.1
Config.status:creating Ext/phar/phar.phar.1
Config.status:creating main/php_config.h
config.status:executing default Commands

#make && make Install
If there is no error, the following is displayed:
Installing Shared extensions:/usr/local/php55/lib/php/extensions/no-debug-non-zts-20121212/
Installing PHP CLI binary:/usr/local/php55/bin/
Installing PHP CLI man page:/usr/local/php55/php/man/man1/
Installing PHP FPM binary:/usr/local/php55/sbin/
Installing PHP FPM config:/usr/local/php55/etc/
Installing PHP FPM man page:/usr/local/php55/php/man/man8/
Installing PHP FPM Status page:/usr/local/php55/php/fpm/
Installing PHP CGI binary:/usr/local/php55/bin/
Installing PHP CGI man page:/usr/local/php55/php/man/man1/
Installing Build Environment:/usr/local/php55/lib/php/build/
Installing header files:/usr/local/php55/include/php/
Installing helper Programs:/usr/local/php55/bin/
Installing man pages:/usr/local/php55/php/man/man1/
Installing PEAR Environment:/usr/local/php55/lib/php/
[PEAR] archive_tar-installed:1.3.12
[PEAR] console_getopt-installed:1.3.1
[PEAR] structures_graph-installed:1.0.4
[PEAR] xml_util-installed:1.2.3
[PEAR] pear-installed:1.9.5
Wrote PEAR system config file at:/usr/local/php55/etc/pear.conf
Want to add:/usr/local/php55/lib/php to your php.ini include_path
/root/soft/php-5.5.16/build/shtool install-c Ext/phar/phar.phar/usr/local/php55/bin
Installing PDO headers:/usr/local/php55/include/php/ext/pdo/

#make Clean

#cp Php.ini-production/etc/php.ini
#./php-m | Less//view PHP compiled built-in modules, if not you need to add additional, specifically how to configure see lamp
Note: A common error when testing PHP modules is that you cannot load certain modules, for example: PHP warning:php startup:unable to load dynamic library '/usr/local/php55/lib/php/ Extensions/no-debug-non-zts-20121212/curl.so '-/usr/local/php55/lib/php/extensions/no-debug-non-zts-20121212/ Curl.so:cannot open Shared object file:no such file or directory in Unknown on line 0. Make such a mistake, don't be nervous. This is because you are a compiled and installed PHP module, and here you are prompted that the modules you cannot load are usually the modules that need to be loaded when you install the RPM package. The configuration files of these modules are usually in the/etc/php.d/directory, you only need to delete these files, test again will not be wrong. And you find that failed to load the module, in the PHP built-in module is already ready to use, this is the PHP source code compilation.

#cd/usr/local/php55/etc///Hint: Why always use absolute path, is afraid of some people do not know I cut to cut, where to cut. Fool's technology, must let you know why ....
#cp-P Php-fpm.conf.default php-fpm.conf
[[email protected] etc]#/USR/LOCAL/PHP55/SBIN/PHP-FPM//start FPM process
[[Email protected] etc]# PS aux | grep fpm
Root 17161 0.0 0.9 207092 4972? Ss 10:38 0:00 php-fpm:master process (/usr/local/php55/etc/php-fpm.conf)
Nobody 17162 0.0 0.8 207092 4388? S 10:38 0:00 Php-fpm:pool www
Nobody 17163 0.0 0.8 207092 4388? S 10:38 0:00 Php-fpm:pool www
Root 17165 0.0 0.1 103244 848 pts/0 s+ 10:38 0:00 grep fpm
[Email protected] etc]# NETSTAT-NULTP | grep fpm
TCP 0 0* LISTEN 17161/php-fpm

From the above information, PHP-FPM has been successfully launched and supervised the interpretation of PHP programs

Related Content Introduction:
1.freetype: Some of the most popular font libraries
2.zlib: The compression library of the universal format on the Internet
3.--enable-sockets: Allow PHP to support socket communication
4.mcrypt: Additional encryption library with encryption enabled
5. If you are using PHP5.3 or above, to connect to the MySQL database, you can specify MYSQLND so that you do not have to install the MySQL or MySQL development package first. Mysqlnd is available from php5.3, and can be bound to it at compile time (instead of relying on a specific MySQL client binding, but it is the default from the beginning of the php5.4.
6.--ENALBE-FPM This option is to compile PHP into a separate server managed by PHP-FPM. Nginx does not integrate PHP into the internal functions like Apache, so you must add this option when compiling PHP, otherwise nginx and PHP will not be integrated through fastcgi. Because of this feature, PHP can be made into clusters.

5. Integration of Nginx and PHP
Nginx and PHP integration can be proxy_pass and fastcgi two ways to integrate, but usually through the fastcgi way to integrate, as for the benefits of fastcgi, please check it yourself ...
Modify configuration file:/usr/local/nginx/conf/nginx.conf
Ensure that you have the following configuration for the consolidated PHP:
Location ~ \.php$ {
root/usr/local/nginx/html; This is the root directory of the specified Web site, php files outside the root of the site are not interpreted and will prompt file not found.
Fastcgi_index index.php;
# Fastcgi_param Script_filename/scripts$fastcgi_script_name; This is the Nginx document default configuration, but in the new version of Nginx is not valid, will be prompted not to the root directory
Fastcgi_param script_filename $document _root$fastcgi_script_name;
Include Fastcgi_params;

#service nginx configtest//Check the configuration file for syntax errors
#service nginx Restart//I have configured the Nginx boot file, if you need to, please go to nginx installation deployment inside copy one.
Create a index.php test file with the following content:
Phpinfo ();

Test the PHP page for normal parsing and access to:
If everything works, it will return to the PHP test page, which is also a page that looks at the parameters and functions of PHP, and must be

6. Testing the LNMP architecture
Installed nginx,mysql,php, the final test of these three programs on the Linux platform is able to run through, here with a simple PHP test file can be.
#vim index.php
Modify the contents of the configuration file as follows:
$links = mysql_connect (' localhost ', ' root ', ' lxm ');
if ($links)
echo "Yes";
echo "No";

Process: When the client requests the PHP file, Nginx will submit the PHP file to the backend php-fpm for parsing, and then PHP call Mysql_connect to the back-end database connection test, if successful return yes, failure will return no


650) this.width=650; "style=" Float:none; "title=" 2.jpg "src=" http://s3.51cto.com/wyfs02/M00/47/88/ Wkiol1p8fdgzjveyaacvuwprdlc992.jpg "alt=" Wkiol1p8fdgzjveyaacvuwprdlc992.jpg "/>

The result returned is: no
What is this? To view logs:
650) this.width=650; "style=" Float:none; "title=" 3.jpg "src=" http://s3.51cto.com/wyfs02/M02/47/86/ Wkiom1p8exrijvqeaag9gjwttsy648.jpg "alt=" Wkiom1p8exrijvqeaag9gjwttsy648.jpg "/>

Look at the above error, it seems that mysql_connect this function is not found. Strange Why? After many tests, found that the original MySQL sock file location problem. When compiling above, using the--enable-sockets,php default is to find/var/lib/ MySQL under the Mysqld.sock file, which is also the default location of MySQL storage sock, but this document when installing MySQL, sock placed in the TMP directory, so you need to modify the php.ini file to specify the location of the sock file.
Locate the following option and specify the Soket location
Mysql.default_socket =/tmp/mysqld.sock

Restart PHP-FPM:
#pkill PHP-FPM

At this point, the test again, return yes, indicating that the test was successful and that the entire LNMP architecture has been measured.

650) this.width=650; "style=" Float:none; "title=" 1.jpg "src=" http://s3.51cto.com/wyfs02/M01/47/86/ Wkiom1p8exrdcgaaaacdcu8gjck406.jpg "alt=" Wkiom1p8exrdcgaaaacdcu8gjck406.jpg "/>

To this, LNMP platform is completed, more technical documents please see tell .....

Fool's technical------not afraid you will NOT!!!!

This article is from the "Fool's Technology" blog, please be sure to keep this source http://mingyang.blog.51cto.com/2807508/1545100

Web Platform Architecture: LNMP (centos6.5+nginx+mysql+php)

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.