PHP7+Nginx的配置与安装教程详解

本文分步骤,通过文字说明的形式给大家介绍PHP7+Nginx的配置与安装的相关知识,感兴趣的朋友参考下吧。

下面PHP粉丝网小编把PHP7+Nginx的配置与安装教程分享给大家,供大家参考,本文写的不好还请见谅。

系统环境:centos6.5 x64

软件版本:nginx-1.10.0 php-7.0.6

安装 Nginx

Nginx官网:http://nginx.org/

先安装编译依赖的一些组件,代码如下:

yum install pcre pcre-devel openssl openssl-devel -y

1、解压程序包,代码如下:

tar xf nginx-1.10.0.tar.gz

cd nginx-1.10.0

2、预编译配置参数,代码如下:

  1. ./configure --user=www \
  2. --group=www \
  3. --prefix=/data/server/nginx \
  4. --with-http_stub_status_module \
  5. --without-http-cache \
  6. --with-http_ssl_module \
  7. --with-http_gzip_static_module

3、执行编译,代码如下:

make && make install

4、替换配置文件

nginx.conf

  1. user www www;
  2. worker_processes 1;
  3. error_log /u01/data/log/nginx/error.log crit;
  4. pid /u01/data/server/nginx/logs/nginx.pid;
  5. #Specifies the value for maximum file descriptors that can be opened by this process.
  6. worker_rlimit_nofile 65535;
  7. events
  8. {
  9. use epoll;
  10. worker_connections 65535;
  11. }
  12. http {
  13. include mime.types;
  14. default_type application/octet-stream;
  15. #charset gb2312;
  16. server_names_hash_bucket_size 128;
  17. client_header_buffer_size 32k;
  18. large_client_header_buffers 4 32k;
  19. client_max_body_size 8m;
  20. sendfile on;
  21. tcp_nopush on;
  22. keepalive_timeout 60;
  23. tcp_nodelay on;
  24. fastcgi_connect_timeout 300;
  25. fastcgi_send_timeout 300;
  26. fastcgi_read_timeout 300;
  27. fastcgi_buffer_size 64k;
  28. fastcgi_buffers 4 64k;
  29. fastcgi_busy_buffers_size 128k;
  30. fastcgi_temp_file_write_size 128k;
  31. gzip on;
  32. gzip_min_length 1k;
  33. gzip_buffers 4 16k;
  34. gzip_http_version 1.0;
  35. gzip_comp_level 2;
  36. gzip_types text/plain application/x-javascript text/css application/xml;
  37. gzip_vary on;
  38. #limit_zone crawler $binary_remote_addr 10m;
  39. log_format main '$remote_addr - "$request_time" [$time_local] "$request" '
  40. '"$status" $body_bytes_sent "$http_referer" '
  41. '"$http_user_agent" $http_x_forwarded_for';
  42. log_format '$remote_addr - $remote_user [$time_local] "$request" '
  43. '$status $body_bytes_sent "$http_referer" '
  44. '"$http_user_agent" $http_x_forwarded_for "$request_time"';
  45. include /u01/alidata/server/nginx/conf/vhosts/*.conf;

虚拟主机配置文件模板

  1. server {
  2. listen 8080;
  3. server_name localhost;
  4. index index.html index.htm index.php;
  5. root /u01/data/www;
  6. location ~ .*\.(php|php5)?$
  7. {
  8. fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
  9. fastcgi_index index.php;
  10. include fastcgi.conf;
  11. }
  12. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  13. {
  14. expires 30d;
  15. }
  16. location ~ .*\.(js|css)?$
  17. {
  18. expires 1h;
  19. }
  20. ###this is to use open website lianjie like on apache##
  21. location / {
  22. if (!-e $request_filename) {
  23. rewrite ^(.*)$ /index.php?s=$1 last;
  24. break;
  25. }
  26. }
  27. location ~ /.svn/ {
  28. deny all;
  29. }
  30. ###end##
  31. access_log /u01/data/log/nginx/access/test.log main;
  32. }

5、提供Nginx启动脚本

  1. #!/bin/bash
  2. #
  3. nginxd=/u01/data/server/nginx/sbin/nginx
  4. nginx_config=/u01/data/server/nginx/conf/nginx.conf
  5. nginx_pid=/u01/data/server/nginx/logs/nginx.pid
  6. RETVAL=0
  7. prog="nginx"
  8. [ -x $nginxd ] || exit 0
  9. # Start nginx daemons functions.
  10. start() {
  11. if [ -e $nginx_pid ] && netstat -tunpl | grep nginx &> /dev/null;then
  12. echo "nginx already running...."
  13. exit 1
  14. fi
  15. echo -n $"Starting $prog!"
  16. $nginxd -c ${nginx_config}
  17. RETVAL=$?
  18. echo
  19. [ $RETVAL = 0 ] && touch /var/lock/nginx
  20. return $RETVAL
  21. }
  22. # Stop nginx daemons functions.
  23. stop() {
  24. echo -n $"Stopping $prog!"
  25. $nginxd -s stop
  26. RETVAL=$?
  27. echo
  28. [ $RETVAL = 0 ] && rm -f /var/lock/nginx
  29. }
  30. # reload nginx service functions.
  31. reload() {
  32. echo -n $"Reloading $prog!"
  33. stop() {
  34. echo -n $"Stopping $prog!"
  35. $nginxd -s stop
  36. RETVAL=$?
  37. echo
  38. [ $RETVAL = 0 ] && rm -f /var/lock/nginx
  39. }
  40. # reload nginx service functions.
  41. reload() {
  42. echo -n $"Reloading $prog!"
  43. #kill -HUP `cat ${nginx_pid}`
  44. $nginxd -s reload
  45. RETVAL=$?
  46. echo
  47. }
  48. # See how we were called.
  49. case "$1" in
  50. start)
  51. start
  52. ;;
  53. stop)
  54. stop
  55. ;;
  56. reload)
  57. reload
  58. ;;
  59. restart)
  60. stop
  61. start
  62. ;;
  63. *)
  64. echo $"Usage: $prog {start|stop|restart|reload|help}"
  65. exit 1
  66. esac
  67. exit $RETVAL

只需要稍加修改程序路径就可立即使用

安装 PHP7

PHP官网:http://php.net/

PHP扩展:http://pecl.php.net/

先安装一些为编译依赖的组建

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、解压程序包,代码如下:

tar xf php-7.0.6.tar.bz2

cd php-7.0.6

2、安装编译依赖的图片库,代码如下:

  1. jpegsrc.v6b.tar.gz
  2. libpng-1.2.50.tar.gz
  3. freetype-2.1.10.tar.gz
  4. # 安装 jpegsrc.v6b.tar.gz
  5. #这个需要先创建好存放程序的文件夹不然会报错
  6. mkdir /usr/local/jpeg.6/{bin,lib,include,man/man1} -pv
  7. tar xf jpegsrc.v6b.tar.gz
  8. cd jpeg-6b/
  9. ./configure --prefix=/usr/local/jpeg.6/
  10. make && make install
  11. # 安装 libpng-1.2.50.tar.gz
  12. tar xf libpng-1.2.50.tar.gz
  13. cd libpng-1.2.50
  14. ./configure --prefix=/usr/local/libpng.1.2.50
  15. make && make install
  16. # 安装 freetype-2.1.10.tar.gz
  17. tar xf freetype-2.1.10.tar.gz
  18. cd freetype-2.1.10
  19. ./configure --prefix=/usr/local/freetype.2.1.10/
  20. make && make install

3、预编译配置参数

  1. ./configure --prefix=/data/server/php \
  2. --enable-opcache \
  3. --with-config-file-path=/u01/data/server/php/etc \
  4. --enable-mysqlnd \
  5. --with-mysqli=mysqlnd \
  6. --with-pdo-mysql=mysqlnd \
  7. --enable-fpm \
  8. --enable-static \
  9. --enable-inline-optimization \
  10. --enable-sockets \
  11. --enable-wddx \
  12. --enable-zip \
  13. --enable-calendar \
  14. --with-gd \
  15. --with-iconv \
  16. --with-openssl \
  17. --with-zlib \
  18. --enable-bcmath \
  19. --enable-soap \
  20. --with-xmlrpc \
  21. --enable-mbstring \
  22. --enable-shared \
  23. --with-curl \
  24. --enable-xml \
  25. --enable-ftp \
  26. --with-mcrypt \
  27. --with-mhash \
  28. --enable-shmop \
  29. --enable-sysvsem \
  30. --enable-mbregex \
  31. --enable-gd-native-ttf \
  32. --enable-pcntl \
  33. --enable-session \
  34. --with-gettext \
  35. --with-freetype-dir=/usr/local/freetype.2.1.10 \
  36. --with-jpeg-dir=/usr/local/jpeg.6 \
  37. --with-png-dir=/usr/local/libpng.1.2.50 \
  38. --disable-ipv6 \
  39. --disable-debug \
  40. --disable-maintainer-zts \
  41. --disable-rpath \
  42. --disable-fileinfo \
  43. --without-gdbm \

4、执行编译,代码如下:

make && make install

5、提供php.ini文件,代码如下:

cp php.ini-production /u01/data/server/php/etc/php.ini

配置php.ini

  1. # 在840行左右-设置PHP的opcache和memcache扩展库
  2. zend_extension=opcache.so
  3. extension=memcache.so
  4. # 722行左右-设置PHP的扩展库路径
  5. extension_dir = "/u01/data/server/php7/lib/php/extensions/no-debug-non-zts-20151012/"
  6. # 避免PHP信息暴露在http头中
  7. expose_php = Off
  8. # 避免暴露php调用mysql的错误信息
  9. display_errors = Off
  10. # 开启PHP错误日志(路径在php-fpm.conf中配置)
  11. log_errors = On
  12. # 设置PHP的时区
  13. date.timezone = PRC
  14. # 开启opcache(1733行左右)
  15. opcache.enable=1
  16. # 设置PHP脚本允许访问的目录
  17. # open_basedir = /usr/share/nginx/html;

6、配置php-fpm

php-fpm.conf 进程服务主配置文件

  1. # 设置错误日志的路径
  2. error_log = /var/log/php-fpm/error.log
  3. # 引入www.conf文件中的配置
  4. include=/usr/local/php7/etc/php-fpm.d/*.conf
  5. # 设置主进程打开的最大文件数
  6. rlimit_files = 102400
  7. www.conf 进程服务扩展配置文件
  8. # 设置用户和用户组
  9. user = www
  10. group = www
  11. # 设置php监听方式
  12. # listen = 127.0.0.1:9000
  13. # 注意这里要设置PHP套接字文件的权限,默认是root,Nginx无法访问。
  14. listen = /var/run/php-fpm/php-fpm.sock
  15. # 开启慢日志
  16. slowlog = /var/log/php-fpm/php-slow.log
  17. request_slowlog_timeout = 10s
  18. # 设置工作进程数(根据实际情况设置)
  19. pm.max_children = 50
  20. # 这里需要注意,pm.start_servers 不能小于 pm.min_spare_servers
  21. pm.start_servers = 5
  22. pm.min_spare_servers = 5
  23. pm.max_spare_servers = 10
  24. pm.max_requests = 10240
  25. # 设置php的session目录(所属用户和用户组都是www)
  26. php_value[session.save_handler] = files
  27. php_value[session.save_path] = /var/tmp/php/session

7、提供php-fpm启动脚本

  1. #! /bin/sh
  2. #
  3. prefix=/u01/data/server/php7
  4. exec_prefix=${prefix}
  5. php_fpm_BIN=${exec_prefix}/sbin/php-fpm
  6. php_fpm_CONF=${prefix}/etc/php-fpm.conf
  7. php_fpm_PID=${prefix}/var/run/php-fpm.pid
  8. php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
  9. wait_for_pid () {
  10. try=0
  11. while test $try -lt 35 ; do
  12. case "$1" in
  13. 'created')
  14. if [ -f "$2" ] ; then
  15. try=''
  16. break
  17. fi
  18. ;;
  19. 'removed')
  20. if [ ! -f "$2" ] ; then
  21. try=''
  22. break
  23. fi
  24. ;;
  25. esac
  26. echo -n .
  27. try=`expr $try + 1`
  28. sleep 1
  29. done
  30. }
  31. case "$1" in
  32. start)
  33. echo -n "Starting php-fpm "
  34. $php_fpm_BIN --daemonize $php_opts
  35. if [ "$?" != 0 ] ; then
  36. echo " failed"
  37. exit 1
  38. fi
  39. wait_for_pid created $php_fpm_PID
  40. if [ -n "$try" ] ; then
  41. echo " failed"
  42. exit 1
  43. else
  44. echo " done"
  45. fi
  46. ;;
  47. stop)
  48. echo -n "Gracefully shutting down php-fpm "
  49. if [ ! -r $php_fpm_PID ] ; then
  50. echo "warning, no pid file found - php-fpm is not running ?"
  51. exit 1
  52. fi
  53. kill -QUIT `cat $php_fpm_PID`
  54. wait_for_pid removed $php_fpm_PID
  55. if [ -n "$try" ] ; then
  56. echo " failed. Use force-quit"
  57. exit 1
  58. else
  59. echo " done"
  60. fi
  61. ;;
  62. status)
  63. if [ ! -r $php_fpm_PID ] ; then
  64. echo "php-fpm is stopped"
  65. exit 0
  66. fi
  67. PID=`cat $php_fpm_PID`
  68. if ps -p $PID | grep -q $PID; then
  69. echo "php-fpm (pid $PID) is running..."
  70. else
  71. echo "php-fpm dead but pid file exists"
  72. fi
  73. ;;
  74. force-quit)
  75. echo -n "Terminating php-fpm "
  76. if [ ! -r $php_fpm_PID ] ; then
  77. echo "warning, no pid file found - php-fpm is not running ?"
  78. exit 1
  79. fi
  80. kill -TERM `cat $php_fpm_PID`
  81. wait_for_pid removed $php_fpm_PID
  82. if [ -n "$try" ] ; then
  83. echo " failed"
  84. exit 1
  85. else
  86. echo " done"
  87. fi
  88. ;;
  89. restart)
  90. $0 stop
  91. $0 start
  92. ;;
  93. reload)
  94. echo -n "Reload service php-fpm "
  95. if [ ! -r $php_fpm_PID ] ; then
  96. echo "warning, no pid file found - php-fpm is not running ?"
  97. exit 1
  98. fi
  99. kill -USR2 `cat $php_fpm_PID`
  100. echo " done"
  101. ;;
  102. *)
  103. echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
  104. exit 1
  105. ;;
  106. esac

八、启动php-fpm程序

  1. /etc/init.d/php-fpm start
  2. # 修改套接字文件权限
  3. chown -R /var/run/php-fpm/