A. 特点
1. 高效、内存使用少。
2. 权限分离,用户间互不干扰。
B. 应用程序说明
Nginx : 事件驱动的 Web 服务器,采用模块化设计,小巧、高效。
PHP-CGI : PHP的CGI接口版本(本文使用FastCGI高效接口)。
C. 整个架构的简单说明
Nginx 处理所有的 Web 请求,它将 PHP 的请求 Match 出,发送给上游服务器处理,这里的上游服务器就是 PHP-CGI。
PHP-CGI 工作在 FastCGI 模式,它侦听着一个地址端口(或 Unix socket文件,建议组合权限使用 Unix Socket 更安全),Nginx 会连接并发送请求及回收结果并发送给客户浏览器。
Nginx 运行于 www-data 用户环境,这要求 www-data 用户有所有">虚拟主机用户的主目录访问权限。每个虚拟主机拥有自己的 PHP-CGI 进程组(PHP-CGI 可工作在多进程模式),运行于自己的用户环境,本方案并没有设计动态的 PHP-CGI 进程管理器用于对资源的负载均衡。
Nginx 使用了 HTTP OwnerMatch 模块,使得它能够控制每个虚拟主机的每个 Location 有哪些用户的文件的访问权限。
D. 以 Ubuntu 10.04 系统为例的配置实例
1. 安装应用程序
sudo
apt-get install nginx mysql-server php5-cgi php5-mysql
PS:建议下载补丁版本的 Nginx
2. 配置 Nginx
指定 Nginx 的进行用户,和工作进程数,其它根据实际需要作出调整。
sudo vim /etc/nginx/nginx.conf
user www-data; # 指定使用 www-data 执行 Nginxworker_processes 2; # 指定 2 个子工作进程
3. 配置虚拟主机用户
a. 创建用户目录
sudo mkdir -p /var/web/username/{config,cert,bin,run}
b. 创建用户与组
本方案使用 sftp 作为用户的文件管理器,虚拟主机用户没有终端。sftp 方案见我的另一日志。
sudo useradd -m -g sftp -s /bin/false usernamesudo mkdir -p /home/username/web/{www,logs}sudo ln -s /home/username /var/web/username/home
c. 虚拟主机配置文件模板
/var/web/username/config/vhost 为虚拟主机配置文件,将软链接到 /etc/nginx/sites-enabled/ 目录中。以下是模板,修改其中的 username 和 server_name 值。
# vhost# Heiher <admin@heiher.info> # HTTP Serverserver { listen 80; ## listen for ipv4 server_name localhost; access_log /var/web/username/home/web/logs/access.log; error_log /var/web/username/home/web/logs/error.log; location / { root /var/web/username/home/web/www; index index.html index.htm index.php; ## Rewrite if (!-e $request_filename) { rewrite ^(.+)$ /index.php?q=$1 last; } omallow username sftp; # 允许访问隶属于 username:sftp 的文件 omdeny all; # 禁止访问其它所有文件 } # pass the PHP scripts to FastCGI server listening on socket file # location ~ \.php$ { if (!-e $request_filename) { return 404; } fastcgi_pass unix:/var/web/username/run/pfw.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/web/username/home/web/www/$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; }} # HTTPS Serverserver { listen 443; ## listen for ipv4 server_name localhost; ssl on; ssl_certificate /var/web/username/cert/cert.pem; ssl_certificate_key /var/web/username/cert/cert.key; ssl_session_timeout 5m; ssl_protocols SSLv3 TLSv1; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+
RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP; ssl_prefer_server_ciphers on; access_log /var/web/username/home/web/logs/access.log; error_log /var/web/username/home/web/logs/error.log; location / { root /var/web/username/home/web/www; index index.html index.htm index.php; ## Rewrite if (!-e $request_filename) { rewrite ^(.+)$ /index.php?q=$1 last; } omallow username sftp; omdeny all; } # pass the PHP scripts to FastCGI server listening on socket file # location ~ \.php$ { if (!-e $request_filename) { return 404; } fastcgi_pass unix:/var/web/username/run/pfw.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/web/username/home/web/www/$fastcgi_script_name; fastcgi_param HTTPS on; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; }}