Php+nginx Environment collocation

Source: Internet
Author: User
Tags epoll install openssl ldap mcrypt mysql client openssl library php source code automake

One. Nginx Installation

Nginx can be installed using the default packages of each platform, this article is to introduce the use of source code compilation installation, including specific compilation parameters information.

Before the official start, the compilation environment GCC g++ Development Library and so on need to be installed in advance, here by default you have installed.

The UBUBTU platform compilation environment can use the following directives

Apt-get Install Build-essentialapt-get Install Libtool

The CentOS Platform compilation environment uses the following directives

Install Make:

Yum-y install gcc automake autoconf libtool make

Install g++:

Yum Install gcc gcc-c++

The following officially begins
---------------------------------------------------------------------------
Generally we need to first install Pcre, Zlib, the former in order to rewrite rewrite, the latter in order to gzip compression.
1. Select the source directory
Can be any directory, this article is selected/USR/LOCAL/SRC

Cd/usr/local/src

2. Installing the Pcre Library
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/Download the latest pcre source package, download the compile and install the Pcre package using the command below:

Cd/usr/local/srcwget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz TAR-ZXVF PCRE-8.34.TAR.GZCD Pcre-8.34./configuremakemake Install

3. Installing the Zlib Library
http://zlib.net/zlib-1.2.8.tar.gz Download the latest zlib source package, download the compile and install the zlib package using the command below:

Cd/usr/local/srcwget HTTP://ZLIB.NET/ZLIB-1.2.8.TAR.GZTAR-ZXVF ZLIB-1.2.8.TAR.GZCD Zlib-1.2.8./configuremakemake Install

4. Install SSL (some VPS does not install SSL by default)

Cd/usr/local/srcwget HTTP://WWW.OPENSSL.ORG/SOURCE/OPENSSL-1.0.1C.TAR.GZTAR-ZXVF openssl-1.0.1c.tar.gz

5. Installing Nginx

Nginx generally has two versions, the stable version and the development version, you can choose one of these two versions according to your purpose, below is the detailed steps of installing nginx into the/usr/local/nginx directory:

Cd/usr/local/srcwget HTTP://NGINX.ORG/DOWNLOAD/NGINX-1.4.2.TAR.GZTAR-ZXVF NGINX-1.4.2.TAR.GZCD nginx-1.4.2./ Configure--sbin-path=/usr/local/nginx/nginx--conf-path=/usr/local/nginx/nginx.conf--pid-path=/usr/local/nginx/ Nginx.pid--with-http_ssl_module--with-pcre=/usr/local/src/pcre-8.34--with-zlib=/usr/local/src/zlib-1.2.8-- With-openssl=/usr/local/src/openssl-1.0.1cmakemake Install

--with-pcre=/usr/src/pcre-8.34 refers to the pcre-8.34 source path.
--with-zlib=/usr/src/zlib-1.2.7 refers to the zlib-1.2.7 source path.

After the installation is successful, the/usr/local/nginx directory is as follows

fastcgi.conf            koi-win             nginx.conf.defaultfastcgi.conf.default    logs                Scgi_paramsfastcgi_params          mime.types          scgi_params.defaultfastcgi_params.default  mime.types.default  uwsgi_paramshtml                    Nginx               uwsgi_params.defaultkoi-utf                 nginx.conf          Win-utf

6. Start
Ensure that the 80 port of the system is not occupied by other programs, run the/usr/local/nginx/nginx command to start Nginx,

Netstat-ano|grep 80

If the results are not found after the execution, the result is ignored this step (Ubuntu must be started with sudo, or can only be run in the foreground)

Sudo/usr/local/nginx/nginx

Open the browser to access the IP of this machine if the browser appears Welcome to nginx! Indicates that Nginx is installed and running successfully.

-----------------------------------------------------
Here Nginx installation is complete, if you just deal with static HTML, you do not have to continue to install

If you need to work with PHP scripts, you also need to install PHP-FPM.

The following installation troubleshooting

Attached: Errors and some help information that may be encountered

1.1 Compilation Pcre Error

libtool:compile:unrecognized option '-dhave_config_h ' libtool:compile:Try ' Libtool--help ' for more information.make[1 ]: * * * [Pcrecpp.lo] Error 1make[1]: Leaving directory '/usr/local/src/pcre-8.34 ' make: * * * [ALL] Error 2

Workaround: Install g++, don't forget to re-configure

Apt-get Install G++apt-get Install Build-essentialmake Clean./configuremake

1.2 Make Error

Make: * * * No rule to do target ' build ', needed by ' default '. Stop: /CONFIGURE:ERROR:SSL modules require the OpenSSL library. You can either does not enable the modules, or install the OpenSSL Libraryinto the system, or build the OpenSSL library stat Ically from the Sourcewith nginx by using--with-openssl= option.

Follow the installation method of step 4th or
Under Ubuntu

Apt-get Install Opensslapt-get Install Libssl-dev

Under CentOS

Yum-y Install OpenSSL Openssl-devel

2.nginx compilation options

Make is used to compile, it reads the instruction from the makefile, and then compiles.

The make install is used to install, and it also reads instructions from makefile and installs to the specified location.

The Configure command is used to detect the target characteristics of your installation platform. It defines all aspects of the system, including Nginx's allowed connection processing methods, such as it detects if you have CC or GCC, does not require CC or GCC, it is a shell script, and at the end of execution it creates a makefile file. The Nginx Configure command supports the following parameters:

  • --prefix=pathDefine a directory that stores files on the server, which is the installation directory of Nginx. Default usage/usr/local/nginx。
  • --sbin-path=pathSets the path of the Nginx executable file, which defaults to prefix/sbin/nginx .
  • --conf-path=pathSet the path to the nginx.conf configuration file. Nginx allows you to start with a different configuration file via the-C option on the command line. The default is prefix/conf/nginx.conf .
  • --pid-path=path  设置nginx.pid文件,将存储的主进程的进程号。安装完成后,可以随时改变的文件名 , 在nginx.conf配置文件中使用 PID指令。默认情况下,文件名 为prefix/logs/nginx.pid.
  • --error-log-path=pathSets the name of the primary error, warning, and diagnostic file. After the installation is complete, you can change the file name at any time, using the Error_log directive in the nginx.conf configuration file. By default, the file name is prefix/logs/error.log .
  • --http-log-path=pathSets the name of the log file for the HTTP server for the primary request. After the installation is complete, you can change the file name at any time, using the Access_log directive in the nginx.conf configuration file. By default, the file name is prefix/logs/access.log .
  • --user=nameThe user who set the Nginx worker process. After the installation is complete, you can change the name of the user directive that is used in the nginx.conf configuration file at any time. The default user name is nobody.
  • --group=nameSets the user group for the Nginx worker process. After the installation is complete, you can change the name of the user directive that is used in the nginx.conf configuration file at any time. The default is non-privileged users.
  • --with-select_module--without-select_module 启用或禁用构建一个模块来允许服务器使用select()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。
  • --with-poll_module--without-poll_moduleenables or disables building a module to allow the server to use the poll () method. The module will be created automatically if the platform does not support Kqueue,epoll,rtsig or/dev/poll.
  • --without-http_gzip_module-Do not compile the response module of the compressed HTTP server. Compiling and running this module requires a zlib library.
  • --without-http_rewrite_moduleThe rewrite module is not compiled. Compiling and running this module requires PCRE library support.
  • --without-http_proxy_module-Do not compile the Http_proxy module.
  • --with-http_ssl_module-Use the HTTPS protocol module. By default, the module is not built. It is necessary to establish and run the OpenSSL library for this module.
  • --with-pcre=path-Set the source path of the Pcre library. The source code of the Pcre Library (version 4.4-8.30) needs to be downloaded and decompressed from the Pcre website. The rest of the work is nginx./configure and make to complete. Regular expressions are used in the location directive and in the Ngx_http_rewrite_module module.
  • --with-pcre-jit-compilation Pcre contains "Just-in-time compilation" (1.1.12, Pcre_jit instructions).
  • --with-zlib=path-Set the source path of the zlib library. To download and unzip from zlib (version 1.1.3-1.2.5). The rest of the work is nginx./configure and make complete. The Ngx_http_gzip_module module requires the use of zlib.
  • --with-cc-opt=parameters-Set additional parameters to be added to the cflags variable. For example, when you use the Pcre library on FreeBSD, you need to use: --with-cc-opt="-I /usr/local/include。 . Add as needed select()支持的文件数量 :--with-cc-opt="-D FD_SETSIZE=2048".
  • --with-ld-opt=parameters-Set additional parameters that will be used during the link. For example, when using the Pcre Library of the system under FreeBSD, you should specify:--with-ld-opt="-L /usr/local/lib".

Typical instances (below in order to demonstrate the need to write on multiple lines, the content needs to be on the same line at execution)

./configure    --sbin-path=/usr/local/nginx/nginx    --conf-path=/usr/local/nginx/nginx.conf    --pid-path= /usr/local/nginx/nginx.pid    --with-http_ssl_module    --with-pcre=. /pcre-4.4    --with-zlib=. /zlib-1.1.3



One. Nginx PHP-FPM installation Configuration

Nginx itself cannot handle PHP, it is just a Web server, when the request is received, if it is a PHP request, then sent to the PHP interpreter processing, and return the results to the client.

Nginx is generally the request to send fastcgi management process processing, FASCGI management process Select the CGI sub-process processing results and return to the Nginx

This article takes php-fpm as an example to explain how to make Nginx support PHP

First, compile and install PHP-FPM

What is PHP-FPM

PHP-FPM is a PHP fastcgi manager that is only for PHP and can be downloaded in http://php-fpm.org/download.

PHP-FPM is actually a patch of PHP source code designed to integrate FASTCGI process management into a PHP package. It must be patch into your PHP source code and can be used after compiling and installing PHP.

The new version of PHP has been integrated PHP-FPM, is no longer a third-party package, recommended to use . PHP-FPM provides a better way to manage the PHP process, can effectively control memory and process, can be smooth overloaded PHP configuration, more than spawn-fcgi has more advantages, so by the official PHP included. In./configure with the –ENABLE-FPM parameter can be opened PHP-FPM, the other parameters are configured PHP, the meaning of the specific options can be viewed here.

Pre-Installation Preparation
Executed under CentOS

Yum-y install gcc automake autoconf libtool makeyum-y install gcc gcc-c++ glibcyum-y install libmcrypt-devel Mhash-deve L libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel GL IBC glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel Curl curl-devel e2fsprogs e2fsprogs-devel krb5 k Rb5-devel libidn libidn-devel OpenSSL openssl-devel

New PHP-FPM installation (recommended installation method)

wget Http://cn2.php.net/distributions/php-5.4.7.tar.gztar zvxf PHP-5.4.7.TAR.GZCD Php-5.4.7./configure--prefix=/usr/local/php--enable-fpm--with-mcrypt--enable-mbstring--disable-pdo--with-curl- -disable-debug--disable-rpath--enable-inline-optimization--with-bz2--with-zlib--enable-sockets--enable-sysvsem --enable-sysvshm--enable-pcntl--enable-mbregex--with-mhash--enable-zip--with-pcre-regex--with-mysql-- With-mysqli--with-gd--with-jpeg-dirmake All install 

old version manual patch PHP-FPM installation (old version of the program has gone, everyone new version of it, here to do a show)
wget http://cn2.php.net/get/php-5.2.17.tar.gz
wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
Tar zvxf php-5.2.17.tar.gz
gzip-cd php-5.2.17-fpm-0.5.14.diff.gz | patch-d php-5.2.17-p1
CD 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-openssl-enable-fpm-enable-mbstring
-with-freetype-dir- With-jpeg-dir-with-png-dir-with-zlib-dir-with-libxml-dir=/usr-enable-xml
-with-mhash-with-mcrypt- Enable-pcntl-enable-sockets  -with-bz2-with-curl-with-curlwrappers
-enable-mbregex-with-gd- Enable-gd-native-ttf-enable-zip-enable-soap-with-iconv-enable-bcmath
-enable-shmop-enable-sysvsem- Enable-inline-optimization-with-ldap-with-ldap-sasl-enable-pdo
-with-pdo-mysql
make all install

Both of these methods can be installed PHP-FPM, after the installation content in the/usr/local/php directory

The above completed the installation of PHP-FPM.

Here are the settings for the PHP-FPM running user

CD/USR/LOCAL/PHPCP Etc/php-fpm.conf.default Etc/php-fpm.confvi etc/php-fpm.conf

Modify
user = Www-data
Group = Www-data

If the Www-data user does not exist, add the Www-data user first
Groupadd Www-data
Useradd-g Www-data Www-data

Second, compile and install Nginx

Then follow Http://www.nginx.cn/install to install Nginx

Third, modify the Nginx configuration file to support PHP-FPM

After Nginx installation is complete, modify nginx config file to nginx.conf

Where the server segment adds the following configuration, note the Red content configuration, otherwise there will be no input file specified. Error

# Pass the PHP scripts to FastCGI server listening on 127.0.0.1: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;
}

Iv. Creating a test PHP file

Creating PHP Files

Create the index.php file under/usr/local/nginx/html, and enter the following:

<?php    Echo phpinfo ();? >

Five, start the service

Start PHP-FPM and Nginx

/USR/LOCAL/PHP/SBIN/PHP-FPM #手动打补丁的启动方式/usr/local/php/sbin/php-fpm Startsudo/usr/local/nginx/nginx

PHP-FPM Close restart See end of article

VI. Browser access

Visit http://Your server ip/index.php, you can see the PHP information.

Errors you may encounter when installing PHP-FPM:

1. Error when PHP configure

Configure:error:XML configuration could not being found

Apt-get Install LIBXML2 Libxml2-dev (ubuntu) yum-y install LIBXML2 libxml2-devel (under CentOS)

2. Please reinstall the BZIP2 distribution

wget HTTP://WWW.BZIP.ORG/1.0.5/BZIP2-1.0.5.TAR.GZTAR-ZXVF bzip2-1.0.5.tar.gzcd Bzip2-1.0.5makemake Install

3. there is a line--with-mysql=/usr in the PHP configuration file.
When installing the prompt:
Configure:error:Cannot find MySQL header files under Yes.
Note that the MySQL client library is not bundled anymore.

This is due to the installation of MySQL when the MySQL header file is not installed, or the path is not specified correctly, PHP can not find the header file caused by the error message.
Workaround.
(1.) Check to see if your system has MySQL header installed
Find/-name Mysql.h
If there is. Please specify--with-mysql=/and your normal path.
If not. Take a look at the next step.
(2.) Redhat Installation
RPM-IVH mysql-devel-4.1.12-1.i386.rpm
(3.) Ubuntu Installation
Apt-get Install Libmysqlclient15-dev
(4.) The last step of PHP configuration option to add--WITH-MYSQL=/USR!

4.No input file specified.

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;
}

5. If PHP Configure is missing the library, you can install the library (Ubuntu)

sudo apt-get install make Bison flex gcc patch autoconf Subversion Locate
sudo apt-get install Libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 LIBCU Rl4-openssl-dev Libpq-dev libpq5 Libsyck0-dev

6. mcrypt.h not found. Please reinstall Libmcrypt

Apt-get Install Libmcrypt-dev

Or

Cd/usr/local/src
wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
TAR-ZXVF libmcrypt-2.5.8.tar.gz
cd/usr/local/src/libmcrypt-2.5.8
./configure--prefix=/usr/local
Make
Make install

7. php-fpm 5.4.7 How do I turn off reboot?

PHP-FPM in PHP 5.4.7 no longer supports commands such as/USR/LOCAL/PHP/SBIN/PHP-FPM (Start|stop|reload) that php-fpm previously had, and signal control is required:

The master process can understand the following signals

INT, term immediately terminate QUIT smooth terminate USR1 reopen log file USR2 Smooth reload all worker processes and reload the configuration and binary modules

Example:

PHP-FPM off:

Kill-int ' Cat/usr/local/php/var/run/php-fpm.pid '

PHP-FPM Restart:

KILL-USR2 ' Cat/usr/local/php/var/run/php-fpm.pid '

To view the number of PHP-FPM processes:

PS aux | Grep-c PHP-FPM

8. Execute PHP at the command line, prompting for the command not found

-bash:/usr/bin/php:no such file or directory

Vi/etc/profile

Add a line of configuration at the bottom of the file
Export Path=/usr/local/php/bin: $PATH

Save exit

Source/etc/profile

Reference:

Http://www.nginx.cn/install

Http://www.nginx.cn/231.html

Http://blog.chinaunix.net/uid-21374062-id-2951960.html




Php+nginx Environment collocation

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.