设为首页 收藏本站
查看: 644|回复: 0

[经验分享] linux nginx 配置以及排错

[复制链接]

尚未签到

发表于 2018-11-8 11:49:05 | 显示全部楼层 |阅读模式
  nginx实例配置过程:
  直接从网络上下载rpm安装包:nginx-0.6.39-4.el4.i386.rpm
  直接安装 rpm -ivh nginx-0.6.39-4.el4.i386.rpm  在RHEL4.0可以直接安装,无需其他安装包依赖
  然后是配置/etc/nginx/nginx.conf
  具体配置内容:
  user  www www;           #用户 用户组
  worker_processes  2;     #这里根据CPU个数设置,但是可以实际的多一些
  error_log   /var/log/nginx/error.log;       #这个是日志的位置
  #error_log  /var/log/nginx/error.log  notice;
  #error_log  /var/log/nginx/error.log  info;
  pid        /var/run/nginx.pid;             #这个默认就可以
  events {
  worker_connections  2048;            #可以设置稍微大一些 1024的倍数
  }
  http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] $request '
  '"$status" $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  /var/log/nginx/logs/access.log  main;
  sendfile        on;
  #tcp_nopush     on;
  #keepalive_timeout  0;
  keepalive_timeout  65;
  #gzip  on;
  server {
  listen       8080;            #这里是监听端口
  server_name  localhost;
  #charset koi8-r;
  #access_log  /var/log/nginx/host.access.log  main;
  location / {
  root   /var/www/html/;    #这里是网站服务器的根目录,记得如果测试,请在该目录下面touch index.html 生成一个index.html 文件
  index  index.html index.htm;
  }
  #error_page  404              /404.html;
  # redirect server error pages to the static page /50x.html
  #
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
  root   html;
  }
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #    proxy_pass   http://127.0.0.1;
  #}
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ \.php$ {                   #这里是通过fastcgi来为NGINX开始php服务功能
  root           /var/www/html/;    #设置目录
  fastcgi_pass   127.0.0.1:9000;    #设置监听端口
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  include        fastcgi_params;
  }
  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #    deny  all;
  #}
  }
  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #    listen       8000;
  #    listen       somename:8080;
  #    server_name  somename  alias  another.alias;
  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}
  # HTTPS server
  #
  #server {
  #    listen       443;
  #    server_name  localhost;
  #    ssl                  on;
  #    ssl_certificate      cert.pem;
  #    ssl_certificate_key  cert.key;
  #    ssl_session_timeout  5m;
  #    ssl_protocols  SSLv2 SSLv3 TLSv1;
  #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  #    ssl_prefer_server_ciphers   on;
  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}
  }
  光是安装nginx无法使用php 需要配置/etc/nginx/nginx.conf   其实就是把相应字段的#号去掉。
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ \.php$ {                   #这里是通过fastcgi来为NGINX开始php服务功能
  root           /var/www/html/;    #设置目录   这个设置完成后,打开IE  http://192.168.133.128,默认会查找/var/www/html/index.php这个文件
  fastcgi_pass   127.0.0.1:9000;    #设置监听端口
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  include        fastcgi_params;
  }
  还需要安装软件spawn-fcgi-1.6.3-1.el4.i386.rpm,这个文件算是lighttp下面的一个,主要用来启动php-cgi
  还需要php安装是cgi模式,这样会生成php-cgi文件
  我直接使用rhel4.0,里面默认安装的是PHP4.0,并没有php-cgi文件(直接安装php-cli-5.3.6-3.el4.remi.i386.rpm文件好像也不可行)
  因此直接删除php4.0 安装PHP 5.3.6 (cli) (built: Apr 15 2011 20:47:07)
  具体删除php4.0就不细说了,查看安装的php相关文件 rpm -qa|grep php ,然后删除所有相关文件
  下载php5.3.6相关文件,列表如下:
  -rw-r--r--  1 root root   1282958 Mar  8 16:11 mysql-libs-5.5.11-1.el4.remi.i386.rpm
  -rw-r--r--  1 root root   2020731 Mar  8 16:11 MySQL-shared-5.5.11-1.rhel4.i386.rpm
  -rw-r--r--  1 root root   1350089 Mar  8 15:54 php-5.3.6-3.el4.remi.i386.rpm
  -rw-r--r--  1 root root   2648988 Mar  8 15:40 php-cli-5.3.6-3.el4.remi.i386.rpm
  -rw-r--r--  1 root root    985199 Mar  8 15:46 php-common-5.3.6-3.el4.remi.i386.rpm
  -rw-r--r--  1 root root    628187 Mar  8 15:59 php-devel-5.3.6-3.el4.remi.i386.rpm
  -rw-r--r--  1 root root     53092 Mar  8 15:56 php-ldap-5.3.6-3.el4.remi.i386.rpm
  -rw-r--r--  1 root root    149426 Mar  8 15:56 php-mysql-5.3.6-3.el4.remi.i386.rpm
  -rw-r--r--  1 root root    113652 Mar  8 16:05 php-pdo-5.3.6-3.el4.remi.i386.rpm
  -rw-r--r--  1 root root    306385 Mar  8 15:57 php-pear-5.1.1-1.1.rhel4.ct.i386.rpm
  -rw-r--r--  1 root root    197728 Mar  8 16:07 sqlite-3.3.6-0.3.el4.i386.rpm
  正常情况下php-pear-5.1.1-1.1.rhel4.ct.i386.rpm 无法安装,需要[root@bogon test]# rpm -ivh php-pear-5.1.1-1.1.rhel4.ct.i386.rpm
  error: Failed dependencies:
  php = 5.1.1-1.1.rhel4.ct is needed by php-pear-5.1.1-1.1.rhel4.ct.i386
  所以这个我干脆就没有安装
  安装完以上这些后,就直接查找php-cgi文件
  [root@bogon test]# find / -name 'php-cgi'
  /usr/bin/php-cgi
  然后使用命令:
  [root@bogon test]# spawn-fcgi -a 127.0.0.1 -p 9000 -u www -f /usr/bin/php-cgi
  spawn-fcgi: child spawned successfully: PID: 7980
  查看php-cgi是否启动:
  [root@bogon test]# ps -aux |grep php-cgi
  Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
  www       7980  0.1  0.2 31552 5164 ?        Ss   09:39   0:00 /usr/bin/php-cgi
  root      8009  0.0  0.0  4800  564 pts/1    R+   09:40   0:00 grep php-cgi
  这里看到OK了,建议OK后重新启动nginx   service nginx restart
  配置Nginx 配置虚拟主机
  vi /etc/nginx/nginx.conf
  具体配置如下:请把相关行的#去掉
  # another virtual host using mix of IP-, name-, and port-based configuration
  82     #
  83     server {
  84         listen       8000;         ####虚拟主机监听的端口8000
  85     #    listen       somename:8080;
  86     server_name  somename  alias  another.alias;
  87
  88      location / {
  89        root  /var/www/html/www;     ####虚拟主机目录
  90             index  index.html index.htm;
  91         }
  92     }
  93
  配置完后重新启动 service nginx restart
  然后直接打开网页  http://192.168.133.128:8000
  网页直接打开的是/var/www/html/www/index.htm   说明虚拟主机配置成功。
  也可以通过netstat -antup |grep nginx
  tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      19013/nginx.conf    ####这个说明 虚拟主机已经OK
  tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN      19013/nginx.conf
  问题1:
  打开IE,直接输入地址***.php:    屏幕显示 No input file specified
  解决方案:
  方法1首先php.ini的配置中
  将这条cgi.fix_pathinfo=1前面的;去掉    后再重启php-cgi,php显示正常
  记得修改了php.ini要重启fastcgi服务。 我这里单修改这个,还是不成,又用了方法2,才OK。
  方法2
  nginx中的配置有些麻烦
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
  每个虚机要根据自己不通的虚机设置不能的目录,要保证这个路径正确。
  fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;不能在fastcgi_pass 127.0.0.1:9000;的前面。
  打开:http://192.168.133.128:8080/index.php
  显示如下:this is php web test one !!!!!
  问题2:
  spawn-fcgi -a 127.0.0.1 -p 9000 -u www -f /usr/bin/php-cgi  使用这条命令提示如下:
  spawn-fcgi: child exited with: 126   当时使用PHP4.0 ,系统中没有php-cgi这个文件
  spawn-fcgi: child exited with: 13    使用PHP5.3.6 ,命令中多了-C 64 出错
  spawn-fcgi: child exited with: 2      使用PHP5.3.6 ,命令中多了-C 64 出错


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-632384-1-1.html 上篇帖子: Nginx HA (主从模式) 下篇帖子: nginx lua模块安装 让nginx支持lua-opsfq的博客
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表