Linux under the source mode installation Nginx and PHP (FastCGI) (1/2)

Source: Internet
Author: User
Tags bz2 fpm ftp install php php error vars zts zend

Here the CentOS 5.5 under Nginx 1.2.3 and PHP 5.2.17, the other version of the installation method is similar.

First, create Nginx and PHP-specific users and user groups

The code is as follows Copy Code
1.[root@localhost/]# Groupadd www
2.[root@localhost/]# useradd-s/sbin/nologin-m-G www www

Second, the installation and optimization of PHP and PHP-FPM
PHP-FPM is a third party fastcgi process manager, which is developed as a patch in PHP, and it needs to be compiled with the PHP code when it is installed. Because Nginx is a lightweight HTTP Server, you must use a Third-party fastcgi processor to parse PHP.

1. Download and install the PHP dependencies first

The code is as follows Copy Code
1.# download Libmcrypt, two files
2.[root@localhost downloads]# wget-c "http://downloads.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/ Libmcrypt-2.5.8.tar.gz?ts=1347243907&use_mirror=ncu "
3.[root@localhost downloads]# wget-c "http://downloads.sourceforge.net/project/mcrypt/MCrypt/2.6.8/ MCRYPT-2.6.8.TAR.GZ?R=&TS=1347244005&USE_MIRROR=NCHC "
4.# Download Libmhash
5.[root@localhost downloads]# wget-c "http://downloads.sourceforge.net/project/mhash/mhash/0.9.9.9/ Mhash-0.9.9.9.tar.bz2?ts=1347245105&use_mirror=ncu "
6.
7.# First install Libmcrypt
8.[root@localhost downloads]# tar zxvf libmcrypt-2.5.8.tar.gz
9.[root@localhost downloads]# CD libmcrypt-2.5.8
10.[root@localhost libmcrypt-2.5.8]#./configure
11.[root@localhost libmcrypt-2.5.8]# make && make install
12.[root@localhost libmcrypt-2.5.8]# Ldconfig
13.[root@localhost libltdl]# CD libltdl/
14.[root@localhost libltdl]#./configure--enable-ltdl-install
15.[root@localhost libltdl]# make && make install
16.
17.# Next install Mhash, because mcrypt need mhash dependencies
18.[root@localhost libltdl]# CD. /.. /
19.[root@localhost downloads]# tar jxvf mhash-0.9.9.9.tar.bz2
20.[root@localhost downloads]# CD mhash-0.9.9.9
21.[root@localhost mhash-0.9.9.9]#./configure
22.[root@localhost mhash-0.9.9.9]# make && make install
23.
24.# Final Installation MCrypt
25.[root@localhost mhash-0.9.9.9]# CD.
26.[root@localhost downloads]# tar xzxvf mcrypt-2.6.8.tar.gz
27.[root@localhost downloads]# CD mcrypt-2.6.8
28.# add Mhash to the LD library path
29.[root@localhost mcrypt-2.6.8]# Export Ld_library_path=/usr/local/lib
30.[root@localhost mcrypt-2.6.8]# Ldconfig
31.[root@localhost mcrypt-2.6.8]#./configure
32.[root@localhost mcrypt-2.6.8]# make && make install
33.[root@localhost mcrypt-2.6.8]# CD.

2. Download PHP and the corresponding version of PHP-FPM

The code is as follows Copy Code
1.[root@localhost downloads]# wget-c Http://www.php.net/get/php-5.2.17.tar.gz/from/this/mirror
2.# in the Http://php-fpm.org/downloads directory to select the same as the installation of PHP version of PHP-FPM, otherwise it will cause compatibility problems
3.[root@localhost downloads]# wget-c http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
4.# if it is a 5.2.x version, you also need to download the PHP hash conflict patch, 5.3+ do not need to download the
5.[root@localhost downloads]# wget-c https://github.com/laruence/laruence.github.com/raw/master/ Php-5.2-max-input-vars/php-5.2.17-max-input-vars.patch

3. Install PHP, PHP-FPM and corresponding patches

The code is as follows Copy Code

[Root@localhost downloads]# tar xzvf php-5.2.17.tar.gz
# write PHP-FPM to the PHP source in the form of patches
[root@localhost downloads]# GZIP-CD php-5.2.17-fpm-0.5.14.diff.gz | Patch-d PHP-5.2.17-P1
# Write hash conflict patch, PHP 5.3+ does not need
[Root@localhost downloads]# patch-d Php-5.2.17-p1 < Php-5.2.17-max-input-vars.patch
[Root@localhost downloads]# CD php-5.2.17
# here will install PHP to the/usr/local/php directory, citing the MySQL installation directory for/usr/local/mysql, here needs to be modified according to the actual situation
[Root@localhost php-5.2.17]#./configure--prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc-- With-mysql=/usr/local/mysql--with-mysqli=/usr/local/mysql/bin/mysql_config--with-iconv-dir=/usr/local-- With-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib--with-libxml-dir=/usr--enable-xml-- Enable-discard-path--enable-safe-mode--enable-bcmath--enable-shmop--enable-sysvsem--enable-inline-optimization --with-curl--with-curlwrappers--enable-mbregex--enable-fastcgi--enable-fpm--enable-force-cgi-redirect-- Enable-mbstring--with-mcrypt--with-gd--enable-gd-native-ttf--with-openssl--with-mhash-- Enable-sockets--with-ldap--WITH-LDAP-SASL--with-xmlrpc--enable-zip--enable-soap
# If you appear Thank for using PHP. The configuration is successful, otherwise the corresponding dependent libraries will be installed according to the error
[Root@localhost php-5.2.17]# make zend_extra_libs= '-liconv '
[Root@localhost php-5.2.17]# make install
# if it is php5.2.17, the version shown after installing the hash conflict patch is PHP 5.2.17P1
# Copy the default PHP configuration file
[Root@localhost php-5.2.17]# CP Php.ini-dist/usr/local/php/etc/php.ini
[Root@localhost php-5.2.17]# CD ...

4. Compile and install PHP extension module (choose according to the actual situation)

The code is as follows Copy Code
1.# set PHP root directory, note switch to the actual PHP directory
2.[root@localhost downloads]# export php_home= "/usr/local/php"
3.
4.# installation <a href= "Http://pecl.php.net/package/PDO_MYSQL" target= "_blank" >pdo_mysql</a>
5.[root@localhost downloads]# wget-c http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
6.[root@localhost downloads]# tar zxvf pdo_mysql-1.0.2.tgz
7.[root@localhost downloads]# CD pdo_mysql-1.0.2
8.# Note Switch to the actual MySQL directory
9.[root@localhost pdo_mysql-1.0.2]# $PHP _home/bin/phpize
10.[root@localhost pdo_mysql-1.0.2]#./configure--with-php-config= $PHP _home/bin/php-config--with-pdo-mysql=/usr/ Local/mysql
11.[root@localhost pdo_mysql-1.0.2]# make && make install
12.[root@localhost pdo_mysql-1.0.2]# CD.
13.
14.# installation Eaccelerator
15.[root@localhost downloads]# wget-c https://github.com/downloads/eaccelerator/eaccelerator/eaccelerator-0.9.6.1.tar.bz2
16.[root@localhost downloads]# tar jxvf eaccelerator-0.9.6.1.tar.bz2
17.[root@localhost downloads]# CD eaccelerator-0.9.6.1
18.[root@localhost eaccelerator-0.9.6.1]# $PHP _home/bin/phpize
19.[root@localhost eaccelerator-0.9.6.1]#./configure--enable-shared--with-php-config= $PHP _home/bin/php-config
20.[root@localhost eaccelerator-0.9.6.1]# make && make install
21.[root@localhost eaccelerator-0.9.6.1]# CD.

Next, modify the php.ini file to enable the installation of Third-party modules

The code is as follows Copy Code
1.# Open php.ini, find Extension_dir
2.[root@localhost downloads]# vim $PHP _home/etc/php.ini
3.# sets Extension_dir to the new directory/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
4.extension_dir= "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
5.# Increase Extension
6.extension=eaccelerator.so
7.extension=pdo_mysql.so
8.##-------Press the Shift+g key to skip to the end of the configuration file, plus eaccelerator configuration information:
9.[eaccelerator]
10.# shared memory, Unit m
11.eaccelerator.shm_size= "32"
12.eaccelerator.cache_dir= "/tmp/eaccelerator"
13.eaccelerator.enable= "1"
14.eaccelerator.optimizer= "1"
15.eaccelerator.check_mtime= "1"
16.eaccelerator.debug= "0"
17.eaccelerator.filter= ""
18.eaccelerator.shm_ttl= "0"
19.eaccelerator.shm_prune_period= "0"
20.eaccelerator.shm_only= "0"
21st.
22.[root@localhost downloads]# Mkdir/tmp/eaccelerator
23.[root@localhost downloads]# chmod 777/tmp/eaccelerator
24.# this time running PHP should be able to see Eaccelerator has been loaded successfully
25.[root@localhost downloads]# $PHP _home/bin/php-version
26.PHP 5.2.17p1 (CLI) (built:sep 10 2012 18:41:32)
27.Copyright (c) 1997-2010 the PHP Group
28.Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
With Eaccelerator v0.9.6.1, the Copyright (c) 2004-2010 eaccelerator, by Eaccelerator

5. Configuration and Optimization php-fpm
PHP-FPM The default configuration file is in $php_home/etc/php-fpm.conf, the following is a description of the commonly used configuration items:

1. <value name= "listen_address" >127.0.0.1:9000</value> to configure the IP address and port that the fastcgi process listens to, and the default is 127.0.0.1:9000
2. <value name= "display_errors" >0</value> to configure whether to display PHP error messages, the default is 0 does not display, set to 1 display information
3. <value name= " The user ">www</value><value name=" group >www</value> is used to set up users and groups of users who are running the fastcgi process. The user and user groups specified here must be the same as specified in the Nginx configuration file.
4. <value name= "Max_children" >128</value> the number of processes used to set fastcgi. According to official recommendations, servers less than 2G of memory can open only 64 processes, and 4GB or more memory servers can start 256 processes.
5. <value name= "Request_terminate_timeout" >0s</value> the time to execute the script, by default, 0 indicates infinite execution.
6. <value name= "Rlimit_files" >65535</value> set php-fpm the limit on opening file descriptors, which defaults to 1024. This value must be associated with the number of open files for the Linux kernel, and if you set this value to 65535 you must perform ULIMIT-HSN 65535 on the Linux command line.
7. <value name= "Max_requests" >1024</value> sets the maximum number of requests per children to be closed automatically, and the default setting is 500.
8. <value name= "Allowed_clients" >127.0.0.1</value> set the IP address that allows access to the FASTCGI process parser, if the IP address is not specified here, You will not be able to accept the PHP parsing request that nginx forwarded over.
6. Start fastcgi process

1.[root@localhost/]# Ulimit-shn 65535
2.# start is started, reload can reload php.ini configuration information without interruption of process
3.[root@localhost/]#/usr/local/php/sbin/php-fpm Start
The PHP-FPM has many parameters in addition to start, such as stop for forced termination, quit for smooth termination, restart restart process, reload reload PHP configuration file php.ini without terminating PHP.

Iii. installation of Nginx
1. First install the Pcre-devel library required by Nginx

The purpose of installing the Pcre Library is to allow Nginx to support HTTP rewrite modules. The installation is simple and the process is as follows:

1.# Download installs the newest Pcre library

The code is as follows Copy Code
2.[root@localhost downloads]# wget-c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.tar.gz
3.[root@localhost downloads]# tar xzvf pcre-8.31.tar.gz
4.[root@localhost downloads]# CD pcre-8.31
5.[root@localhost pcre-8.31]#./configure
6.[root@localhost pcre-8.31]# make && make install
7.[root@localhost pcre-8.31]# CD.

2. Installation Tcmalloc upgrade Nginx performance (optional)

Tcmalloc, called Thread-caching Malloc, is a member of the Open Source tool google-perftools Google developed. Compared to the standard GLIBC Library's malloc, the Tcmalloc library is much higher in memory allocation efficiency and speed.

If the 64-bit system is the first need to install the Libunwind library, 32-bit systems do not need to install.

The code is as follows Copy Code
1.[root@localhost downloads]# wget-c http://download.savannah.gnu.org/releases/libunwind/libunwind-1.0.1.tar.gz
2.[root@localhost downloads]# tar xzvf libunwind-1.0.1.tar.gz
3.[root@localhost downloads]# CD libunwind-1.0.1
4.[root@localhost libunwind-1.0.1]# cflags=-fpic./configure
5.[root@localhost libunwind-1.0.1]# make Cflags=-fpic
6.[root@localhost libunwind-1.0.1]# make Cflags=-fpic Install
7.[root@localhost libunwind-1.0.1]# CD.

Then install Google-perftools, which can be downloaded from https://code.google.com/p/gperftools/downloads/list.

The code is as follows Copy Code
1.[root@localhost downloads]# wget-c https://gperftools.googlecode.com/files/gperftools-2.0.tar.gz
2.[root@localhost downloads]# tar xzvf gperftools-2.0.tar.gz
3.[root@localhost downloads]# CD gperftools-2.0
4.[root@localhost gperftools-2.0]#./configure
5.[root@localhost gperftools-2.0]# make && make install
6.[root@localhost gperftools-2.0]# echo "/usr/local/lib" >/etc/ld.so.conf.d/usr_local_lib.conf
7.[root@localhost gperftools-2.0]# Ldconfig

3. Install Nginx

The code is as follows Copy Code
1.[root@localhost downloads]# wget-c http://nginx.org/download/nginx-1.2.3.tar.gz
2.[root@localhost downloads]# tar xzvf nginx-1.2.3.tar.gz
3.[root@localhost downloads]# CD nginx-1.2.3
4.[root@localhost nginx-1.2.3]#./configure--prefix=/usr/local/nginx--user=www--group=www--with-http_stub_status _module--with-http_ssl_module--with-google_perftools_module
5.[root@localhost nginx-1.2.3]# make && make install

Nginx is relatively simple to install and, by default, compiled and installed nginx contains most of the available modules. You can view information about individual configuration items through the "./configure--help" option, or you can view the Nginx official document Http://nginx.org/en/docs/install.html.

Home 1 2 last page

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.