For nginx + php + mysql rpm package installation and configuration reference

Source: Internet
Author: User
Tags website server nginx server
This article mainly records the general process of using nginx, php, and mysql to build a web site. The system environment is CentOS6.3. To avoid many unexpected errors during source code compilation, most of the software in linux is directly installed in yum mode. Nginx -------------------- because it is ultimately necessary to build a web site for development, install ngi

This article mainly records the general process of using nginx, php, and mysql to build a web site. The system environment is CentOS6.3. To avoid many unexpected errors during source code compilation, most of the software in linux is directly installed in yum mode. Nginx -------------------- because it is ultimately necessary to build a web site for development, install ngi

This article mainly records the general process of using nginx, php, and mysql to build a web site. The system environment is CentOS6.3. To avoid many unexpected errors during source code compilation, most of the software in linux is directly installed in yum mode.

Nginx

--------------------

This is because a web site is built for development. Some software is required to install nginx, including:

GCC compiler: used to compile C language programs. It is required to compile the nginx source code.

Yum install-y gcc

PCRE Library: Perl is compatible with formal expressions. If a formal expression is used in the configuration file nginx. conf, you must compile the PCRE Library into nginx during nginx compilation. Because nginx's HTTP module relies on it to parse regular expressions.

Yum install-y pcrepcre-devel

Note: pcre-devel is the Development Library required for secondary development using PCRE, including header files, which is also necessary for compiling nginx.

Zlib library: Used to compress HTTP package content in gzip format. configure gzip on in conf, and specify to use gzip to compress HTTP responses of Certain types (content-type) to reduce the amount of network transmission. Zlib-devel is also required by the secondary library.

Yum install-y zlibzlib-devel

OpenSSL Development Library: Used to Support HTTP transmission over the SSL protocol. If you want to use MD5, SHA1, and other hash functions, you also need to install it.

Yum install-yopenssl openssl-devel

Download the nginxsource code from http://nginx.org/and decompress it. After Entering the source code directory, perform the following operations:

./Configure

Make

Make install

Installed in the/usr/local/nginx directory by default.

PHP

--------------------

Install php in yum.

Yum install-y php

[Root @ alex] # rpm-qa | grep-I php

Php-5.3.3-27.el6_5.x86_64

Php-common-5.3.3-27.el6_5.x86_64 // php dependency package

Php-cli-5.3.3-27.el6_5.x86_64 // php dependency package

Php is installed in this way, but nginx and php are still two relatively independent components. No direct connection is established by default, which means nginx cannot parse the php file at this time. As a lightweight Web Server, nginx does not support direct calling or parsing of external programs. All external programs (including PHP parsing) must be called through the FastCGI interface. So why didn't FastCGI be installed when the Apache server parses PHP? Because Apache runs php with its built-in mod_php module by default. In fact, you can configure Apache to use FastCGI to execute PHP scripts, this article does not discuss this. For more information, see https://www.centos.bz/2011/12/configure-apache-run-php-as-fastcgi /.

Advantages of fastcgi:

Stable: fastcgi runs cgi in an independent process pool (Multiple CGI interpreter processes can be started at the same time). If a single process dies, the system can easily discard it, then, allocate a new process to run the processing logic.

Performance: fastcgi separates the processing of dynamic logic from the server, and leaves the heavy-load IO processing to the host server, so that the host server can concentrate on IO, for a common dynamic web page, logical processing may only be a small part. A large number of static IO processing, such as images, does not require the participation of Logic Programs.

Scalability: fastcgi is a neutral technical standard that fully supports processing programs written in any language (php, java, perl, ruby, c ++, python ...).

Fastcgi also supports distributed operations. That is, the host running the FastCGI program can be separated from the host running the website server.

To use FastCGI to execute PHP script parsing on the nginx server, you also need to install a FastCGI Process Manager to maintain the cgi interpreter process. Now commonly used is a process manager called PHP-FPM, It is a third-party FastCGI Process Manager, as a PHP patch for development, in the installation needs to be compiled together with the PHP source, that is, the PHP-FPM is compiled into the PHP kernel. Because we use the rpm package for installation, we can directly use yum to install php-fpm.

Yum install-y php-fpm

[Root @ alex ~] # Rpm-qa | grep php-fpm

Php-fpm-5.3.3-27.el6_5.x86_64

[Root @ alex ~] # Rpm-ql php-fpm

/Etc/logrotate. d/php-fpm

// Etc/php-fpm.conf

// Etc/php-fpm.d

// Etc/php-fpm.d/www. conf

/Etc/rc. d/init. d/php-fpm

/Etc/sysconfig/php-fpm

/Usr/sbin/php-fpm

/Usr/share/doc/php-fpm-5.3.3

......

Php-fpm can be executed in two ways. Like Apache, the number of processes can be divided into dynamic and static Based on the settings, one is to directly start a specified number of php-fpm processes without increasing or decreasing them. The other is to start a certain number of php-fpm processes at the beginning, when the Request volume increases, the maximum number of php-fpm processes is dynamically increased. When idle, idle processes are automatically released to a lower limit. The following shows the configuration instance of php-fpm on my cloud host.

[Root @ alex ~] # Cat/etc/php-fpm.d/www. conf | grep-v ';' | grep-v ^ $

[Www]

Listen = 127.0.0.1: 9000 // listen to port 9000 of the local host. This is very important !!

Listen. allowed_clients = 127.0.0.1

User = apache

Group = apache

Pm = dynamic // dynamic mode

Pm. max_children = 50 // you can add up to 50 processes.

Pm. start_servers = 5 // start five processes

Pm. min_spare_servers = 5

Pm. max_spare_servers = 35

Slowlog =/var/log/php-fpm/www-slow.log

Php_admin_value [error_log] =/var/log/php-fpm/www-error.log

Php_admin_flag [log_errors] = on

Php_value [session. save_handler] = files

Php_value [session. save_path] =/var/lib/php/session

Start php-fpm according to the above configuration. It will listen to 9000 of the current host and splice and send php resolution requests to this port. Php-fpm is ready, and nginx will be configured later to forward the permission to interpret the php file to port 9000.

[Root @ alex ~] #/Etc/init. d/php-fpm start

View the php-fpm process that opens port 9000. It is a master process and five sub-processes.

[Root @ alex ~] # Lsof-I: 9000

Command pid user fd type device size/OFF NODE NAME

Php-fpm26340 root 7u IPv4 3342038 0t0 TCP localhost: cslistener (LISTEN)

Php-fpm 26341 apache 0u IPv4 3342038 0t0 TCP localhost: cslistener (LISTEN)

Php-fpm 26342 apache 0u IPv4 3342038 0t0 TCP localhost: cslistener (LISTEN)

Php-fpm 26343 apache 0u IPv4 3342038 0t0 TCP localhost: cslistener (LISTEN)

Php-fpm 26344 apache 0u IPv4 3342038 0t0 TCP localhost: cslistener (LISTEN)

Php-fpm 26345 apache 0u IPv4 3342038 0t0 TCP localhost: cslistener (LISTEN)

Next, we will modify the nginx configuration file nginx. conf so that nginx supports PHP through FastCGI. Delete location ~ \. Php $ {...} Set the ";" comment before the segment.

[Root @ alex] # vim/usr/local/nginx/conf/nginx. conf

# Pass the PHP scriptsto FastCGI server listening on Fig: 9000

#

Location ~ \. Php $ {

Root html;

Fastcgi_pass 127.0.0.1: 9000;

Fastcgi_index index. php;

Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;

Include fastcgi_params;

}

Note: fastcgi_param is the Request Path of the script file. By default, nginx. conf contains the following content:

Fastcgi_param SCRIPT_FILENAME/scripts $ fastcgi_script_name;

You need to modify the/scripts field to $ document_root.

Through the above settings and re-reload the configuration file, nginx will know to pass the PHP file to port 9000 of the Local Machine for processing by fastcgi.

MySQL

--------------------

Install myqlThe software involved includes:

Mysql: mysql client;

Mysql-server: mysql server;

Php-mysql: provides library files for php extension mysql;

Mysql-libs: mysql development kit;

Run the following command for quick installation.

Yum install-y mysql *

Yum install-y php-mysql

[Root @ alex ~] # Rpm-qa | grep mysql

Mysql-5.1.73-3.el6_5.x86_64

Mysql-server-5.1.73-3.el6_5.x86_64

Php-mysql-5.3.3-27.el6_5.x86_64

Mysql-libs-5.1.73-3.el6_5.x86_64

Start the mysql server.

[Root @ alex ~] #/Etc/init. d/mysqld start

[Root @ alex ~] # Lsof-I: 3306

Command pid user fd type device size/OFF NODE NAME

Mysqld 19492 mysql 10u IPv4 2203509 0t0 TCP *: mysql (LISTEN)

[Root @ alex ~] # Ps aux | grep mysql | grep-v grep

Root 2427 0.0 0.2 106060 1316? S May15 0: 00/bin/sh/usr/bin/mysqld_safe -- datadir =/var/lib/mysql -- socket =/var/lib/mysql. sock -- pid-file =/var/run/mysqld. pid -- basedir =/usr -- user = mysql

Mysql 2558 0.0 4.4 584452 22244? Sl May15/usr/libexec/mysqld -- basedir =/usr -- datadir =/var/lib/mysql -- user = mysql -- log-error =/var/lib/mysql/alex. err -- pid-file =/var/run/mysqld. pid -- socket =/var/lib/mysql. sock -- Ports = 3306

It can be seen that the mysql database server has been successfully started and listening to port 3306.

After Mysql is successfully installed, the mysql administrator defaults to the root user. However, unlike the root user in Linux, the initial password for logging on to mysql is blank. When the client connects to mysql, you only need to enter the mysql command. The security is too low. Therefore, it is necessary to use the mysqladmin command to modify the password when the root user connects to the mysql server.

[Root @ alex ~] # Mysqladmin-u root passwordxxxxxxxx

Now use the root account to reconnect.

[Root @ alex ~] # Mysql-u root-p

Enter password:

Welcome to the MySQL monitor. Commands end with; or \ g.

Your MySQL connection id is 18

Server version: 5.1.73 Source distribution

Mysql> show databases; // displays the existing database

+ -------------------- +

| Database |

+ -------------------- +

| Information_schema |

| Mysql |

| Test |

+ -------------------- +

3 rows in set (0.00 sec)

Mysql> use mysql; // open the mysql database

Database changed

Mysql> show tables;

+ --------------------------- +

| Tables_in_mysql |

+ --------------------------- +

| Columns_priv |

| Db |

| Event |

| Func |

..... Omitted .....

| Time_zone_transition |

| Time_zone_transition_type |

| User |

+ --------------------------- +

23 rows in set (0.00 sec)

Create a test file index. php that uses php to connect to mysql in the nginx main directory.

[Root @ localhost html] vim index. php

Echo"
--------------- Mysql ------------- ";

$ Link = mysql_connect ("localhost", "root", "xxxxxx ");

If ($ link) echo "connect to mysql... OK ";

Else echo "connect to mysql... failed ";

Mysql_close ();

?>

Running result:

Browser: http: // ip address/

---------------mysql-------------

connect tomysql..........ok

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.