nginx在ubuntu上的安装

1、安装Nginx

  1. apt-get install nginx

(要最新版本下载下来编译吧)装完应该能正常运行了。如果之前有装APACHE要改下端口。。。或者直接

  1. apt-get remove apache2
  2. /etc/init.d/nginx stop
  3. /etc/init.d/nginx start

2、安装php-cgi

  1. apt-get install php-cgi

(要自定义安装的编译吧)

3、改php-cgi的配置

Ubuntu下是/etc/php5/cgi/php.ini之前有安装过php的话会复制apache的配置文件打开cgi.fix_pathinfo选项:

  1. cgi.fix_pathinfo=1;

4、改Nginx的fastcgi传递参数

Ubuntu下是/etc/nginx/fastcgi_params默认应该已经设置好了,内容差不多就下面那样:

代码:

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

5、创建VHost配置(这里只说Ubuntu下的,也就路径和包含文件目录的区别了)在/etc/nginx/sites-available/下创建服务器配置文件比如myserver

  1. ln -n /etc/nginx/sites-available/myserver /etc/nginx/sites-enabled/myserver

其实懒的话直接创建在enabled里也没问题-。-内容填:

代码:

  1. server {
  2. listen 80;
  3. server_name myserver.com;
  4. access_log /var/log/nginx/myserver.access.log;location / {
  5. root /wwwroot/myserver;
  6. index index.php;
  7. autoindex off;
  8. }
  9. location ~ \.php$ {
  10. include /etc/nginx/fastcgi_params;
  11. fastcgi_param SCRIPT_FILENAME /wwwroot/myserver/$fastcgi_script_name;
  12. fastcgi_pass 127.0.0.1:9000;
  13. fastcgi_index index.php;
  14. }
  15. # redirect server error pages to the static page /50x.html
  16. error_page 500 502 503 504 /50x.html;
  17. location = /50x.html {
  18. root /var/www/nginx-default;
  19. }
  20. # deny access to .htaccess files, if Apache’s document root
  21. location ~ /\.ht {
  22. deny all;
  23. }
  24. }

6、安装spawn-fcgi

下载可到这里,这东西已经独立出来了http://redmine.lighttpd.net/projects/spawn-fcgi/wiki下完后解压编译

  1. sudo ./configure prefix=/usr/local
  2. sudo make
  3. sudo make install

7、启动spaw-fcgi

  1. sudo spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -F 10

哦,对后面10进程不爽的人可以取消或是加到100

8、重启Nginx

  1. /etc/init.d/nginx restart

9、定义下hosts就可以看结果啦编辑/etc/hosts加入

  1. 127.0.0.1 myserver.com

然后打开浏览器就可以看结果啦~有问题欢迎指出~~

原文: https://wizardforcel.gitbooks.io/nginx-doc/content/Text/6.3_nginx_ubuntu.html