Nginx系列教程:LNMP多用户虚拟主机方案

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; }}

时间: 2024-08-16 15:37:02

Nginx系列教程:LNMP多用户虚拟主机方案的相关文章

Nginx系列教程:LNMP多用户动态进程管理虚拟主机方案

A. 特点1. 高效.内存使用少.2. 权限分离,用户间互不干扰.3. 动态进程管理,资源分配均衡. B. 应用程序说明Nginx:事件驱动的 Web 服务器,采用模块化设计,小巧.高效.PHP-FPM:支持快速进程管理的 PHP FastCGI 接口版本,用它实现动态进程管理,提高资源使用效率. C. 整个架构的简单说明Nginx 处理所有的 Web 请求,它将 PHP 的请求 Match 出,发送给上游服务器处理,这里的上游服务器就是 PHP-CGI.PHP-CGI 工作在 FastCGI

Nginx系列教程:Nginx二级子域名完美方案

对于批量添加http://www.aliyun.com/zixun/aggregation/14840.html">虚拟主机的需求,Apache 有 vhost_alias 模块帮忙.Nginx 呢? 其实神马都不需要, 同样可以实现 Nginx 二级域名匹配子文件夹,且不匹配 "www",但可以匹配包含 "www" 的子域名. 首先看看目前网上搜到的方法. if ( $host ~* (.*)\.(.*)\.(.*)) { set $subdoma

Nginx系列教程:在server_name指令中使用正则表达式

server_http://www.aliyun.com/zixun/aggregation/11696.html">name的匹配顺序 nginx中的server_name指令主要用于配置基于名称虚拟主机,server_name指令在接到请求后的匹配顺序分别为:1.准确的server_name匹配,例如: server { listen 80; server_name howtocn.org www.howtocn.org; ...} 2.以*通配符开始的字符串: server { li

Nginx系列教程:HTTP OwnerMatch模块

我编写了一个 Nginx 模块 HTTP OwnerMatch 解决了 Nginx http://www.aliyun.com/zixun/aggregation/14840.html">虚拟主机间可通过链接型文件(硬链接和符号链接)跨站访问的问题.通过这个模块可以指定每个虚拟主机的每个 Location 可以或不可以访问的哪些用户的文件. 配置文件实例 location / { root html; index index.html index.htm; omallow heiher;

Nginx系列教程:NginxHttpOwnerMatch模块(解决链接型文件跨站访问)

概述 该模块提供了一个简单文件基于所有者的http://www.aliyun.com/zixun/aggregation/38609.html">访问控制. nginx_http_owner_match_module模块使得它可以控制具体文件的所有者和组织的访问. 访问规则检查是根据其声明的顺序. 配置范例: location / {  omallow heiher;  # allow access files of heiher  omallow jack sftp; # allow a

Nginx系列教程:faq整理

faq中大部分问题来自http://www.stackoverflow.com与http://www.serverfault.com,有些可能未经过本人验证. Nginx如何替换错误 Q:能否用503错误(负载过高或临时不可用)来代替502错误作为应答返回?A:配置fastcgi_intercept_errors指令并将其设置为on,然后使用error_page指令: location / { fastcgi_pass 127.0.0.1: 9001; fastcgi_intercept_err

Nginx系列教程:Nginx中的正则表达式

1.if指令 所有的Nginx内置变量都可以通过if指令和正则表达式来进行匹配,并且根据匹配结果进行一些操作,如下: if ($http_user_agent ~ MSIE) { rewrite ^(.*)$ /msie/$1 break;} if ($http_cookie ~* "id=([^;] +)(?:;|$)" ) { set $id $1;} 使用符号~*和~模式匹配的正则表达式: ~为区分 大小写的匹配. ~*不区分大小写的匹配(匹配firefox的正则同时匹配Fire

阿里云虚拟主机+wordpress建站教程 阿里云虚拟主机+wordpress建站教程

此文针对技术小白,全过程为本人实际操作.由于我也是个小白,所以操作顺序或许不规范,如果不对的地方还请内行人指正. 第一步:购买域名. 购买途径很多,优势劣势其它答主也说得很清楚,我直接选择了万网. 截图没截取完整,在每个域名的右侧会有操作按钮,点击"解析",由于你只买了域名(门牌号),还没有服务器(房子)可以放置你的网页(家具等),"解析"的作用就是知道你的门牌号之后可以找到你家,这时候需要你购买服务器. 第二步:购买服务器/主机 点击"申请开通"

Nginx + PHP5.3中的虚拟主机功能加强

没有Apache php_admin_value对open_basedir的限制,也没有.htaccess对php.ini的自定义. PHP5.3及时地对这方面进行弥补,可能很多系统管理员还没有意识,php5.3内置了对nginx这类软件类apache的支持: 使用[PATH]和[HOST]对php.ini进行自定义 举例:  代码如下 复制代码 [HOST=www.111cn.net] open_basedir=/var/www/www.111cn.net:/tmp [PATH=/var/ww