Php7+nginx Configuration and installation Tutorial detailed _php example

Source: Internet
Author: User
Tags fpm openssl php session php error php error log php script unpack file permissions

The following cloud-Habitat community Small series of Php7+nginx configuration and installation of the tutorial to share to everyone, for your reference, this article is not good to write please forgive me.

System environment: centos6.5 x64

Software version: nginx-1.10.0 php-7.0.6

Install Nginx

Nginx Official website: http://nginx.org/

To install some of the components of a compilation dependency first

Copy Code code as follows:

Yum install pcre pcre-devel OpenSSL openssl-devel-y

1, unpack the package

Copy Code code as follows:

Tar XF nginx-1.10.0.tar.gz
CD nginx-1.10.0

2. Pre-compiling configuration parameters

Copy Code code as follows:

./configure--user=www \
--GROUP=WWW \
--prefix=/data/server/nginx \
--with-http_stub_status_module \
--without-http-cache \
--with-http_ssl_module \
--with-http_gzip_static_module

3. Perform compilation

Copy Code code as follows:

Make && make install

4, replace the configuration file

Nginx.conf

User www www.
Worker_processes 1;
Error_log/u01/data/log/nginx/error.log Crit;
Pid/u01/data/server/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors the can is opened by this process.
Worker_rlimit_nofile 65535;  events {use Epoll; worker_connections 65535} http {include mime.types; Default_type Application/octet-stream; #charset
gb2312;
Server_names_hash_bucket_size 128;
Client_header_buffer_size 32k;
Large_client_header_buffers 4 32k;
Client_max_body_size 8m;
Sendfile on;
Tcp_nopush on;
Keepalive_timeout 60;
Tcp_nodelay on;
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Fastcgi_buffer_size 64k;
Fastcgi_buffers 4 64k;
Fastcgi_busy_buffers_size 128k;
Fastcgi_temp_file_write_size 128k;
gzip on;
Gzip_min_length 1k;
Gzip_buffers 4 16k;
Gzip_http_version 1.0;
Gzip_comp_level 2;
Gzip_types text/plain application/x-javascript text/css application/xml;
Gzip_vary on;
#limit_zone crawler $binary _remote_addr 10m; Log_format Main ' $reMote_addr-"$request _time" [$time _local] "$request" "$status" $body _bytes_sent "$http _referer" "" $http _user_agent "
$http _x_forwarded_for '; Log_format ' $remote _addr-$remote _user [$time _local] "$request" "$status $body _bytes_sent" $http _referer "" $http _us
Er_agent "$http _x_forwarded_for" $request _time "; include/u01/alidata/server/nginx/conf/vhosts/*.conf;

Virtual Host configuration file template

server {
listen 8080;
server_name localhost;
Index index.html index.htm index.php;
root/u01/data/www;
Location ~. *\. (PHP|PHP5) $
{
Fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
Fastcgi_index index.php;
Include fastcgi.conf;
}
Location ~. *\. (gif|jpg|jpeg|png|bmp|swf) $
{
expires 30d;
}
Location ~. *\. (JS|CSS)? $
{
expires 1h;
}
# # #this is to use open website Lianjie like on apache##
location/{
if (!-e $request _filename) {
rewrite ^ (. *) $/index.php?s=$1 last;
break;
}
}
Location ~/.svn/{
deny all;
}
# # #end # #
Access_log/u01/data/log/nginx/access/test.log main;
}

5. Provide nginx startup script

#!/bin/bash # Nginxd=/u01/data/server/nginx/sbin/nginx nginx_config=/u01/data/server/nginx/conf/nginx.conf nginx_ Pid=/u01/data/server/nginx/logs/nginx.pid retval=0 prog= "Nginx" [-X $nginxd] | |
Exit 0 # Start Nginx daemons functions. Start () {if [e $nginx _pid] && netstat-tunpl | grep nginx &>/dev/null;then Echo Nginx already running
... "Exit 1 fi echo-n $" Starting $prog! "$nginxd-C ${nginx_config} retval=$?
echo [$RETVAL = 0] && touch/var/lock/nginx return $RETVAL} # Stop Nginx daemons functions. Stop () {echo-n $ "stopping $prog!" $nginxd-S stop retval=$? Echo [$RETVAL = 0] && rm-f/var/lock/nginx} # RE
Load Nginx service functions. Reload () {echo-n $ "reloading $prog!" Stop () {echo-n $ stopping $prog! "$nginxd-S stop retval=$? Echo [$RETVAL = 0]
&& rm-f/var/lock/nginx} # Reload Nginx service functions. Reload () {echo-n $ "reloading $prog!" #kill-hup ' cat ${nginx_pid} ' $nginxd-S reload retval=$? Echo} # How we were called.
Case "in Start" start;;
stop) stop;;
reload) reload;;
restart) stop start;; * echo $ "Usage: $prog {start|stop|restart|reload|help}" Exit 1 Esac Exit $RETVAL

You just need to modify the program path to use it immediately.

Install PHP7

PHP Website: http://php.net/

PHP Extensions: http://pecl.php.net/

Install some build-dependent builds first

Yum-y install gcc gcc-c++ gcc-g77 make libtool autoconf patch unzip automake libxml2 libxml2-devel ncurses ncurses-devel Libtool-ltdl-devel Libtool-ltdl libmcrypt libmcrypt-devel libpng libpng-devel libjpeg-devel OpenSSL Openssl-devel Curl Curl-devel libxml2 libxml2-devel ncurses ncurses-devel libtool-ltdl-devel libtool-ltdl autoconf automake libaio*

1, unpack the package

Copy Code code as follows:

Tar XF php-7.0.6.tar.bz2
CD php-7.0.6

2, install the compilation relies on the picture library

Copy Code code as follows:

Jpegsrc.v6b.tar.gz
Libpng-1.2.50.tar.gz
Freetype-2.1.10.tar.gz

# install jpegsrc.v6b.tar.gz
#这个需要先创建好存放程序的文件夹不然会报错
mkdir/usr/local/jpeg.6/{bin,lib,include,man/man1}-PV
Tar XF jpegsrc.v6b.tar.gz 
cd jpeg-6b/
./configure--prefix=/usr/local/jpeg.6/make
&& make Install
# installs libpng-1.2.50.tar.gz
tar XF libpng-1.2.50.tar.gz
cd libpng-1.2.50
./configure- prefix=/usr/local/libpng.1.2.50 make
&& make install
# install freetype-2.1.10.tar.gz
tar XF freetype-2.1.10.tar.gz
cd freetype-2.1.10
./configure--prefix=/usr/local/freetype.2.1.10/
make && make Install

3. Pre-compiling configuration parameters

/configure--prefix=/data/server/php \--enable-opcache \--with-config-file-path=/ U01/DATA/SERVER/PHP/ETC \--enable-mysqlnd \--with-mysqli=mysqlnd \--with-pdo-mysql=mysqlnd \--enable-fpm \-- enable-static \--enable-inline-optimization \--enable-sockets \--enable-wddx \--enable-zip \--enable-calendar \--wit 
H-GD \--with-iconv \--with-openssl \--with-zlib \--enable-bcmath \--enable-soap \--with-xmlrpc \--enable-mbstring \ --enable-shared \--with-curl \--enable-xml \--enable-ftp \--with-mcrypt \--with-mhash \--enable-shmop \--enable-sy Svsem \--enable-mbregex \--enable-gd-native-ttf \--enable-pcntl \--enable-session \--with-gettext \--with-freetype-d ir=/usr/local/freetype.2.1.10 \--with-jpeg-dir=/usr/local/jpeg.6 \--with-png-dir=/usr/local/libpng.1.2.50 \--  
Disable-ipv6 \--disable-debug \--disable-maintainer-zts \--disable-rpath \--disable-fileinfo \--without-gdbm \ 

4. Perform compilation

Copy Code code as follows:

Make && make install

5. Provide php.ini documents

Copy Code code as follows:

CP Php.ini-production/u01/data/server/php/etc/php.ini

Configure PHP.ini

# Around 840 lines-set PHP's Opcache and memcache extension libraries
zend_extension=opcache.so
extension=memcache.so
# 722 lines or so-set PHP's extension library path
extension_dir = "/u01/data/server/php7/lib/php/extensions/no-debug-non-zts-20151012/"
# Avoid exposing PHP information to HTTP headers
expose_php = off
# Avoid exposing PHP calling MySQL error message
display_errors = Off
# Open the PHP error log (the path is configured in php-fpm.conf)
log_errors = on
# set PHP time zone
date.timezone = PRC
# open Opcache (1733 lines or so)
opcache.enable=1
# Set up PHP script to allow access to the directory
# open_basedir =/usr/share/nginx/html;

6. Configure PHP-FPM

php-fpm.conf Process Service Master configuration file

# Set the path of the error log
error_log =/var/log/php-fpm/error.log
# introduces the configuration in the www.conf file
include=/usr/local/php7/etc/ php-fpm.d/*.conf
# Set the maximum number of files opened by the main process
rlimit_files = 102400
www.conf Process Service Extension Profile
# Set users and user groups user
= www
group = www
# set PHP listening mode
# listen = 127.0.0.1:9000 
# Note Here to set the permissions of the PHP socket file, the default is Root,nginx inaccessible.
listen =/var/run/php-fpm/php-fpm.sock
# Open Slow log
Slowlog =/var/log/php-fpm/php-slow.log
Request _slowlog_timeout = 10s
# Set Number of worker processes (based on actual situation)
Pm.max_children =
# Here you need to note that Pm.start_servers cannot be less than pm.min_ Spare_servers
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = ten
pm.max_ Requests = 10240
# Set PHP Session Directory (both users and user groups are WWW)
Php_value[session.save_handler] = files
php_value[ Session.save_path] =/var/tmp/php/session

7. Provide php-fpm startup script

#! /bin/sh # PREFIX=/U01/DATA/SERVER/PHP7 Exec_prefix=${prefix} php_fpm_bin=${exec_prefix}/sbin/php-fpm php_fpm_CONF=$ {prefix}/etc/php-fpm.conf php_fpm_pid=${prefix}/var/run/php-fpm.pid php_opts= "--fpm-config $php _fpm_conf--pid $php _fpm_pid "Wait_for_pid () {try=0 while Test $try-lt the ' in ' created ') if [-f ' $];
Then try= ' break fi;; ' Removed ') if [!-f ' $ '];
Then try= ' break fi;;
Esac Echo-n.  try= ' expr $try + 1 ' sleep 1 do} case ' "in Start" echo-n "Starting php-fpm" $php _fpm_bin--daemonize $php _opts If [ "$?"!= 0]; Then echo "Failed" Exit 1 fi wait_for_pid created $php _fpm_pid if [-N "$try"];
Then echo "Failed" Exit 1 else echo "done" FI;; Stop) echo-n "gracefully shutting down php-fpm" if [! -R $php _fpm_pid]; Then echo "Warning, no PID file found-php-fpm is not running?" Exit 1 fi kill-quit ' cat $php _fpm_pid ' wait_for_pid ' rem Oved $php _fpm_pid If [-N "$try"]; Then echo "failed. Use Force-quit ' exit 1 else echo ' done ' fi
;; Status if [!-R $php _fpm_pid]; Then echo "PHP-FPM is stopped" Exit 0 fi pid= ' cat $php _fpm_pid ' if ps-p $PID | Grep-q $PID;
Then echo "PHP-FPM (PID $PID) is running ..." Else echo "php-fpm dead but PID file exists" FI; Force-quit) echo-n "terminating php-fpm" if [! -R $php _fpm_pid]; Then echo "Warning, no PID file found-php-fpm is not running?" Exit 1 fi kill-term ' cat $php _fpm_pid ' wait_for_pid ' rem Oved $php _fpm_pid If [-N "$try"];
Then echo "Failed" Exit 1 else echo "done" FI;;
Restart) $ stop $ start;; Reload) echo-n "Reload service php-fpm" if [! -R $php _fpm_pid]; 
Then echo "Warning, no PID file found-php-fpm isn't running?" Exit 1 fi kill-usr2 ' cat $php _fpm_pid ' echo "done";;
*) echo "Usage: $ {Start|stop|force-quit|restart|reload|status}" exit 1;; Esac

Eight, start the PHP-FPM program

/ETC/INIT.D/PHP-FPM start
# Modify socket file permissions
chown-r/var/run/php-fpm/

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.