woyoudn 发表于 2018-11-13 12:46:46

CentOS6.4安装配置nginx+pcre+php/fpm-galean

  本文出自:http://http://www.huxun360.com/view_blog/27
  1.安装pcre-devel
  yum -y install gcc gcc-c++ rpm-build mysql*php* automake autoconf libtool make --skip-broken
  pcre是Nginx 所需的pcre-devel库,安装pcre库是为了让Nginx支持HTTPRewrite模块
  pcre下载地址:http://down.51cto.com/data/904014
  #unzip pcre-8.33.zip
  #cd pcre-8.33
  #./configure
  #make
  #make install
  2. 安装nginx
  下载地址:http://nginx.org/   这里下载的是nginx-1.4.2稳定版
  #tar zxvf nginx-1.4.2.tar.gz
  # cd nginx-1.4.2
  # ./configure --help    #查看可使用的模块,比如:
  --prefix=PATH                      set installation prefix    # 设置安装目录为PATH
  --with-http_stub_status_module   enable ngx_http_stub_status_module   #用来启用NginxStatus功能,以监控Nginx的当前状态。
  --with-google_perftools_module   enable ngx_google_perftools_module    #使Nginx支持google-perftools的模块,使用TCMALLOC优化Nginx的性能时会用到。
  # ./configure --with-http_stub_status_module --prefix=/usr/local/nginx
  # make
  # make install
  3.Nginx的全局配置
  Nginx的整个配置配置文件是以block的形式组织的,每个block一般以一个大括号“{}”来表示,block可以分为几个层次,整个配置文件中main指令位于最高层,在main层下面可以有Events,HTTP等层级,而在HTTP层中又包含server层,即server block,server block中又可分为location层,并且一个server block中可以包含多个location block.

  # vi   /usr/local/nginx/conf/nginx.conf
  usernobody;    #设置Nginx Worker进程运行的用户,默认是nobody不用管。
  worker_processes1;    #指定Nginx开启的进程数,默认个就够了
  #error_loglogs/error.log;
  error_loglogs/error.lognotice;    #定义错误日志文件输出文件和级别
  #error_loglogs/error.loginfo;
  pid      logs/nginx.pid;    #Nginx进程ID存储的位置
  google_perftools_profiles /tmp/tcmalloc;
  events {
  worker_connections1024;    #指定连接数上限
  }
  4.http模块的配置
  http {
  include       mime.types;    #配置文件包含文件的设定,特别对于多虚拟主机的设置时可以减小主配置文件的复杂度。
  default_typeapplication/octet-stream;    #设定默认类型为二进制流,当文件类型未定义时使用这种类型,例如在没有配置PHP环境时,Nginx是不予解析的,此时用浏览器访问PHO文件就会出现下载窗口。
  log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  #上面这段代码设置日志的格式,默认就行。
  access_loglogs/access.logmain;
  sendfile      on;
  keepalive_timeout65;
  server {
  listen       80;
  server_namelocalhost;
  #location用于匹配网页位置
  location / {
  root   html;    #设置网页根目录
  indexindex.html index.htm;    #index设置默认首页地址
  }
  error_page   500 502 503 504/50x.html;
  location = /50x.html {
  root   html;
  }
  }
  default_typeapplication/octet-stream;
  log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  access_loglogs/access.logmain;
  sendfile      on;
  keepalive_timeout65;
  5. 添加一台server虚拟主机www.galean.com,根目录为/var/www/galean
  server{
  listen          80;
  server_name   www.galean.com;
  access_log      logs/galean.access.log main;
  location / {
  index index.html;
  root /var/www/galean;
  }
  }
  6.Nginx的启动
  /usr/local/nginx/sbin/nginx -t         # 检测配置文件的准确性,如果配置文件非默认目录需用 "-c" 指定配置文件位置
  /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
  /usr/local/nginx/sbin/nginx -v    #查看版本信息
  nginx version: nginx/1.4.2
  启动Nginx
  /usr/local/nginx/sbin/nginx
  查看Nginx是否启动
  # ps -ef|grep nginx
  nobody   13254 132530 10:12 ?      00:00:00 php-fpm: pool nginx
  nobody   13255 132530 10:12 ?      00:00:00 php-fpm: pool nginx
  nobody   13256 132530 10:12 ?      00:00:00 php-fpm: pool nginx
  nobody   13257 132530 10:12 ?      00:00:00 php-fpm: pool nginx
  nobody   13258 132530 10:12 ?      00:00:00 php-fpm: pool nginx
  root   21621   10 14:47 ?      00:00:00 nginx: master process /opt/nginx/sbin/nginx
  nobody   21622 216210 14:47 ?      00:00:00 nginx: worker process
  打开浏览器 输入IP可以看到如下,说明服务器启动成功

  Nginx的关闭:
  Nginx的关闭:kill -QUIT pid
  Nginx的平滑重启:kill -HUPpid
  QUIT:表示处理玩当前请求后,关闭进程
  HUP:表示重新加载配置,也就是关闭原有的进程,并开启新的工作进程,此操作不会中断用户的访问请求。
  USER1:用于Nginx的日志切换,也就是重新打开一个日志文件。
  USER2: 用于平滑升级可执行程序。
  WINCH:从容关闭工作进程。
  7.安装PHP和PHP-FPM
  我用的是yum默认安装PHP,如果是编译安装在./configure编译选项中要加上“--enable-fastcgi”来启用PHP对FastCGI的支持,“--enable-fpm”是激活对FastCGI模式的fpm支持。
  yum -y install   php*
  PHP全局配置文件是:/etc/php.ini
  PHP-FPM引擎的配置文件是:/etc/php-fpm.conf
  这里我们先看如何启动 php-fpm:
  1,查找php-fpm在sbin下的可执行启动文件的位置,我的默认是/usr/sbin/php-fpm
  # locate php-fpm
  /etc/php-fpm.conf    #配置文件
  /usr/sbin/php-fpm    #启动文件
  2.查看可选项:
  # /usr/sbin/php-fpm -h
  Usage: php-fpm [-n] [-e] [-h] [-i] [-m] [-v] [-t] [-p ] [-g ] [-c ] [-d foo[=bar]] [-y ] [-D] [-F]
  -c | Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]   Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -h               This help
  -i               PHP information
  -m               Show compiled in modules
  -v               Version number
  -p, --prefix

  Specify>  -g, --pid
  Specify the PID file location.
  -y, --fpm-config

  Specify>  -t, --test       Test FPM configuration and exit
  -D, --daemonizeforce to run in background, and ignore daemonize option from config file
  -F, --nodaemonize
  force to stay in foreground, and ignore daemonize option from config file
  -R, --allow-to-run-as-root
  Allow pool to run as root (disabled by default)
  根据上边提示 “-D”为后台启动服务
  #/usr/sbin/php-fpm-D
  查看是否启动成功,php-fpm默认启动监听的是9000端口:
  #netstat -tnlpu|grep 9000
  tcp      0      0 127.0.0.1:9000            0.0.0.0:*                   LISTEN      5841/php-fpm
  那如何关闭php-fpm呢?可使用如下命令关闭:
  kill -QUIT ‘cat /var/run/php-fpm/php-fpm.pid’
  8.配置Nginx来支持php
  下面是Nginx下支持PHP解析的一个虚拟主机配置实例:
  server{
  listen          80;
  server_name www.galean1.com;
  location / {
  index index.html index.php;
  root /var/www/galean1;
  }
  location ~ \.php$ {
  root            /var/www/galean1;
  fastcgi_pass    127.0.0.1:9000;
  fastcgi_index   index.php;
  fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include         fastcgi_params;
  }
  }
  注意:
  location指定的是PHP后缀的文件都交给127.0.0.1:9000来处理,这里的IP地址和端口就是FastCGI进程监听的IP地址和端口。
  fastcgi_param指定PHP动态程序的主目录,我这里是$document_root,也就是/var/www/galean1。
  接下来,重新启动Nginx
  测试Nginx对PHP的解析
  # echo '' > /var/www/galean1/phpinfo.php
  然后在浏览器里输入http://www.galean1.com/phpinfo.php 看是否正确解析,会出现PHP安装配置以及功能列表统计信息。

  关于nginx启动出错,提示:/usr/local/nginx1.5/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
  缺少lib文件导致
  # ldd /usr/local/nginx1.5/sbin/nginx
  linux-vdso.so.1 =>(0x00007fff9fb20000)
  libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc861b2f000)
  libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fc8618f8000)
  libpcre.so.1 => not found
  libc.so.6 => /lib64/libc.so.6 (0x00007fc861564000)
  /lib64/ld-linux-x86-64.so.2 (0x00007fc861d52000)
  libfreebl3.so => /lib64/libfreebl3.so (0x00007fc861302000)
  libdl.so.2 => /lib64/libdl.so.2 (0x00007fc8610fd000)
  可以看出 libpcre.so.1 => not found 并没有找到,进入/lib目录中手动链接下
  # cd /lib64
  # ln -s libpcre.so.0.0.1 libpcre.so.1
  # ldd /usr/local/nginx1.5/sbin/nginx
  linux-vdso.so.1 =>(0x00007fff3dfff000)
  libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fb41ff27000)
  libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fb41fcf0000)
  libpcre.so.1 => /lib64/libpcre.so.1 (0x00007fb41fac3000)
  libc.so.6 => /lib64/libc.so.6 (0x00007fb41f730000)
  /lib64/ld-linux-x86-64.so.2 (0x00007fb42014a000)
  libfreebl3.so => /lib64/libfreebl3.so (0x00007fb41f4ce000)
  libdl.so.2 => /lib64/libdl.so.2 (0x00007fb41f2c9000)

页: [1]
查看完整版本: CentOS6.4安装配置nginx+pcre+php/fpm-galean