nginx在freebsd上的安装

一 . 安装必备软件 MySQL+PHP+Pcre

  1. cd /usr/ports/database/mysql50-server && make install clean
  2. cd /usr/lang/php5/ && make install clean 选择对cgi mysql等的支持
  3. cd /usr/devel/pcre && make install clean

用ports安装 /usr/ports/www/nginx, make install clean

二、弄了一个fastcgi的脚本,来自lighttpd

1) cd /usr/ports/www/lighttpd

2) make

3) cp /usr/ports/www/lighttpd/work/lighttpd-1.4.18/src/spawn-cgi /usr/bin

4) make clean

三、修改配置文件:

1,/usr/local/etc/nginx/nginx.conf:

  1. user www www;
  2. worker_processes 10;
  3. error_log /usr/local/etc/nginx/logs/nginx_error.log crit;
  4. worker_rlimit_nofile 51200;
  5. events
  6. {
  7. use epoll;
  8. worker_connections 51200;
  9. }
  10. http
  11. {
  12. include conf/mime.types;
  13. default_type application/octet-stream;
  14. charset gb2312;
  15. server_names_hash_bucket_size 128;
  16. keepalive_timeout 60;
  17. tcp_nodelay on;
  18. gzip on;
  19. gzip_min_length 1k;
  20. gzip_buffers 4 8k;
  21. gzip_http_version 1.1;
  22. gzip_types text/plain application/x-javascript. text/css text/html application/xml;
  23. server
  24. {
  25. listen 80;
  26. server_name www.test.com;
  27. index index.html index.htm index.php;
  28. root /usr/local/www/data/;
  29. location ~ .*\.php?$
  30. {
  31. include fcgi.conf;
  32. fastcgi_pass 127.0.0.1:9000;
  33. fastcgi_index index.php;
  34. }
  35. log_format access '$remote_addr - $remote_user [$time_local] "$request" '
  36. '$status $body_bytes_sent "$http_referer" '
  37. '"$http_user_agent" $http_x_forwarded_for';
  38. access_log /usr/local/nginx/logs/access.log access;
  39. }
  40. }

2 ,先将php.ini的配置中 cgi.fix_pathinfo=1 这样php-cgi方能正常使用SCRIPT_FILENAME这个变量。

3,编辑fcgi.conf文件,加入

  1. fastcgi_param GATEWAY_INTERFACE CGI/1.1;
  2. fastcgi_param SERVER_SOFTWARE nginx;
  3. fastcgi_param QUERY_STRING $query_string;
  4. fastcgi_param REQUEST_METHOD $request_method;
  5. fastcgi_param CONTENT_TYPE $content_type;
  6. fastcgi_param CONTENT_LENGTH $content_length;
  7. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  8. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  9. fastcgi_param REQUEST_URI $request_uri;
  10. fastcgi_param DOCUMENT_URI $document_uri;
  11. fastcgi_param DOCUMENT_ROOT $document_root;
  12. fastcgi_param SERVER_PROTOCOL $server_protocol;
  13. fastcgi_param REMOTE_ADDR $remote_addr;
  14. fastcgi_param REMOTE_PORT $remote_port;
  15. fastcgi_param SERVER_ADDR $server_addr;
  16. fastcgi_param SERVER_PORT $server_port;
  17. fastcgi_param SERVER_NAME $server_name;
  18. # PHP only, required if PHP was built with --enable-force-cgi-redirect
  19. #fastcgi_param REDIRECT_STATUS 200;

四,启动

1, 启动fcgi

  1. /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -f /usr/local/bin/php-cgi

参数说明:

-f 指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置

-a 绑定到地址addr

-p 绑定到端口port

-s 绑定到unix socket的路径path

-C 指定产生的FastCGI的进程数,默认为5(仅用于PHP)

-P 指定产生的进程的PID文件路径

-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等

也可建立脚本

1) ee /usr/bin/php-fastcgi

  1. #!/bin/sh
  2. /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -f /usr/local/bin/php-cgi

2) chmod 755 /usr/bin/php-fastcgi

3) ee /usr/local/etc/rc.d/init-fastcgi

  1. #!/bin/bash
  2. PHP_SCRIPT=/usr/bin/php-fastcgi
  3. RETVAL=0
  4. case "$1" in
  5. start)
  6. $PHP_SCRIPT
  7. RETVAL=$?
  8. ;;
  9. stop)
  10. killall -9 php
  11. RETVAL=$?
  12. ;;
  13. restart)
  14. killall -9 php
  15. $PHP_SCRIPT
  16. RETVAL=$?
  17. ;;
  18. *)
  19. echo "Usage: php-fastcgi {start|stop|restart}"
  20. exit 1
  21. ;;
  22. esac
  23. exit $RETVAL

4) chmod 755 /usr/local/etc/rc.d/init-fastcgi

2,启动nginx

nginx -t -c /usr/local/etc/nginx/nginx.conf 测试配置是否正确

如果屏幕显示以下两行信息,说明配置文件正确:

  1. the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
  2. the configuration file /usr/local/etc/nginx/nginx.conf was tested successfully

启动:

  1. nginx -c /usr/local/etc/nginx/nginx.conf

开机自动启动加入/etc/rc.conf

  1. nginx_enable="YES"

nginx参数:

-c </path/to/config>为 Nginx 指定一个配置文件,来代替缺省的。

-t不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。

-v显示 nginx 的版本。

-V显示 nginx 的版本,编译器版本和配置参数。