NGINX_PHP_PHP-FPM Website Environment Deployment Manual

Source: Internet
Author: User
Tags fpm gettext mcrypt pear php memcached php mongodb zts openldap

1. Download the stable version of the source code installation files:


Nginx-1.6.2.tar.gz

http://nginx.org/download/


Php-5.5.23.tar.gz

http://php.net/downloads.php


PCRE (make Nginx support HTTP rewrite module): pcre-8.35.tar.gz

Ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/


Tcmalloc can be used to optimize the performance of Nginx, need the following two packages, generally configured on the physical machine to improve performance, if the virtual machine do not configure this:

Libunwind-1.1.tar.gz (only required for 64-bit systems)

http://ftp.yzu.edu.tw/nongnu/libunwind/

Gperftools-2.1.tar.gz

http://download.csdn.net/detail/chunmanchunman/8201565


Libmcrypt-2.5.8.tar.gz

http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/


Mcrypt-2.6.8.tar.gz

http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/


Jpegsrc.v9.tar.gz

http://www.ijg.org/files/


Libpng-1.6.17.tar.gz

http://sourceforge.net/projects/libpng/files/libpng16/1.6.17/


Freetype-2.5.5.tar.gz

http://ftp.twaren.net/Unix/NonGNU/freetype/


Mhash-0.9.9.9.tar.gz

http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/





2. Compile and install Nginx and configure:


1) Check the dependent packages and do not install them manually:

Make gcc gcc-c++ autoconf libjpeg-turbo libjpeg-turbo-devel libpng

Libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib

Zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses

Ncurses-devel Curl Curl-devel e2fsprogs e2fsprogs-devel krb5-libs

Krb5-devel libidn libidn-devel OpenSSL openssl-devel OpenLDAP

Openldap-devel openldap-clients openldap-servers Portreserve

GD Gd-devel


# yum Install e2fsprogs-devel openldap-devel openldap-clients

Openldap-servers Portreserve GD Gd-devel


2) Modify the system kernel and resource limits to optimize Nginx performance:

# vi/etc/sysctl.conf

Net.ipv4.tcp_max_tw_buckets = number of 10000 # timewait

Net.ipv4.ip_local_port_range = 1024 65000 # allows the system to open the port range

Net.ipv4.tcp_tw_recycle = 1 # Enable timewait Fast recycle, but will cause the Android phone connection daemon to time out frequently, so set to 0 on the app server

Net.ipv4.tcp_tw_reuse = 1 # Enable timewait reuse with new TCP

Net.ipv4.tcp_syncookies = 1 # turn on SYN Cookies

Net.core.somaxconn = 262144 # system capable of initiating TCP connections simultaneously

Net.core.netdev_max_backlog = 262144 #网口比内核处理数据包快时, maximum number of packets that are allowed to be sent to the queue

Net.ipv4.tcp_max_orphans = 262144 # Sets the maximum number of TCP sockets that are not associated to any one user file handle

Net.ipv4.tcp_max_syn_backlog = 262144

Net.ipv4.tcp_synack_retries = 1

Net.ipv4.tcp_syn_retries = 1

Net.ipv4.tcp_fin_timeout = 1

Net.ipv4.tcp_keepalive_time = 30

#/sbin/sysctl-p


# vi/etc/security/limits.conf

* Soft Nofile 10240

* Hard Nofile 65536


3) Compile and install pcre-8.35, the system comes with a version too low:

# CD/USR/LOCAL/SRC

# tar XZVF pcre-8.35.tar.gz

# CD pcre-8.35

#./configure--prefix=/usr/local/pcre

# Make && make install


4) Compile and install libunwind-1.1 and gperftools-2.1 if the virtual machine does not have to install both packages:

# tar XZVF libunwind-1.1.tar.gz

# CD libunwind-1.1

# cflags=-fpic./configure

# Make Cflags=-fpic

# Make Cflags=-fpic Install


# tar XZVF gperftools-2.1.tar.gz

# CD gperftools-2.1

#./configure

# Make && make install


# echo "/usr/local/lib" >/etc/ld.so.conf.d/usr_local_lib.conf

#/sbin/ldconfig



5) Compile and install Nginx:


# CD/USR/LOCAL/SRC

# tar XZVF nginx-1.6.2.tar.gz

# CD nginx-1.6.2

# VI AUTO/CC/GCC

# Debug # comment out these two lines, turn off debug mode

# cflags= "$CFLAGS-G"

#./configure--prefix=/usr/local/nginx--with-pcre=/usr/local/src/pcre-8.35--with-http_ssl_module--with-http_ Realip_module--with-http_image_filter_module--with-http_gzip_static_module--with-http_sub_module--with-http_ Stub_status_module


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

# # The following configuration method is to enable TCMALLOC on the physical machine to optimize nginx performance

#./configure--user=nginxphp--group=nginxphp--prefix=/usr/local/nginx--with-pcre=/usr/local/src/pcre-8.35-- With-google_perftools_module--with-http_ssl_module--with-http_realip_module--with-http_image_filter_module-- With-http_gzip_static_module--with-http_sub_module--with-http_stub_status_module

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

(Note:--with-pcre points to the Pcre source extracted directory)


# make

# make Install


# Vi/etc/profile

Export Path=/usr/local/nginx/sbin: $PATH


# Source/etc/profile


# nginx-v

Nginx version:nginx/1.6.2

Built by GCC 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

TLS SNI Support Enabled

Configure arguments:--prefix=/usr/local/nginx--with-pcre=/usr/local/src/pcre-8.35--with-http_ssl_module-- With-http_realip_module--with-http_image_filter_module--with-http_gzip_static_module--with-http_sub_module-- With-http_stub_status_module



6) Add the thread directory for Google-perftools Tcmalloc, this step is to configure the Tcmalloc feature enabled, without the configuration if Tcmalloc is not enabled:


# Mkdir/tmp/tcmalloc

# chmod 0777/tmp/tcmalloc


7) Modify Nginx configuration file, this step is configured to enable the Tcmalloc feature, if not enabled Tcmalloc is not configured:


# vi/usr/local/nginx/conf/nginx.conf

User nobody nobody;


#pid Logs/nginx.pid;

Google_perftools_profiles/tmp/tcmalloc;


8) Start, stop, hot restart Nginx service:


#/usr/local/nginx/sbin/nginx


# Ps-ef|grep Nginx

Root 30048 1 0 18:18? 00:00:00 Nginx:master Process/usr/local/nginx/sbin/nginx

nginxphp 30049 30048 0 18:18? 00:00:00 Nginx:worker Process

# lsof-n |grep Tcmall

Nginx 30049 nginxphp 9w REG 8,3 0 2106703/tmp/tcmalloc.30049


# Kill-quit Master_pid

# kill-quit ' Cat/usr/local/nginx/logs/nginx.pid '


# kill-hup ' Cat/usr/local/nginx/logs/nginx.pid '


6) Add Nginx for system service:

# Vi/etc/rc.d/init.d/nginx


#!/bin/bash


# nginx Startup script for the Nginx HTTP Server

# It is v.0.0.2 version.

# Chkconfig:-85 15

# Description:nginx is a high-performance Web and proxy server.

# It has a lot of features, but it's not for everyone.

# Processname:nginx

# Pidfile:/var/run/nginx.pid

# config:/usr/local/nginx/conf/nginx.conf


Nginxd=/usr/local/nginx/sbin/nginx

Nginx_config=/usr/local/nginx/conf/nginx.conf

Nginx_pid=/usr/local/nginx/logs/nginx.pid


Retval=0

Prog= "Nginx"


# Source function library.

. /etc/rc.d/init.d/functions


# Source Networking configuration.

. /etc/sysconfig/network


# Check that networking are up.

[${networking} = "No"] && exit 0

[-X $nginxd] | | Exit 0


# Start Nginx daemons functions.


Start () {

If [-e $nginx _pid];then

echo "Nginx already running ..."

Exit 1

Fi

Echo-n $ "Starting $prog:"

Daemon $nginxd-C ${nginx_config}

Retval=$?

Echo

[$RETVAL = 0] && Touch/var/lock/subsys/nginx

Return $RETVAL

}


# Stop Nginx daemons functions.


Stop () {

Echo-n $ "Stopping $prog:"

Killproc $nginxd

Retval=$?

Echo

[$RETVAL = 0] && rm-f/var/lock/subsys/nginx/usr/local/nginx/logs/nginx.pid

}


Reload () {

Echo-n $ "Reloading $prog:"

#kill-hup ' Cat ${nginx_pid} '

Killproc $nginxd-hup

Retval=$?

Echo

}


# See how we were called.


Case "$" in

Start

Start

;;

Stop

Stop

;;

Reload

Reload

;;

Restart

Stop

Start

;;

Status

Status $prog

Retval=$?

;;

*)


echo $ "Usage: $prog {start|stop|restart|reload|status|help}"

Exit 1

Esac


Exit $RETVAL



# chmod 775/etc/rc.d/init.d/nginx

# chkconfig Nginx on

# chkconfig--level off


3. Compile and install PHP (after version 5.3 php-fpm is integrated into the PHP kernel) and configure:


1) Check the dependent packages and do not install them manually:

Make gcc gcc-c++ libxml2 libxml2-devel autoconf Libjpeg-turbo

Libjpeg-turbo-devel libpng libpng-devel FreeType freetype-devel

Zlib zlib-devel glibc glibc-devel glib2 glib2-devel


# yum Install Libjpeg-turbo-devel


2) Update several PHP-dependent software source packages:

# CD/USR/LOCAL/SRC

# tar XZVF jpegsrc.v9.tar.gz

# CD Jpeg-9

#./configure--prefix=/usr/local/jpeg9--enable-shared--enable-static

# Make && make install


# tar XZVF libpng-1.6.17.tar.gz

# CD libpng-1.6.17

#./configure--prefix=/usr/local/png--enable-shared

# Make && make install


# tar XZVF freetype-2.5.5.tar.gz

# CD freetype-2.5.5

#./configure--prefix=/usr/local/freetype--enable-shared

# Make && make install


# tar XZVF libmcrypt-2.5.8.tar.gz

# CD libmcrypt-2.5.8

#./configure--prefix=/usr/local/libmcrypt--enable-shared

# Make && make install


# tar XZVF mhash-0.9.9.9.tar.gz

# CD mhash-0.9.9.9

#./configure--prefix=/usr/local/mhash--enable-shared

# Make && make install



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

# # This test environment is not installed successfully, there will be a chance to test again

# tar XZVF mcrypt-2.6.8.tar.gz

# CD mcrypt-2.6.8

#./configure--prefix=/usr/local/mcrypt

--with-libmcrypt-prefix=/usr/local/libmcrypt--enable-shared

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


3) Compile and install PHP and PHP-FPM:


# CD/USR/LOCAL/SRC

# tar XZVF php-5.5.23.tar.gz

# CD php-5.5.23


(Note: The following compilation method is for MySQL server on another server)

#./configure--prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc

--with-mysql=mysqlnd--with-mysqli=mysqlnd--with-pdo-mysql=mysqlnd

--with-jpeg-dir=/usr/local/jpeg9

--with-png-dir=/usr/local/png--with-freetype-dir=/usr/local/freetype

--with-mhash=/usr/local/mhash--with-mcrypt=/usr/local/libmcrypt--WITH-GD

--with-iconv--with-zlib--with-openssl--with-xmlrpc--with-gettext--with-curl

--without-pear--enable-fpm--enable-bcmath--enable-shmop--enable-sysvsem

--enable-mbstring--enable-ftp--enable-gd-native-ttf--enable-pcntl

--enable-sockets--enable-zip--enable-soap--enable-session--enable-xml

--enable-inline-optimization--enable-mbregex


http://www.cnxct.com/some-errors-on-compile-php-5-3-8-with-pdo_mysql-and-mysqlnd/


(Note: PHP5.4 and above version of the default connection MySQL driver is mysqlnd, do not need to add with-mysql at compile time, with-mysqli, with-pdo-mysql compile parameters also line)


(Description: This compilation method is for MySQL on this machine, only used in some cases, such as an integrated installation environment, the database and application server are installed together)

#./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-mysql-sock=/tmp/mysqld.sock--with-jpeg-dir=/usr/local/jpeg9

--with-png-dir=/usr/local/png--with-freetype-dir=/usr/local/freetype

--with-mhash=/usr/local/mhash--with-mcrypt=/usr/local/libmcrypt--WITH-GD

--with-iconv--with-zlib--with-openssl--with-xmlrpc--with-gettext--with-curl

--without-pear--enable-fpm--enable-bcmath--enable-shmop--enable-sysvsem

--enable-mbstring--enable-ftp--enable-gd-native-ttf--enable-pcntl

--enable-sockets--enable-zip--enable-soap--enable-session--enable-xml

--enable-inline-optimization--enable-mbregex


# make

# make Install


4) Configure PHP and PHP-FPM:

# cp/usr/local/src/php-5.5.23/php.ini-production

/usr/local/php/etc/php.ini


# Vi/usr/local/php/etc/php.ini

Disable_functions =

Passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_ Restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_ Free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_ ERROR,POSIX_GETCWD, Posix_getegid,posix_geteuid,posix_getgid, Posix_getgrgid,posix_getgrnam,posix_getgroups, Posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, Posix_getppid,posix_getpwnam,posix_getpwuid, Posix_ Getrlimit, Posix_getsid,posix_getuid,posix_isatty, Posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_ Setgid, Posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

(Description: List the functions php can disable, if some programs need to use this function, you can delete, cancel disable)


Date.timezone = PRC--Set time zone

expose_php = Off-Suppresses the display of PHP version information

Short_open_tag = on-support PHP short tag


# Cp/usr/local/php/etc/php-fpm.conf.default

/usr/local/php/etc/php-fpm.conf


# vi/usr/local/php/etc/php-fpm.conf

[Global]

PID = Run/php-fpm.pid

[WWW]

user = Nobody

Group = Nobody


5) Start, stop PHP-FPM:


#/USR/LOCAL/PHP/SBIN/PHP-FPM


6) Configure PHP-FPM for system service:

# CP/USR/LOCAL/SRC/PHP-5.5.23/SAPI/FPM/INIT.D.PHP-FPM

/etc/init.d/php-fpm

# chmod +X/ETC/INIT.D/PHP-FPM

# Chkconfig PHP-FPM on

# chkconfig--level php-fpm off

# service PHP-FPM Start


4. Configure Nginx to support PHP:


User nobody nobody; --Integrated environment This user name and group should be the same as the PHP-FPM

HTTP {

server {

Location/{

root HTML;

Index index.php index.html index.htm; --Increase index.php

}

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: To change to $document_root or absolute path after script_filename)



# vi/usr/local/nginx/html/index.php

<?php

Phpinfo ();

?>



5. In the browser access: http://192.168.1.12, see the following information, indicating that the configuration was successful:


http://zyan.cc/nginx_php_v7/


Http://www.linuxidc.com/Linux/2014-06/103266p2.htm



6. Configure the PHP memcached extension:


1) Download the source code installation file:


File version: memcache-2.2.7.tgz (Stable version) (older extensions, unused)

: Http://pecl.php.net/package/memcache


File version: libmemcached-1.0.18.tar.gz (compare new extensions, use this)

: Https://launchpad.net/libmemcached/+download


File version: Memcached-2.2.0.tgz (for use with above)

: http://pecl.php.net/package/memcached




# # Optional Php5&jquery-based memcached visual management monitoring Tool

File version: memadmin-1.0.12.tar.gz

: http://www.junopen.com/memadmin/


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

2) Install PHP memcache extension (This may be used if there is an older environment)


# CD/USR/LOCAL/SRC

# tar XZVF memcache-2.2.7.tgz

# CD memcache-2.2.7

#/usr/local/php/bin/phpize

#./configure--with-php-config=/usr/local/php/bin/php-config

--enable-memcache

# make

# make Install

Installing Shared extensions:

/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/

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



2) Install PHP memcached extensions:


# CD/USR/LOCAL/SRC

# tar XZVF libmemcached-1.0.18.tar.gz

# CD libmemcached-1.0.18

#./configure--prefix=/usr/local/libmemcached

# make

# make Install


# tar XZVF memcached-2.2.0.tgz

# CD memcached-2.2.0

#/usr/local/php/bin/phpize

#./configure--with-php-config=/usr/local/php/bin/php-config

--with-libmemcached-dir=/usr/local/libmemcached--enable-memcached

# make

# make Install

Installing Shared extensions:

/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/



3) Install Memcached Visual Management Monitoring tool, Optional:


# CD/USR/LOCAL/SRC

# tar XZVF memadmin-1.0.12.tar.gz

# MV Memadmin/usr/local/nginx/html/


4) Enable PHP memcached dynamic extension:


# Vi/usr/local/php/etc/php.ini


Extension=memcached.so


#extension =memcache.so


5) Reload the PHP-FPM service process so that the newly added memcached dynamic extension takes effect:


# Service PHP-FPM Reload




7. Configure the PHP MongoDB extension:


1) Download the source code installation file:


File version: mongo-1.6.10.tgz (Stable version)

: Http://pecl.php.net/package/mongo


2) Install the PHP mongodb extension:


# CD/USR/LOCAL/SRC

# tar XZVF mongo-1.6.10.tgz

# CD mongo-1.6.10

#/usr/local/php/bin/phpize

#./configure--with-php-config=/usr/local/php/bin/php-config--enable-mongo

# make

# make Install

Installing Shared extensions:

/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/


# ls/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/

memcached.so mongo.so Opcache.a opcache.so


3) enable the MongoDB dynamic extension of PHP:


# Vi/usr/local/php/etc/php.ini


Extension=mongo.so


4) Reload the PHP-FPM service process so that the newly added MongoDB dynamic extension takes effect:


# Service PHP-FPM Reload


This article from the "Technical Backup" blog, reproduced please contact the author!

NGINX_PHP_PHP-FPM Website Environment Deployment Manual

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.