静态文件服务器采用 Nginx 部署在Ubuntu8.04 ,但同时为了防备应用服务器出现故障及时切换,也要安装php5
安装前需要有编译环境 gcc autoconf automake build-essential libc6-dev libstdc++6-4.0-dev
开始安装
1.安装Nginx
$ sudo apt-get install nginx
2.启动Nginx
$ sudo /etc/init.d/nginx start
访问http://192.168.4.218 出现 Welcome to nginx! 表示安装成功
3.配置Nginx在Ubuntu启动时自启动 (如果手动编译安装,否则不需要)
$ sudo update-rc.d nginx defaults
4.改php-cgi的配置
Ubuntu下是/etc/php5/cgi/php.ini
打开cgi.fix_pathinfo选项:
cgi.fix_pathinfo=1;
5.改Nginx的fastcgi传递参数
Ubuntu下是/etc/nginx/fastcgi_params,默认应该已经设置好了,内容差不多就下面那样:
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
6.创建VHost配置
在/etc/nginx/sites-available/下创建服务器配置文件比如myserver
$ sudo ln -n /etc/nginx/sites-available/myserver /etc/nginx/sites-enabled/myserver
其实懒的话直接创建在enabled里也没问题
server {
listen 80;
server_name myserver.com;
root /wwwroot/myserver;
access_log /var/log/nginx/myserver.access.log;
location / {
root /wwwroot/myserver;
index index.php;
autoindex off;
}
location ~ /.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /wwwroot/myserver/$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}
# deny access to .htaccess files, if Apache's document root
location ~ //.ht {
deny all;
}
}
root /wwwroot/myserver
即root和server_name同级
这段即相当于apache的默认目录 如果没有这个的话,容易在执行php 文件的时候,会提示”No input file specified”。
9.安装spawn-fcgi
下载可到这里,这东西已经独立出来了
http://redmine.lighttpd.net/projects/spawn-fcgi/wiki
下完后解压编译
$ sudo ./configure --prefix=/usr/local
$ sudo make
$ sudo make install
10配置fastcig_params
(1).在/etc/nginx/fastcgi_params 文件最后,加入一行,可以用sudo gedit /etc/nginx/fastcgi_params打开文件
加入此行:fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
11.启动spaw-fcgi
$ sudo spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -C 10
最后-C 10是进程数,根据情况修改
我们可以将这行代码加入到/etc/rc.local文件底部,这样系统启动的时候也可以同时启动PHP的FastCGI进程。
sudo /usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -C 5
12.重启Nginx
$ /etc/init.d/nginx restart